-
Notifications
You must be signed in to change notification settings - Fork 70
/
conway-shader.html
282 lines (221 loc) · 8.71 KB
/
conway-shader.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
<!DOCTYPE html>
<html>
<head>
<title>A-Frame - custom shaders</title>
<meta name="description" content="A-Frame - custom shaders">
<script src="js/aframe-master-v1.3.0.min.js"></script>
<script src="js/aframe-environment-component.js"></script>
<script src="js/controller-listener.js"></script>
<script src="js/player-move.js"></script>
<script src="js/raycaster-extras.js"></script>
</head>
<body>
<script>
AFRAME.registerComponent('conway-game-of-life', {
schema: {
updateInterval: {type: 'float', default: 0.05},
displayPlaneId: {type: 'string', default: "#displayPlane"},
hiddenPlaneId: {type: 'string', default: "#hiddenPlane"},
},
init: function() {
console.log("life started")
const rtWidth = 1024;
const rtHeight = 1024;
this.renderTarget0 = new THREE.WebGLRenderTarget(rtWidth, rtHeight, {depthBuffer: false});
this.renderTarget0.texture.magFilter = THREE.NearestFilter;
this.renderTarget0.texture.minFilter = THREE.NearestFilter;
this.renderTarget0.texture.generateMipmaps = false;
this.renderTarget1 = new THREE.WebGLRenderTarget(rtWidth, rtHeight, {depthBuffer: false});
this.renderTarget1.texture.magFilter = THREE.NearestFilter;
this.renderTarget1.texture.minFilter = THREE.NearestFilter;
this.renderTarget1.texture.generateMipmaps = false;
// Materials for Conway's Game of Life =========================================================
function createConwayMaterial(renderTarget, resolution=1024, state=0)
{
return new THREE.ShaderMaterial( {
uniforms: {
tex: { type: "t", value: renderTarget.texture},
resolution: { type: "float", value: resolution },
state: { type: "int", value: state },
},
vertexShader: `
varying vec2 vUv;
void main()
{
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`,
fragmentShader: `
// returns a random-ish value in [0, 1]
float random(vec2 v)
{
return fract(sin(dot(v, vec2(12.9898, 78.233)))*43758.5453123);
}
varying vec2 vUv;
uniform sampler2D tex;
uniform float resolution;
uniform int state;
void main()
{
// state 0: display texture (used for copying data)
if (state == 0)
{
gl_FragColor = texture2D(tex, vUv);
}
// state 1: random pixels
else if (state == 1)
{
float r = random( floor(vUv * resolution) / resolution );
// some on, some off
if (r < 0.5)
r = 0.0;
else
r = 1.0;
gl_FragColor = vec4( r,r,r, 1 );
}
// state 2: apply Conway's Game of Life rules
else if (state == 2)
{
float s = 1.0 / resolution;
vec4 data = 0.0 + texture2D(tex, vUv + vec2(-s, -s))
+ texture2D(tex, vUv + vec2(-s, 0))
+ texture2D(tex, vUv + vec2(-s, +s))
+ texture2D(tex, vUv + vec2(0, -s))
+ texture2D(tex, vUv + vec2(0, +s))
+ texture2D(tex, vUv + vec2(+s, -s))
+ texture2D(tex, vUv + vec2(+s, 0))
+ texture2D(tex, vUv + vec2(+s, +s));
// use r component to count alive neighbors
if (data.r < 2.0 || data.r > 3.0) // death
{
gl_FragColor = vec4(0, 0, 0, 1);
}
else if (data.r == 3.0) // life
{
gl_FragColor = vec4(1, 1, 1, 1);
}
else // same color when two neighbors alive
{
gl_FragColor = texture2D(tex, vUv);
}
}
// state X: something went wrong (red alert)
else
{
gl_FragColor = vec4(1,0,0,1);
}
}
`
});
}
// =========================================================
// separate scene #0 for texture processing
const geometry0 = new THREE.PlaneGeometry(2, 2);
this.material0 = createConwayMaterial( this.renderTarget1, 1024, 1 ); // after first update, switch to state 0 to copy pixels
const plane0 = new THREE.Mesh(geometry0, this.material0);
this.rtScene0 = new THREE.Scene();
this.rtScene0.add(plane0);
// separate scene #1 for texture processing
const geometry1 = new THREE.PlaneGeometry(2, 2);
this.material1 = createConwayMaterial( this.renderTarget0, 1024, 2 );
const plane1 = new THREE.Mesh(geometry1, this.material1);
this.rtScene1 = new THREE.Scene();
this.rtScene1.add(plane1);
// both scenes can use the same basic camera
this.rtCamera = new THREE.OrthographicCamera();
this.rtCamera.position.z = 0.1;
// only update textures periodically
this.timer = 0;
this.clock = new THREE.Clock();
// initialize during first tick()
this.plane0 = null;
this.plane1 = null;
},
tick: function() {
if (this.plane0 == null)
{
this.plane0 = document.querySelector(this.data.displayPlaneId);
this.plane1 = document.querySelector(this.data.hiddenPlaneId);
this.plane0.object3D.children[0].material = this.material0;
this.plane1.object3D.children[0].material = this.material1;
}
let deltaTime = this.clock.getDelta();
this.timer += deltaTime;
if (this.timer > this.data.updateInterval)
{
// console.log("tick update")
// reset the timer
this.timer = 0;
// temporarily disable XR to prevent screen flickering
// thanks to Synn, Marquizzo, and Mugen87
// https://github.com/mrdoob/three.js/issues/18746#issuecomment-591441598
const renderer = this.el.sceneEl.renderer;
const currentRenderTarget = renderer.getRenderTarget();
const currentXrEnabled = renderer.xr.enabled;
const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
renderer.xr.enabled = false;
renderer.shadowMap.autoUpdate = false;
// take a snapshot of scene0, copy it into renderTarget0.
// the texture from renderTarget0 is used in material1,
// so this updates material1 in the main scene.
renderer.setRenderTarget(this.renderTarget0);
renderer.render(this.rtScene0, this.rtCamera);
// connect up the loop: after material1 has copied initial data,
// set material0 state to copy pixel data from renderTarget1
// (this has no effect after first update)
this.material0.uniforms.state.value = 0;
// take a snapshot of scene1, copy it into renderTarget1.
// the texture from renderTarget1 is used in material0,
// so this updates material0 in the main scene.
renderer.setRenderTarget(this.renderTarget1);
renderer.render(this.rtScene1, this.rtCamera);
renderer.setRenderTarget(null);
// enable XR
renderer.xr.enabled = currentXrEnabled;
renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
renderer.setRenderTarget(currentRenderTarget);
}
},
});
</script>
<a-scene environment>
<a-assets>
<img id="gradient" src="images/gradient-fade.png" />
</a-assets>
<a-sky color = "#000337"></a-sky>
<a-entity
id="player"
position="0 0 0"
player-move="controllerListenerId: #controller-data;
navigationMeshClass: environmentGround;">
<a-camera></a-camera>
<a-entity
id="controller-data"
controller-listener="leftControllerId: #left-controller;
rightControllerId: #right-controller;">
</a-entity>
<a-entity
id="left-controller"
oculus-touch-controls="hand: left">
</a-entity>
<a-entity
id="right-controller"
oculus-touch-controls="hand: right"
raycaster="objects: .raycaster-target, .environmentGround;"
raycaster-extras="controllerListenerId: #controller-data;
beamImageSrc: #gradient; beamLength: 0.5;">
</a-entity>
</a-entity>
<a-entity conway-game-of-life="displayPlaneId: #displayPlane;
hiddenPlaneId: #hiddenPlane;">
</a-entity>
<a-plane
id="displayPlane"
position = "0 1 -2"
width="2" height="2">
</a-plane>
<a-plane id="hiddenPlane" visible="false"></a-plane>
</a-scene>
</body>
</html>