-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.2.js
More file actions
276 lines (228 loc) · 9.05 KB
/
5.2.js
File metadata and controls
276 lines (228 loc) · 9.05 KB
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
"use strict"; // good practice - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
/*global THREE, Coordinates, $, document, window, dat*/
import * as THREE from "three";
import { OBJLoader } from "three/addons/loaders/OBJLoader.js";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import {dat} from "/lib/dat.gui.min.js";
import {Coordinates} from "../lib/Coordinates.js";
let cameraControls;
const clock = new THREE.Clock();
let camera, renderer, headlight, light;
function fillScene() {
window.scene = new THREE.Scene();
window.scene.fog = new THREE.Fog( 0xAAAAAA, 2000, 4000 );
window.scene.add( new THREE.AmbientLight( 0x222222 ) );
light = new THREE.DirectionalLight( 0xFFFFFF, 1.5 );
light.position.set( -200, 200, -400 );
window.scene.add( light );
//window.scene.add( new THREE.AmbientLight( 0xFFFFFF ) );
//headlight = new THREE.PointLight( 0xFFFFFF, 1);
//window.scene.add( headlight );
const headMaterial = new THREE.MeshLambertMaterial();
headMaterial.color.r = 104/255;
headMaterial.color.g = 1/255;
headMaterial.color.b = 5/255;
const hatMaterial = new THREE.MeshPhongMaterial({shininess: 100});
hatMaterial.color.r = 24/255;
hatMaterial.color.g = 38/255;
hatMaterial.color.b = 77/255;
hatMaterial.specular.setRGB( 0.5, 0.5, 0.5 );
const bodyMaterial = new THREE.MeshPhongMaterial({shininess: 100});
bodyMaterial.color.setRGB( 31/255, 86/255, 169/255 );
bodyMaterial.specular.setRGB( 0.5, 0.5, 0.5 );
const glassMaterial = new THREE.MeshPhongMaterial({
color: 0x0,
specular: 0xFFFFFF,
shininess: 100,
opacity: 0.3,
transparent: true
});
const legMaterial = new THREE.MeshPhongMaterial({shininess: 4});
legMaterial.color.setHex( 0xAdA79b );
legMaterial.specular.setRGB( 0.5, 0.5, 0.5 );
const footMaterial = new THREE.MeshPhongMaterial({color: 0x960f0b, shininess: 30});
footMaterial.specular.setRGB( 0.5, 0.5, 0.5 );
const crossbarMaterial = new THREE.MeshPhongMaterial({color: 0x808080, specular: 0xFFFFFF, shininess: 400});
const eyeMaterial = new THREE.MeshPhongMaterial({color: 0x000000, specular: 0x303030, shininess: 4});
let sphere, cylinder, cube;
const bevelRadius = 1.9; // TODO: 2.0 causes some geometry bug.
// MODELS
// base
cube = new THREE.Mesh(
new THREE.BoxGeometry( 20+64+110, 4, 2*77+12 ), footMaterial );
cube.position.x = -45; // (20+32) - half of width (20+64+110)/2
cube.position.y = 4/2; // half of height
cube.position.z = 0; // centered at origin
window.scene.add( cube );
// feet
cube = new THREE.Mesh(
new THREE.BoxGeometry( 20+64+110, 52, 6 ), footMaterial );
cube.position.x = -45; // (20+32) - half of width (20+64+110)/2
cube.position.y = 52/2; // half of height
cube.position.z = 77 + 6/2; // offset 77 + half of depth 6/2
window.scene.add( cube );
cube = new THREE.Mesh(
new THREE.BoxGeometry( 20+64+110, 52, 6 ), footMaterial );
cube.position.x = -45; // (20+32) - half of width (20+64+110)/2
cube.position.y = 52/2; // half of height
cube.position.z = -(77 + 6/2); // negative offset 77 + half of depth 6/2
window.scene.add( cube );
cube = new THREE.Mesh(
new THREE.BoxGeometry( 64, 104, 6 ), footMaterial );
cube.position.x = 0; // centered on origin along X
cube.position.y = 104/2;
cube.position.z = 77 + 6/2; // negative offset 77 + half of depth 6/2
window.scene.add( cube );
cube = new THREE.Mesh(
new THREE.BoxGeometry( 64, 104, 6 ), footMaterial );
cube.position.x = 0; // centered on origin along X
cube.position.y = 104/2;
cube.position.z = -(77 + 6/2); // negative offset 77 + half of depth 6/2
window.scene.add( cube );
// legs
cube = new THREE.Mesh(
new THREE.BoxGeometry( 60, 282+4, 4 ), legMaterial );
cube.position.x = 0; // centered on origin along X
cube.position.y = 104 + 282/2 - 2;
cube.position.z = 77 + 6/2; // negative offset 77 + half of depth 6/2
window.scene.add( cube );
cube = new THREE.Mesh(
new THREE.BoxGeometry( 60, 282+4, 4 ), legMaterial );
cube.position.x = 0; // centered on origin along X
cube.position.y = 104 + 282/2 - 2;
cube.position.z = -(77 + 6/2); // negative offset 77 + half of depth 6/2
window.scene.add( cube );
// body
sphere = new THREE.Mesh(
new THREE.SphereGeometry( 104/2, 32, 16, 0, Math.PI * 2, Math.PI/2, Math.PI ), bodyMaterial );
sphere.position.x = 0;
sphere.position.y = 160;
sphere.position.z = 0;
window.scene.add( sphere );
// cap for top of hemisphere
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 104/2, 104/2, 0, 32 ), bodyMaterial );
cylinder.position.x = 0;
cylinder.position.y = 160;
cylinder.position.z = 0;
window.scene.add( cylinder );
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 12/2, 12/2, 390 - 100, 32 ), bodyMaterial );
cylinder.position.x = 0;
cylinder.position.y = 160 + 390/2 - 100;
cylinder.position.z = 0;
window.scene.add( cylinder );
// glass stem
sphere = new THREE.Mesh(
new THREE.SphereGeometry( 116/2, 32, 16 ), glassMaterial );
sphere.position.x = 0;
sphere.position.y = 160;
sphere.position.z = 0;
window.scene.add( sphere );
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 24/2, 24/2, 390, 32 ), glassMaterial );
cylinder.position.x = 0;
cylinder.position.y = 160 + 390/2;
cylinder.position.z = 0;
window.scene.add( cylinder );
// head
sphere = new THREE.Mesh(
new THREE.SphereGeometry( 104/2, 32, 16 ), headMaterial );
sphere.position.x = 0;
sphere.position.y = 160 + 390;
sphere.position.z = 0;
window.scene.add( sphere );
// hat
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 142/2, 142/2, 10, 32 ), hatMaterial );
cylinder.position.x = 0;
cylinder.position.y = 160 + 390 + 40 + 10/2;
cylinder.position.z = 0;
window.scene.add( cylinder );
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 80/2, 80/2, 70, 32 ), hatMaterial );
cylinder.position.x = 0;
cylinder.position.y = 160 + 390 + 40 + 10 + 70/2;
cylinder.position.z = 0;
window.scene.add( cylinder );
// crossbar
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 5, 5, 200, 32 ), crossbarMaterial );
cylinder.position.set( 0, 360, 0 );
cylinder.rotation.x = 90 * Math.PI / 180.0;
window.scene.add( cylinder );
// nose
cylinder = new THREE.Mesh(
new THREE.CylinderGeometry( 6, 14, 70, 32 ), headMaterial );
cylinder.position.set( -70, 530, 0 );
cylinder.rotation.z = 90 * Math.PI / 180.0;
window.scene.add( cylinder );
// eyes
const sphGeom = new THREE.SphereGeometry(10, 32, 16);
// left eye
sphere = new THREE.Mesh( sphGeom, eyeMaterial );
sphere.position.set( -48, 560, 0 );
let eye = new THREE.Object3D();
eye.add( sphere );
eye.rotation.y = 20 * Math.PI / 180.0;
window.scene.add( eye );
// right eye
sphere = new THREE.Mesh( sphGeom, eyeMaterial );
sphere.position.set( -48, 560, 0 );
eye = new THREE.Object3D();
eye.add( sphere );
eye.rotation.y = -20 * Math.PI / 180.0;
window.scene.add( eye );
}
function init() {
const canvasWidth = 846;
const canvasHeight = 494;
// For grading the window is fixed in size; here's general code:
//var canvasWidth = window.innerWidth;
//var canvasHeight = window.innerHeight;
// RENDERER
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.setSize(canvasWidth, canvasHeight);
renderer.setClearColor( 0xAAAAAA, 1.0 );
// CAMERA
camera = new THREE.PerspectiveCamera( 35, canvasWidth/ canvasHeight, 1, 4000 );
camera.position.set( -1160, 350, -600 );
// CONTROLS
cameraControls = new OrbitControls(camera, renderer.domElement);
cameraControls.target.set(0,310,0);
}
function drawHelpers() {
Coordinates.drawGround({size:10000});
Coordinates.drawGrid({size:10000,scale:0.01});
}
function addToDOM() {
const container = document.getElementById('webGL');
const canvas = container.getElementsByTagName('canvas');
if (canvas.length>0) {
container.removeChild(canvas[0]);
}
container.appendChild( renderer.domElement );
}
function animate() {
window.requestAnimationFrame(animate);
render();
}
function render() {
const delta = clock.getDelta();
cameraControls.update(delta);
// Set the position of the directional light to follow the camera
light.position.set(camera.position.x, camera.position.y, camera.position.z);
renderer.render(window.scene, camera);
}
try {
init();
fillScene();
drawHelpers();
addToDOM();
animate();
} catch(e) {
const errorReport = "Your program encountered an unrecoverable error, can not draw on canvas. Error was:<br/><br/>";
$('#webGL').append(errorReport+e.stack);
}