Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | 1izNoob |
I am currently trying to understand shaders from this tutorial: Outlines for 2D sprites - GDQuest
where an outline for a character is created with this code:
shader_type canvas_item;
uniform vec4 line_color : hint_color = vec4(1);
uniform float line_thickness : hint_range(0, 10) = 1.0;
void fragment() {
vec2 size = TEXTURE_PIXEL_SIZE * line_thickness;
float outline = texture(TEXTURE, UV + vec2(-size.x, 0)).a;
outline += texture(TEXTURE, UV + vec2(0, size.y)).a;
outline += texture(TEXTURE, UV + vec2(size.x, 0)).a;
outline += texture(TEXTURE, UV + vec2(0, -size.y)).a;
outline += texture(TEXTURE, UV + vec2(-size.x, size.y)).a;
outline += texture(TEXTURE, UV + vec2(size.x, size.y)).a;
outline += texture(TEXTURE, UV + vec2(-size.x, -size.y)).a;
outline += texture(TEXTURE, UV + vec2(size.x, -size.y)).a;
outline = min(outline, 1.0);
vec4 color = texture(TEXTURE, UV);
COLOR = mix(color, line_color, outline - color.a);
}
I understand all of it except for outline. For example, for
float outline = texture(TEXTURE, UV + vec2(-size.x, 0)).a;
we look at the TEXTURE and shift each pixel a bit to the left bu using UV, and then we take the transparency of that with ‘.a’. All of that makes sense. What I don’t understand is why this is stored in a float? So here would be all my questions:
- How could one float be used to represent all the pixels to the left? Wouldn’t a float just a single value like 1.0?
- When we add all the other sides, how is that working? Don’t we just add individual floats like 1.0 + -1.0? I am really struggling to grasp how that could represent all the pixels.
(I guess along with that 3. Since there is no print function for shaders, is there any way I could print the values? I tried creating a uniform variable with get_shader_params in GDScript… but that didn’t work)
I am quite sure I have some more fundamental misunderstand how shaders work, would really appreciate someone helping! So many thanks in advance
About the debugging/printing, what I always did for debugging shaders is to make them return not their original value but instead the value I want to debug.
E.g. if you want to check only the value of “outline”, I’d change the last line to:
COLOR = vec4(outline, outline, outline, 1.0)
That way, you can at least see the value (well, approximately anyway).
TheSHEEEP | 2020-11-25 13:51