-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
95 lines (78 loc) · 2.23 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
<body>
<script src="js/three.min.js"></script>
<script src="js/dat.gui.min.js"></script>
<script id="vertexShader" type="x-shader/x-vertex">
void main() {
gl_Position = vec4( position, 1.0 );
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform vec2 u_resolution;
uniform vec3 u_mouse;
uniform sampler2D u_currentTexture;
uniform int u_frameCount;
uniform float u_mouseSize;
uniform vec3 u_newLifeColor;
uniform vec3 u_survivorColor;
uniform int u_paused;
//random noise function for initial texture
highp float rand(vec2 co)
{
highp float a = 12.9898;
highp float b = 78.233;
highp float c = 43758.5453;
highp float dt= dot(co.xy ,vec2(a,b));
highp float sn= mod(dt,3.14);
return fract(sin(sn) * c);
}
float v(float xrel, float yrel)
{
vec2 xy;
xy.x = mod(gl_FragCoord.x + xrel, u_resolution.x);
xy.y = mod(gl_FragCoord.y + yrel, u_resolution.y);
return texture2D(u_currentTexture, xy/u_resolution).a;
}
float neighborSum()
{
return v(-1.,-1.) +
v(-1.,0.) +
v(-1.,1.) +
v(0.,-1.) +
v(0.,1.) +
v(1.,-1. )+
v(1.,0.) +
v(1.,1.);
}
void main()
{
float minRes = min(u_resolution.x, u_resolution.y);
vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / minRes;
float inputSize = u_mouseSize / minRes;
float fate = float(v(0.,0.) == 1.);
float before = fate;
if(u_frameCount < 2) //create random initial texture first 2 frames of program
{
float _rand = rand(uv*256.);
fate = clamp(floor(_rand * _rand * _rand * 1.2), 0., 1.);
}
else if (u_paused == 1)//calculate neighbor totals
{
float sum = neighborSum();
bool a = (sum == 3. || (fate==1. && (sum == 2.)));
fate = float(a);
}
bool userInput = (u_mouse.z > 0.) &&
((uv.x >= u_mouse.x - inputSize && uv.x < u_mouse.x + inputSize) &&
(uv.y >= u_mouse.y - inputSize && uv.y < u_mouse.y + inputSize));
fate += float(userInput);
gl_FragColor = vec4(fate);
//change color based on status change
if(fate != before) {
gl_FragColor = vec4(vec3(u_survivorColor)/255.,fate);
} else if (before == fate && v(0.,0.) == 1.) {
gl_FragColor = vec4(vec3(u_newLifeColor)/255.,fate);
}
}
</script>
<script src="app.js"></script>
</body>