-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
117 lines (110 loc) · 3.3 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
<!DOCTYPE html>
<html>
<head>
<title>Sandpile</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
margin: 0;
overflow: hidden;
font-family: monospace;
}
canvas {
width: 100%;
}
div {
position: absolute;
width: 100%;
top: 0;
left: 0;
text-align: center;
margin: 8px 0;
}
input {
vertical-align: middle;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div>
Generation: <span id="gen">0</span><br>
Generations per frame: <input type="range" id="speed" min="1" max="300"> (<span id="speedValue">0</span>)
</div>
<script id="2d-vertex-shader" type="x-shader/x-vertex">
attribute vec2 a_position;
attribute vec2 a_texCoord;
uniform vec2 u_resolution;
varying vec2 v_texCoord;
void main() {
gl_Position = vec4(a_position, 0, 1);
v_texCoord = a_texCoord;
}
</script>
<script id="2d-fragment-shader" type="x-shader/x-fragment">
precision highp float;
varying vec2 v_texCoord;
uniform int u_counter;
uniform sampler2D u_image;
uniform vec2 u_resolution;
int get(float x, float y) {
vec2 p = 1.0/u_resolution;
vec2 coord = v_texCoord + vec2(x, y)*(1.0/u_resolution);
if(coord.x < p.x || coord.x > 1. || coord.y < p.y || coord.y > 1.) return 0;
else if(coord.x >= 0.5 && coord.x <= 0.5+p.x && coord.y >= 0.5 && coord.y <= 0.5+p.y) return 255;
else return int(texture2D(u_image, coord).r*255.0);
}
int mod(int a, int b) {
return int(float(a) - (float(b) * floor(float(a)/float(b))));
}
void main() {
vec2 p = 1.0/u_resolution;
vec3 color;
int h = get(0.0, 0.0);
int hLeft = get(-1.0, 0.0);
int hRight = get(1.0, 0.0);
int hTop = get(0.0, 1.0);
int hBottom = get(0.0, -1.0);
if(h >= 4) h -= 4;
if(hLeft >= 4) h += 1;
if(hRight >= 4) h += 1;
if(hTop >= 4) h += 1;
if(hBottom >= 4) h += 1;
if(v_texCoord.x < p.x || v_texCoord.y < p.y) h = 0;
color = vec3(float(h)/255.0, 0.0, 0.0);
gl_FragColor = vec4(color, 1.0);
}
</script>
<script id="final-fragment-shader" type="x-shader/x-fragment">
precision highp float;
varying vec2 v_texCoord;
uniform int u_counter;
uniform sampler2D u_image;
uniform vec2 u_resolution;
uniform vec3 u_color1;
uniform vec3 u_color2;
uniform vec3 u_color3;
uniform vec3 u_color4;
uniform vec2 u_size;
uniform float u_scale;
void main() {
vec3 color;
vec2 coord = ((v_texCoord-0.5)*u_scale)*(u_resolution/u_size)+0.5+(1.0/u_size)/2.0;
if(coord.x < 0.0 || coord.x > 1.0 || coord.y < 0.0 || coord.y > 1.0) {
color = vec3(0.98, 0.98, 0.98);
} else {
int height = int(texture2D(u_image, coord).r*255.0) + int(texture2D(u_image, coord).g*255.0)*256 + int(texture2D(u_image, coord).b*255.0)*256*256;
if(height == 0) color = vec3(0.98, 0.98, 0.98);
if(height == 1) color = u_color1/255.;
if(height == 2) color = u_color2/255.;
if(height == 3) color = u_color3/255.;
if(height > 3) color = u_color4/255.;
}
gl_FragColor = vec4(color, 1.0);
}
</script>
<script src="webgl-utils.js"></script>
<script src="main.js"></script>
</body>
</html>