This repository was archived by the owner on Dec 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvanite.js
640 lines (467 loc) · 15.2 KB
/
vanite.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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
"use strict"; // good practice - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
/*global THREE, Coordinates, $, document, window, dat*/
var camera, scene, renderer;
var cameraControls, effectController;
var clock = new THREE.Clock();
var gridX = false;
var gridY = false;
var gridZ = false;
var axes = false;
var ground = true;
/** measures const */
var tableTopWidthX;
var tableTopWidthZ;
var tableTopHeight;
/** letters part */
var letter1;
var letter2;
var skybox;
var skyboxGeo;
var lightCandle;
var dirLight1;
var dirLight2;
var ambientLight;
var phongBalancedMaterial;
var candleMaterial;
var candle;
function init() {
var canvasWidth = window.innerWidth - window.innerWidth / 90;
var canvasHeight = window.innerHeight - window.innerHeight / 5;
// For grading the window is fixed in size; here's general code:
//var canvasWidth = window.innerWidth;
//var canvasHeight = window.innerHeight;
var canvasRatio = canvasWidth / canvasHeight;
// RENDERER
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.setSize(canvasWidth, canvasHeight);
renderer.setClearColor( 0xAAAAAA, 1.0 );
renderer.autoClear = false;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
// CAMERA
camera = new THREE.PerspectiveCamera( 45, canvasRatio, 1, 40000 );
// CONTROLS
cameraControls = new THREE.OrbitControls(camera, renderer.domElement);
camera.position.set( -2000, 500, 0 );
cameraControls.target.set(4,301,92);
fillScene();
}
function setMaterial(obj, material) {
obj.traverse(function(obj) {
obj.material = material;
});
}
function loadShader(shadertype) {
return document.getElementById(shadertype).textContent;
}
function createShaderMaterial(id, light, ambientLight) {
var shaderTypes = {
'phongBalanced' : {
uniforms: {
"uDirLightPos": { type: "v3", value: new THREE.Vector3() },
"uDirLightColor": { type: "c", value: new THREE.Color( 0xFFFFFF ) },
"uAmbientLightColor": { type: "c", value: new THREE.Color( 0x050505 ) },
"uMaterialColor": { type: "c", value: new THREE.Color( 0xFFFFFF ) },
"uSpecularColor": { type: "c", value: new THREE.Color( 0xFFFFFF ) },
uKd: {
type: "f",
value: 0.7
},
uKs: {
type: "f",
value: 0.3
},
shininess: {
type: "f",
value: 100.0
},
uGroove: {
type: "f",
value: 1.0
}
}
}
};
var shader = shaderTypes[id];
var u = THREE.UniformsUtils.clone(shader.uniforms);
// this line will load a shader that has an id of "vertex" from the .html file
var vs = loadShader("vertex");
// this line will load a shader that has an id of "fragment" from the .html file
var fs = loadShader("fragment");
var material = new THREE.ShaderMaterial({ uniforms: u, vertexShader: vs, fragmentShader: fs });
material.uniforms.uDirLightPos.value = light.position;
material.uniforms.uDirLightColor.value = light.color;
material.uniforms.uAmbientLightColor.value = ambientLight.color;
return material;
}
function fillScene() {
// SCENE
scene = new THREE.Scene();
// scene.fog = new THREE.Fog( 0x808080, 3000, 6000 );
//SKYBOX
const skyboxGeo = new THREE.BoxGeometry(10000, 10000, 10000);
const skybox = new THREE.Mesh(skyboxGeo,
new THREE.MeshBasicMaterial( { color: 0x00000, side: THREE.BackSide }));
scene.add(skybox);
// LIGHTS
var light = new THREE.PointLight( 0xFFA500, 1, 500, 2 );
light.castShadow = true;
lightCandle = light;
light.position.set( -60, 430, -150 );
// var spheresize = 10;
// var pointLightHelper = new THREE.PointLightHelper( light, spheresize );
// scene.add( pointLightHelper );
scene.add( light );
light.shadow.mapSize.width = 512; // default
light.shadow.mapSize.height = 512; // default
light.shadow.camera.near = 0.5; // default
light.shadow.camera.far = 500; // default
ambientLight = new THREE.AmbientLight( 0x222222 );
ambientLight.intensity = 0.0;
dirLight1 = new THREE.DirectionalLight( 0xFFFFFF, 1.0 );
dirLight1.position.set( 200, 400, 500 );
dirLight1.intensity = 0.0;
dirLight2 = new THREE.DirectionalLight( 0xFFFFFF, 1.0 );
dirLight2.position.set( -400, 200, -300 );
dirLight2.intensity = 0.0;
scene.add(ambientLight);
scene.add(dirLight1);
scene.add(dirLight2);
var materialColor = new THREE.Color();
materialColor.setRGB(1.0, 0.8, 0.6);
phongBalancedMaterial = createShaderMaterial("phongBalanced", lightCandle, ambientLight);
phongBalancedMaterial.uniforms.uMaterialColor.value.copy(materialColor);
phongBalancedMaterial.side = THREE.DoubleSide;
if (ground) {
Coordinates.drawGround({size:1000});
}
if (gridX) {
Coordinates.drawGrid({size:1000,scale:0.01});
}
if (gridY) {
Coordinates.drawGrid({size:1000,scale:0.01, orientation:"y"});
}
if (gridZ) {
Coordinates.drawGrid({size:1000,scale:0.01, orientation:"z"});
}
if (axes) {
Coordinates.drawAllAxes({axisLength:300,axisRadius:2,axisTess:50});
}
createTable();
createLetter();
candle = createCandle(new THREE.MeshPhongMaterial( {color: 0x915136, shininess: 100, side: THREE.DoubleSide} ));
}
//
function addToDOM() {
var container = document.getElementById('webGL');
container.appendChild( renderer.domElement );
}
function render() {
var delta = clock.getDelta();
cameraControls.update(delta);
if ( effectController.newGridX !== gridX || effectController.newGridY !== gridY || effectController.newGridZ !== gridZ || effectController.newGround !== ground || effectController.newAxes !== axes)
{
gridX = effectController.newGridX;
gridY = effectController.newGridY;
gridZ = effectController.newGridZ;
ground = effectController.newGround;
axes = effectController.newAxes;
fillScene();
}
letter1.rotation.z = THREE.Math.degToRad(effectController.lz);
letter2.rotation.z = THREE.Math.degToRad(effectController.lz2);
if (!effectController.enableLighting) {
lightCandle.intensity = 0.0;
dirLight1.intensity = 1.0;
dirLight2.intensity = 1.0;
ambientLight.intensity = 1.0;
} else {
lightCandle.intensity = 1.0;
dirLight1.intensity = 0.0;
dirLight2.intensity = 0.0;
ambientLight.intensity = 0.0;
}
if (lightCandle != null && effectController.enableLighting) {
lightCandle.distance = effectController.lightDistance;
lightCandle.intensity = effectController.lightIntensity;
lightCandle.castShadow = effectController.enableShadow;
}
if (effectController.controlAmbientLight)
ambientLight.intensity = effectController.ambientLightIntensity;
if (!effectController.enableShader)
setMaterial(candle, new THREE.MeshPhongMaterial( {color: 0x915136, shininess: 100, side: THREE.DoubleSide} ));
else setMaterial(candle, phongBalancedMaterial);
renderer.render(scene, camera);
}
function createLetter() {
letter1 = new THREE.Object3D();
var letter = new THREE.Object3D();
letter2 = new THREE.Object3D();
var geometry = new THREE.BoxGeometry(100, 100, 5);
var material = new THREE.MeshLambertMaterial( {color: 0xFFFFFF} );
createLetter2(letter2, geometry, material );
createLetter1(letter1, geometry, material );
letter1.add(letter2);
createLetterCrane(letter, geometry, material );
letter.add(letter1);
scene.add(letter);
}
function createLetterCrane( part, geometry, material)
{
var plane = new THREE.Mesh( geometry, material );
plane.rotation.x = Math.PI/2;
plane.rotation.z = Math.PI/4;
plane.position.y = tableTopHeight + 20;
plane.position.x = 100;
plane.castShadow = true;
part.add(plane);
}
function createLetter1( part, geometry, material ) {
var plane = new THREE.Mesh( geometry, material );
plane.rotation.x = Math.PI/2;
plane.rotation.z = Math.PI/4;
plane.position.y = tableTopHeight + 20;
plane.position.x = 100;
plane.rotateX(50);
plane.position.y += 15;
plane.castShadow = true;
part.add(plane);
}
function createLetter2( part, geometry, material ) {
var plane = new THREE.Mesh( geometry, material );
plane.rotation.x = Math.PI/2;
plane.rotation.z = Math.PI/4;
plane.position.y = tableTopHeight + 20;
plane.position.x = 100;
plane.rotateX(50);
plane.position.y += 15;
plane.scale.y = 0.5;
plane.position.z = 50;
plane.position.x = 50;
plane.position.y = plane.position.y + 5;
plane.rotateX(-100);
plane.castShadow = true;
part.add(plane);
}
function createTable()
{
var table = new THREE.Object3D();
var geometry = new THREE.BoxGeometry(700, 700, 30);
var material = new THREE.MeshLambertMaterial( {color: 0x582900} );
var cube = new THREE.Mesh( geometry, material );
tableTopWidthX = -200;
tableTopWidthZ = 300;
tableTopHeight = 250;
cube.rotation.x = Math.PI/2;
cube.position.y = 0;
cube.scale.x = 1.4;
cube.scale.y = 1.4;
cube.receiveShadow = true;
table.add(cube);
var tableTop = cube.clone(false);
tableTop.position.y = 25;
tableTop.scale.x = 1.5;
tableTop.scale.z = 0.5;
tableTop.scale.y = 1.5;
tableTop.receiveShadow = true;
table.add(tableTop);
table.position.y = tableTopHeight;
table.scale.x = 0.5;
table.scale.y = 0.5;
table.scale.z = 0.5;
table.receiveShadow = true;
scene.add(table);
var cylGeometry = new THREE.CylinderGeometry(20, 20, 250, 32);
var cyl = new THREE.Mesh( cylGeometry, material );
cyl.position.y = 130;
cyl.position.x = 200;
cyl.position.z = 180;
scene.add(cyl);
var cyl = cyl.clone();
cyl.position.z = -180;
scene.add(cyl);
var cyl = cyl.clone();
cyl.position.x = -200;
cyl.position.z = 180;
scene.add(cyl);
var cyl = cyl.clone();
cyl.position.z = -180;
scene.add(cyl);
}
function createCandle(material) {
//3dObj
var candle = new THREE.Object3D();
// disque
var geometry = new THREE.CircleGeometry( 75, 100 );
var circle = new THREE.Mesh( geometry, material );
circle.position.y = 20;
circle.rotation.x = -Math.PI/2;
circle.castShadow = true;
circle.receiveShadow = true;
candle.add(circle);
// Lathe
var points = [];
for ( let i = 0; i < 10; i ++ ) {
points.push( new THREE.Vector2( Math.sin( i * 0.2 ) * 10 + 5, ( i - 5 ) * 2 ) );
}
var latheGeometry = new THREE.LatheGeometry( points );
var lathe = new THREE.Mesh( latheGeometry, material );
lathe.position.y = 50;
lathe.rotation.x = Math.PI;
lathe.scale.x = 3;
lathe.scale.y = 3;
lathe.scale.z = 3;
lathe.castShadow = true;
lathe.receiveShadow = true;
candle.add( lathe );
var geometry = new THREE.CylinderGeometry( 10, 16, 6, 64, 64 );
var cylinder = new THREE.Mesh( geometry, material );
cylinder.position.y = 30;
cylinder.scale.x = 4;
cylinder.scale.y = 4;
cylinder.scale.z = 4;
cylinder.castShadow = true;
cylinder.receiveShadow = true;
candle.add(cylinder)
var geometry = new THREE.ConeGeometry( 25, 10, 30, 52, true );
var cone = new THREE.Mesh( geometry, material );
cone.position.y = 80;
cone.rotation.x = Math.PI;
cone.scale.x = 2.5;
cone.scale.y = 2.5;
cone.scale.z = 2.5;
cone.castShadow = true;
candle.add( cone );
lathe = new THREE.Mesh( latheGeometry, material );
lathe.position.y = 90;
lathe.rotation.x = Math.PI;
lathe.scale.x = 1.5;
lathe.scale.y = 1.5;
lathe.scale.z = 1.5;
lathe.castShadow = true;
candle.add( lathe );
geometry = new THREE.CylinderGeometry( 7, 7, 30, 20);
cylinder = new THREE.Mesh( geometry, material );
cylinder.position.y = 100;
cylinder.castShadow = true;
candle.add(cylinder);
geometry = new THREE.RingGeometry( 5, 12, 10 );
var ring = new THREE.Mesh( geometry, material );
ring.position.y = 110;
ring.rotation.x = Math.PI/2;
candle.add(ring);
geometry = new THREE.CylinderGeometry( 17, 6, 34, 64, 64 );
cylinder = new THREE.Mesh( geometry, material );
cylinder.position.y = 130;
candle.add(cylinder);
geometry = new THREE.TorusGeometry( 20, 3, 16, 100 );
var torus = new THREE.Mesh( geometry, material );
torus.position.y = 148;
torus.rotation.x = 55;
torus.castShadow = true;
candle.add( torus );
geometry = new THREE.CylinderGeometry( 15, 15, 18, 12, 12 );
cylinder = new THREE.Mesh( geometry, material );
cylinder.position.y = 155;
candle.add(cylinder);
geometry = new THREE.TorusGeometry( 13, 3, 16, 100 );
var torus = new THREE.Mesh( geometry, material );
torus.position.y = 163;
torus.rotation.x = 55;
candle.add( torus );
candle.position.y += tableTopHeight;
candle.position.z -= tableTopWidthZ - 150;
candle.position.x -= 60;
var cireMaterial = new THREE.MeshLambertMaterial( { color: 0xFFF0bc } );
geometry = new THREE.CylinderGeometry( 12, 12, 8, 12, 12 );
cylinder = new THREE.Mesh( geometry, cireMaterial );
cylinder.position.y = 165;
candle.add(cylinder);
geometry = new THREE.CylinderGeometry( 2, 2, 10, 12, 12 );
cylinder = new THREE.Mesh( geometry,
new THREE.MeshBasicMaterial( {color: 0x00000} ) );
cylinder.position.y = 170;
candle.add(cylinder);
candle.castShadow = true;
scene.add(candle);
return candle;
}
function importObj() {
var loader = new THREE.OBJLoader();
// console.log("testObj");
// Charger le fichier
loader.load(
'res/obj/skull.obj',
function ( object ) {
var material = new THREE.MeshLambertMaterial( { color: 0x582900 } );
object.rotation.x = -Math.PI/2;
object.rotation.z = Math.PI/4;
object.position.x = tableTopWidthX + 100;
object.position.y = tableTopHeight + 75;
object.position.z = tableTopWidthZ - 150;
object.material = material;
object.traverse( function ( child ) {
if ( child instanceof THREE.Mesh ) {
child.castShadow = true;
}
} );
scene.add( object );
}
);
}
function animate() {
window.requestAnimationFrame(animate);
importObj();
render()
}
function setupGui() {
effectController = {
newGridX: gridX,
newGridY: gridY,
newGridZ: gridZ,
newGround: ground,
newAxes: axes,
enableShadow: true,
enableLighting: true,
lightDistance: 500,
lightIntensity: 1,
controlAmbientLight: false,
ambientLightIntensity: 1.0,
enableShader: false,
lz: 0,
lz2: 0
};
var gui = new dat.GUI({ autoPlace: false });
var customContainer = document.getElementById('gui-container');
customContainer.appendChild(gui.domElement);
var h = gui.addFolder("Grid display")
h.add(effectController, "newGridX").name("Show XZ grid");
h.add( effectController, "newGridY" ).name("Show YZ grid");
h.add( effectController, "newGridZ" ).name("Show XY grid");
h.add( effectController, "newGround" ).name("Show ground");
h.add( effectController, "newAxes" ).name("Show axes");
h = gui.addFolder("Letter angles")
h.add(effectController, "lz", -180.0, 180.0, 0.025).name("Letter z");
h.add(effectController, "lz2", -180.0, 180.0, 0.025).name("Letter2 z");
h = gui.addFolder("Light lamp")
h.add(effectController, "lightDistance", 0.0, 1000.0, 0.025).name("Light distance");
h.add(effectController, "lightIntensity", 0.0, 100.0, 0.025).name("Light intensity");
h = gui.addFolder("Global lighting")
h.add(effectController, "enableShadow").name("Enable shadow");
h.add(effectController, "enableLighting").name("Enable lighting");
h.add(effectController, "controlAmbientLight").name("Control Ambient Lighting");
h.add(effectController, "ambientLightIntensity", 0.0, 1.0, 0.000025).name("Ambient Light intensity");
h = gui.addFolder("Shader")
h.add(effectController, "enableShader").name("Enable shader");
}
try {
init();
setupGui();
addToDOM();
animate();
} catch(e) {
var errorReport = "Your program encountered an unrecoverable error, can not draw on canvas. Error was:<br/><br/>";
//$('#container').append(errorReport+e);
}