I am following the GDQuest shader secrets course and want to create an outline shader but I am confused about the datatypes.
The code so far looks like this:
shader_type canvas_item;
uniform vec4 line_color: source_color = vec4(1.0);
uniform float line_thickness: hint_range(0.0, 1.0) = 1.0;
void fragment() {
vec2 size = TEXTURE_PIXEL_SIZE * line_thickness;
float left = texture(TEXTURE, UV + vec2(-size.x, 0)).a;
float right = texture(TEXTURE, UV + vec2(size.x, 0)).a;
float up = texture(TEXTURE, UV + vec2(0, size.y)).a;
float down = texture(TEXTURE, UV + vec2(0, -size.y)).a;
float sum = left + right + up + down;
// This value represents the black-and-white mask we'll use to outline the sprite.
float outline = min(sum, 1.0);
vec4 color = texture(TEXTURE, UV);
// We apply the outline color by calculating a mask surrounding the sprite.
COLOR = mix(color, line_color, outline - color.a);
}
And the one thing I do not get is the value that is stored inside of the floats inside the fragment function (left, right, up, down) and why they are added together.
As far as I understand the shader looks at a pixel and then samples the pixels to the left, right, top and bottom, then we store the alpha value in the variables. But wouldn't that just return the alpha value for those pixels, i.e. a value between 0 and 1? And then we add them together with a maximum of 1, I just don't get how this works and the explanations in the tutorial don't cover it. Can someone help?
Sorry for the vague question, basically what I am looking for:
what value is stored inside of the floats
why are the values added together