-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps-rear-view-camera.js
338 lines (279 loc) · 10.5 KB
/
ps-rear-view-camera.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
"use strict"; // good practice - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
////////////////////////////////////////////////////////////////////////////////
// Rearview camera //
////////////////////////////////////////////////////////////////////////////////
/*global THREE, Coordinates, $ */
var camera, rearCam, scene, renderer;
var cameraControls;
var clock = new THREE.Clock();
var canvasWidth;
var canvasHeight;
var rearTarget;
function fillScene() {
scene = new THREE.Scene();
//scene.fog = new THREE.Fog( 0xDDDDDD, 3000, 4000 );
// ArrowHelper
var sourcePos = new THREE.Vector3(0, 0, 1) ;
var targetPos = new THREE.Vector3(0, 0, 50);
var direction = new THREE.Vector3().subVectors(targetPos, sourcePos) ;
var arrow = new THREE.ArrowHelper(direction.clone().normalize(), sourcePos, direction.length(), 0x00ff00);
scene.add(arrow);
console.log(">>> arrow ", arrow.position ) ;
var newSourcePos = new THREE.Vector3(10, 10, 10);
var newTargetPos = new THREE.Vector3(60, 10, 10);
arrow.position.set(newSourcePos);
direction = new THREE.Vector3().subVectors(newTargetPos, newSourcePos);
arrow.setDirection(direction.normalize());
arrow.setLength(direction.length());
// LIGHTS
scene.add( new THREE.AmbientLight( 0x222222 ) );
var light = new THREE.DirectionalLight( 0xFFFFFF, 0.7 );
light.position.set( 200, 500, 500 );
scene.add( light );
light = new THREE.DirectionalLight( 0xFFFFFF, 0.9 );
light.position.set( -200, -100, -400 );
scene.add( light );
// some objects
var x,z;
for ( x = -4500; false && x <= 4500; x += 1000 ) {
for ( z = -4500; z <= 4500; z += 1000 ) {
var coneMaterial = new THREE.MeshLambertMaterial();
// color wheel
var coneDistance = Math.sqrt( x*x + z*z );
// convert from angle to value in 2*PI range
var colorHue = Math.acos( x / coneDistance );
if ( z > 0 )
{
colorHue = Math.PI*2 - colorHue;
}
var colorSat = 1 - (coneDistance / 6364);
coneMaterial.color.setHSL((colorHue+1)/(Math.PI*2), colorSat, 0.6 );
coneMaterial.ambient.copy( coneMaterial.color );
var cone = new THREE.Mesh(
new THREE.CylinderGeometry( 100 - colorSat*100, colorSat*100, 300 ), coneMaterial );
cone.position.set( x, 150, z );
scene.add( cone );
}
}
// MATERIALS
var headMaterial = new THREE.MeshLambertMaterial( );
headMaterial.color.r = 104/255;
headMaterial.color.g = 1/255;
headMaterial.color.b = 5/255;
var 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 );
var bodyMaterial = new THREE.MeshPhongMaterial( { shininess: 100 } );
bodyMaterial.color.setRGB( 31/255, 86/255, 169/255 );
bodyMaterial.specular.setRGB( 0.5, 0.5, 0.5 );
var glassMaterial = new THREE.MeshPhongMaterial( { color: 0x0, specular: 0xFFFFFF, shininess: 100, opacity: 0.3, transparent: true } );
var legMaterial = new THREE.MeshPhongMaterial( { shininess: 4 } );
legMaterial.color.setHex( 0xAdA79b );
legMaterial.specular.setRGB( 0.5, 0.5, 0.5 );
var footMaterial = new THREE.MeshPhongMaterial( { color: 0x960f0b, shininess: 30 } );
footMaterial.specular.setRGB( 0.5, 0.5, 0.5 );
var sphere, cylinder, cube;
var bevelRadius = 1.9; // TODO: 2.0 causes some geometry bug.
// MODELS
// base
cube = new THREE.Mesh(
new THREE.BeveledBlockGeometry( 20+64+110, 4, 2*77+12, bevelRadius ), 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
//scene.add( cube );
// feet
cube = new THREE.Mesh(
new THREE.BeveledBlockGeometry( 20+64+110, 52, 6, bevelRadius ), 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
scene.add( cube );
cube = new THREE.Mesh(
new THREE.BeveledBlockGeometry( 20+64+110, 52, 6, bevelRadius ), 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
scene.add( cube );
cube = new THREE.Mesh(
new THREE.BeveledBlockGeometry( 64, 104, 6, bevelRadius ), 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
scene.add( cube );
cube = new THREE.Mesh(
new THREE.BeveledBlockGeometry( 64, 104, 6, bevelRadius ), 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
scene.add( cube );
// legs
cube = new THREE.Mesh(
new THREE.BeveledBlockGeometry( 60, 282+4, 4, bevelRadius ), 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
scene.add( cube );
cube = new THREE.Mesh(
new THREE.BeveledBlockGeometry( 60, 282+4, 4, bevelRadius ), 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
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;
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;
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;
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;
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;
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;
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;
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;
scene.add( cylinder );
var crossbarMaterial = new THREE.MeshPhongMaterial( { color: 0x808080, specular: 0xFFFFFF, shininess: 400 } );
var eyeMaterial = new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x303030, shininess: 4 } );
// 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;
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;
scene.add( cylinder );
// eyes
var sphGeom = new THREE.SphereGeometry( 10, 32, 16 );
// left eye
sphere = new THREE.Mesh( sphGeom, eyeMaterial );
sphere.position.set( -48, 560, 0 );
var eye = new THREE.Object3D();
eye.add( sphere );
eye.rotation.y = 20 * Math.PI / 180.0;
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;
scene.add( eye );
}
function init() {
canvasWidth = 846;
canvasHeight = 494;
// For grading the window is fixed in size; here's general code:
//var canvasWidth = window.innerWidth;
//var canvasHeight = window.innerHeight;
// allocate just once, instead of per render
rearTarget = new THREE.Vector3();
// RENDERER
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.setSize(canvasWidth, canvasHeight);
renderer.setClearColorHex( 0xDDDDDD, 1.0 );
// don't clear when second viewport is drawn
renderer.autoClear = false;
// CAMERA
camera = new THREE.PerspectiveCamera( 45, canvasWidth / canvasHeight, 1, 7000 );
camera.position.set( -810, 310, -1230 );
rearCam = new THREE.PerspectiveCamera( 45, canvasWidth / canvasHeight, 1, 7000 );
rearCam.position.set( -810, 310, -1230 );
rearCam.lookAt( new THREE.Vector3( 0, 0, 0 ) );
// CONTROLS
cameraControls = new THREE.OrbitAndPanControls(camera, renderer.domElement);
cameraControls.target.set(0,130,0);
}
function drawHelpers() {
Coordinates.drawGround({size:10000});
Coordinates.drawGrid({size:10000,scale:0.01});
}
function addToDOM() {
var container = document.getElementById('container');
var canvas = container.getElementsByTagName('canvas');
if (canvas.length>0) {
container.removeChild(canvas[0]);
}
container.appendChild( renderer.domElement );
}
function animate() {
window.requestAnimationFrame(animate);
render();
}
function render() {
var delta = clock.getDelta();
cameraControls.update(delta);
renderer.enableScissorTest( false );
renderer.setViewport( 0, 0, canvasWidth, canvasHeight );
renderer.clear();
renderer.render( scene, camera );
// Student: set rearCam so it's pointing in the opposite direction than the camera
// rearview render
renderer.enableScissorTest( true );
// setScissor could be set just once in this particular case,
// since it never changes, and then just enabled/disabled
renderer.setScissor( 0.75 * canvasWidth, 0.75 * canvasHeight,
0.25 * canvasWidth, 0.25 * canvasHeight );
renderer.setViewport( 0.75 * canvasWidth, 0.75 * canvasHeight,
0.25 * canvasWidth, 0.25 * canvasHeight );
renderer.clear();
renderer.render( scene, rearCam );
}
try {
init();
fillScene();
drawHelpers();
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);
}