git.lucas.co / cce-compositor
Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git

scenefx/render/fx_renderer/shaders/gradient.frag (1.1K)

 1 vec4 gradient(vec4 colors[LEN], int count, vec2 size, vec2 grad_box, vec2 origin, float degree, bool linear, bool blend) {
 2 	float step;
 3 
 4 	vec2 normal = (gl_FragCoord.xy - grad_box)/size;
 5 	vec2 uv = normal - origin;
 6 
 7 	float rad = radians(degree);
 8 
 9 	if (linear) {
10 		uv *= vec2(1.0)/vec2(abs(cos(rad)) + abs(sin(rad)));
11 
12 		vec2 rotated = vec2(uv.x * cos(rad) - uv.y * sin(rad) + origin.x,
13 						uv.x * sin(rad) + uv.y * cos(rad) + origin.y);
14 
15 		step = rotated.x;
16 	} else {
17 		vec2 uv = normal - origin;
18 		uv = vec2(uv.x * cos(rad) - uv.y * sin(rad),
19 				uv.x * sin(rad) + uv.y * cos(rad));
20 
21 		uv = vec2(-atan(uv.y, uv.x)/3.14159265 * 0.5 + 0.5, 0.0);
22 		step = uv.x;
23 	}
24 
25 	if (!blend) {
26 		float smooth_fac = 1.0/float(count);
27 		int ind = int(step/smooth_fac);
28 
29 		return colors[ind];
30 	}
31 
32 	float smooth_fac = 1.0/float(count - 1);
33 	int ind = int(step/smooth_fac);
34 	float at = float(ind)*smooth_fac;
35 
36 	vec4 color = colors[ind];
37 	if(ind > 0) color = mix(colors[ind - 1], color, smoothstep(at - smooth_fac, at, step));
38 	if(ind <= count - 1) color = mix(color, colors[ind + 1], smoothstep(at, at + smooth_fac, step));
39 
40 	return color;
41 }