Files
shaders/learning/PixelSpirit/the_diamond.frag
2024-06-01 14:44:18 +02:00

50 lines
1.2 KiB
GLSL

#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
#define PI 3.14159265359
const vec3 uAColor = vec3(.5,.5,.5);
const vec3 uBColor = vec3(.5,.5,.5);
const vec3 uCColor = vec3(1.,1.,1.);
const vec3 uDColor = vec3(.0,.33,.67);
//get colors from http://dev.thi.ng/gradients/
vec3 cosPalette(float t) {
return uAColor + uBColor*cos(6.28318*(uCColor*t+uDColor));
}
//starting to refactor these so i can use them as individual snippets without dependencies
float rhombSDF(vec2 st) {
vec2 st1 = (st*2.-1.)*2.;
vec2 st2 = (vec2(st.x,1.-st.y)*2.-1.)*2.;
float triangle1 = max(abs(st1.x)*.866025+st1.y*.5,-st1.y*.5);
float triangle2 = max(abs(st2.x)*.866025+st2.y*.5,-st2.y*.5);
return max(triangle1, triangle2);
}
float fill(float x, float size) {
return 1.-step(size, x);
}
float stroke(in float x_coor, float s, float width){
float d = step(s,x_coor+width*.5)-step(s,x_coor-width*.5);
return clamp(d, 0.,1.);
}
void main() {
vec2 st = gl_FragCoord.xy/u_resolution;
vec3 color = vec3(0.0,0.0,0.0);
float alpha = 1.0;
float sdf = rhombSDF(st);
color += fill(sdf,.425);
color += stroke(sdf, .5,.05);
color += stroke(sdf, .6,.05);
gl_FragColor = vec4(color, alpha);
}