-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
473 lines (383 loc) · 18.7 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Threejs Game</title>
<link href="src/css/main.css" media="all" rel="stylesheet" type="text/css">
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="src/js/three.min.js"></script>
<script src="src/js/cannon.min.js"></script>
<script src="src/js/threejs/loaders/MTLLoader.js"></script>
<script src="src/js/threejs/loaders/OBJLoader.js"></script>
<script src="src/js/threejs/loaders/OBJMTLLoader.js"></script>
<script src="src/js/threejs/controls/CannonPointerLockControls.js"></script>
<script src="src/js/threejs/libs/stats.min.js"></script>
<div id="blocker">
<div id="instructions">
<span style="font-size:40px">Click to play</span>
<br />
(Z, Q, S, D = Move, SPACE = Jump, MOUSE = Look around)
</div>
</div>
<script>
var DISTANCE = 6;
var camera, scene, renderer, stats, controls;
var geometry, material, mesh;
var time = Date.now();
var tank;
var sphereShape, sphereBody, world, physicsMaterial, walls=[], balls=[], ballMeshes=[], boxes=[], boxMeshes=[];
var objects = [];
var rays = [
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(1, 0, 1),
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(1, 0, -1),
new THREE.Vector3(0, 0, -1),
new THREE.Vector3(-1, 0, -1),
new THREE.Vector3(-1, 0, 0),
new THREE.Vector3(-1, 0, 1)
];
var blocker = document.getElementById( 'blocker' );
var instructions = document.getElementById( 'instructions' );
pointerLockInit();
initCannon();
init();
animate();
function initCannon()
{
// Setup our world
world = new CANNON.World();
world.quatNormalizeSkip = 0;
world.quatNormalizeFast = false;
var solver = new CANNON.GSSolver();
world.defaultContactMaterial.contactEquationStiffness = 1e9;
world.defaultContactMaterial.contactEquationRegularizationTime = 4;
solver.iterations = 7;
solver.tolerance = 0.1;
var split = true;
if(split)
world.solver = new CANNON.SplitSolver(solver);
else
world.solver = solver;
world.gravity.set( 0, -9.78, 0);
world.broadphase = new CANNON.NaiveBroadphase();
// Create a slippery material (friction coefficient = 0.0)
physicsMaterial = new CANNON.Material("slipperyMaterial");
var physicsContactMaterial = new CANNON.ContactMaterial(physicsMaterial,
physicsMaterial,
0.0, // friction coefficient
0.3 // restitution
);
// We must add the contact materials to the world
world.addContactMaterial(physicsContactMaterial);
// Create a sphere
var mass = 150, radius = 1.3;
sphereShape = new CANNON.Sphere(radius);
sphereBody = new CANNON.RigidBody(mass,sphereShape,physicsMaterial);
sphereBody.position.set( 0, 5, 0 );
sphereBody.linearDamping = 0.9;
world.add(sphereBody);
// Create a plane
var groundShape = new CANNON.Plane();
var groundBody = new CANNON.RigidBody(0,groundShape,physicsMaterial);
groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1,0,0),-Math.PI/2);
world.add(groundBody);
}
function init() {
////////////////////////////////////////////////////////////////////////////////////////////////
// camera
////////////////////////////////////////////////////////////////////////////////////////////////
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
////////////////////////////////////////////////////////////////////////////////////////////////
// scene
////////////////////////////////////////////////////////////////////////////////////////////////
scene = new THREE.Scene();
//scene.fog = new THREE.Fog( 0xffffff, 0 );
////////////////////////////////////////////////////////////////////////////////////////////////
// lights
////////////////////////////////////////////////////////////////////////////////////////////////
var light = new THREE.DirectionalLight( 0xffffff , 1.5 );
light.position.set( 1000, 1000, 0 ); //200 200 0
light.target.position.set( -100, 0, -200 ); // -100 0 -200
light.castShadow = true;
light.shadowDarkness = 0.5;
light.shadowCameraNear = 0.01;
light.shadowCameraVisible = true;
scene.add( light );
////////////////////////////////////////////////////////////////////////////////////////////////
// controls
////////////////////////////////////////////////////////////////////////////////////////////////
controls = new THREE.CannonPointerLockControls( camera, sphereBody );
scene.add( controls.getObject() );
////////////////////////////////////////////////////////////////////////////////////////////////
// floor
////////////////////////////////////////////////////////////////////////////////////////////////
createFloor();
function createFloor()
{
var geometry = new THREE.PlaneGeometry( 500, 500, 20, 20 );
geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
for ( var i = 0, l = geometry.vertices.length; i < l; i ++ ) {
var vertex = geometry.vertices[ i ];
vertex.x += Math.random() * 20 - 10;
//vertex.y += Math.random() * 2;
vertex.z += Math.random() * 20 - 10;
}
var texture = THREE.ImageUtils.loadTexture('src/textures/terrain/grasslight-big.jpg');
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set( 20, 20 );
var texture_nm = THREE.ImageUtils.loadTexture('src/textures/terrain/grasslight-big-nm.jpg');
texture_nm.wrapS = texture_nm.wrapT = THREE.RepeatWrapping;
texture_nm.repeat.set( 20, 20 );
var material = new THREE.MeshPhongMaterial({
shininess: 2,
specular: 0x888888,
shading: THREE.SmoothShading,
map: texture,
normalMap: texture_nm
});
var mesh = new THREE.Mesh( geometry, material );
mesh.receiveShadow = true;
mesh.castShadow = false;
mesh.flipSided = false;
mesh.position.set( 0, 0, 0 );
scene.add( mesh );
}
////////////////////////////////////////////////////////////////////////////////////////////////
// objects
////////////////////////////////////////////////////////////////////////////////////////////////
addJsonOBJ( 'src/models/building2/building2.js', -10, 0, -20, 10, 0);
addJsonOBJ( 'src/models/building9/building9.js', 8, 0, -14, 10, 190);
addJsonOBJ( 'src/models/vehicules/tank/t55/t55.js', 20, 0, 20, 0.02, 100);
function addJsonOBJ( path, x, y, z, scale, angle)
{
var loader = new THREE.JSONLoader();
loader.load( path, function ( geometry, materials ) {
mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );
mesh.position.set( x, y, z );
mesh.rotation.y = - Math.PI * angle / 180;
mesh.scale.set( scale, scale, scale );
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add( mesh );
objects.push( mesh );
});
}
function addOBJMTL( pathOBJ, pathMTL, x, y, z, scale, angle )
{
var loader = new THREE.OBJMTLLoader();
loader.load( pathOBJ, pathMTL, function ( object ) {
object.position.set( x, y, z );
object.rotation.y = - Math.PI * angle / 180;
object.scale.set( scale, scale, scale );
object.castShadow = true;
object.receiveShadow = true;
scene.add( object );
objects.push( object );
});
}
function addBox()
{
var x = ( Math.random() - 0.5 ) * 20;
var y = 1 + ( Math.random() - 0.5 ) * 1;
var z = ( Math.random() - 0.5 ) * 20;
var boxBody = new CANNON.RigidBody( 5, boxShape );
var boxMesh = new THREE.Mesh( boxGeometry, material );
world.add( boxBody );
scene.add( boxMesh );
boxBody.position.set( x, y, z );
boxMesh.position.set( x, y, z );
boxMesh.castShadow = true;
boxMesh.receiveShadow = true;
boxes.push( boxBody );
boxMeshes.push( boxMesh );
}
// Add boxes
var halfExtents = new CANNON.Vec3( 1, 1, 1);
var boxShape = new CANNON.Box( halfExtents );
var boxGeometry = new THREE.BoxGeometry( halfExtents.x * 2, halfExtents.y * 2, halfExtents.z * 2 );
for ( var i = 0; i < 7; i++ ) {
addBox();
}
// Add linked boxes
var size = 0.5;
var he = new CANNON.Vec3(size,size,size*0.1);
var boxShape = new CANNON.Box(he);
var mass = 0;
var space = 0.1*size;
var N=5, last;
var boxGeometry = new THREE.BoxGeometry(he.x*2,he.y*2,he.z*2);
for(var i=0; i<N; i++){
var boxbody = new CANNON.RigidBody(mass,boxShape);
var boxMesh = new THREE.Mesh( boxGeometry, material );
boxbody.position.set(5,(N-i)*(size*2+2*space) + size*2+space,0);
boxbody.linearDamping=0.01;
boxbody.angularDamping=0.01;
boxMesh.castShadow = true;
boxMesh.receiveShadow = true;
world.add(boxbody);
scene.add(boxMesh);
boxes.push(boxbody);
boxMeshes.push(boxMesh);
if(i!=0){
// Connect this body to the last one
var c1 = new CANNON.PointToPointConstraint(boxbody,new CANNON.Vec3(-size,size+space,0),last,new CANNON.Vec3(-size,-size-space,0));
var c2 = new CANNON.PointToPointConstraint(boxbody,new CANNON.Vec3(size,size+space,0),last,new CANNON.Vec3(size,-size-space,0));
world.addConstraint(c1);
world.addConstraint(c2);
} else {
mass=0.3;
}
last = boxbody;
}
////////////////////////////////////////////////////////////////////////////////////////////////
// stats
////////////////////////////////////////////////////////////////////////////////////////////////
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.bottom = '0px';
document.body.appendChild(stats.domElement);
////////////////////////////////////////////////////////////////////////////////////////////////
// renderer
////////////////////////////////////////////////////////////////////////////////////////////////
renderer = new THREE.WebGLRenderer({ antialias: true });
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;
renderer.setClearColor( 0xffffff );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate()
{
requestAnimationFrame( animate );
if ( controls.enabled ) {
world.step( 1/60 );
// Update ball positions
for ( var i = 0; i < balls.length; i++) {
balls[i].position.copy(ballMeshes[i].position);
balls[i].quaternion.copy(ballMeshes[i].quaternion);
}
// Update box positions
for ( var i = 0; i < boxes.length; i++ ) {
boxes[i].position.copy(boxMeshes[i].position);
boxes[i].quaternion.copy(boxMeshes[i].quaternion);
}
}
controls.update( Date.now() - time );
renderer.render( scene, camera );
time = Date.now();
stats.update();
}
////////////////////////////////////////////////////////////////////////////////////////////////
// shoot
////////////////////////////////////////////////////////////////////////////////////////////////
var ballShape = new CANNON.Sphere( 0.2 );
var ballGeometry = new THREE.SphereGeometry( ballShape.radius );
var shootDirection = new THREE.Vector3();
var shootVelo = 20;
var projector = new THREE.Projector();
function getShootDir ( targetVec )
{
var vector = targetVec;
targetVec.set( 0, 0, 1 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Ray(sphereBody.position, vector.sub( sphereBody.position ).normalize() );
targetVec.x = ray.direction.x;
targetVec.y = ray.direction.y;
targetVec.z = ray.direction.z;
}
window.addEventListener("click",function(e){
if ( controls.enabled == true ) {
var x = sphereBody.position.x;
var y = sphereBody.position.y;
var z = sphereBody.position.z;
var ballBody = new CANNON.RigidBody( 1, ballShape );
var ballMesh = new THREE.Mesh( ballGeometry, material );
world.add( ballBody );
scene.add( ballMesh );
ballMesh.castShadow = true;
ballMesh.receiveShadow = true;
balls.push( ballBody );
ballMeshes.push( ballMesh );
getShootDir( shootDirection );
ballBody.velocity.set( shootDirection.x * shootVelo,
shootDirection.y * shootVelo,
shootDirection.z * shootVelo );
// Move the ball outside the player sphere
x += shootDirection.x * ( sphereShape.radius*1.02 + ballShape.radius );
y += shootDirection.y * ( sphereShape.radius*1.02 + ballShape.radius );
z += shootDirection.z * ( sphereShape.radius*1.02 + ballShape.radius );
ballBody.position.set( x, y, z );
ballMesh.position.set( x, y, z );
}
});
function pointerLockInit()
{
// http://www.html5rocks.com/en/tutorials/pointerlock/intro/
var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
if ( havePointerLock ) {
var element = document.body;
var pointerlockchange = function ( event ) {
if ( document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element ) {
controls.enabled = true;
blocker.style.display = 'none';
} else {
controls.enabled = false;
blocker.style.display = '-webkit-box';
blocker.style.display = '-moz-box';
blocker.style.display = 'box';
instructions.style.display = '';
}
}
var pointerlockerror = function ( event ) {
instructions.style.display = '';
}
// Hook pointer lock state change events
document.addEventListener( 'pointerlockchange', pointerlockchange, false );
document.addEventListener( 'mozpointerlockchange', pointerlockchange, false );
document.addEventListener( 'webkitpointerlockchange', pointerlockchange, false );
document.addEventListener( 'pointerlockerror', pointerlockerror, false );
document.addEventListener( 'mozpointerlockerror', pointerlockerror, false );
document.addEventListener( 'webkitpointerlockerror', pointerlockerror, false );
instructions.addEventListener( 'click', function ( event ) {
instructions.style.display = 'none';
// Ask the browser to lock the pointer
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
if ( /Firefox/i.test( navigator.userAgent ) ) {
var fullscreenchange = function ( event ) {
if ( document.fullscreenElement === element || document.mozFullscreenElement === element || document.mozFullScreenElement === element ) {
document.removeEventListener( 'fullscreenchange', fullscreenchange );
document.removeEventListener( 'mozfullscreenchange', fullscreenchange );
element.requestPointerLock();
}
}
document.addEventListener( 'fullscreenchange', fullscreenchange, false );
document.addEventListener( 'mozfullscreenchange', fullscreenchange, false );
element.requestFullscreen = element.requestFullscreen || element.mozRequestFullscreen || element.mozRequestFullScreen || element.webkitRequestFullscreen;
element.requestFullscreen();
} else {
element.requestPointerLock();
}
}, false );
} else {
instructions.innerHTML = 'Your browser doesn\'t seem to support Pointer Lock API';
}
}
</script>
</body>
</html>