-
Notifications
You must be signed in to change notification settings - Fork 1
/
Oulu.js
382 lines (321 loc) · 9.08 KB
/
Oulu.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
/* -*- js-indent-level: 8 -*- */
/*jslint white: true */
/*
OuluThreeJS
Author: Playsign
Date: 2013
*/
// MAIN
var container, scene, carCamera, flyCamera, renderer, controls, flyControls, stats, directionalLight;
var flyMode = false;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
var car, oulu, colliderBuildings, colliderGround;
var debugMode = false;
var controlsCar = {
moveForward: false,
moveBackward: false,
moveLeft: false,
moveRight: false
};
// FUNCTIONS
function init() {
// SCENE
scene = new THREE.Scene();
// CAR CAMERA
var SCREEN_WIDTH = window.innerWidth,
SCREEN_HEIGHT = window.innerHeight;
var VIEW_ANGLE = 45,
ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT,
NEAR = 0.1,
FAR = 20000;
carCamera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
scene.add(carCamera);
carCamera.position.set(0, 0, 0);
// FLY CAMERA
flyCamera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
scene.add(flyCamera);
flyCamera.position.set(0, 0, 0); // don't touch this! modify freelook.js --> yawObject.position instead
// RENDERER
if (Detector.webgl)
renderer = new THREE.WebGLRenderer({
antialias: true
});
else
renderer = new THREE.CanvasRenderer();
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
// For shadows
// renderer.shadowMapEnabled = true;
// renderer.shadowMapSoft = true;
// renderer.shadowCameraNear = 3;
// renderer.shadowCameraFar = camera.far;
// renderer.shadowCameraFov = 50;
// renderer.shadowMapBias = 0.0039;
// renderer.shadowMapDarkness = 0.5;
// renderer.shadowMapWidth = 1024;
// renderer.shadowMapHeight = 1024;
container = document.getElementById('ThreeJS');
container.appendChild(renderer.domElement);
// EVENTS
THREEx.WindowResize(renderer, carCamera);
THREEx.FullScreen.bindKey({
charCode: 'm'.charCodeAt(0)
});
document.addEventListener('keydown', onKeyDown, false);
document.addEventListener('keyup', onKeyUp, false);
// FLY CONTROLS
// flyControls = new THREE.FlyControls(camera);
// flyControls.movementSpeed = 1000;
// flyControls.domElement = renderer.domElement;
// flyControls.rollSpeed = Math.PI / 12; // Math.PI / 24
// flyControls.autoForward = false;
// flyControls.dragToLook = true;
flyControls = new THREE.FreeLookControls(flyCamera, renderer.domElement);
flyControls.enabled = false;
scene.add(flyControls.getObject());
// STATS
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.bottom = '0px';
stats.domElement.style.zIndex = 100;
container.appendChild(stats.domElement);
// White directional light at half intensity shining from the top.
directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(100, 100, 100);
directionalLight.castShadow = true;
scene.add(directionalLight);
////////////
// CUSTOM //
////////////
// Note: if imported model appears too dark,
// add an ambient light in this file
// and increase values in model's exported .js file
// to e.g. "colorAmbient" : [0.75, 0.75, 0.75]
var jsonLoader = new THREE.JSONLoader();
jsonLoader.load("Masterscene.js", function(geometry, material) {
addModelToScene(geometry, material, "oulu");
});
jsonLoader.load("ColliderBuildings.js", function(geometry, material) {
addModelToScene(geometry, material, "colliderbuildings");
});
jsonLoader.load("ColliderGround.js", function(geometry, material) {
addModelToScene(geometry, material, "colliderground");
});
// addModelToScene function is called back after model has loaded
var ambientLight = new THREE.AmbientLight(0x6b6b6b);
scene.add(ambientLight);
// CAR
car = new THREE.Car();
car.modelScale = 0.8;
car.backWheelOffset = 0.02;
car.MAX_SPEED = 0.9; //25
car.MAX_REVERSE_SPEED = -0.5; //-15
car.FRONT_ACCELERATION = 0.4; //12
car.BACK_ACCELERATION = 0.5; //15
car.WHEEL_ANGULAR_ACCELERATION = 1; //1.5
car.FRONT_DECCELERATION = 0.5 //10
car.WHEEL_ANGULAR_DECCELERATION = 1; //1.0
car.STEERING_RADIUS_RATIO = 0.23; //0.23
car.callback = function(object) {
addCar(object, 142, 15, -20, 1); //10
};
car.loadPartsJSON("GreenCar.js", "GreenCar.js");
animate();
}
function addCar(object, x, y, z, s) {
console.log("Add car");
object.root.position.set(x, y, z);
scene.add(object.root);
// object.root.castShadow = true;
// object.root.receiveShadow = true;
}
var texcache = {};
var useTexcache = false;
var allMaterials = [];
var allGeometries = [];
var unloadTextures = null;
function addModelToScene(geometry, materials, type, path) {
if(path === undefined){
path = "./images/";
}
var material, newMesh;
var basicMaterial;
allGeometries.push(geometry);
if (type == "oulu" && debugMode === false) {
var newMaterials = [];
for (var i = 0; i < materials.length; i++) {
if (materials[i].map) {
// JPG TO DDS
console.log( materials[i].map);
var ddsName = materials[i].map.sourceFile.substr(0, materials[i].map.sourceFile.lastIndexOf(".")) + ".dds";
// console.log("ddsName: " + ddsName);
var texpath = path + ddsName;
if (useTexcache && texcache.hasOwnProperty(texpath)) {
map = texcache[texpath];
console.log("tex cache hit: " + texpath);
} else {
map = texcache[texpath] = THREE.ImageUtils.loadCompressedTexture(texpath);
map.wrapS = map.wrapT = THREE.RepeatWrapping; // for dds only
map.repeat.set(1, 1); // for dds only
// map = THREE.ImageUtils.loadCompressedTexture( materials[i].map.sourceFile + ".dds" );
map.minFilter = map.magFilter = THREE.LinearFilter;
map.anisotropy = 4;
}
basicMaterial = new THREE.MeshBasicMaterial({
map: map
})
newMaterials.push(basicMaterial);
allMaterials.push(basicMaterial);
} else {
newMaterials.push(materials[i]);
allMaterials.push(materials[i]);
// console.log("png: " + i);
}
}
unloadTextures = function () {
console.log("unloading textures");
for (var i = 0; i < allMaterials.length; i++)
allMaterials[i].dispose();
for (var i = 0; i < allGeometries.length; i++)
allGeometries[i].dispose();
for (var key in texcache)
if (texcache.hasOwnProperty(key))
texcache[key].dispose();
console.log("done");
}
material = new THREE.MeshFaceMaterial(newMaterials);
newMesh = new THREE.Mesh(geometry, material);
oulu = newMesh;
// oulu.castShadow = true;
// oulu.receiveShadow = true;
} else if (type == "colliderbuildings") {
material = new THREE.MeshFaceMaterial(materials);
newMesh = new THREE.Mesh(geometry, material);
if (debugMode == false) {
newMesh.visible = false;
}
colliderBuildings = newMesh;
} else if (type == "colliderground") {
material = new THREE.MeshFaceMaterial(materials);
newMesh = new THREE.Mesh(geometry, material);
if (debugMode == false) {
newMesh.visible = false;
}
colliderGround = newMesh;
}
newMesh.scale.set(1.5, 1.5, 1.5);
scene.add(newMesh);
}
function animate() {
requestAnimationFrame(animate);
render();
update();
}
function setFlyMode(flying) {
if (flying === true || flying === false) {
flyMode = flying;
flyControls.dragging = false;
flyControls.enabled = flying;
flying === true ? THREEx.WindowResize(renderer, flyCamera) : THREEx.WindowResize(renderer, carCamera);
} else {
console.log("setFlyMode illegal parameter")
}
}
function update() {
var delta = clock.getDelta(); // seconds.
if (flyMode === true) {
flyControls.update(delta * 1000);
} else {
if (car && car.bodyMesh) {
var relativeCameraOffset = new THREE.Vector3(0, 3, -15);
var cameraOffset = relativeCameraOffset.applyMatrix4(car.bodyMesh.matrixWorld);
carCamera.position.x = cameraOffset.x;
carCamera.position.y = cameraOffset.y;
carCamera.position.z = cameraOffset.z;
carCamera.lookAt(car.root.position);
carCamera.position.y += 3;
}
// update car model
car.updateCarModel(delta, controlsCar);
}
stats.update();
}
function render() {
flyMode ? renderer.render(scene, flyCamera) : renderer.render(scene, carCamera);
}
function onKeyDown(event) {
switch (event.keyCode) {
case 38:
/*up*/
controlsCar.moveForward = true;
break;
case 87:
/*W*/
controlsCar.moveForward = true;
break;
case 40:
/*down*/
controlsCar.moveBackward = true;
break;
case 83:
/*S*/
controlsCar.moveBackward = true;
break;
case 37:
/*left*/
controlsCar.moveLeft = true;
break;
case 65:
/*A*/
controlsCar.moveLeft = true;
break;
case 39:
/*right*/
controlsCar.moveRight = true;
break;
case 68:
/*D*/
controlsCar.moveRight = true;
break;
case 70:
/*F*/
setFlyMode(!flyMode);
break;
}
};
function onKeyUp(event) {
switch (event.keyCode) {
case 38:
/*up*/
controlsCar.moveForward = false;
break;
case 87:
/*W*/
controlsCar.moveForward = false;
break;
case 40:
/*down*/
controlsCar.moveBackward = false;
break;
case 83:
/*S*/
controlsCar.moveBackward = false;
break;
case 37:
/*left*/
controlsCar.moveLeft = false;
break;
case 65:
/*A*/
controlsCar.moveLeft = false;
break;
case 39:
/*right*/
controlsCar.moveRight = false;
break;
case 68:
/*D*/
controlsCar.moveRight = false;
break;
}
};