-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathship.html
458 lines (413 loc) · 13 KB
/
ship.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
* {
margin:0;
padding:0;
}
body {
width: 100%;
height: 100%;
overflow: hidden;
}
</style>
</head>
<body>
<!-- http://www.cs.utah.edu/~shirley/papers/sunsky/sunsky.pdf -->
<script src="libs/three.min.js"></script>
<script src="libs/dat.gui.min.js"></script>
<script src="libs/stats.min.js"></script>
<script src="libs/OrbitControls.js"></script>
<script src="libs/Detector.js"></script>
<script src="libs/Mirror.js"></script>
<script src="libs/WaterShader.js"></script>
<script src="libs/SkyShader.js"></script>
<script>
// global variables
var renderer;
var scene;
var camera;
var control; //GUI
var stats; //FPS
var cameraControl; //ortagonal camera
//moving ahead
var clock = new THREE.Clock();
// ---
var rotationSpeed = 0.05;
var translationSpeed = 150;
// ---
var rotateLeft = false;
var rotateRight = false;
var moveAhead = false;
var moveBackwards = false;
window.onkeydown = onKeyDown;
window.onkeyup = onKeyUp;
//web audio
var audioContext;
var sound;
var panner;
/*
* Initializes the scene, camera and objects. Called when the window is
* loaded by using window.onload (see below)
*/
var container, stats;
var camera, scene, renderer;
var sphere;
//sailboat
var boat;
var parameters = {
width: 2000,
height: 2000,
widthSegments: 250,
heightSegments: 250,
depth: 1500,
param: 4,
filterparam: 1
}
var waterNormals;
//raycaster
var mouse = new THREE.Vector2(), INTERSECTED, raycaster;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 0.5, 3000000 );
camera.position.set( 2000, 750, 2000 );
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.userPan = false;
controls.userPanSpeed = 0.0;
controls.maxDistance = 5000.0;
controls.maxPolarAngle = Math.PI * 0.495;
controls.center.set( 0, 500, 0 );
var light = new THREE.HemisphereLight( 0xffffbb, 0x080820, 1 );
light.position.set( - 1, 0.2, - 1 );
scene.add( light );
//water
waterNormals = new THREE.ImageUtils.loadTexture( 'images/materials/waternormals.jpg' );
waterNormals.wrapS = waterNormals.wrapT = THREE.RepeatWrapping;
water = new THREE.Water( renderer, camera, scene, {
textureWidth: 512,
textureHeight: 512,
waterNormals: waterNormals,
alpha: 1.0,
sunDirection: light.position.clone().normalize(),
sunColor: 0xffffff,
waterColor: 0x001e0f,
distortionScale: 50.0,
} );
mirrorMesh = new THREE.Mesh(
new THREE.PlaneBufferGeometry( parameters.width * 500, parameters.height * 500 ),
water.material
);
mirrorMesh.add( water );
mirrorMesh.rotation.x = - Math.PI * 0.5;
scene.add( mirrorMesh );
//raycaster
raycaster = new THREE.Raycaster();
//sky initiation
initSky();
//sailboat
var jsonLoader = new THREE.JSONLoader();
jsonLoader.load( "objects/boat/boat-body.json", addModelToScene );
function addModelToScene( geometry, materials ) {
var material = new THREE.MeshPhongMaterial({
color: 0x31738F
});
boat = new THREE.Mesh( geometry, material );
boat.scale.set(2,2,2);
boat.position.set(0,-200,0);
boat.castShadow = true;
boat.recieveShadow = true;
boat.name = "boat";
scene.add(boat);
loadBaloon();
loadGlass();
loadMetalOne();
loadMetalTwo();
loadPlane();
loadSoft();
render();
}
function loadBaloon() {
jsonLoader.load( "objects/boat/boat-baloon.json", loadBaloonModel );
function loadBaloonModel( geometry, materials ) {
var material = new THREE.MeshPhongMaterial({
color: 0xffff00
});
var baloon = new THREE.Mesh( geometry, material );
baloon.name = "baloon";
boat.add(baloon);
}
}
function loadGlass() {
jsonLoader.load( "objects/boat/boat-glass.json", loadGlassModel );
function loadGlassModel( geometry, materials ) {
var material = new THREE.MeshPhongMaterial({
color: 0x000000,
transparent: true,
opacity: 0.65
});
var glass = new THREE.Mesh( geometry, material );
glass.name = "glass";
boat.add(glass);
}
}
function loadMetalOne() {
jsonLoader.load( "objects/boat/boat-metal-1.json", loadMetalOneModel );
function loadMetalOneModel( geometry, materials ) {
var material = new THREE.MeshPhongMaterial({
color: 0x333333
});
var metalOne = new THREE.Mesh( geometry, material );
metalOne.name = "metal-one";
boat.add(metalOne);
}
}
function loadMetalTwo() {
jsonLoader.load( "objects/boat/boat-metal-2.json", loadMetalTwoModel );
function loadMetalTwoModel( geometry, materials ) {
var material = new THREE.MeshPhongMaterial({
color: 0x333333
});
var metalTwo = new THREE.Mesh( geometry, material );
metalTwo.name = "metal-two";
boat.add(metalTwo);
}
}
function loadPlane() {
jsonLoader.load( "objects/boat/boat-plane.json", loadPlaneModel );
function loadPlaneModel( geometry, materials ) {
var material = new THREE.MeshPhongMaterial({
color: 0x666666
});
var plane = new THREE.Mesh( geometry, material );
plane.name = "plane";
boat.add(plane);
}
}
function loadSoft() {
jsonLoader.load( "objects/boat/boat-soft.json", loadSoftModel );
function loadSoftModel( geometry, materials ) {
var material = new THREE.MeshPhongMaterial({
color: 0xffffff
});
var soft = new THREE.Mesh( geometry, material );
soft.name = "soft";
boat.add(soft);
}
}
// web audio
window.AudioContext = window.AudioContext||window.webkitAudioContext;
audioContext = new AudioContext();
var sound = new Audio();
sound.addEventListener("canplaythrough",play,false);
sound.src = "sound/youtopia.mp3";
sound.loop = true;
function play() {
var source = audioContext.createMediaElementSource(sound);
panner = audioContext.createPanner();
panner.panningModel = 'HRTF';
panner.distanceModel = 'inverse';
panner.coneOuterGain = 10;
panner.refDistance = 100;
panner.maxDistance = 100000;
panner.rolloffFactor = 1;
panner.coneOuterAngle = 0; // outside this, volume is coneOuterGain
panner.coneInnerAngle = 360; // inside this, volume is full
// Set the panner node to be at the origin looking in the +z direction.
panner.setPosition(0,0,0);
panner.setOrientation(0,0,0); // unit vector
// Connect source->panner->destination
panner.connect(audioContext.destination);
source.connect(panner);
sound.play();
}
//stats
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
stats.domElement.style.zIndex = 100;
document.body.appendChild( stats.domElement );
//resize
window.addEventListener( 'resize', onWindowResize, false );
//mousemove
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'mousedown', onDocumentMouseClick, false );
}
function onDocumentMouseMove( event ) {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}
function onDocumentMouseClick( event ) {
//raycater
// find intersections
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( scene.children );
if ( intersects.length > 0 ) {
INTERSECTED = intersects[ 0 ].object;
try {
INTERSECTED.material.color.r = Math.random();
INTERSECTED.material.color.g = Math.random();
INTERSECTED.material.color.b = Math.random();
}
catch(e) {}
} else {
// if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
INTERSECTED = null;
}
}
function initSky(){
// Add Sky Mesh
sky = new THREE.Sky();
scene.add( sky.mesh );
// Add Sun Helper
sunSphere = new THREE.Mesh( new THREE.SphereGeometry( 20000, 30, 30 ),
new THREE.MeshBasicMaterial({color: 0xffffff, wireframe: false }));
sunSphere.position.y = -700000;
sunSphere.visible = true;
scene.add( sunSphere );
/// GUI
// var effectController = {
// turbidity: 10,
// reileigh: 2,
// mieCoefficient: 0.005,
// mieDirectionalG: 0.8,
// luminance: 1,
// inclination: 0.49, // elevation / inclination
// azimuth: 0.25, // Facing front,
// sun: !true
// }
var effectController = {
turbidity: 1,
reileigh: 1,
mieCoefficient: 0.0333,
mieDirectionalG: 0.75,
luminance: 0.75,
inclination: 0.44, // elevation / inclination
azimuth: 0.13, // Facing front,
sun: !true,
shipSpeed: 150
}
var distance = 400000;
function guiChanged() {
var uniforms = sky.uniforms;
uniforms.turbidity.value = effectController.turbidity;
uniforms.reileigh.value = effectController.reileigh;
uniforms.luminance.value = effectController.luminance;
uniforms.mieCoefficient.value = effectController.mieCoefficient;
uniforms.mieDirectionalG.value = effectController.mieDirectionalG;
var theta = Math.PI * (effectController.inclination - 0.5);
var phi = 2 * Math.PI * (effectController.azimuth - 0.5);
sunSphere.position.x = distance * Math.cos(phi);
sunSphere.position.y = distance * Math.sin(phi) * Math.sin(theta);
sunSphere.position.z = distance * Math.sin(phi) * Math.cos(theta);
sunSphere.visible = effectController.sun;
sky.uniforms.sunPosition.value.copy(sunSphere.position);
//ship
translationSpeed = effectController.shipSpeed;
}
var gui = new dat.GUI();
gui.add( effectController, "turbidity", 1.0, 20.0, 0.1 ).onChange( guiChanged );
gui.add( effectController, "reileigh", 0.0, 4, 0.001 ).onChange( guiChanged );
gui.add( effectController, "mieCoefficient", 0.0, 0.1, 0.001 ).onChange( guiChanged );
gui.add( effectController, "mieDirectionalG", 0.0, 1, 0.001 ).onChange( guiChanged );
gui.add( effectController, "luminance", 0.0, 2).onChange( guiChanged );;
gui.add( effectController, "inclination", 0, 1, 0.0001).onChange( guiChanged );
gui.add( effectController, "azimuth", 0, 1, 0.0001).onChange( guiChanged );
gui.add( effectController, "sun").onChange( guiChanged );
var shipFolder = gui.addFolder('Ship');
shipFolder.add(effectController, 'shipSpeed', 150, 500, 25).name('Ship Speed').onChange(guiChanged);
guiChanged();
camera.lookAt(sunSphere.position)
}
function onKeyDown(evt) {
switch (evt.keyCode) {
case 65: // 'a'
rotateLeft = true;
break;
case 68: // 'd'
rotateRight = true;
break;
case 87: // 'w'
moveAhead = true;
break;
case 83: // 's'
moveBackwards = true;
break;
}
}
function onKeyUp(evt) {
switch (evt.keyCode) {
case 65: // 'a'
rotateLeft = false;
break;
case 68: // 'd'
rotateRight = false;
break;
case 87: // 'w'
moveAhead = false;
break;
case 83: // 's'
moveBackwards = false;
break;
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
var time = performance.now() * 0.001;
//moving
var dt = clock.getDelta();
var boat = scene.getObjectByName('boat');
if(boat) {
boat.rotation.z = Math.sin(time)/120;
//moving
var dYaw, dPos;
if (rotateLeft) {
dYaw = dt*rotationSpeed;
boat.rotation.y += dYaw;
}
if (rotateRight) {
dYaw = dt*rotationSpeed;
boat.rotation.y -= dYaw;
}
if (moveAhead) {
dPos = dt*translationSpeed;
boat.translateX(-dPos);
}
if (moveBackwards) {
dPos = dt*translationSpeed;
boat.translateX(dPos);
}
}
//web audio
var p = camera.position;
audioContext.listener.setPosition(p.x, p.y, p.z);
if(panner&&boat) {
var boatPosition = boat.position;
panner.setPosition(boatPosition.x,boatPosition.y,boatPosition.z);
}
water.material.uniforms.time.value += 1.0 / 60.0;
controls.update();
stats.update();
water.render();
renderer.render( scene, camera );
}
</script>
</body>
</html>