thebookofshaders/12/vorono-00.frag

54 lines
1.2 KiB
GLSL
Raw Normal View History

2016-05-02 21:19:51 +00:00
// Author: @patriciogv
// Title: 4 cells voronoi
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
st.x *= u_resolution.x/u_resolution.y;
vec3 color = vec3(.0);
// Cell positions
2016-05-03 21:33:11 +00:00
vec2 point[5];
point[0] = vec2(0.83,0.75);
point[1] = vec2(0.60,0.07);
point[2] = vec2(0.28,0.64);
point[3] = vec2(0.31,0.26);
point[4] = u_mouse/u_resolution;
2016-05-02 21:19:51 +00:00
float m_dist = 1.; // minimun distance
2016-05-03 21:33:11 +00:00
vec2 m_point; // minimum position
2016-05-02 21:19:51 +00:00
2016-05-03 21:33:11 +00:00
// Iterate through the points positions
2016-05-02 21:19:51 +00:00
for (int i = 0; i < 5; i++) {
2016-05-03 21:33:11 +00:00
float dist = distance(st, point[i]);
2016-05-02 21:19:51 +00:00
if ( dist < m_dist ) {
// Keep the closer distance
m_dist = dist;
2016-05-03 21:33:11 +00:00
// Kepp the position of the closer point
m_point = point[i];
2016-05-02 21:19:51 +00:00
}
}
2016-05-03 21:33:11 +00:00
// Add distance field to closest point center
color += m_dist*2.;
2016-05-02 21:19:51 +00:00
2016-05-03 21:33:11 +00:00
// tint acording the closest point position
color.rg = m_point;
2016-05-02 21:19:51 +00:00
// Show isolines
2016-05-03 21:33:11 +00:00
color -= abs(sin(80.0*m_dist))*0.07;
2016-05-02 21:19:51 +00:00
2016-05-03 21:33:11 +00:00
// Draw point center
2016-05-02 21:19:51 +00:00
color += 1.-step(.02, m_dist);
gl_FragColor = vec4(color,1.0);
}