/
3JSFPS.html
831 lines (719 loc) · 30.1 KB
/
3JSFPS.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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
<!doctype html>
<html lang="en">
<head>
<title>3JS FPS</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel=stylesheet href="css/base.css"/>
</head>
<body>
<script src="js/Three.js"></script>
<script src="js/Detector.js"></script>
<script src="js/Stats.js"></script>
<script src="js/THREEx.KeyboardState.js"></script>
<script src="js/THREEx.FullScreen.js"></script>
<script src="js/THREEx.WindowResize.js"></script>
<!-- Simplfied gamepad interaction (patched version) -->
<script src="js/gamepad_uncompressed.js"></script>
<!-- ---------------- Custom Shader Code ------------------------ -->
<script id="vertexShader" type="x-shader/x-vertex">
varying vec2 vUv;
void main()
{
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
</script>
<!-- fragment shader a.k.a. pixel shader -->
<script id="fragmentShader" type="x-shader/x-vertex">
uniform sampler2D baseTexture;
uniform float baseSpeed;
uniform sampler2D noiseTexture;
uniform float noiseScale;
uniform float alpha;
uniform float time;
varying vec2 vUv;
void main()
{
vec2 uvTimeShift = vUv + vec2( -0.7, 1.5 ) * time * baseSpeed;
vec4 noiseGeneratorTimeShift = texture2D( noiseTexture, uvTimeShift );
vec2 uvNoiseTimeShift = vUv + noiseScale * vec2( noiseGeneratorTimeShift.r, noiseGeneratorTimeShift.b );
vec4 baseColor = texture2D( baseTexture, uvNoiseTimeShift );
baseColor.a = alpha;
gl_FragColor = baseColor;
}
</script>
<!-- ----------------------------------------------------------- -->
<audio loop="loop" autoplay="autoplay">
<source src="audio/deep-solar-wind.ogg" />
<source src="audio/deep-solar-wind.mp3" />
</audio>
<div id="ThreeJS" style="position: absolute; left:0px; top:0px"></div>
<script>
/*
Three.js "tutorials by example"
Author: Lee Stemkoski
Date: August 2013 (three.js v59)
Entry for #7dFPS 2013
Websites used for textures:
http://www.textures123.com/free/game-texture.html
http://www.tutorialsforblender3d.com/Textures/Textures_index.html
http://seamless-pixels.blogspot.com/
*/
// MAIN
// standard global variables
var container, scene, camera, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
// custom global variables
var person;
var gravity = new THREE.Vector3(0,-10,0);
var walls = [];
init();
animate();
// FUNCTIONS
function init()
{
// SCENE
scene = new THREE.Scene();
// CAMERA
var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 1, FAR = 20000;
camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
scene.add(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) });
// 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.DirectionalLight(0xffffff);
light.position.set(0,250,0);
scene.add(light);
// SKYBOX/FOG
var imagePrefix = "images/autumn-";
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 );
scene.add( skyBox );
////////////
// CUSTOM //
////////////
// person should have all sorts of things attached.
// I like to switch between 1st person / 3rd person view (w/ visible mesh) for debugging collision detection
person = new THREE.Object3D();
person.add(camera);
camera.position.set(0,35,10); // first-person view
person.position.set(-600,100,500);
person.rotation.y = -Math.PI / 2.0;
boundingG = new THREE.CubeGeometry(40,80,40);
// radiusAtTop, radiusAtBottom, height, segmentsAroundRadius, segmentsAlongHeight,
// boundingG = new THREE.CylinderGeometry(20,20,80,8,2);
// better collision but FPS drops too much
boundingG.computeBoundingSphere();
boundingM = new THREE.MeshBasicMaterial( {color:0xff0000, transparent:true, wireframe:true} );
bounding = new THREE.Mesh( boundingG, boundingM );
bounding.visible = false;
person.add(bounding);
person.velocity = new THREE.Vector3(0,0,0);
scene.add(person);
var coordinator = function(z,x,y)
{
return new THREE.Vector3(50*x, 50*y, 50*z);
}
this.cubeG = [];
cubeG[1] = new THREE.CubeGeometry( 50, 50, 50 );
cubeG[2] = new THREE.CubeGeometry( 50, 50, 50 );
cubeG[3] = new THREE.CubeGeometry( 50, 50, 50 );
// glass
cubeG[4] = new THREE.PlaneGeometry( 50, 50 ); // front/back walls
cubeG[5] = new THREE.PlaneGeometry( 50, 50 ); // left/right walls
cubeG[6] = new THREE.PlaneGeometry( 50, 50 ); // ceiling/floor
// force field
cubeG[7] = new THREE.PlaneGeometry( 50, 50 ); // front/back walls
cubeG[8] = new THREE.PlaneGeometry( 50, 50 ); // left/right walls
cubeG[9] = new THREE.PlaneGeometry( 50, 50 ); // ceiling/floor
var cubeM = [];
cubeM[1] = new THREE.MeshBasicMaterial({ map: new THREE.ImageUtils.loadTexture("images/alien-alloy.png") });
// stair material; want sides different from top for greater visibility
var m1 = new THREE.MeshBasicMaterial({ map: new THREE.ImageUtils.loadTexture("images/alien-carving.png") });
var m2 = new THREE.MeshBasicMaterial({ map: new THREE.ImageUtils.loadTexture("images/alien-carving-gray.png") });
var mArray = [m2,m2,m1,m1,m2,m2];
cubeM[2] = new THREE.MeshFaceMaterial( mArray )
cubeM[3] = new THREE.MeshBasicMaterial({ map: new THREE.ImageUtils.loadTexture("images/x-crate.png") });
cubeM[4] = new THREE.MeshBasicMaterial({ map: new THREE.ImageUtils.loadTexture("images/blue-glowy.jpg"), transparent:true, opacity:0.5});
cubeM[5] = new THREE.MeshBasicMaterial({ map: new THREE.ImageUtils.loadTexture("images/blue-glowy.jpg"), transparent:true, opacity:0.5});
cubeM[6] = new THREE.MeshBasicMaterial({ map: new THREE.ImageUtils.loadTexture("images/blue-glowy.jpg"), transparent:true, opacity:0.5});
///////////////////////////////////////////////////////////////////////////////////////////////
var noiseTexture = new THREE.ImageUtils.loadTexture( 'images/cloud.png' );
noiseTexture.wrapS = noiseTexture.wrapT = THREE.RepeatWrapping;
var baseTexture = new THREE.ImageUtils.loadTexture( 'images/electric-blue-light.jpg' );
baseTexture.wrapS = baseTexture.wrapT = THREE.RepeatWrapping;
// use "this." to create global object
this.customUniforms = {
baseTexture: { type: "t", value: baseTexture },
baseSpeed: { type: "f", value: 0.15 },
noiseTexture: { type: "t", value: noiseTexture },
noiseScale: { type: "f", value: 0.5337 },
alpha: { type: "f", value: 1.0 },
time: { type: "f", value: 1.0 }
};
// create custom material from the shader code above
// that is within specially labeled script tags
var customMaterial = new THREE.ShaderMaterial(
{
uniforms: customUniforms,
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
transparent: true, blending: THREE.AdditiveBlending, side:THREE.DoubleSide, alphaTest: 0.5
} );
cubeM[7] = customMaterial;
cubeM[8] = customMaterial;
cubeM[9] = customMaterial;
/////////////////////////////////////////////////////////////////////////////////////////////
var cubeMap = [];
// cubeMap will be recycled to hold references to meshes
cubeMap[0] = [ [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,2,2,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,3,3,3,3,3,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,3,2,2,2,3,2,2,2,2,2,2,2,1],
[1,0,0,0,0,0,0,0,3,2,2,2,3,2,2,2,2,2,2,2,1],
[1,0,0,0,0,0,0,0,3,2,2,2,3,2,2,2,2,2,2,2,1],
[1,0,0,0,0,0,0,0,3,3,3,3,3,1,1,1,1,2,2,2,1],
[1,0,0,0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,1,9,9,9,9,9,9,1],
[1,9,9,9,9,9,9,1,0,0,0,0,0,1,9,9,9,9,9,9,1],
[1,9,9,9,9,9,9,1,0,0,0,0,0,1,9,9,9,9,9,9,1],
[1,9,9,9,9,9,9,1,0,0,0,0,0,1,9,9,9,9,9,9,1],
[1,9,9,9,9,9,9,1,1,1,1,1,1,1,9,9,9,9,9,9,1],
[1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,1],
[1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,1],
[1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,1],
[1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,1],
[1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,1],
[1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ];
// due to performance issues, merge all cubes with a given material into a single geometry
var mergedGeo = [];
for (var i = 0; i < 10; i++)
mergedGeo[i] = new THREE.Geometry();
for (var y = 0; y < cubeMap.length; y++)
for (var x = 0; x < cubeMap[0].length; x++)
for (var z = 0; z < cubeMap[0][0].length; z++)
{
var style = cubeMap[y][x][z];
if (style == 0) continue; // no cube/plane at this location
var cube = new THREE.Mesh( cubeG[style] );
cube.position.set(50*(cubeMap[0].length - x) , 50*y, 50*z);
if (style == 4 || style == 7)
cube.rotation.set(0,-Math.PI/2,0); // front/back
if (style == 6 || style == 9)
cube.rotation.set(-Math.PI/2,0,0); // floor/ceiling
THREE.GeometryUtils.merge( mergedGeo[style], cube );
}
for (var i = 1; i < 10; i++)
{
var mesh = new THREE.Mesh( mergedGeo[i], cubeM[i] );
scene.add(mesh);
walls.push(mesh);
}
var floorT = THREE.ImageUtils.loadTexture("images/rocky-ground.jpg");
floorT.wrapS = floorT.wrapT = THREE.RepeatWrapping;
floorT.repeat.set(10,10);
var floor = new THREE.Mesh( new THREE.CubeGeometry(5000,50,5000),
new THREE.MeshBasicMaterial({
map: floorT
})
);
floor.position.set(0,-50,0);
walls.push(floor);
scene.add(floor);
//// EXTRAS
///////////////////////////////////////////////////////////////////////////
// MODEL
var jsonLoader = new THREE.JSONLoader();
this.star = new THREE.Object3D();
star.position.set(0,100,0);
star.position = coordinator(10,11,2.5);
scene.add(star);
// triforce, bub, android, star, treasure-chest, mage, sword, flounder
jsonLoader.load( "models/star.js", function( geometry, materials )
{
var material = new THREE.MeshFaceMaterial( materials );
var starMesh = new THREE.Mesh( geometry, material );
starMesh.scale.set(16,16,16);
// star.position.set(0,100,0);
star.add( starMesh );
});
// addModelToScene function is called back after model has loaded
var ambientLight = new THREE.AmbientLight(0xaaaaaa);
scene.add(ambientLight);
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// CHEAP GLOW EFFECT
// colored sprite with alpha blending
/*
// use sprite b/c appears the same from all angles
var spriteMaterial = new THREE.SpriteMaterial(
{
map: new THREE.ImageUtils.loadTexture( 'images/glow.png' ),
useScreenCoordinates: false, alignment: THREE.SpriteAlignment.center,
color: 0x0000ff, transparent: true, blending: THREE.AdditiveBlending
});
var sprite = new THREE.Sprite( spriteMaterial );
sprite.scale.set(100, 100, 1.0);
star.add(sprite);
*/
/*
// apply the material to a surface.
// using a plane rather than a sprite b/c rotation desired
var glowPlane = new THREE.Mesh(
new THREE.PlaneGeometry( 80, 80 ),
new THREE.MeshBasicMaterial(
{
map: new THREE.ImageUtils.loadTexture( "images/glow.png" ),
side: THREE.DoubleSide,
color: 0xffff00, transparent: true, blending: THREE.AdditiveBlending
})
);
star.add( glowPlane );
*/
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// Mouse Look (Free Look) controls
this.mouseLook = { x:0, y:0 };
document.addEventListener( 'click', function ( event )
{
var havePointerLock = 'pointerLockElement' in document ||
'mozPointerLockElement' in document ||
'webkitPointerLockElement' in document;
if ( !havePointerLock ) return;
var element = document.body;
// Ask the browser to lock the pointer
element.requestPointerLock = element.requestPointerLock ||
element.mozRequestPointerLock ||
element.webkitRequestPointerLock;
// Ask the browser to lock the pointer
element.requestPointerLock();
// Hook pointer lock state change events
document.addEventListener( 'pointerlockchange', pointerLockChange, false);
document.addEventListener( 'mozpointerlockchange', pointerLockChange, false);
document.addEventListener('webkitpointerlockchange', pointerLockChange, false);
// Hook mouse move events
// document.addEventListener("mousemove", this.moveCallback, false);
}, false );
}
function moveCallback(e)
{
var movementX = e.movementX || e.mozMovementX || e.webkitMovementX || 0;
var movementY = e.movementY || e.mozMovementY || e.webkitMovementY || 0;
// store movement amounts; will be processed by update function.
mouseLook.x += movementX;
mouseLook.y += movementY;
}
function pointerLockChange(event)
{
var element = document.body;
if (document.pointerLockElement === element ||
document.mozPointerLockElement === element ||
document.webkitPointerLockElement === element)
{
// Pointer was just locked, enable the mousemove listener
document.addEventListener("mousemove", moveCallback, false);
}
else
{
// Pointer was just unlocked, disable the mousemove listener
document.removeEventListener("mousemove", moveCallback, false);
}
}
function projectXZ(v)
{ return new THREE.Vector3(v.x, 0, v.z); }
function animate()
{
requestAnimationFrame( animate );
render();
update();
}
function update()
{
stats.update();
var delta = clock.getDelta(); // seconds since last update
var moveDistance = 200 * delta; // 200 pixels per second // should be velocity?
var rotateAngle = Math.PI / 4 * delta; // pi/4 radians (45 degrees) per second
var cursorSpeed = 200 * delta;
if ( star != null )
star.rotateY( rotateAngle * 0.9 );
// for animated texture
customUniforms.time.value += delta;
if (keyboard.pressed("P"))
{
camera.position.set(0,35,10); // first-person view
person.position.set(50,100,50);
person.rotation.y = -Math.PI / 2.0;
person.velocity = new THREE.Vector3(0,0,0);
}
// movement controls
var move = { xDist: 0, yAngle: 0, zDist: 0 };
// check if browser supports gamepad and if a gamepad is plugged in.
if ( Gamepad.supported && Gamepad.getState(0) )
{
var pad = Gamepad.getState(0);
// forwards/backwards
if (Math.abs(pad.leftStickY) > 0.15)
move.zDist += moveDistance * pad.leftStickY;
// strafe left/right
if (Math.abs(pad.leftStickX) > 0.15)
move.xDist += moveDistance * pad.leftStickX;
// rotate left/right
if (Math.abs(pad.rightStickX) > 0.15)
move.yAngle -= rotateAngle * pad.rightStickX;
// camera look up/down
if (Math.abs(pad.rightStickY) > 0.15)
camera.rotateX( -rotateAngle * pad.rightStickY );
// press "A" to jump
if ( pad.faceButton0 && (person.velocity.y == 0) )
person.velocity = new THREE.Vector3(0,12,0);
}
// keyboard fallback
// forwards/backwards
if (keyboard.pressed("W"))
move.zDist -= moveDistance;
if (keyboard.pressed("S"))
move.zDist += moveDistance;
// turn left/right
if (keyboard.pressed("Q"))
move.yAngle += rotateAngle;
if (keyboard.pressed("E"))
move.yAngle -= rotateAngle;
// left/right (strafe)
if ( keyboard.pressed("A") )
move.xDist -= moveDistance;
if ( keyboard.pressed("D") )
move.xDist += moveDistance;
// process data from mouse look
// (if inactive, there will be no change)
move.yAngle -= rotateAngle * mouseLook.x * 0.1;
mouseLook.x = 0;
// up/down (debugging fly)
if ( keyboard.pressed("T") )
{
person.velocity = new THREE.Vector3(0,0,0);
person.translateY( moveDistance );
}
if ( keyboard.pressed("G") )
{
person.velocity = new THREE.Vector3(0,0,0);
person.translateY( -moveDistance );
}
person.translateZ( move.zDist );
person.rotateY( move.yAngle );
person.translateX( move.xDist );
person.updateMatrix();
// look up/down
if ( keyboard.pressed("3") ) // third-person view
camera.position.set(0,50,250);
if ( keyboard.pressed("1") ) // first-person view
camera.position.set(0,35,10);
if ( keyboard.pressed("R") )
camera.rotateX( rotateAngle );
if ( keyboard.pressed("F") )
camera.rotateX( -rotateAngle );
// process data from mouse look
// (if inactive, there will be no change)
camera.rotateX( -rotateAngle * mouseLook.y * 0.05 );
mouseLook.y = 0;
// limit camera to +/- 45 degrees (0.7071 radians) or +/- 60 degrees (1.04 radians)
camera.rotation.x = THREE.Math.clamp( camera.rotation.x, -1.04, 1.04 );
// pressing both buttons moves look angle to horizon
if ( keyboard.pressed("R") && keyboard.pressed("F") )
camera.rotateX( -6 * camera.rotation.x * rotateAngle );
// collision detection!
if ( collision( walls ) )
{
person.translateX( -move.xDist );
person.rotateY( -move.yAngle );
person.translateZ( -move.zDist );
person.updateMatrix();
if ( collision( walls ) )
console.log( "Something's wrong with collision..." );
}
// TODO: make sure there is no double-jump glitch
// (e.g. hold down space sometimes results in double-jump)
if ( keyboard.pressed("space") && (person.velocity.y == 0) )
person.velocity = new THREE.Vector3(0,12,0);
person.velocity.add( gravity.clone().multiplyScalar( delta ) );
person.translateY( person.velocity.y );
person.updateMatrix();
if ( collision(walls) )
{
person.translateY( -person.velocity.y );
person.updateMatrix();
person.velocity = new THREE.Vector3(0,0,0);
}
} // end of function update()
// returns true on intersection
function collision( wallArray )
{
/*
// coarse collision detection, create a list of candidates to check thoroughly
var candidates = [];
for (var i = 0; i < walls.length; i++)
{
if ( person.position.distanceTo(wallArray[i].position) <
(person.children[1].geometry.boundingSphere.radius + wallArray[i].geometry.boundingSphere.radius) )
candidates.push( wallArray[i] );
}
*/
// send rays from center of person to each vertex in bounding geometry
for (var vertexIndex = 0; vertexIndex < person.children[1].geometry.vertices.length; vertexIndex++)
{
var localVertex = person.children[1].geometry.vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4( person.matrix );
var directionVector = globalVertex.sub( person.position );
var ray = new THREE.Raycaster( person.position, directionVector.clone().normalize() );
var collisionResults = ray.intersectObjects( wallArray );
if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() )
return true;
}
return false;
}
function render()
{
renderer.render( scene, camera );
}
</script>
</body>
</html>