forked from patriciogonzalezvivo/thebookofshaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetaballs.frag
55 lines (41 loc) · 1.27 KB
/
metaballs.frag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Author: @patriciogv - 2015
// Title: Metaballs
#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);
// Scale
st *= 5.;
// Tile the space
vec2 i_st = floor(st);
vec2 f_st = fract(st);
float m_dist = 1.; // minimum distance
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));
// 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);
// Position of the cell
vec2 pos = neighbor + offset - f_st;
// Cell distance
float dist = length(pos);
// Metaball it!
m_dist = min(m_dist, m_dist*dist);
}
}
// Draw cells
color += step(0.060, m_dist);
gl_FragColor = vec4(color,1.0);
}