-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
565 lines (508 loc) · 20.2 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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<!DOCTYPE html>
<html>
<head>
<title>Rubiks Cube Configurator</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="webgl-container"></div>
<div id="gui-container"></div>
<div id="gui-custom">
<div id="sub-gui-custom">
<div>
<h2>Rubik’s Cube Configurator</h2>
</div>
<h5>Configure your own Rubik's Cube!</h5>
<div>
<button type="button" class="collapsible">Cube Types</button>
<div class="content">
<div class="gallery">
<a target="_blank" onClick="load3x3Cube(); return false;" href="">
<img src="img/3x3_cube.JPG" alt="3x3_cube" width="100" height="100">
</a>
<div class="desc">3x3 Cube (Default)</div>
</div>
<div class="gallery">
<a target="_blank" onClick="load2x2Cube(); return false;" href="">
<img src="img/2x2_cube.JPG" alt="3x3_cube" width="100" height="100">
</a>
<div class="desc">2x2 Cube (Noobs)</div>
</div>
</div>
</div>
<br>
<div>
<button type="button" class="collapsible">Color Templates</button>
<div class="content">
<div class="gallery">
<div class="desc" onClick="loadClassicColors(); return false;">Classic</div>
</div>
<div class="gallery">
<div class="desc" onClick="loadBrightColors(); return false;">Bright</div>
</div>
<div class="gallery">
<div class="desc" onClick="loadRetroColors(); return false;">Retro</div>
</div>
<div class="gallery">
<div class="desc" onClick="loadAquaColors(); return false;">Aqua</div>
</div>
</div>
</div>
<br>
<div>
<button type="button" class="collapsible action" onclick="onRotateCubeClick()">Rotate Cube</button>
</div>
</div>
</div>
<script type="text/javascript" src="js/three.js"></script>
<script type="text/javascript" src="js/OrbitControls.js"></script>
<script type="text/javascript" src="js/ColladaLoader.js"></script>
<script type="text/javascript" src="js/dat.gui.js"></script>
<script type="text/javascript">
//Configurator
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
var contList = document.getElementsByClassName('content');
for (var i = 0; i < contList.length; ++i) {
if (this !== contList[i] && contList[i].style.display === "block") {
contList[i].style.display = "none";
contList[i].previousElementSibling.classList.toggle("active");
}
}
content.style.display = "block";
}
});
}
function load3x3Cube() {
loader.load('assets/rcube_3x3.dae', function (collada) {
group.visible = false;
scene.remove(group);
let dae = collada.scene;
group = new THREE.Group();
dae.children.forEach(element => {
let daeMesh = element.children[0];
let mesh = new THREE.Mesh(daeMesh.geometry, meshMaterial);
group.add(mesh);
});
group.position.set(0, 0, 0); //x,z,y- if you think in blender dimensions ;)
group.rotation.set(0, 1, 0);
group.rotation.y = -0.5 * Math.PI;
group.scale.set(2, 2, 2);
group.visible = true;
scene.add(group);
render();
});
}
function load2x2Cube() {
loader.load('assets/rcube_2x2.dae', function (collada) {
group.visible = false;
scene.remove(group);
let dae = collada.scene;
group = new THREE.Group();
for (i = 0; i < dae.children.length; i++) {
let daeMesh = dae.children[i].children[0];
let mesh = new THREE.Mesh(daeMesh.geometry, meshMaterial);
if (i == dae.children.length-1) {
mesh.position.set(3.5, 3.1, -2.4);
}
group.add(mesh);
}
dae.children.forEach(element => {
});
group.position.set(-3, -3, -3); //x,z,y- if you think in blender dimensions ;)
group.rotation.set(0, 0, 0);
group.rotation.y = -0.5 * Math.PI;
group.scale.set(2, 2, 2);
group.visible = true;
scene.add(group);
render();
});
}
function loadClassicColors() {
for (i = 0; i < meshMaterial.length; i++) {
meshMaterial[i].color = classicColors[i].color;
}
render();
}
function loadBrightColors() {
for (i = 0; i < meshMaterial.length; i++) {
meshMaterial[i].color = brightColors[i].color;
}
render();
}
function loadRetroColors() {
for (i = 0; i < meshMaterial.length; i++) {
meshMaterial[i].color = retroColors[i].color;
}
render();
}
function loadAquaColors() {
for (i = 0; i < meshMaterial.length; i++) {
meshMaterial[i].color = aquaColors[i].color;
}
render();
}
function onRotateCubeClick() {
guiControls.animated = !guiControls.animated;
if (guiControls.animated) animate();
}
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
//ThreeJS
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000);
var renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setClearColor(0x444444, 1.0);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
var webglContainer = document.getElementById('webgl-container');
webglContainer.appendChild(renderer.domElement);
// camera controls
var camControls = new THREE.OrbitControls(camera, renderer.domElement);
camControls.damping = 0.2;
camControls.addEventListener('change', render);
// window resize handler
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
render();
}
var axes = new THREE.AxisHelper(20);
var grid = new THREE.GridHelper(80, 80);
var planeGeometry = new THREE.PlaneGeometry(80, 80, 10, 10);
var planeMaterial = new THREE.MeshLambertMaterial({
color: 0x888888
});
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = -0.5 * Math.PI;
plane.position.x = 0;
plane.position.y = -7.00;
plane.position.z = 0;
var meshMaterial = [
new THREE.MeshLambertMaterial( { color: 0x000000 } ),
new THREE.MeshLambertMaterial( { color: 0xFFFFFF } ),
new THREE.MeshLambertMaterial( { color: 0x0000FF } ),
new THREE.MeshLambertMaterial( { color: 0xFF8000 } ),
new THREE.MeshLambertMaterial( { color: 0xFF0000 } ),
new THREE.MeshLambertMaterial( { color: 0x00FF00 } ),
new THREE.MeshLambertMaterial( { color: 0xFFFF00 } )
];
var classicColors = [
new THREE.MeshLambertMaterial( { color: 0x000000 } ),
new THREE.MeshLambertMaterial( { color: 0xFFFFFF } ),
new THREE.MeshLambertMaterial( { color: 0x0000FF } ),
new THREE.MeshLambertMaterial( { color: 0xFF8000 } ),
new THREE.MeshLambertMaterial( { color: 0xFF0000 } ),
new THREE.MeshLambertMaterial( { color: 0x00FF00 } ),
new THREE.MeshLambertMaterial( { color: 0xFFFF00 } )
];
var brightColors = [
new THREE.MeshLambertMaterial( { color: 0xFFFFFF } ),
new THREE.MeshLambertMaterial( { color: 0xFFEE81 } ),
new THREE.MeshLambertMaterial( { color: 0xFBF8E6 } ),
new THREE.MeshLambertMaterial( { color: 0x068FFC } ),
new THREE.MeshLambertMaterial( { color: 0xFEDA00 } ),
new THREE.MeshLambertMaterial( { color: 0x68C2FF } ),
new THREE.MeshLambertMaterial( { color: 0x74F2C6 } )
];
var retroColors = [
new THREE.MeshLambertMaterial( { color: 0x000000 } ),
new THREE.MeshLambertMaterial( { color: 0x270245 } ),
new THREE.MeshLambertMaterial( { color: 0x871A85 } ),
new THREE.MeshLambertMaterial( { color: 0xFF2941 } ),
new THREE.MeshLambertMaterial( { color: 0xFEFF38 } ),
new THREE.MeshLambertMaterial( { color: 0xFE18D3 } ),
new THREE.MeshLambertMaterial( { color: 0x4206F1 } )
];
var aquaColors = [
new THREE.MeshLambertMaterial( { color: 0xFFFFFF } ),
new THREE.MeshLambertMaterial( { color: 0xFFFFFF } ),
new THREE.MeshLambertMaterial( { color: 0x1FFCFE } ),
new THREE.MeshLambertMaterial( { color: 0x1887FA } ),
new THREE.MeshLambertMaterial( { color: 0x0033B9 } ),
new THREE.MeshLambertMaterial( { color: 0x47A5CB } ),
new THREE.MeshLambertMaterial( { color: 0x262626 } )
];
// ---- 3D CONTENT CREATION -----------------------------------------------------
var group = new THREE.Group();
var loader = new THREE.ColladaLoader();
loader.options.convertUpAxis = true;
load3x3Cube();
// ---- END OF 3D CONTENT CREATION ---------------------------------------------
// position and point the camera to the center of the scene
camera.position.x = -35;
camera.position.y = 30;2
camera.position.z = 50;
camera.lookAt(scene.position);
// add subtle ambient lighting
var ambiColor = "#FFFFFF";
var ambientLight = new THREE.AmbientLight(ambiColor, 0.4);
scene.add(ambientLight);
// add headlight
var headColor = "#FFFFFF";
var headLight = new THREE.PointLight (headColor, 0.33);
headLight.position.set(camera.position.x, camera.position.y, camera.position.z);
scene.add(headLight);
// add spotlight
var spotColor = "#FFFFFF";
var spotLight = new THREE.SpotLight(spotColor);
spotLight.penumbra = 0.572;
spotLight.position.set(-40, 60, -10);
spotLight.shadow.camera.near = 2;
spotLight.shadow.camera.far = 200;
spotLight.shadow.camera.fov = 130;
spotLight.target = plane;
spotLight.distance = 0;
scene.add(spotLight);
// add GUI control elements
var guiControls = new function () {
//general
this.showAxes = false;
this.showGrid = false;
this.showPlane = false;
this.asWireframes = false;
this.withShadow = false;
this.withFog = false;
//animation
this.animated = false;
this.rotationSpeed = 0.03;
//light
this.withAmbient = true;
this.withSpotLight = true;
this.withHeadLight = true;
this.ambientColor = ambiColor;
this.headColor = headColor;
this.spotColor = spotColor;
this.intensity = 2;
this.distance = 100;
this.exponent = 0.1;
this.angle = 3.5;
//material
this.opacity = meshMaterial[0].opacity;
this.transparent = meshMaterial[0].transparent;
this.visible = meshMaterial[0].visible;
this.side = "front";
this.color = meshMaterial[0].color.getStyle();
this.emissive = meshMaterial[0].emissive.getHex();
}
var gui = new dat.GUI();
var guiContainer = document.getElementById('gui-container');
guiContainer.appendChild(gui.domElement);
var guiGen = gui.addFolder("General");
guiGen.add(guiControls, 'showAxes').onChange(function (e) {
showAxes = e;
if (showAxes) {
scene.add(axes);
} else {
scene.remove(axes);
}
render();
});
guiGen.add(guiControls, 'showGrid').onChange(function (e) {
showGrid = e;
if (showGrid) {
scene.add(grid);
} else {
scene.remove(grid);
}
render();
});
guiGen.add(guiControls, 'showPlane').onChange(function (e) {
showPlane = e;
if (showPlane) {
scene.add(plane);
} else {
scene.remove(plane);
}
render();
});
var guiAni = gui.addFolder("Animation");
guiAni.add(guiControls, 'animated').onChange(function (e) {
animated = e;
if (animated) animate();
});
guiAni.add(guiControls, 'rotationSpeed', 0, 0.25);
guiLights = gui.addFolder('Lights');
guiLights.add(guiControls, 'withAmbient').onChange(function (e) {
withAmbient = e;
if (withAmbient) {
scene.add(ambientLight);
} else {
scene.remove(ambientLight);
}
render();
});
guiLights.addColor(guiControls, 'ambientColor').onChange(function (e) {
ambientLight.color = new THREE.Color(e);
render();
});
guiLights.add(guiControls, 'withHeadLight').onChange(function (e) {
withHeadLight = e;
if (withHeadLight) {
scene.add(headLight);
} else {
scene.remove(headLight);
}
render();
});
guiLights.addColor(guiControls, 'headColor').onChange(function (e) {
headLight.color = new THREE.Color(e);
render();
});
guiLights.addColor(guiControls, 'spotColor').onChange(function (e) {
spotLight.color = new THREE.Color(e);
render();
});
guiLights.add(guiControls, 'angle', 0, Math.PI * 2).onChange(function (e) {
spotLight.angle = e;
render();
});
guiLights.add(guiControls, 'intensity', 0, 5).onChange(function (e) {
spotLight.intensity = e;
});
guiLights.add(guiControls, 'distance', 0, 200).onChange(function (e) {
spotLight.distance = e;
render();
});
guiLights.add(guiControls, 'exponent', 0, 100).onChange(function (e) {
spotLight.exponent = e;
render();
});
var guiMat = gui.addFolder("Material");
guiMat.addColor(guiControls, 'color').onChange(function (e) {
for (i = 0; i < meshMaterial.length; i++) {
meshMaterial[i].color.setStyle(e);
}
render();
});
guiMat.addColor(guiControls, 'emissive').onChange(function (e) {
for (i = 0; i < meshMaterial.length; i++) {
meshMaterial[i].emissive = new THREE.Color(e);
}
render();
});
guiMat.add(guiControls, 'side', ["front", "back", "double" ]).onChange(function (e) {
switch (e) {
case "front":
meshMaterial[0].side = THREE.FrontSide;
break;
case "back":
meshMaterial[0].side = THREE.BackSide;
break;
case "double":
meshMaterial[0].side = THREE.DoubleSide
break;
}
meshMaterial[0].needsUpdate = true;;
render();
});
guiMat.add(guiControls, 'transparent').onChange(function (e) {
for (i = 0; i < meshMaterial.length; i++) {
meshMaterial[i].transparent = e;
}
render();
});
guiMat.add(guiControls, 'opacity', 0, 1).onChange(function (e) {
for (i = 0; i < meshMaterial.length; i++) {
meshMaterial[i].opacity = e;
}
render();
});
guiMat.add(guiControls, 'visible').onChange(function (e) {
for (i = 0; i < meshMaterial.length; i++) {
meshMaterial[i].visible = e;
}
render();
});
gui.close();
// init and call render function
function render() {
headLight.position.set(camera.position.x, camera.position.y, camera.position.z);
renderer.render(scene, camera);
}
initShadow(true);
initFog(true);
render();
setShadow(false);
setFog(false);
// animate the 3D objects
var invert = 1;
var phase = 0;
function animate() {
if (guiControls.animated) {
// rotate the cube around its axes
group.rotation.x += guiControls.rotationSpeed;
group.rotation.y += guiControls.rotationSpeed / 2;
group.rotation.z += guiControls.rotationSpeed / 3;
}
if (guiControls.animated) {
// request re-rendering
requestAnimationFrame(animate);
}
render();
}
// shadow cast on plane
function initShadow(value) {
// SET receiveShadow AND castShadow FLAGS OF 3D ELEMENTS WITH SHADOW
plane.receiveShadow = value;
group.castShadow = value;
spotLight.castShadow = value;
spotLight.shadow.mapSize.width = 2048;
spotLight.shadow.mapSize.height = 2048;
renderer.shadowMap.enabled = value;
if (value) {
guiGen.add(guiControls, 'withShadow').onChange(function (e) {
withShadow = e;
setShadow(withShadow);
});
}
}
function setShadow(value) {
if (value) {
renderer.shadowMap.autoUpdate = true;
} else {
renderer.shadowMap.autoUpdate = false;
renderer.clearTarget(spotLight.shadow.map);
}
render();
}
// fog
var fog;
function initFog(value) {
if (value) {
fog = new THREE.Fog(0xeeeeee, 0.0000001, 150);
scene.fog = fog;
guiGen.add(guiControls, 'withFog').onChange(function (e) {
withFog = e;
setFog(withFog);
});
}
}
function setFog(value) {
if (!fog) return;
if (value) {
fog.near = 0.000000001;
fog.far = 150;
} else {
fog.near = 8000;
fog.far = 8000;
}
render();
}
</script>
</body>
</html>