fragmentShader = `
uniform vec2 u_resolution;
uniform float u_time;
float random (vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))*
43758.5453123);
}
bool side (float p0, float p1, float v0, float v1, float w0, float w1) {
float s = (w0 - v0) * (p1 - v1) - (w1 - v1) * (p0 - v0);
return s > 0.;
//return true;
}
float sqr (float x) {
return x * x;
}
float dist2 (float v0, float v1, float w0, float w1) {
return sqr(v0 - w0) + sqr(v1 - w1);
}
float distToSegmentSquared (float p0, float p1, float v0, float v1, float w0, float w1) {
float l2 = dist2(v0, v1, w0, w1);
float d;
if (l2 == 0.) {
d = dist2(p0, p1, v0, v1);
}
float t = ((p0 - v0) * (w0 - v0) + (p1 - v1) * (w1 - v1)) / l2;
t = max(0., min(1., t));
d = dist2(p0, p1, v0 + t * (w0 - v0), v1 + t * (w1 - v1));
return sqrt(d)/.5;
}
void main() {
float v0 = 0.3;
float v1 = 0.3;
float w0 = 0.7;
float w1 = 0.7;
vec2 st = gl_FragCoord.xy/u_resolution.xy;
//vec2 st = gl_FragCoord.xy;
float c1 = 1.0 - distToSegmentSquared(st.x, st.y, v0, v1, w0, w1)/random(st);
float c2 = 1.0 - distToSegmentSquared(st.x, st.y, v0, v1, w0, w1)/random(st);
bool s = side(st.x, st.y, v0, v1, w0, w1);
if (s) {
gl_FragColor=vec4(c1/.2, c2, 0.,1.0);
} else {
gl_FragColor=vec4(c1/20., c2, 0.,1.0);
}
}
`