-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.html
454 lines (404 loc) · 13.6 KB
/
cube.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
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
453
454
<html>
<head>
<title>You dont see this!</title>
<style>
canvas {
width: 100%;
height: 100%;
}
body {
background-color: #4d5057;
}
h1 {
text-transform: uppercase;
}
h1 {
color: #252525;
text-align: center;
line-height: 1.5;
position: relative;
text-decoration: none;
font-family: "Franklin Gothic Medium", "Arial Narrow", Arial, sans-serif;
font-size: 36px;
font-size: 5vw;
}
.input {
width: 70%;
margin: 0 auto;
font-size: 1.4em;
}
input {
width: 100%;
height: 80px;
text-align: center;
margin-top: 80px;
font-size: 1.5em;
background-color: transparent;
border: none;
border-top: 3px solid wheat;
border-bottom: 3px solid wheat;
color: wheat;
}
</style>
<!-- <script src="js/three.min.js"></script> FIX ME! -->
<script src="http://127.0.0.1:8000/three.js"></script>
<!-- <script src="http://127.0.0.1:8000/jquery-3.4.1.min.js"></script> -->
<!-- <script src="http://127.0.0.1:8000/three.module.js"></script> -->
<!-- <script src="js/OrbitControls.js"></script> FIX ME! -->
<script src="http://127.0.0.1:8000/OrbitControls.js"></script>
<script src="http://127.0.0.1:8000/placeholdem.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.min.js"></script>
<!-- https://jsfiddle.net/b452w0p8/ -->
</head>
<body>
<script>
function make_cube(scene, letter, x, y, z) {
var size = 10;
var buffer = 2;
var x = - x * size;
var y = - y * size;
var z = - z * size;
var cube_color = 0xc70039;
if (x == 0 && y == 0 && z == 0) {
cube_color = 0x48D1CC;
}
var geometry = new THREE.BoxGeometry(
size - buffer,
size - buffer,
size - buffer
);
var material = new THREE.MeshBasicMaterial({
color: cube_color,
});
var cube = new THREE.Mesh(geometry, material);
cube.letter = letter;
cube.input = "";
cube.material.transparent = true;
cube.material.opacity = 0.5;
cube.selected = false;
cube.position.set(x, y, z);
cube.size = size;
function getCoordinates () {
return [-this.position.x/this.size, -this.position.y/this.size, -this.position.z/this.size];
}
cube.getCoordinates = getCoordinates;
scene.add(cube);
var edges = new THREE.EdgesGeometry(geometry);
var line_color = 0xffffff;
var line = new THREE.LineSegments(
edges,
new THREE.LineBasicMaterial({
color: line_color,
linewidth: 2,
})
);
line.position.set(x, y, z);
scene.add(line);
cube.patterns = [];
return cube;
}
</script>
<script>
function makeTextSprite( message , direction, coords, scale=10)
{
var textOptions = {
size: 2,
height: 0.1,
font: font,
};
var textGeom = new THREE.TextGeometry( message, textOptions );
textGeom.computeBoundingBox();
var textWidth = textGeom.boundingBox.max.x - textGeom.boundingBox.min.x;
var material = new THREE.MeshBasicMaterial({ color: 0xffffff});
var mesh = new THREE.Mesh(textGeom, material);
if (scale > 0){
coords[direction] += textWidth + scale;
}else {
coords[direction] += -scale;
}
mesh.position.set(coords[0], coords[1], coords[2]);
return mesh
}
function create_text(cube) {
var textOptions = {
size: 7,
height: 4,
curveSegments: 10,
font: font,
};
var textGeom = new THREE.TextGeometry(cube.input, textOptions);
var material = new THREE.MeshBasicMaterial({
color: 0xffffff,
// overdraw: true,
// wireframe: true,
});
// material.transparent=true;
// material.opacity = 0.8;
cube.text_mesh = new THREE.Mesh(textGeom, material);
textGeom.computeBoundingBox();
var textWidth = textGeom.boundingBox.max.x - textGeom.boundingBox.min.x;
var textHeight =
textGeom.boundingBox.max.y - textGeom.boundingBox.min.y;
var textDepth = textGeom.boundingBox.max.z - textGeom.boundingBox.min.z;
// console.log(cube.position);
cube.text_mesh.position.set(
cube.position.x,
cube.position.y,
cube.position.z
);
cube.text_mesh.geometry.center();
cube.group = new THREE.Object3D();
cube.group.add(cube.text_mesh);
console.log(cube.group);
window.scene.add(cube.group);
}
var font = undefined;
function loadFont() {
var loader = new THREE.FontLoader();
loader.load(
// resource URL
"https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/fonts/helvetiker_bold.typeface.json",
// onLoad callback
function (response) {
font = response;
},
// onProgress callback
function (xhr) {
console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
},
// onError callback
function (err) {
console.log("An error happened");
}
);
}
</script>
<script>
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var renderer, camera, meshObjects;
var SELECTION;
function loadPuzzle() {
var grid = window.puzzle;
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
1,
10000
);
var controls = new THREE.OrbitControls(camera, renderer.domElement);
var cubes = [];
console.log(grid);
for (var i = 0; i < grid.length; i++) {
if (Array.isArray(grid[i])) {
var Y_array = grid[i];
for (var j = 0; j < Y_array.length; j++) {
if (Array.isArray(Y_array[j])) {
var X_array = Y_array[j];
for (var k = 0; k < X_array.length; k++) {
// console.log(i + " " + j + " " + k)
var cube = make_cube(scene, X_array[k], i, j, k);
cubes.push(cube);
}
} else {
// Use Z coord as 0
var cube = make_cube(scene, Y_array[j], i, j, 0);
cubes.push(cube);
}
}
} else {
var cube = make_cube(scene, grid[i], i, 0, 0);
cubes.push(cube);
}
}
var a = 0;
for (p of window.patterns[0]) {
var b = 0;
for (k of p){
var sp = makeTextSprite(k, 2, [-a*10, -b*10, 0], scale=5)
sp.rotateY(Math.PI/2)
for (var c = 0; c < window.patterns[1].length; c++){
var cu = cubes[(a*window.patterns.length + b)*p.length + c]
cu.patterns.push(sp);
}
b++;
}
a++;
}
a = 0;
for (p of window.patterns[1]) {
var b = 0;
for (k of p){
var sp = makeTextSprite(k, 1, [-b*10, 0, -a*10], scale=5)
sp.rotateY(Math.PI/2)
sp.rotateZ(-Math.PI/2)
for (var c = 0; c < window.patterns[2].length; c++){
var cu = cubes[a + b*window.patterns.length*window.patterns[1].length + c * window.patterns[1].length]
cu.patterns.push(sp);
}
b++;
}
a++;
}
a = 0;
for (p of window.patterns[2]) {
var b = 0;
for (k of p){
var sp = makeTextSprite(k, 0, [0, -a*10, -b*10], scale=5)
sp.rotateY(Math.PI)
for (var c = 0; c < window.patterns[0].length; c++){
var cu = cubes[a*p.length + b + c * window.patterns.length*p.length]
cu.patterns.push(sp);
}
b++;
}
a++;
}
window.cubes = cubes;
window.scene = scene;
camera.position.z = 100;
controls.update();
function render() {
requestAnimationFrame(render);
controls.update();
for (var c of cubes) {
if (typeof c.group != "undefined" && c.group.children.length > 0 && window.lookatme == true) {
c.group.children[0].lookAt(camera.position);
// c.group.children[0].translate(0.05);
}
// AVOID THE FOLLOWING!
// if (typeof c.patterns != "undefined" && c.patterns.length > 0){
// for (p of c.patterns){
// p.lookAt(camera.position);
// }
// }
}
renderer.render(scene, camera);
}
render();
}
function hidePatterns(c){
for (pat of c.patterns){
window.scene.remove(pat);
}
}
function doSomethingWithPattern(data){
for (d of data){
console.log(window.patterns[d[0]][d[1]][d[2]]);
}
}
function showPatterns(c){
let coords = c.getCoordinates();
translate_c(coords, doSomethingWithPattern);
for (pat of c.patterns){
window.scene.add(pat);
}
}
function onDocumentMouseDown(event) {
if (window.puzzle_loaded == false) {
return;
}
event.preventDefault();
mouse.x = (event.clientX / renderer.domElement.clientWidth) * 2 - 1;
mouse.y = -(event.clientY / renderer.domElement.clientHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects(cubes);
if (intersects.length > 0) {
// console.log(intersects[0].object != SELECTION)
if (intersects[0].object != SELECTION) {
if (SELECTION == null) {
// Alternatively, we could initialize the first cube as selected and we don't have to deal with this case anymore. I like this approach more though
console.log(intersects[0].object.letter);
SELECTION = intersects[0].object;
SELECTION.selected = true;
SELECTION.currentHex = SELECTION.material.color.getHex();
SELECTION.material.color.setHex(0xffac03);
showPatterns(SELECTION);
} else {
SELECTION.selected = false;
SELECTION.material.color.setHex(SELECTION.currentHex);
hidePatterns(SELECTION);
SELECTION = null;
// console.log(intersects[0].object.letter);
console.log(intersects[0].object.letter);
SELECTION = intersects[0].object;
SELECTION.selected = true;
SELECTION.currentHex = SELECTION.material.color.getHex();
SELECTION.material.color.setHex(0xffac03);
showPatterns(SELECTION);
}
}
}
}
document.addEventListener("mousedown", onDocumentMouseDown, false);
document.onkeydown = function (evt) {
var charCode = evt.which || evt.keyCode;
var charStr = String.fromCharCode(charCode);
if (
(/[a-zA-Z0-9-_]/.test(charStr) && window.puzzle_loaded == true && SELECTION.selected == true) ||
charCode == 8
// || charCode == 17
) {
if (charCode == 17) {
console.log("HIDE ME!");
}
if (charCode == 8) {
SELECTION.group.remove(SELECTION.text_mesh);
window.scene.remove(SELECTION.group);
SELECTION.input = "";
return;
}
if (SELECTION.input != "") {
SELECTION.group.remove(SELECTION.text_mesh);
window.scene.remove(SELECTION.group);
}
SELECTION.input = charStr;
create_text(SELECTION);
}
};
</script>
<script>
window.puzzle_loaded = false;
loadFont();
function set_puzzle(vectors, patterns){
window.patterns = patterns;
window.puzzle = vectors;
loadPuzzle();
var x = document.getElementById("homepage");
x.hidden = true
window.puzzle_loaded = true;
}
function fetch_puzzle(string) {
get_puzzle(string, set_puzzle);
}
window.lookatme = true;
</script>
<div id="homepage">
<h1 align="center">Regex Crossword Generator</h1>
<div class="input">
<input
type="text"
class="words"
placeholder="Enter a list of words"
/>
</div>
</div>
<script>
Placeholdem(document.querySelectorAll("[placeholder]"));
const node = document.getElementsByClassName("words")[0];
node.addEventListener("keyup", function (event) {
if (event.key === "Enter") {
console.log("Got enter. Sending words");
values = node.value;
if (values.length >= 1) {
fetch_puzzle(values);
}
}
});
</script>
</body>
</html>