![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | roflcopterDorrie |
I am attempting to take a 2D shader and convert it to a spatial shader (using Godot 3.1).
For example, I am trying to use the second shader of The Book of Shaders: uniforms.
It is fairly straight forward and works fine in a canvas_item shader type
From:
void main() {
vec2 st = gl_FragCoord.xy/u_resolution;
gl_FragColor = vec4(st.x,st.y,0.0,1.0);
}
To:
shader_type canvas_item;
void fragment() {
vec2 st = FRAGCOORD.xy/ (1.0/SCREEN_PIXEL_SIZE);
COLOR.rgb = vec3(st.x, st.y, 0.0);
}
However, I can’t seem to do this same technique for a spatial shader because SCREEN_PIXEL doesn’t exist for spatial shaders.
What is the alternative to get the 0.0 to 1.0 version using FRAGCOORD for a spatial shader?
I have tried everything I can think of from Spatial shaders — Godot Engine (3.1) documentation in English with no luck.
I would have thought VIEWPORT_SIZE would be the spatial equivalent, but no luck, it turns out yellow all the way through.
shader_type spatial;
void fragment()
{
vec2 st = FRAGCOORD.xy / VIEWPORT_SIZE;
ALBEDO = vec3(st.x, st.y, 0.0);
}
Does anyone have ideas where I am going wrong?
In 3d, you normally apply shaders to surfaces. Surfaces are mapped to textures and often to shaders via UV coordinates (Vector2). Those are similar to screen coordinates amd typically go from 0 to 1. So a square face which should show the whole texture is mapped to UV 0,0 in one corner and 1,1 in the other corner. UVs are defined for each vertex ajd are available interpolated in the fragment shader. You typically map UVs to 3D objects in the 3D editor (i.e. Blender).
wombatstampede | 2019-03-23 20:20