-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.js
452 lines (413 loc) · 18 KB
/
custom.js
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
let timer;
let playing = false;
let framesperchange = 30;
// set inner height and width based on screen size
const winWidth = window.innerWidth;
const winHeight = window.innerHeight;// global vars
let scene, camera, renderer, controls, mesh, projector, materials, wireframe;
let objects = [];
let turnOn = [];
let turnOff = [];
let p, q, torusrad, tuberad, tubular, radial, totalfaces;
let importval = "";
let exportval = "";
init();
update();
function setRandom() {
for (let i = 0; i < totalfaces; i++) {
if (Math.random() < 0.5) {
mesh.geometry.faces[i].color.setRGB(0.110, 0.918, 0.259);
mesh.geometry.faces[i].materialIndex = 1;
} else {
mesh.geometry.faces[i].color.setHex(0x000030);
mesh.geometry.faces[i].materialIndex = 0;
}
}
mesh.geometry.colorsNeedUpdate = true;
}
function setNew() {
p = Number(document.getElementById("p").value);
q = Number(document.getElementById("q").value);
torusrad = Number(document.getElementById("torusrad").value);
tuberad = Number(document.getElementById("tuberad").value);
tubular = Number(document.getElementById("tubular").value);
radial = Number(document.getElementById("radial").value);
totalfaces = tubular * radial;
setKnot();
}
function setKnot() {
camera.position.set(0, 0, 2);
scene.remove(mesh);
scene.remove(wireframe);
let geometry = new THREE.TorusKnotGeometry(torusrad, tuberad, tubular, radial, p, q);
let material = new THREE.MeshBasicMaterial({ vertexColors: THREE.FaceColors });
mesh = new THREE.Mesh(geometry, material);
for (let i = 0; i < totalfaces; i++) {
mesh.geometry.faces[i].color.setHex(0x000030);
mesh.geometry.faces[i].materialIndex = 0;
}
wireframe = new THREE.WireframeHelper(mesh);
scene.add(wireframe);
scene.add(mesh);
objects = [];
objects.push(mesh);
console.log("switched to custom");
}
function setExample() {
for (let i = 0; i < totalfaces; i += ((radial * 5) + 1)) {
if (i % radial < radial - 2) {
mesh.geometry.faces[i].color.setRGB(0.110, 0.918, 0.259);
mesh.geometry.faces[i + 1].color.setRGB(0.110, 0.918, 0.259);
mesh.geometry.faces[i + radial].color.setRGB(0.110, 0.918, 0.259);
mesh.geometry.faces[i + radial + 2].color.setRGB(0.110, 0.918, 0.259);
mesh.geometry.faces[i + (2 * radial)].color.setRGB(0.110, 0.918, 0.259);
mesh.geometry.faces[i].materialIndex = 1;
mesh.geometry.faces[i + 1].materialIndex = 1;
mesh.geometry.faces[i + radial].materialIndex = 1;
mesh.geometry.faces[i + radial + 2].materialIndex = 1;
mesh.geometry.faces[i + (2 * radial)].materialIndex = 1;
}
}
mesh.geometry.colorsNeedUpdate = true;
}
function setDefault() {
p = 2;
q = 3;
radial = 32;
tubular = 256;
torusrad = 0.27;
tuberad = 0.1;
totalfaces = tubular * radial;
camera.position.set(0, 0, 1.5);
scene.remove(mesh);
scene.remove(wireframe);
let geometry = new THREE.TorusKnotGeometry(0.27, 0.1, 256, 32);
let material = new THREE.MeshBasicMaterial({ vertexColors: THREE.FaceColors });
mesh = new THREE.Mesh(geometry, material);
for (let i = 0; i < 8192; i++) {
mesh.geometry.faces[i].color.setHex(0x000030);
mesh.geometry.faces[i].materialIndex = 0;
}
wireframe = new THREE.WireframeHelper(mesh);
scene.add(wireframe);
scene.add(mesh);
objects = [];
objects.push(mesh);
console.log("switched to 3,2");
document.getElementById("p").value = String(p);
document.getElementById("q").value = String(q);
document.getElementById("torusrad").value = String(torusrad);
document.getElementById("tuberad").value = String(tuberad);
document.getElementById("tubular").value = String(tubular);
document.getElementById("radial").value = String(radial);
}
function setTiming(value) {
document.getElementById("rangeLabel").innerHTML = "Updates every " + value + " frames";
framesperchange = Number(value);
timer = 0;
}
function clearState() {
for (let i = 0; i < totalfaces; i++) {
mesh.geometry.faces[i].color.setHex(0x000030);
mesh.geometry.faces[i].materialIndex = 0;
}
mesh.geometry.colorsNeedUpdate = true;
console.log("cleared");
}
function toggleSim() {
console.log(!playing);
playing = !playing;
timer = 0;
if (playing) {
document.getElementById("play").innerHTML = "Pause"
} else {
document.getElementById("play").innerHTML = "Play"
}
}
function openImport() {
Swal.fire({
title: "Import Faces",
html: "<input type='text' id='importval'>",
confirmButtonText: 'Import',
focusConfirm: false,
preConfirm: () => {
const importval = Swal.getPopup().querySelector('#importval').value;
if (!importval) {
Swal.showValidationMessage('Please enter a code');
} else if (importval[0] != 'a') {
if(importval.length === 8192){
Swal.showValidationMessage('This code is for the main page. Please enter a custom code');
}else if(importval.length === 32768){
Swal.showValidationMessage('This code is for the crazy knot. Please enter a custom code');
}else{
Swal.showValidationMessage('Please enter a custom code');
}
}
return importval
}
}).then((result) => {
if (result.value) {
importval = result.value;
setImport();
}
})
}
function setImport() {
p = Number(importval.substring(importval.indexOf("a") + 1, importval.indexOf("b")));
q = Number(importval.substring(importval.indexOf("b") + 1, importval.indexOf("c")));
torusrad = Number(importval.substring(importval.indexOf("c") + 1, importval.indexOf("d")));
tuberad = Number(importval.substring(importval.indexOf("d") + 1, importval.indexOf("e")));
tubular = Number(importval.substring(importval.indexOf("e") + 1, importval.indexOf("f")));
radial = Number(importval.substring(importval.indexOf("f") + 1, importval.indexOf("g")));
totalfaces = tubular * radial;
setKnot();
let sequence = importval.substring(importval.indexOf("g") + 1);
console.log("length: " + sequence.length);
console.log("total: " + totalfaces);
for (let i = 0; i < totalfaces; i++){
let pointer = Number(importval[i]);
if (pointer === 1) {
mesh.geometry.faces[i].color.setRGB(0.110, 0.918, 0.259);
mesh.geometry.faces[i].materialIndex = 1;
} else {
mesh.geometry.faces[i].color.setHex(0x000030);
mesh.geometry.faces[i].materialIndex = 0;
}
}
document.getElementById("p").value = String(p);
document.getElementById("q").value = String(q);
document.getElementById("torusrad").value = String(torusrad);
document.getElementById("tuberad").value = String(tuberad);
document.getElementById("tubular").value = String(tubular);
document.getElementById("radial").value = String(radial);
}
function openExport() {
setExport();
Swal.fire({
title: "Copy the following text",
html: `<input type="text" value="${exportval}" readonly>`,
confirmButtonText: 'Copied!',
})
}
function setExport() {
exportval = "a";
exportval += String(p);
exportval += "b";
exportval += String(q);
exportval += "c";
exportval += String(torusrad);
exportval += "d";
exportval += String(tuberad);
exportval += "e";
exportval += String(tubular);
exportval += "f";
exportval += String(radial)
exportval += "g";
for (let i = 0; i < totalfaces; i++) {
exportval += String(mesh.geometry.faces[i].materialIndex);
}
}
function init() {
timer = 0;
scene = new THREE.Scene({ antialias: true });
scene.background = new THREE.Color(0x000000);
camera = new THREE.PerspectiveCamera(45, winWidth / winHeight, 0.01, 1000);
camera.position.set(0, 0, 1.5);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(winWidth, winHeight);
document.getElementById("three").appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, document.getElementById("three"));
setDefault();
// declare this statmenet whenever u change color
//mesh.geometry.colorsNeedUpdate = true;
projector = new THREE.Projector();
document.getElementById("three").addEventListener("mousedown", onMouseDown);
function onMouseDown(event) {
event.preventDefault();//x
var vector = new THREE.Vector3((event.clientX / winWidth) * 2 - 1, -(event.clientY / winHeight) * 2 + 1, 0.5);
projector.unprojectVector(vector, camera);
var ray = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());
var intersects = ray.intersectObjects(objects);
if (intersects.length > 0) {
console.log(intersects[0].faceIndex);
if (mesh.geometry.faces[intersects[0].faceIndex].materialIndex) {
mesh.geometry.faces[intersects[0].faceIndex].color.setHex(0x000030);
mesh.geometry.faces[intersects[0].faceIndex].materialIndex = 0;
} else {
mesh.geometry.faces[intersects[0].faceIndex].color.setRGB(0.110, 0.918, 0.259);
mesh.geometry.faces[intersects[0].faceIndex].materialIndex = 1;
}
mesh.geometry.colorsNeedUpdate = true;
console.log(mesh.geometry.faces);
}
}
window.addEventListener('resize', onWindowResize);
}
function update() {
requestAnimationFrame(update);
controls.update();
renderer.render(scene, camera);
if (timer === (framesperchange - 1)) {
if (playing) {
console.log("second");
getState();
}
timer = 0;
} else {
timer++;
}
}
function onWindowResize() {
camera.aspect = winWidth / winHeight;
camera.updateProjectionMatrix();
renderer.setSize(winWidth, winHeight);
}
function getState() {
for (let i = 0; i < totalfaces; i++) {
setValue(i)
}
turnOn.forEach((index) => {
mesh.geometry.faces[index].color.setRGB(0.110, 0.918, 0.259);
mesh.geometry.faces[index].materialIndex = 1;
});
turnOff.forEach((index) => {
mesh.geometry.faces[index].color.setHex(0x000030);
mesh.geometry.faces[index].materialIndex = 0;
});
mesh.geometry.colorsNeedUpdate = true;
turnOn = [];
turnOff = [];
}
function setValue(index) {
let neighbourValue = 0;
//values of neighbours
// i think all of this is done
if (index < radial) {
if (index % radial == 0) {
//same row
neighbourValue += mesh.geometry.faces[index + 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + radial - 1].materialIndex;
//row below
neighbourValue += mesh.geometry.faces[index + totalfaces - radial].materialIndex;
neighbourValue += mesh.geometry.faces[index + totalfaces - radial + 1].materialIndex;
neighbourValue += mesh.geometry.faces[totalfaces - 1].materialIndex;
//row above
neighbourValue += mesh.geometry.faces[index + radial].materialIndex;
neighbourValue += mesh.geometry.faces[index + radial + 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + (2 * radial) - 1].materialIndex;
} else if (index % radial == (radial - 1)) {
//same row
neighbourValue += mesh.geometry.faces[index - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index - (radial - 1)].materialIndex;
//row below
neighbourValue += mesh.geometry.faces[index + totalfaces - radial].materialIndex;
neighbourValue += mesh.geometry.faces[index + totalfaces - radial - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + totalfaces - (2 * radial) + 1].materialIndex;
//row above
neighbourValue += mesh.geometry.faces[index + radial].materialIndex;
neighbourValue += mesh.geometry.faces[index + radial - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + 1].materialIndex;
} else {
//same row
neighbourValue += mesh.geometry.faces[index - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + 1].materialIndex;
//row below
neighbourValue += mesh.geometry.faces[index + totalfaces - radial].materialIndex;
neighbourValue += mesh.geometry.faces[index + totalfaces - radial + 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + totalfaces - radial - 1].materialIndex;
//row above
neighbourValue += mesh.geometry.faces[index + radial].materialIndex;
neighbourValue += mesh.geometry.faces[index + radial + 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + radial - 1].materialIndex;
}
// i think this is done
} else if (index > totalfaces - radial - 1) {
if (index % radial == 0) {
//32760
//same row
neighbourValue += mesh.geometry.faces[index + 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + (radial - 1)].materialIndex;
//row below
neighbourValue += mesh.geometry.faces[index - radial].materialIndex;
neighbourValue += mesh.geometry.faces[index - radial + 1].materialIndex;
neighbourValue += mesh.geometry.faces[index - 1].materialIndex;
//row above
neighbourValue += mesh.geometry.faces[index - totalfaces + radial].materialIndex;
neighbourValue += mesh.geometry.faces[index - totalfaces + radial + 1].materialIndex;
neighbourValue += mesh.geometry.faces[index - totalfaces + (2 * radial) - 1].materialIndex;
} else if (index % radial == (radial - 1)) {
//32767
//same row
neighbourValue += mesh.geometry.faces[index - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index - (radial - 1)].materialIndex;
//row below
neighbourValue += mesh.geometry.faces[index - radial].materialIndex;
neighbourValue += mesh.geometry.faces[index - radial - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index - (2 * radial) + 1].materialIndex;
//row above
neighbourValue += mesh.geometry.faces[index - totalfaces + radial].materialIndex;
neighbourValue += mesh.geometry.faces[0].materialIndex;
neighbourValue += mesh.geometry.faces[index - totalfaces + radial - 1].materialIndex;
} else {
//same row
neighbourValue += mesh.geometry.faces[index - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + 1].materialIndex;
//row below
neighbourValue += mesh.geometry.faces[index - radial].materialIndex;
neighbourValue += mesh.geometry.faces[index - radial + 1].materialIndex;
neighbourValue += mesh.geometry.faces[index - radial - 1].materialIndex;
//row above
neighbourValue += mesh.geometry.faces[index - totalfaces + radial].materialIndex;
neighbourValue += mesh.geometry.faces[index - totalfaces + radial + 1].materialIndex;
neighbourValue += mesh.geometry.faces[index - totalfaces + radial - 1].materialIndex;
}
//these cases are done
} else if (index % radial === 0) {
//same row
neighbourValue += mesh.geometry.faces[index + 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + radial - 1].materialIndex;
//row below
neighbourValue += mesh.geometry.faces[index - radial].materialIndex;
neighbourValue += mesh.geometry.faces[index - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index - (radial - 1)].materialIndex;
//row above
neighbourValue += mesh.geometry.faces[index + radial].materialIndex;
neighbourValue += mesh.geometry.faces[index + radial + 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + (2 * radial) - 1].materialIndex;
} else if (index % radial === (radial - 1)) {
//same row
neighbourValue += mesh.geometry.faces[index - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index - (radial - 1)].materialIndex;
//row below
neighbourValue += mesh.geometry.faces[index - radial].materialIndex;
neighbourValue += mesh.geometry.faces[index - (radial + 1)].materialIndex;
neighbourValue += mesh.geometry.faces[index - (2 * radial) + 1].materialIndex;
//row above
neighbourValue += mesh.geometry.faces[index + radial].materialIndex;
neighbourValue += mesh.geometry.faces[index + radial - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + 1].materialIndex;
} else {
//same row
neighbourValue += mesh.geometry.faces[index - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + 1].materialIndex;
//row below
neighbourValue += mesh.geometry.faces[index - radial].materialIndex;
neighbourValue += mesh.geometry.faces[index - radial - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index - radial + 1].materialIndex;
//row above
neighbourValue += mesh.geometry.faces[index + radial].materialIndex;
neighbourValue += mesh.geometry.faces[index + radial - 1].materialIndex;
neighbourValue += mesh.geometry.faces[index + radial + 1].materialIndex;
}
if (mesh.geometry.faces[index].materialIndex) {
if (!(neighbourValue === 2 || neighbourValue === 3)) {
console.log("turn off: " + index);
turnOff.push(index);
}
} else {
if (neighbourValue === 3) {
console.log("turn on: " + index);
turnOn.push(index);
}
}
}