• Shaders
  • What does this basic shader line mean?

This is a really basic shader I'm studying so I know how this works. I understand most of it, I recognize the patterns but what I don't understand is what exactly gets passed to the final outcome of fragment()

shader_type canvas_item;

uniform float bendy: hint_range(0.1,10.0);

float plot(vec2 st, float pct) {
	return  smoothstep( pct-0.02, pct, st.y) -
		smoothstep(pct, pct+0.02, st.y);
}

void fragment() {
	float y = pow(abs(UV.x), bendy);
	vec3 color = vec3(y);
	float pct = plot(UV, y);
	color = (1.0-pct)*color+pct*vec3(0.0,1.0,0.0);
	COLOR = vec4(color, 1.0);
}

This line: color = (1.0-pct)*color+pct*vec3(0.0,1.0,0.0); is where I get tripped. What is going on in this equation?

Below is what the shaders show. They draw a line and uniform float bendy bends them, I think, according to the value of y in the plot() function. Number values denote the value of bendy.

@some_lame_kid said: This line: color = (1.0-pct)*color+pct*vec3(0.0,1.0,0.0); is where I get tripped. What is going on in this equation?

"(1-x) y + x z" is a simple way of combining two values (y and z) such that the total will always be in the range of each value. So if y and z vary from 0 to 1, the total will also stay in the range 0 to 1. "x" is the fraction of z you are using -- and "1-x" varies inversely -- the more y you use, the less z you get.

So that line of code is combining a certain amount of "color" with an inverse amount of green [vec3(r,g,b)], which presumably draws the green line.

Write a Reply...