thebookofshaders/12/cellnoise-02.frag

66 lines
1.5 KiB
GLSL
Raw Normal View History

2016-05-03 13:41:52 +00:00
// Author: @patriciogv
// Title: CellularNoise
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
vec2 random2( vec2 p ) {
return fract(sin(vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3))))*43758.5453);
}
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
st.x *= u_resolution.x/u_resolution.y;
vec3 color = vec3(.0);
2017-08-19 10:11:58 +00:00
// Scale
2016-05-03 13:41:52 +00:00
st *= 3.;
2017-08-19 10:11:58 +00:00
2016-05-03 13:41:52 +00:00
// Tile the space
vec2 i_st = floor(st);
vec2 f_st = fract(st);
float m_dist = 1.; // minimun distance
2017-08-19 10:11:58 +00:00
2016-05-03 13:41:52 +00:00
for (int y= -1; y <= 1; y++) {
for (int x= -1; x <= 1; x++) {
// Neighbor place in the grid
vec2 neighbor = vec2(float(x),float(y));
2017-08-19 10:11:58 +00:00
2016-05-03 13:41:52 +00:00
// Random position from current + neighbor place in the grid
vec2 point = random2(i_st + neighbor);
// Animate the point
point = 0.5 + 0.5*sin(u_time + 6.2831*point);
2017-08-19 10:11:58 +00:00
2016-05-03 13:41:52 +00:00
// Vector between the pixel and the point
vec2 diff = neighbor + point - f_st;
2017-08-19 10:11:58 +00:00
2016-05-03 13:41:52 +00:00
// Distance to the point
float dist = length(diff);
// Keep the closer distance
m_dist = min(m_dist, dist);
}
}
// Draw the min distance (distance field)
color += m_dist;
// Draw cell center
color += 1.-step(.02, m_dist);
2017-08-19 10:11:58 +00:00
2016-05-03 13:41:52 +00:00
// Draw grid
color.r += step(.98, f_st.x) + step(.98, f_st.y);
2017-08-19 10:11:58 +00:00
2016-05-03 13:41:52 +00:00
// Show isolines
// color -= step(.7,abs(sin(27.0*m_dist)))*.5;
2017-08-19 10:11:58 +00:00
2016-05-03 13:41:52 +00:00
gl_FragColor = vec4(color,1.0);
2017-08-19 10:11:58 +00:00
}