forked from xpl/turing-2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
567 lines (497 loc) · 19.5 KB
/
index.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<html>
<head>
<title>Cellular Expression</title>
<meta http-equiv="Content-Type" content="charset=utf-8">
<meta name="description" content="An interactive paint application driven by cellular automation and WebGL.">
<meta property="og:title" content="Cellular Expression" />
<meta property="og:type" content="website" />
<meta property="og:description" content="An interactive paint application driven by cellular automation." />
<meta property="og:url" content="http://xpl.github.com/expression/" />
<meta property="og:image" content="http://xpl.github.com/expression/preview.jpg" />
<meta property="og:site_name" content="Cellular Expression" />
<meta property="fb:admins" content="1174148385" />
<link rel="image_src" href="http://xpl.github.com/expression/preview.jpg" />
<link rel="stylesheet" type="text/css" href="node_modules/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="main.css" />
<script src="https://www.google.com/jsapi"></script>
<script src="node_modules/jquery-dist/dist/jquery.min.js"></script>
<script src="jquery.mousewheel.js"></script>
<script src="node_modules/underscore/underscore-min.js"></script>
<script src="node_modules/useless/build/useless.micro.js"></script>
<script src="node_modules/useless/build/useless.devtools.js"></script>
<script src="node_modules/useless/base/Rx.js"></script>
<script src="node_modules/useless/base/AOP.js"></script>
<script src="dom/node+.js"></script>
<script src="dom/reference_holder.js"></script>
<script src="gl-matrix-min.js"></script>
<script src="util.js"></script>
<script src="3d.js"></script>
<script src="main.js"></script>
<!-- shaders -->
<script id="cell-random-noise-fs" type="x-shader/x-fragment">
#version 100
precision highp float;
varying vec2 fragmentPosition, texCoord;
uniform vec2 seed, seed2, seed3, screenSpace;
uniform float gridSize;
float rand (vec2 co){
return fract (sin (dot (co.xy,vec2 (12.9898,78.233))) * 43758.5453);
}
void main (void) {
float gridSize = pow (2.0, gridSize);
//float gridSize = pow (2.0, 16.0);
vec2 gridPosition = fragmentPosition / (screenSpace * gridSize);
vec2 grid = fract (gridPosition);
float thresh = (gridSize - 1.1) / gridSize;
if (grid.x > thresh && grid.y > thresh) {
/*gl_FragColor = vec4 (
1.0,
rand (seed + texCoord.xx),
rand (seed2 + texCoord.yy), 1.0);*/
/*gl_FragColor = vec4 (
1.0,
abs (fragmentPosition.x),
abs (fragmentPosition.y), 1.0);*/
/*gl_FragColor = vec4 (
1.0,
rand (seed + texCoord.xx),
rand (seed2 + texCoord.yy), 1.0);*/
/*gl_FragColor = vec4 (
1.0,
fract (rand (seed)),
fract (rand (seed2)), 1.0);*/
/*gl_FragColor = vec4 (
1.0,
rand (seed + texCoord.xx),
rand (seed2 + texCoord.yy), 1.0);
gl_FragColor = vec4 (
1.0,
abs (fragmentPosition.x),
abs (fragmentPosition.y), 1.0);
gl_FragColor = vec4 (
1.0,
rand (fract (fragmentPosition.xx * 4.0)),
rand (fract (fragmentPosition.yy * 2.0)), 1.0);
gl_FragColor = vec4 (
1.0,
fragmentPosition.x,
fragmentPosition.y, 1.0);*/
gl_FragColor = vec4 (
rand (texCoord),
seed.x,
seed.y, 1.0);
} else {
/*vec2 gridPosition = fragmentPosition / (screenSpace * gridSize);
gl_FragColor = vec4 (0.0,
texCoord.x,
texCoord.y,
rand (seed + floor (gridPosition)));*/
gl_FragColor = vec4 (0.0);
}
//gl_FragColor = vec4 (1.0);
}
</script>
<script id="update-program-fs" type="x-shader/x-fragment">
#version 100
precision mediump float;
varying vec2 texCoord;
uniform sampler2D previousStep, rules, forkRules;
uniform vec2 screenSpace, seed;
uniform float lifeDecrease;
vec4 pixelAt (vec2 offset) {
return texture2D (previousStep, texCoord + offset * screenSpace);
}
float rand (vec2 co){
return fract (sin (dot (co.xy,vec2 (12.9898,78.233))) * 43758.5453);
}
float decreaseLife (float life) {
return max (0.0, life - lifeDecrease);
}
float dotdot (vec2 x) {
return dot (x, x);
}
/* -1, 1 0, 1 1, 1
-1, 0 ++ 1, 0
-1,-1 0,-1, 1,-1 */
vec2 moveDirection8 (float action) {
return
(action > 0.875 ? vec2 ( -1, 1) :
(action > 0.75 ? vec2 ( 0.0, 1.0) :
(action > 0.625 ? vec2 ( 1, 1) :
(action > 0.5 ? vec2 ( 1.0, 0.0) :
(action > 0.375 ? vec2 ( 1, -1) :
(action > 0.25 ? vec2 ( 0.0, -1.0) :
(action > 0.125 ? vec2 ( -1, -1) :
vec2 ( -1.0, 0.0)))))))); }
vec2 moveDirectionX (float action) {
return
(action > 0.75 ? vec2 ( -1, 1) :
(action > 0.50 ? vec2 ( 1, 1) :
(action > 0.25 ? vec2 ( 1, -1) :
vec2 ( -1, -1)))); }
vec2 moveDirectionQ (float action) {
return
(action > 0.74 ? vec2 ( 0.0, -1.0) :
(action > 0.49 ? vec2 ( 1.0, 0.0) :
(action > 0.24 ? vec2 ( 0.0, 1.0) :
vec2 ( -1.0, 0.0))));
}
vec4 getCommand (vec4 cell) {
return texture2D (rules, cell.yz); }
/* Evaluates neighbour cell and returns either:
a) New cell value (if alive and wants to move at current position)
b) Current cell value otherwise
*/
vec4 convolute (vec2 address, vec4 current) { vec4 cell = pixelAt (address);
/* If cell is alive
*/
if (cell.x > 0.95) {
/* Evaluate
*/
vec4 command = getCommand (cell);
float writeSymbol = command.y;
float nextState = command.z;
float moveTape = command.w;
/* If cell wants to move at current fragment position..
*/
if (dotdot (moveDirectionX (moveTape) + address) < 0.01) {
return vec4 (1.0, nextState, current.z, 1.0); }
}
return current;
}
/* ---------------------> */
/* -1, 1 0, 1 1, 1
-1, 0 ++ 1, 0
-1,-1 0,-1, 1,-1 */
vec4 step8 (vec4 cell) { vec4 command = getCommand (cell);
return
convolute (vec2 (-1, 1),
convolute (vec2 ( 0.0, 1.0),
convolute (vec2 ( 1, 1),
convolute (vec2 ( 1.0, 0.0),
convolute (vec2 ( 1, -1),
convolute (vec2 ( 0.0, -1.0),
convolute (vec2 (-1, -1),
convolute (vec2 (-1.0, 0.0),
(cell.x > 0.95) ?
vec4 (0.94, cell.y, command.y, command.w) :
vec4 (decreaseLife (cell.x), cell.y ,cell.z, cell.w))))))))); }
vec4 stepX (vec4 cell) { vec4 command = getCommand (cell);
return
convolute (vec2 (-1, 1),
convolute (vec2 ( 1, 1),
convolute (vec2 ( 1, -1),
convolute (vec2 (-1, -1),
(cell.x > 0.95) ?
vec4 (0.94, cell.y, command.y, command.w) :
vec4 (decreaseLife (cell.x), cell.y ,cell.z, cell.w))))); }
vec4 stepQ (vec4 cell) { vec4 command = getCommand (cell);
return
convolute (vec2 ( 0.0, -1.0),
convolute (vec2 ( 1.0, 0.0),
convolute (vec2 ( 0.0, 1.0),
convolute (vec2 (-1.0, 0.0),
(cell.x > 0.95) ?
vec4 (0.94, cell.y, command.y, // write symbol
command.w) :
vec4 (decreaseLife (cell.x), cell.y ,cell.z, cell.w))))); }
void main (void) {
gl_FragColor = stepX (pixelAt (vec2 (0.0, 0.0)));
}
</script>
<script id="cell-brush-fs" type="x-shader/x-fragment">
#version 100
precision highp float;
varying vec2 fragmentPosition;
varying vec2 texCoord;
uniform sampler2D cells;
uniform sampler2D image;
uniform vec2 brushPosition1;
uniform vec2 brushPosition2;
uniform float brushSize;
uniform mat4 pixelSpace;
uniform vec2 screenSpace;
uniform vec2 seed;
uniform bool noise;
uniform bool animate;
uniform float hue;
uniform float fill;
float rand (vec2 co) {
return fract (sin (dot (co.xy ,vec2 (12.9898,78.233))) * 43758.5453);
}
float randBool (vec2 co) {
return rand (co) > 0.6 ? 1.0 : 0.0;
}
float dotdot (vec2 x) { return dot (x, x); }
float distToSegmentSquared (vec2 v, vec2 w, vec2 p) {
float l2 = dotdot (v - w);
if (l2 == 0.0) return dotdot (p - v);
float t = dot(p - v, w - v) / l2;
if (t < 0.0) return dotdot (p - v);
else if (t > 1.0) return dotdot (p - w);
vec2 projection = v + t * (w - v);
return dotdot (p - projection);
}
vec4 pixelAt (in sampler2D tex, vec2 offset) {
return texture2D (tex, texCoord + offset * screenSpace);
}
void main (void) {
if (distToSegmentSquared (
(vec4(brushPosition1,1,1)*pixelSpace).xy,
(vec4(brushPosition2,1,1)*pixelSpace).xy,
(vec4(fragmentPosition,1,1)*pixelSpace).xy) < brushSize * brushSize) {
/* float gridSize = 64.0;
float noise = rand (seed);
vec2 grid = fract (fragmentPosition / (screenSpace * gridSize));
float thresh = (gridSize - 1.1) / gridSize;
float alive = fill * (grid.x > thresh && grid.y > thresh ? 1.0 : 0.0);
float state = rand (seed);
gl_FragColor = vec4 (alive, state, rand (seed + 0.2), rand (seed + 0.5));*/
gl_FragColor = pixelAt (image, vec2 (0, 0));
} else {
gl_FragColor = pixelAt (cells, vec2 (0, 0));
}
}
</script>
<script id="cell-vs-pixeloffset" type="x-shader/x-vertex">
#version 100
precision highp float;
attribute vec3 position;
uniform vec2 pixelOffset;
varying vec2 fragmentPosition;
varying vec2 texCoord;
void main (void) {
fragmentPosition = position.xy;
texCoord = (position.xy + 1.0) * 0.5;
gl_Position = vec4 (position.xy + pixelOffset, 0.0, 1.0);
}
</script>
<script id="cell-vs" type="x-shader/x-vertex">
#version 100
precision mediump float;
attribute vec3 position;
varying vec2 fragmentPosition;
varying vec2 texCoord;
void main (void) {
fragmentPosition = position.xy;
texCoord = (position.xy + 1.0) * 0.5;
gl_Position = vec4 (position.xy, 0.0, 1.0);
}
</script>
<script id="draw-cells-fs" type="x-shader/x-fragment">
#version 100
precision mediump float;
varying vec2 texCoord;
uniform sampler2D cells, rules;
float pi = 3.14159265358979323846264;
vec3 hue (float H)
{
float R = abs(H * 6.0 - 3.0) - 1.0;
float G = 2.0 - abs(H * 6.0 - 2.0);
float B = 2.0 - abs(H * 6.0 - 4.0);
return clamp(vec3(R,G,B), 0.0, 1.0);
}
vec3 hsl (float h, float s, float l) {
vec3 RGB = hue (h);
float C = (1.0 - abs(2.0 * l - 1.0)) * s;
return (RGB - 0.5) * C + l;
}
void main (void) {
vec4 cell = texture2D (cells, texCoord);
/*float colorOffset = sin(cell.z*pi*6.0);
gl_FragColor = vec4 (hsl (
fract (cell.z + cell.x * colorOffset*0.05),
1.0,
cell.y * 0.5 + cell.x * (1.0 - abs(colorOffset)) * 0.15) * cell.w, 1.0);*/
float cursor = cell.x;
float state = cell.y;
float symbol = cell.z;
float action = cell.w;
//gl_FragColor = vec4 (hsl (symbol, 1.0, cursor), 1.0);
//gl_FragColor = vec4 (texture2D (rules, texCoord).xyz, 1);
gl_FragColor = vec4 (hsl (fract (state * symbol + action), state, cursor), 1.0);
}
</script>
<script id="downsample-cells-fs" type="x-shader/x-fragment">
#version 100
precision highp float;
varying vec2 texCoord;
uniform sampler2D source;
uniform vec2 screenSpace;
uniform vec2 t1, t2;
float clerp (float b, float a, float delta) {
float distance = a - b;
float absDistance = abs (distance);
if (absDistance > 0.5) {
distance = sign (distance) * (absDistance - 1.0);
}
return fract (b + distance * delta);
}
/*
if (a.z > 0.5) {
return b < 0.0 ? a.w : clerp (a.w, b);
} else {
return b;
}*/
vec4 blend (vec4 a, vec4 b) {
float sum = a.z + b.z;
return vec4 (0.0, 0.0, sum, a.z > 0.1 ? (b.w < 0.0 ? a.w : clerp (a.w, b.w, b.w / sum)) : b.w);
}
vec4 sample (float x, float y) {
return texture2D (source, (texCoord - screenSpace * vec2 (1.0)) + screenSpace * (vec2 (x, y) - vec2 (1.0)));
}
void main (void) {
vec4 cell1 = sample (0.0, 0.0), cell2 = sample (1.0, 0.0), cell3 = sample (2.0, 0.0), cell4 = sample (3.0, 0.0);
vec4 cell5 = sample (0.0, 1.0), cell6 = sample (1.0, 1.0), cell7 = sample (2.0, 1.0), cell8 = sample (3.0, 1.0);
vec4 cell9 = sample (0.0, 2.0), cell10 = sample (1.0, 2.0), cell11 = sample (2.0, 2.0), cell12 = sample (3.0, 2.0);
vec4 cell13 = sample (0.0, 3.0), cell14 = sample (1.0, 3.0), cell15 = sample (2.0, 3.0), cell16 = sample (3.0, 3.0);
vec4 sum =
blend (cell1, blend (cell2, blend (cell3, blend (cell4,
blend (cell5, blend (cell6, blend (cell7, blend (cell8,
blend (cell9, blend (cell10, blend (cell11, blend (cell12,
blend (cell13, blend (cell14, blend (cell15, blend (cell16, vec4 (0.0, 0.0, 0.0, -1.0)))))))))))))))));
gl_FragColor = vec4 (0.0, 0.0, sum.z / 16.0, sum.w);
}
</script>
<script id="convert-colors-fs" type="x-shader/x-fragment">
#version 100
precision mediump float;
varying vec2 texCoord;
uniform sampler2D cells, rules;
vec3 rgb2hsl (vec3 v) {
float maxx = max (max (v.r, v.g), v.b), minn = min (min (v.r, v.g), v.b);
float h, s, l = (maxx + minn) * 0.5;
if (abs (maxx - minn) < 0.001) {
return vec3 (0.0, 0.0, maxx); }
else {
float d = maxx - minn;
s = (l > 0.5) ? d / (2.0 - maxx - minn) : d / (maxx + minn);
if (abs (maxx - v.r) < 0.001) { h = (v.g - v.b) / d + (v.g < v.b ? 6.0 : 0.0); }
else if (abs (maxx - v.g) < 0.001) { h = (v.b - v.r) / d + 2.0; }
else if (abs (maxx - v.b) < 0.001) { h = (v.r - v.g) / d + 4.0; }
return vec3 (h / 6.0, s, l);
}
}
uniform sampler2D tex;
float threshold(in float thr1, in float thr2 , in float val) {
if (val < thr1) {return 0.0;}
if (val > thr2) {return 1.0;}
return val;
}
// averaged pixel intensity from 3 color channels
float avg_intensity(in vec4 pix) { return (pix.r + pix.g + pix.b)/3.; }
vec4 get_pixel(in vec2 coords, in float dx, in float dy) { return texture2D(cells,coords + vec2(dx, dy)); }
// returns pixel color
float IsEdge(in vec2 coords){
float dxtex = 1.0 / 512.0 /*image width*/;
float dytex = 1.0 / 512.0 /*image height*/;
float pix[9];
int k = -1;
float delta;
pix[0] = avg_intensity(get_pixel(coords,float(-1)*dxtex,
float(-1)*dytex));
pix[1] = avg_intensity(get_pixel(coords,float(-1)*dxtex,
float( 0)*dytex));
pix[2] = avg_intensity(get_pixel(coords,float(-1)*dxtex,
float( 1)*dytex));
pix[3] = avg_intensity(get_pixel(coords,float( 0)*dxtex,
float(-1)*dytex));
pix[4] = avg_intensity(get_pixel(coords,float( 0)*dxtex,
float( 0)*dytex));
pix[5] = avg_intensity(get_pixel(coords,float( 0)*dxtex,
float( 1)*dytex));
pix[6] = avg_intensity(get_pixel(coords,float( 1)*dxtex,
float(-1)*dytex));
pix[7] = avg_intensity(get_pixel(coords,float( 1)*dxtex,
float( 0)*dytex));
pix[8] = avg_intensity(get_pixel(coords,float( 1)*dxtex,
float( 1)*dytex));
// average color differences around neighboring pixels
delta = (abs(pix[1]-pix[7])+
abs(pix[5]-pix[3]) +
abs(pix[0]-pix[8])+
abs(pix[2]-pix[6])
)/4.;
return threshold(0.25,0.4,clamp(1.8*delta,0.0,1.0));
}
void main (void) {
vec4 cell = texture2D (cells, texCoord);
float cursor = cell.x;
float state = cell.y;
float symbol = cell.z;
float action = cell.w;
vec3 hsl = rgb2hsl (cell.xyz);
//gl_FragColor = vec4 (hsl (fract (state * symbol + action), state, cursor), 1.0);
// out.x = hsl.z
// out.y = hsl.y
// fract (out.y * out.z + out.w) = hsl.x
// out.y * out.z = hls.x - out.w
// out.z = (hsl.x - out.w) / out.y
// out.z = (hsl.x - out.w) / hsl.y
float edge = IsEdge(texCoord.xy);
gl_FragColor = vec4 (pow (cell.xyzw, vec4 (edge)));//vec4 (hsl.x, hsl.y, hsl.z, 1.0);
}
</script>
<script id="downsample-cells-fs" type="x-shader/x-fragment">
#version 100
precision highp float;
varying vec2 texCoord;
uniform sampler2D source;
uniform vec2 screenSpace;
uniform vec2 t1, t2;
float clerp (float b, float a, float delta) {
float distance = a - b;
float absDistance = abs (distance);
if (absDistance > 0.5) {
distance = sign (distance) * (absDistance - 1.0);
}
return fract (b + distance * delta);
}
/*
if (a.z > 0.5) {
return b < 0.0 ? a.w : clerp (a.w, b);
} else {
return b;
}*/
vec4 blend (vec4 a, vec4 b) {
float sum = a.z + b.z;
return vec4 (0.0, 0.0, sum, a.z > 0.1 ? (b.w < 0.0 ? a.w : clerp (a.w, b.w, b.w / sum)) : b.w);
}
vec4 sample (float x, float y) {
return texture2D (source, (texCoord - screenSpace * vec2 (1.0)) + screenSpace * (vec2 (x, y) - vec2 (1.0)));
}
void main (void) {
vec4 cell1 = sample (0.0, 0.0), cell2 = sample (1.0, 0.0), cell3 = sample (2.0, 0.0), cell4 = sample (3.0, 0.0);
vec4 cell5 = sample (0.0, 1.0), cell6 = sample (1.0, 1.0), cell7 = sample (2.0, 1.0), cell8 = sample (3.0, 1.0);
vec4 cell9 = sample (0.0, 2.0), cell10 = sample (1.0, 2.0), cell11 = sample (2.0, 2.0), cell12 = sample (3.0, 2.0);
vec4 cell13 = sample (0.0, 3.0), cell14 = sample (1.0, 3.0), cell15 = sample (2.0, 3.0), cell16 = sample (3.0, 3.0);
vec4 sum =
blend (cell1, blend (cell2, blend (cell3, blend (cell4,
blend (cell5, blend (cell6, blend (cell7, blend (cell8,
blend (cell9, blend (cell10, blend (cell11, blend (cell12,
blend (cell13, blend (cell14, blend (cell15, blend (cell16, vec4 (0.0, 0.0, 0.0, -1.0)))))))))))))))));
gl_FragColor = vec4 (0.0, 0.0, sum.z / 16.0, sum.w);
}
</script>
<script id="simple-vs" type="x-shader/x-vertex">
#version 100
precision mediump float;
attribute vec3 position;
varying vec2 texCoord;
uniform mat4 transform;
void main (void) {
texCoord = ((position + 1.0) * 0.5).xy;
gl_Position = transform * vec4 (position, 1.0);
}
</script>
</head>
<body>
<div class="viewport-container">
<canvas class="viewport"></canvas>
<div class="draw-prompt">
<h1>The Toroidal Turing Machine</h1>
<div class="hint"><em>Esc</em> to reload, <em>Tab</em> to reset, <em>Enter</em> to reprogram</div>
</div>
</div>
</body>
</html>