-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree.html
360 lines (304 loc) · 12.8 KB
/
three.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Game of Life 3D</title>
<style>
canvas { display: block; }
body {
overflow: hidden;
margin: 0;
}
.ui {
position: absolute;
}
button{
margin: 20px;
}
#restart {
margin-top: 20px
}
#glider {
margin-top: 40px
}
#clock {
margin-top: 60px
}
#collision {
margin-top: 80px
}
#beacons {
margin-top: 100px
}
</style>
</head>
<body>
<button onClick="restart()" class="ui" id="restart">Reset</button>
<button onClick="addGlider()" class="ui" id="glider">Add Glider</button>
<button onClick="addClock()" class="ui" id="clock">Add Clock</button>
<button onClick="addCollision()" class="ui" id="collision">Add Collision</button>
<button onClick="addBeacons()" class="ui" id="beacons">Add Beacons</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r99/three.min.js"></script>
<script type="text/javascript" src="./GoLCube.js"></script>
<script type="text/javascript" src="./Glider.js"></script>
<script type="text/javascript" src="./Clock.js"></script>
<script type="text/javascript" src="./Collision.js"></script>
<script type="text/javascript" src="./Beacons.js"></script>
<script>
let scene = new THREE.Scene();
scene.background = new THREE.Color( 0xf0f0f0 );
let camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
let renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
camera.position.z = 30;
camera.position.x = 10;
let cubeSize = 1;
let rules = {
liveMin: 4,
liveMax: 5,
deadMin: 5,
deadMax: 5
}
let black = new THREE.Color("rgb(255, 255, 255)");
let white = new THREE.Color("rgb(0, 0, 0)");
const assignRandomState = function() {
let random = Math.random() >= 0.8 ;
if(random) {
return true
} else {
return false
}
}
function addLight(...pos) {
const color = 0xFFFFFF;
const intensity = 3;
const light = new THREE.DirectionalLight(color, intensity);
light.position.set(...pos);
scene.add(light);
}
addLight(-10, 60, 4);
addLight( 30, 50, -40);
let arraySize = 15
let cubeStore = []
let primaryStateStore = []
let secondaryStateStore = []
//pivot to hold cubes
let pivot = new THREE.Group();
scene.add( pivot );
//Create matrices with cube objects / false initial states
for(let x = 0; x < arraySize; x++) {
let cubeStoreY = new Array(arraySize)
let primaryY = new Array(arraySize)
let secondaryY = new Array(arraySize)
cubeStore.push(cubeStoreY)
primaryStateStore.push(primaryY)
secondaryStateStore.push(secondaryY)
for(let y = 0; y < arraySize; y++) {
let cubeStoreZ = []
let primaryZ = []
let secondaryZ = []
cubeStore[x][y] = cubeStoreZ
primaryStateStore[x][y] = primaryZ
secondaryStateStore[x][y] = secondaryZ
for(let z = 0; z < arraySize; z++) {
let newCube = new GoLCube(x, y, z)
cubeStore[x][y][z] = newCube
primaryStateStore[x][y][z] = false
secondaryStateStore[x][y][z] = false
}
}
}
//Add created cube objects to the scene
for(let x = 0; x < arraySize; x++) {
for(let y = 0; y < arraySize; y++) {
for(let z = 0; z < arraySize; z++) {
primaryStateStore[x][y][z] = assignRandomState()
scene.add(cubeStore[x][y][z].mesh)
}
}
}
function randomizeCells() {
for(let x = 0; x < arraySize; x++) {
for(let y = 0; y < arraySize; y++) {
for(let z = 0; z < arraySize; z++) {
primaryStateStore[x][y][z] = assignRandomState()
}
}
}
}
function addGlider() {
let glider = new Glider()
rules.liveMin = 4
rules.liveMax = 5
rules.deadMin = 5
rules.deadMax = 5
programRunning = false
for(let x = 0; x < arraySize; x++) {
for(let y = 0; y < arraySize; y++) {
for(let z = 0; z < arraySize; z++) {
primaryStateStore[x][y][z] = false
secondaryStateStore[x][y][z] = false
}
}
}
for(let i = 0; i < glider.shape.length; i++) {
primaryStateStore[glider.shape[i][0]][glider.shape[i][1]][glider.shape[i][2]] = true
}
programRunning = true
}
function addCollision() {
let collision = new Collision()
rules.liveMin = 5
rules.liveMax = 7
rules.deadMin = 6
rules.deadMax = 6
programRunning = false
for(let x = 0; x < arraySize; x++) {
for(let y = 0; y < arraySize; y++) {
for(let z = 0; z < arraySize; z++) {
primaryStateStore[x][y][z] = false
secondaryStateStore[x][y][z] = false
}
}
}
for(let i = 0; i < collision.shape.length; i++) {
primaryStateStore[collision.shape[i][0]][collision.shape[i][1]][collision.shape[i][2]] = true
}
programRunning = true
}
function addClock() {
let clock = new Clock()
rules.liveMin = 5
rules.liveMax = 7
rules.deadMin = 6
rules.deadMax = 6
programRunning = false
for(let x = 0; x < arraySize; x++) {
for(let y = 0; y < arraySize; y++) {
for(let z = 0; z < arraySize; z++) {
primaryStateStore[x][y][z] = false
secondaryStateStore[x][y][z] = false
}
}
}
for(let i = 0; i < clock.shape.length; i++) {
primaryStateStore[clock.shape[i][0]][clock.shape[i][1]][clock.shape[i][2]] = true
}
programRunning = true
}
function addBeacons() {
let beacons = new Beacons()
rules.liveMin = 6
rules.liveMax = 7
rules.deadMin = 7
rules.deadMax = 7
programRunning = false
for(let x = 0; x < arraySize; x++) {
for(let y = 0; y < arraySize; y++) {
for(let z = 0; z < arraySize; z++) {
primaryStateStore[x][y][z] = false
secondaryStateStore[x][y][z] = false
}
}
}
for(let i = 0; i < beacons.shape.length; i++) {
primaryStateStore[beacons.shape[i][0]][beacons.shape[i][1]][beacons.shape[i][2]] = true
}
programRunning = true
}
function restart() {
programRunning = false
for(let x = 0; x < arraySize; x++) {
for(let y = 0; y < arraySize; y++) {
for(let z = 0; z < arraySize; z++) {
primaryStateStore[x][y][z] = false
secondaryStateStore[x][y][z] = false
}
}
}
for(let x = Math.floor(arraySize * .10); x < Math.floor(arraySize * .9); x++) {
for(let y = Math.floor(arraySize * .10); y < Math.floor(arraySize * .9); y++) {
for(let z = Math.floor(arraySize * .10); z < Math.floor(arraySize * .9); z++) {
primaryStateStore[x][y][z] = assignRandomState()
}
}
}
programRunning = true
}
function updateNext(x, y, z, stateStore, updateStore) {
let numNeighbors = 0
let translatedX = x
let translatedY = y
let translatedZ = z
for(let xCoord = -1; xCoord < 2; xCoord++) {
for(let yCoord = -1; yCoord < 2; yCoord++) {
for(let zCoord = -1; zCoord < 2; zCoord++) {
//Allows for wrap around, can refactor /w else if
if(x + xCoord === -1) translatedX = arraySize - 1
if(y + yCoord === -1) translatedY = arraySize - 1
if(z + zCoord === -1) translatedZ = arraySize - 1
if(x + xCoord === arraySize) translatedX = 0
if(y + yCoord === arraySize) translatedY = 0
if(z + zCoord === arraySize) translatedZ = 0
if(x + xCoord != arraySize && x + xCoord != -1) translatedX = x + xCoord
if(y + yCoord != arraySize && y + yCoord != -1) translatedY = y + yCoord
if(z + zCoord != arraySize && z + zCoord != -1) translatedZ = z + zCoord
//If value in store is true we can increase the neighbor count
if(stateStore[translatedX][translatedY][translatedZ]) numNeighbors++
}
}
}
//remove 1 for counting self in above loops
if(stateStore[x][y][z]) numNeighbors--
// //rules
if (stateStore[x][y][z] && (numNeighbors >= rules.liveMin && numNeighbors <= rules.liveMax)) {
updateStore[x][y][z] = true
} else if (!stateStore[x][y][z] && (numNeighbors >= rules.deadMin && numNeighbors <= rules.deadMax)) {
updateStore[x][y][z] = true
} else {
updateStore[x][y][z] = false
}
//these rules produced an oscillator
// if (stateStore[x][y][z] && (numNeighbors >= 7 && numNeighbors <= 14)) {
// updateStore[x][y][z] = true
// } else if (!stateStore[x][y][z] && (numNeighbors >= 8 && numNeighbors <= 8)) {
// updateStore[x][y][z] = true
// } else {
// updateStore[x][y][z] = false
// cubeStore[x][y][z].setRed() //need a third state to manage color
//or 10 for alive and, when dead, decrement by 1 each cycle
// }
}
function stepForward() {
for(let x = 0; x < arraySize; x++) {
for(let y = 0; y < arraySize; y++) {
for(let z = 0; z < arraySize; z++) {
if(swapped) {
updateNext(x, y, z, primaryStateStore, secondaryStateStore)
cubeStore[x][y][z].setColorByState(secondaryStateStore[x][y][z], 'secondary')
} else {
updateNext(x, y, z, secondaryStateStore, primaryStateStore)
cubeStore[x][y][z].setColorByState(primaryStateStore[x][y][z], 'primary')
}
}
}
}
swapped = !swapped
}
let programRunning = true
pivot.parent.rotation.x += .8;
pivot.parent.rotation.y += .8;
let swapped = true
function animate() {
setTimeout( function() {
if(!programRunning) return
requestAnimationFrame( animate );
renderer.render( scene, camera );
stepForward();
}, 50);
}
animate();
</script>
</body>
</html>