thebookofshaders/12/metaballs.frag

56 lines
1.3 KiB
GLSL
Raw Normal View History

2016-05-05 14:36:31 +00:00
// Author: @patriciogv - 2015
// Title: Metaballs
2016-05-02 21:19:51 +00:00
#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-02 21:19:51 +00:00
st *= 5.;
2017-08-19 10:11:58 +00:00
2016-05-02 21:19:51 +00:00
// Tile the space
vec2 i_st = floor(st);
vec2 f_st = fract(st);
2017-08-19 10:11:58 +00:00
float m_dist = 1.; // minimun distance
2016-05-02 21:19:51 +00:00
for (int j= -1; j <= 1; j++ ) {
for (int i= -1; i <= 1; i++ ) {
// Neighbor place in the grid
vec2 neighbor = vec2(float(i),float(j));
2017-08-19 10:11:58 +00:00
2016-05-02 21:19:51 +00:00
// Random position from current + neighbor place in the grid
vec2 offset = random2(i_st + neighbor);
// Animate the offset
offset = 0.5 + 0.5*sin(u_time + 6.2831*offset);
2017-08-19 10:11:58 +00:00
// Position of the cell
2016-05-02 21:19:51 +00:00
vec2 pos = neighbor + offset - f_st;
2017-08-19 10:11:58 +00:00
2016-05-02 21:19:51 +00:00
// Cell distance
float dist = length(pos);
// Metaball it!
m_dist = min(m_dist, m_dist*dist);
}
}
// Draw cells
color += step(0.060, m_dist);
2017-08-19 10:11:58 +00:00
2016-05-02 21:19:51 +00:00
gl_FragColor = vec4(color,1.0);
2017-08-19 10:11:58 +00:00
}