-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04-skybox.html
628 lines (526 loc) · 18.4 KB
/
04-skybox.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
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
<html lang="en">
<head>
<title>Skybox (Three.js). Example 04 04</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script src="three.js"></script>
<script src="Detector.js"></script>
<script src="stats.js"></script>
<script src="water-material.js"></script>
<script src="ParticleEngine.js"></script>
<script src="OrbitControls.js"></script>
<script src="THREEx.KeyboardState.js"></script>
<script src="THREEx.FullScreen.js"></script>
<script src="THREEx.WindowResize.js"></script>
<script type="text/javascript" src="three.js"></script>
<script type="text/javascript" src="OBJLoader.js"></script>
<script type="text/javascript" src="jquery-1.9.0.js"></script>
<script type="text/javascript" src="stats.js"></script>
<script type="text/javascript" src="dat.gui.js"></script>
<!-- Importing jQuery library -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<!-- Importing the FlightIndicators library -->
<script src="js/jquery.flightindicators.js"></script>
<!-- Flight Indicators library styles -->
<link rel="stylesheet" type="text/css" href="css/flightindicators.css" />
<link rel=stylesheet href="info.css"/>
<script src="info.js"></script>
<script src="https://dl.dropboxusercontent.com/u/3587259/Code/Threejs/CSS3DRenderer.js"></script>
</head>
<body>
<!-- The block where we want to place an indicator -->
<!--<span id="first_attitude"></span>-->
<div id="infoBox" title="Demo Information">
This three.js demo is part of a collection at
<a href="http://stemkoski.github.io/Three.js/">http://stemkoski.github.io/Three.js/</a>
</div>
<!-- ------------------------------------------------------------ -->
<div id="ThreeJS" style="position: absolute; left:0px; top:0px"></div>
YOLO:
<span style="position: fixed; bottom:0%; left:15%" id="airspeed"></span>
<span style="position: fixed; bottom:0%; left:36%" id="attitude"></span>
<span style="position: fixed; bottom:0%; left:57%" id="altimeter"></span>
<span style="position: fixed; bottom:0%; left:78%" id="heading"></span>
<script>
/*
Three.js "tutorials by example"
Author: Lee Stemkoski
Date: July 2013 (three.js v59dev)
*/
// MAIN
// standard global variables
var container, scene, camera, camera2, camera3, currentcamera, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
// custom global variables
var cube;
var geometryS = new THREE.Object3D();
var plane;
var speed;
var planeXRotation, planeYRotation, planeZRotation;
var planeXRotAdd, planeYRotAdd, planeZRotAdd;
var raycaster;
// GYRO
// First static example
var airspeed = $.flightIndicator('#airspeed', 'airspeed', {showBox: false});
var heading = $.flightIndicator('#heading', 'heading', {heading:150, showBox:true});
var attitude = $.flightIndicator('#attitude', 'attitude', {roll:50, pitch:-20, size:200, showBox : true});
var altimeter = $.flightIndicator('#altimeter', 'altimeter');
//var engine;
init();
geometryS.position.set(0, 0, 0);
//window.setTimeout(animate, 1000);
animate();
// FUNCTIONS
function init()
{
//console.log("MACIEJJJ");
// SCENE
scene = new THREE.Scene();
speed = 10;
planeRoll = 0;
planeXRotation = 0;
planeYRotation = 0;
planeZRotation = 0;
planeXRotAdd = 0;
planeYRotAdd = 0;
planeZRotAdd = 0;
// GYRO
attitude = $.flightIndicator('#attitude', 'attitude', {roll:50, pitch:-20, size:200, showBox : true});
heading = $.flightIndicator('#heading', 'heading', {heading:150, showBox:true});
variometer = $.flightIndicator('#variometer', 'variometer', {vario:-5, showBox:true});
airspeed = $.flightIndicator('#airspeed', 'airspeed', {showBox: false});
altimeter = $.flightIndicator('#altimeter', 'altimeter');
var settings = {
positionStyle : Type.CUBE,
positionBase : new THREE.Vector3( 0, 0, -10 ),
positionSpread : new THREE.Vector3( 2000, 2000, 2000 ),
velocityStyle : Type.CUBE,
velocityBase : new THREE.Vector3( 0, -50, 0 ),
velocitySpread : new THREE.Vector3( 5, 5, 0 ),
accelerationBase : new THREE.Vector3( 100, 0, 0 ),
particleTexture : THREE.ImageUtils.loadTexture( 'raindrop2flip.png' ),
sizeBase : 2.9,
sizeSpread : 2.9,
colorBase : new THREE.Vector3(0.66, 1.0, 0.7), // H,S,L
colorSpread : new THREE.Vector3(0.00, 0.0, 0.2),
opacityBase : 0.6,
particlesPerSecond : 50000,
particleDeathAge : 1.0,
emitterDeathAge : 60
};
//engine = new ParticleEngine();
//engine.setValues( settings );
//engine.initialize();
var img = new THREE.MeshBasicMaterial({ //CHANGED to MeshBasicMaterial
map:THREE.ImageUtils.loadTexture('cockpit.png'),
transparent: true
});
img.map.needsUpdate = true; //ADDED
// plane
plane = new THREE.Mesh(new THREE.PlaneGeometry(40, 20), img);
//scene.add(plane);
// CAMERA
var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
scene.add(camera);
camera.position.set(-400,550,700);
camera.lookAt(scene.position);
currentcamera = camera;
// RENDERER
if ( Detector.webgl )
renderer = new THREE.WebGLRenderer( {antialias:true} );
else
renderer = new THREE.CanvasRenderer();
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
container = document.getElementById( 'ThreeJS' );
container.appendChild( renderer.domElement );
// EVENTS
THREEx.WindowResize(renderer, camera);
THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
// CONTROLS
controls = new THREE.OrbitControls( camera, renderer.domElement );
// STATS
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.bottom = '0px';
stats.domElement.style.zIndex = 100;
container.appendChild( stats.domElement );
// LIGHT
var light = new THREE.PointLight(0xffffff);
light.position.set(100,350,100);
scene.add(light);
//POBIERANIE SAMOLOTU
var gui = new dat.GUI();
var mesh;
var loader = new THREE.OBJLoader();
//http://www.filedropper.com/b-747
//http://u.mobiltur.pl/pobierz.php?nazwa=B-747.obj
//'B-747.obj'
loader.load('B-747.obj', function (geometry) {
var material = new THREE.MeshLambertMaterial({color: 0x5C3A21});
// geometry is a group of children. If a child has one additional child it's probably a mesh
geometry.children.forEach(function (child) {
if (child.children.length == 1) {
if (child.children[0] instanceof THREE.Mesh) {
child.children[0].material = material;
}
}
});
mesh = geometry;
geometry.scale.set(2, 2, 2);
geometry.rotation.x = -0.0;
geometryS = geometry;
// Set right plane rotation (=start)
geometryS.rotation.x = 0;
geometryS.rotation.y = 0;
geometryS.rotation.z = 0;
// Set plane in the right place (=start)
geometryS.position.setX(0);
geometryS.position.setY(0);
geometryS.position.setZ(0);
scene.add(geometry);
//camera.position.set(0, 0, -100);
//geometryS.add( camera );
var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
camera2 = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
camera2.position.set(0, 0, -100);
geometryS.add( camera2 );
camera3 = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
geometryS.add(camera3);
camera3.position.set(0, 0, -100);
plane.position.set( 0, 0, -20);
});
console.log("==========================================");
// SKYBOX
var axes = new THREE.AxisHelper(100);
scene.add( axes );
var imagePrefix = "textures/dawnmountain-";
var directions = ["xpos", "xneg", "ypos", "yneg", "zpos", "zneg"];
var imageSuffix = ".png";
var skyGeometry = new THREE.CubeGeometry( 10000, 10000, 10000 );
var materialArray = [];
for (var i = 0; i < 6; i++)
materialArray.push( new THREE.MeshBasicMaterial({
map: THREE.ImageUtils.loadTexture( imagePrefix + directions[i] + imageSuffix ),
side: THREE.BackSide
}));
var skyMaterial = new THREE.MeshFaceMaterial( materialArray );
var skyBox = new THREE.Mesh( skyGeometry, skyMaterial );
//rain();
// WATER
directionalLight = new THREE.DirectionalLight(0xffffcc, 0.6);
directionalLight.position.set(-600, 400, 0);
//this.directionalLight.target.position.set(0, 0, 0);
directionalLight.castShadow = true;
directionalLight.shadowCameraNear = -100;
directionalLight.shadowCameraFar = 100;
directionalLight.shadowCameraLeft = -100;
directionalLight.shadowCameraRight = 100;
directionalLight.shadowCameraTop = 100;
directionalLight.shadowCameraBottom = -100;
//this.directionalLight.shadowCameraVisible = true;
directionalLight.shadowMapWidth = 2048;
directionalLight.shadowMapHeight = 2048;
directionalLight.shadowDarkness = 0.3;
scene.add(directionalLight);
//water();
scene.add( skyBox );
// RAYCASTING
raycaster = new THREE.Raycaster();
}
function water()
{
// Load textures
var waterNormals = new THREE.ImageUtils.loadTexture('waternormals.jpg');
waterNormals.wrapS = waterNormals.wrapT = THREE.RepeatWrapping;
// Create the water effect
ms_Water = new THREE.Water(renderer, currentcamera, scene, {
textureWidth: 512,
textureHeight: 512,
waterNormals: waterNormals,
alpha: 1.0,
sunDirection: new THREE.Vector3(0, 0, 0).copy(directionalLight.position.normalize()),
sunColor: 0xffffff,
waterColor: 0x001e0f,
distortionScale: 50.0
});
var aMeshMirror = new THREE.Mesh(
new THREE.PlaneBufferGeometry(2000 * 500, 2000 * 500, 10, 10),
ms_Water.material
);
aMeshMirror.add(ms_Water);
aMeshMirror.rotation.x = -Math.PI * 0.5;
scene.add(aMeshMirror);
}
// Depreciated
function rain()
{
var texture = THREE.ImageUtils.loadTexture("raindrop2flip.png");
var geom = new THREE.Geometry();
var material = new THREE.ParticleBasicMaterial({
size: 10,
transparent: true,
opacity: 0.6,
map: texture,
blending: THREE.AdditiveBlending,
sizeAttenuation: true,
color: 0xffffff
});
var range = 40;
for (var i = 0; i < 1500; i++) {
var particle = new THREE.Vector3(
Math.random() * range - range / 2,
Math.random() * range * 1.5,
Math.random() * range - range / 2);
particle.velocityY = 0.1 + Math.random() / 5;
particle.velocityX = (Math.random() - 0.5) / 3;
geom.vertices.push(particle);
}
cloud = new THREE.ParticleSystem(geom, material);
cloud.sortParticles = true;
scene.add(cloud);
}
function animate()
{
requestAnimationFrame( animate );
render();
var planePosition = geometryS.position;
//console.log(planePosition);
x_pos = Math.abs(planePosition.x);
y_pos = Math.abs(planePosition.y);
z_pos = Math.abs(planePosition.z);
if (x_pos < 5000 && y_pos < 5000 && z_pos < 5000) {
update();
} else {
//alert("You destroyed your airplane!");
restart();
}
}
function update()
{
//geometryS.rotation.z = 0;
delta = clock.getDelta();
var moveDistance = 100 * delta * speed;
walking = false;
if (Gamepad.supported)
{
var pads = Gamepad.getStates();
var pad = pads[0]; // assume only 1 player.
if (pad)
{
// adjust for deadzone.
if (Math.abs(pad.leftStickX + pad.rightStickX) > 0.3)
{
geometryS.rotation.y -= delta * (pad.leftStickX + pad.rightStickX);
walking = true;
}
if (Math.abs(pad.leftStickY + pad.rightStickY) > 0.2)
{
geometryS.translateZ( -moveDistance * (pad.leftStickY + pad.rightStickY) );
walking = true;
}
if ( pad.faceButton0 || pad.faceButton1 || pad.faceButton2 || pad.faceButton3 || pad.select || pad.start )
{
geometryS.position.set(0,0,0);
geometryS.rotation.set(0,0,0);
}
}
}
// move forwards / backwards
if ( keyboard.pressed("1") ){
camera3.remove(plane);
// var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
// var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
// camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
// camera.position.set(-400,550,700);
// camera.lookAt(scene.position);
// scene.add(camera2);
currentcamera = camera;
// EVENTS
THREEx.WindowResize(renderer, currentcamera);
THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
// CONTROLS
controls = new THREE.OrbitControls( currentcamera, renderer.domElement );
}
if ( keyboard.pressed("2") ){
camera3.remove(plane);
currentcamera = camera2;
// EVENTS
THREEx.WindowResize(renderer, currentcamera);
THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
// CONTROLS
controls = new THREE.OrbitControls( currentcamera, renderer.domElement );
}
if ( keyboard.pressed("3") ){
camera3.add(plane);
currentcamera = camera3;
// EVENTS
THREEx.WindowResize(renderer, currentcamera);
THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
// CONTROLS
controls = new THREE.OrbitControls( currentcamera, renderer.domElement );
}
if ( keyboard.pressed("down") ){
var rotationYB = geometryS.rotation.y;
var rotationZB = geometryS.rotation.z;
geometryS.rotateX(-0.01);
planeXRotation -= 0.0025;
planeXRotAdd -= 0.0025;
speed -= 0.1;
var rotationYA = geometryS.rotation.y;
var rotationZA = geometryS.rotation.z;
//planeYRotAdd += rotationYA - rotationYB;
//planeZRotAdd += rotationZA - rotationZB;
}
if ( keyboard.pressed("up") ){
var rotationYB = geometryS.rotation.y;
var rotationZB = geometryS.rotation.z;
geometryS.rotateX(0.01);
planeXRotation += 0.0025;
planeXRotAdd += 0.0025;
speed += 0.08;
var rotationYA = geometryS.rotation.y;
var rotationZA = geometryS.rotation.z;
//planeYRotAdd += rotationYA - rotationYB;
//planeZRotAdd += rotationZA - rotationZB;
}
// rotate left/right
if ( keyboard.pressed("left") ){
var rotationXB = geometryS.rotation.x;
planeYRotation += 0.0005;
planeYRotAdd += 0.0005;
planeZRotation -= 0.005;
planeZRotAdd -= 0.005;
geometryS.rotateZ(-0.01);
var rotationXA = geometryS.rotation.x;
//planeXRotAdd += rotationXA - rotationXB;
}
if ( keyboard.pressed("right") ){
var rotationXB = geometryS.rotation.x;
planeYRotation -= 0.0005;
planeYRotAdd -= 0.0005;
planeZRotation += 0.005;
planeZRotAdd += 0.005;
geometryS.rotateZ(0.01);
var rotationXA = geometryS.rotation.x;
//planeXRotAdd += rotationXA - rotationXB;
}
// turn left
if ( keyboard.pressed("a") ){
var rotationXB = geometryS.rotation.x;
var rotationZB = geometryS.rotation.z;
geometryS.rotateY(0.015);
var rotationXA = geometryS.rotation.x;
var rotationZA = geometryS.rotation.z;
planeYRotation += 0.0025;
planeYRotAdd += 0.0025;
//planeXRotAdd += rotationXA - rotationXB;
//planeZRotAdd += rotationZA - rotationZB;
}
// turn right
if ( keyboard.pressed("d") ){
var rotationXB = geometryS.rotation.x;
var rotationZB = geometryS.rotation.z;
geometryS.rotateY(-0.015);
var rotationXA = geometryS.rotation.x;
var rotationZA = geometryS.rotation.z;
planeYRotation -= 0.0025;
planeYRotAdd -= 0.0025;
//planeXRotAdd += rotationXA - rotationXB;
//planeZRotAdd += rotationZA - rotationZB;
}
// speed up
if ( keyboard.pressed("w") ){
speed = speed + 1;
}
// speed down
if ( keyboard.pressed("s") ){
speed = speed - 1;
}
// Airspeed update
airspeed.setAirSpeed(speed*5);
// Attitude update
attitude.setRoll(- planeZRotAdd * 62);
attitude.setPitch(- planeXRotAdd * 72);
// Altimeter update
var planePosition = geometryS.position;
x_pos = Math.abs(planePosition.x);
y_pos = Math.abs(planePosition.y);
z_pos = Math.abs(planePosition.z);
altimeter.setAltitude(planePosition.y + 5000);
altimeter.setPressure(1013 - 0.00115 * y_pos);
// Heading update
heading.setHeading((planeYRotAdd * 85 + 180));
// Rotation - shadow
planeXRotation /= 1.1;
planeXRotAdd += planeXRotation;
planeYRotation /= 1.1;
planeYRotAdd += planeYRotation;
planeZRotation /= 1.1;
planeZRotAdd += planeZRotation;
geometryS.rotateX(planeXRotation);
geometryS.rotateY(planeYRotation);
geometryS.rotateZ(planeZRotation);
// Move aircraft.
transalteSZ = moveDistance/10;
geometryS.translateZ( transalteSZ );
//console.log(geometryS.rotation.z);
controls.update();
stats.update();
// UPDATE PANELS
// Update at 20Hz
// var increment = 0;
// setInterval(function() {
// // Airspeed update
// airspeed.setAirSpeed(80+80*Math.sin(increment/10));
// Heading update
// heading.setHeading(increment);
// }, 50);
}
function render()
{
renderer.render( scene, currentcamera );
//var number = document.createElement( 'span' );
//number.id = "first_attitude";
//number = first_attitude;
//var object = new THREE.CSS3DObject( number );
//console.log(number);
//scene.add(object);
//var CSSrenderer = new THREE.CSS3DRenderer();
//CSSrenderer.setSize(window.innerWidth, window.innerHeight);
//document.body.appendChild(CSSrenderer.domElement);
//document.getElementById('myDiv').innerHTML = '<span id="first_attitude"></span>';
//console.log(document.getElementById('myDiv'));
//var span = document.getElementById("first_attitude");
//scene.add(span);
//console.log(span);
//engine.update( 0.01 * 0.5 );
}
function restart() {
var x;
if (confirm("You destroyed your airplane! \n Would you like to restart the game?") == true) {
controls.update();
speed = 10;
planeXRotation = 0;
planeYRotation = 0;
planeZRotation = 0;
keyboard = new THREEx.KeyboardState();
// Set right plane rotation (=start)
geometryS.rotation.x = 0;
geometryS.rotation.y = 0;
geometryS.rotation.z = 0;
// Set plane in the right place (=start)
geometryS.position.setX(0);
geometryS.position.setY(0);
geometryS.position.setZ(0);
} else {
alert("Thank you!");
}
//document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>