/
42_CellularIterations.fs
175 lines (145 loc) · 3.54 KB
/
42_CellularIterations.fs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// SaturdayShader #42 : Cellular Iterations
// Joseph Fiola (http://www.joefiola.com)
// 2017-04-08
// Based on the tiling and iteration example by Patricio Gonzalez Vivo from the Book of Shaders' chapter on Cellular Noise
// http://thebookofshaders.com/12/
/*{
"CREDIT": "",
"DESCRIPTION": "",
"CATEGORIES": [
"Generators"
],
"INPUTS": [
{
"NAME": "invert",
"TYPE": "bool",
"DEFAULT":1
},
{
"NAME": "zoom",
"TYPE": "float",
"DEFAULT": 4.0,
"MIN": 0.0,
"MAX": 50.0
},
{
"NAME" : "rings",
"TYPE" : "float",
"DEFAULT": 20.0,
"MIN": 0.0,
"MAX": 200.0
},
{
"NAME" : "thickness",
"TYPE" : "float",
"DEFAULT": 0.5,
"MIN": 0.001,
"MAX": 0.995
},
{
"NAME": "grid",
"TYPE": "float",
"DEFAULT": 0.025,
"MIN": 0.0,
"MAX": 1.0
},
{
"NAME": "gridColor",
"TYPE": "bool",
"DEFAULT":1
},
{
"NAME": "rotate",
"TYPE": "float",
"DEFAULT": 0.0,
"MIN": 0,
"MAX": 1
},
{
"NAME": "pos",
"TYPE": "point2D",
"DEFAULT": [0.5,0.5],
"MIN":[0.0,0.0],
"MAX":[1.0,1.0]
},
{
"NAME": "time",
"TYPE": "float",
"DEFAULT": 0.0,
"MIN": 0.0,
"MAX": 1.0
},
{
"NAME": "amplitude",
"TYPE": "float",
"DEFAULT": 0.5,
"MIN": 0.0,
"MAX": 0.5
},
{
"NAME": "offsets",
"TYPE": "point2D",
"DEFAULT": [0.0,0.0],
"MIN":[-2.0,-2.0],
"MAX":[2.0,2.0]
}
]
}*/
#ifdef GL_ES
precision mediump float;
#endif
#define TWO_PI 6.28318530718
vec2 random2( vec2 p ) {
return fract(sin(vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3))))*43758.5453);
}
mat2 rotate2d(float _angle){
return mat2(cos(_angle),-sin(_angle),
sin(_angle),cos(_angle));
}
void main() {
float animate = time * TWO_PI;
vec2 st = gl_FragCoord.xy/RENDERSIZE.xy;
st -= vec2(pos.x,pos.y); //center uv to pos location
st.x *= RENDERSIZE.x/RENDERSIZE.y; // 1:1 ratio
st = rotate2d(rotate*-TWO_PI) * st;
float color = 0.0;
// Scale
st *= zoom;
// Tile the space
vec2 i_st = floor(st);
vec2 f_st = fract(st);
float m_dist = 1.; // minimun distance
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));
// Random position from current + neighbor place in the grid
vec2 point = random2(i_st + neighbor);
// Animate the point
//point = 0.5 + 0.5*sin(animate + 6.2831*point);
point = 0.5 + amplitude*sin(animate + TWO_PI * point)+(offsets);
// Vector between the pixel and the point
vec2 diff = neighbor + point - f_st;
// 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);
// Draw grid
if(grid > 0.0) {
float thick = 1.0 - grid;
if (gridColor) color = color *-1.0 + 1.0;
color -= step(thick, f_st.x) + step(thick, f_st.y);
if (gridColor) color = color *-1.0 + 1.0;
}
// Show isolines
if(rings > 0.0) color += smoothstep(thickness,thickness+(thickness*0.002*zoom)+(rings*0.002*zoom),(0.8,abs(sin(rings*m_dist))));
//invert colors
if (invert) color = color *-1.0 + 1.0;
gl_FragColor = vec4(vec3(color),1.0);
}