-
Notifications
You must be signed in to change notification settings - Fork 3
/
turing.html
239 lines (202 loc) · 7.11 KB
/
turing.html
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/turing.css">
<script id="vertex-shader" type="x-shader/x-vertex">
attribute vec2 aVertexPosition;
attribute vec2 aTextureCoord;
varying vec2 vTextureCoord;
void main(void) {
gl_Position = vec4(aVertexPosition, 1.0, 1.0);
vTextureCoord = aTextureCoord;
}
</script>
<script id="frag-hblur" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform vec2 uTexelSize;
const int aRadius = {{aRadius}};
const int iRadius = {{iRadius}};
const float aWeight = 1.0 / (2.0 * float(aRadius) + 1.0);
const float iWeight = 1.0 / (2.0 * float(iRadius) + 1.0);
void main(void) {
vec4 val = texture2D(uSampler, vTextureCoord) *
vec4(aWeight, iWeight, 1.0, 1.0);
for (int i = 0; i < iRadius; i++) {
float offset = 2.0 * float(i + 1) - 0.5;
vec2 v1 = texture2D(uSampler,
vTextureCoord + vec2(offset * uTexelSize.x, 0.0)).xy;
vec2 v2 = texture2D(uSampler,
vTextureCoord - vec2(offset * uTexelSize.x, 0.0)).xy;
vec2 sum = v1 + v2;
if (offset < 2.0 * float(aRadius)) {
val.x += sum.x * aWeight;
}
val.y += sum.y * iWeight;
}
gl_FragColor = val;
}
</script>
<script id="frag-vblur" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform vec2 uTexelSize;
const int aRadius = {{aRadius}};
const int iRadius = {{iRadius}};
const float aWeight = 1.0 / (2.0 * float(aRadius) + 1.0);
const float iWeight = 1.0 / (2.0 * float(iRadius) + 1.0);
void main(void) {
vec4 val = texture2D(uSampler, vTextureCoord) *
vec4(aWeight, iWeight, 1.0, 1.0);
for (int i = 0; i < iRadius; i++) {
float offset = 2.0 * float(i + 1) - 0.5;
vec2 v1 = texture2D(uSampler,
vTextureCoord + vec2(0.0, offset * uTexelSize.y)).xy;
vec2 v2 = texture2D(uSampler,
vTextureCoord - vec2(0.0, offset * uTexelSize.y)).xy;
vec2 sum = v1 + v2;
if (offset < 2.0 * float(aRadius)) {
val.x += sum.x * aWeight;
}
val.y += sum.y * iWeight;
}
gl_FragColor = val;
}
</script>
<script id="frag-update" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vTextureCoord;
const int numScales = {{numScales}};
const int maxSyms = {{maxSyms}};
uniform sampler2D uScaleSamplers[numScales];
uniform float uDeltas[numScales];
uniform mat3 uSymMatrices[(maxSyms - 1) * numScales];
uniform bool uSymsUsed[(maxSyms - 1) * numScales];
float variation(vec2 vals) {
return abs(vals.x - vals.y);
}
float direction(vec2 vals) {
if (vals.x > vals.y) {
return 1.0;
} else {
return -1.0;
}
}
void main(void) {
vec2 bestVals;
float bestVar = 2.0;
float bestDelta;
for (int i = 0; i < numScales; i++) {
vec2 curVals = texture2D(uScaleSamplers[i], vTextureCoord).xy;
int numSyms = 1;
for (int j = 0; j < maxSyms - 1; j++) {
if (!uSymsUsed[i * (maxSyms - 1) + j]) break;
mat3 symMatrix = uSymMatrices[i * (maxSyms - 1) + j];
vec2 symCoord = (symMatrix * vec3(vTextureCoord, 1.0)).xy;
curVals += texture2D(uScaleSamplers[i], symCoord).xy;
numSyms++;
}
curVals /= float(numSyms);
float curVar = variation(curVals);
if (curVar < bestVar) {
bestVar = curVar;
bestVals = curVals;
bestDelta = uDeltas[i];
}
}
float val = texture2D(uScaleSamplers[0], vTextureCoord).z;
val += direction(bestVals) * bestDelta;
val = clamp(val, 0.0, 1.0);
gl_FragColor = vec4(val, val, val, 1.0);
}
</script>
<script id="frag-draw" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform vec3 uRGB;
uniform bool uUseHue;
void main(void) {
float val = texture2D(uSampler, vTextureCoord).x;
if (uUseHue) {
float hp = mod(360.0 * val + 120.0, 360.0) / 60.0;
float x = 1.0 - abs(mod(hp, 2.0) - 1.0);
if (hp < 1.0) {
gl_FragColor = vec4(1.0, x, 0.0, 1.0);
} else if (hp < 2.0) {
gl_FragColor = vec4(x, 1.0, 0.0, 1.0);
} else if (hp < 3.0) {
gl_FragColor = vec4(0.0, 1.0, x, 1.0);
} else if (hp < 4.0) {
gl_FragColor = vec4(0.0, x, 1.0, 1.0);
} else if (hp < 5.0) {
gl_FragColor = vec4(x, 0.0, 1.0, 1.0);
} else {
gl_FragColor = vec4(1.0, 0.0, x, 1.0);
}
} else {
gl_FragColor = vec4(val * uRGB, 1.0);
}
}
</script>
<script type="text/javascript" src="js/mustache.js"></script>
<script type="text/javascript" src="js/turing.js"></script>
</head>
<body onload="main()">
<header>
<hgroup>
<h1>Cyclic Symmetric Multi-Scale Turing Patterns in WebGL</h1>
<h3>by Alex Favaro</h3>
</hgroup>
<p>
Based on the
<a href="http://www.jonathanmccabe.com/Cyclic_Symmetric_Multi-Scale_Turing_Patterns.pdf">
work by Jonathan McCabe.
</a>
</p>
<p>
<a href="https://github.com/afavaro/TuringPatterns">
Code here.
</a>
</p>
<p>Click anywhere to toggle this description.</p>
<details>
<summary>Settings</summary>
<section>
<h5>Scales</h5>
<label>
<input type="checkbox" data-index="0" checked> Large
</label>
<br>
<label>
<input type="checkbox" data-index="1" checked> Medium
</label>
<br>
<label>
<input type="checkbox" data-index="2" checked> Small
</label>
<br>
<label>
<input type="checkbox" data-index="3" checked> Tiny
</label>
</section>
<section>
<h5>Color</h5>
<div class="color white"></div>
<div class="color red"></div>
<div class="color green"></div>
<div class="color blue"></div>
<div class="color yellow"></div>
<div class="color magenta"></div>
<div class="color cyan"></div>
<div class="color hue"></div>
</section>
</details>
<div id="fps">FPS: <span></span></div>
</header>
<canvas class="main"></canvas>
</body>
</html>