-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
455 lines (367 loc) · 13.1 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Conquer the Cosmos</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
font-family:Monospace;
font-size:13px;
text-align:center;
font-weight: bold;
text-align:center;
}
a {
color:#0078ff;
}
#info {
color: #fff;
position: absolute;
top: 0px; width: 100%;
padding: 5px;
z-index: 100;
}
</style>
</head>
<body>
<div id="info">
Conquer the Cosmos
</div>
<!-- Three Script -->
<script src="js/lib/three.js"></script>
<!-- Physic Engine Script - Cannon.js-->
<script src="js/lib/cannon.min.js"></script>
<!-- Leap Script -->
<script src="js/FlyControls.js"></script>
<!--Libraries -->
<!--<script src="js/lib/three.min.new.js"></script>-->
<script src="js/lib/Detector.js"></script>
<script src="js/lib/stats.min.js"></script>
<!--Tween animation Lib -->
<script src="js/lib/tween.min.js"></script>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var container, stats;
//cannon
var world, mass, body, shape, timeStep=1/60;
//three
var camera, scene, renderer, avGeometry, materials = [];
var controls;
var mouseX = 0, mouseY = 0;
//avatar vars
var sphere, uniforms, attributes;
var avRadius;
var camPosition;
var avatarPosition;
var avatarContainer;
var avatarBody;
var avRadius = 2;
var AvatarMassDensity;
//Scene objects
var clock = new THREE.Clock();
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
//lights
var hemiLight;
//collider function stop imediate repeat
var lastCollide = 0;
//camera Tweening
var tweenCam, tweenCam1, tweenCam2, tweenCamToOrig;
//Scene 1
//Adding random planet vars
var spawnDistanceLimit = 100;
var spawnSpeedLimit = 1;
var spawnMassLimit = 100;
var bodiesCount = 40;
//cannon planet vars
var planets = [];
var planetsShape = [];
//three planet vars
var planetsMesh = [];
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
//THREE Scene
camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 50;
////////////////call Tween setUp Function
tweenSetUp();
scene = new THREE.Scene();
//scene.fog = new THREE.FogExp2( 0x000000, 0.0007 );
//lights
hemiLight = new THREE.HemisphereLight( 0xffffff, 0xffffff, 0.6 );
hemiLight.color.setHSL( 0.6, 1, 0.6 );
hemiLight.groundColor.setHSL( 0.095, 1, 0.75 );
hemiLight.position.set( 0, 500, 0 );
scene.add( hemiLight );
//Cannon world
//creating the physics world
world = new CANNON.World();
world.gravity.set(0,0,-10);
world.broadphase = new CANNON.NaiveBroadphase();
world.solver.iterations = 10;
avatarColliderMesh(avRadius);
var avSegments = 128, avRings = 64;
//THREE Avatar
var avGeometry = new THREE.SphereGeometry( avRadius, avSegments, avRings );
avGeometry.dynamic = true;
avatarContainer = new THREE.Object3D();
avatarContainer.position.set(0,-5,0);
////test material to judge collider size
avMaterialTest = new THREE.MeshNormalMaterial( { shading: THREE.SmoothShading } );
avatar = new THREE.Mesh( avGeometry, avMaterialTest );
//shader for loop
//colour
//placing in the scene
scene.add(avatarContainer);
avatarContainer.add( avatar );
avatar.position.set(0,-5,0);
avatar.rotation.y += 55;
avatarContainer.add(camera);
camera.position.set( 0, 0, 50 );
camera.lookAt(avatarContainer.position);
avatarPosition = new THREE.Vector3();
avatarContainer.updateMatrixWorld();
avPosition = avatarContainer.position;
//Controllers
controls = new THREE.FlyControls( avatarContainer );
controls.movementSpeed = 100;
controls.domElement = container;
controls.rollSpeed = Math.PI / 24;
controls.autoForward = false;
controls.dragToLook = false;
//renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
//set renderer to the container
container.appendChild( renderer.domElement );
//containers for game areas ("scenes")
containerObjects();
//cannon physics materials
var avatar_planet_material = new CANNON.ContactMaterial(avatarPhysMaterial, planetPhysMaterial, 2.0, 40);
//stats for FPS
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
//window resize
window.addEventListener( 'resize', onWindowResize, false );
}
function containerObjects() {
//starting area
planetsContainerArea1 = new THREE.Object3D();
scene.add(planetsContainerArea1);
planetsContainerArea1.position.set(0,0,0);
for (var i = 0; i < bodiesCount; i++) {
createRandomPlanet();
}
}
function colliderFunctions() {
var p = planets.length;
avatarBody.addEventListener("collide", function(e) {
var objHit = planets.indexOf(e.with);
var cMass = avRadius * avatar.scale.x;
var newMass;
if( e.with == planets[planets.indexOf(e.with)]){
var scaleAmount = e.with.mass / (avatar.scale.x * 20);
if( e.with.mass < avatarBody.mass){
//console.log( e.with.mass +'hit'+ cMass);
avatar.scale.x += scaleAmount;
avatar.scale.y += scaleAmount;
avatar.scale.z += scaleAmount;
newMass = avRadius * avatar.scale.x;
world.remove(e.with);
planetsContainerArea1.remove(planetsMesh[objHit]);
world.remove(avatarBody);
avatarColliderMesh(newMass);
} else if( e.with.mass > avatarBody.mass) {
if (Date.now() - lastCollide > 1000){
lastCollide = Date.now();
console.log( e.with.mass +"hit by"+ cMass);
avatar.scale.x -= scaleAmount/4;
avatar.scale.y -= scaleAmount/4;
avatar.scale.z -= scaleAmount/4;
newMass = avRadius * avatar.scale.x;
world.remove(avatarBody);
avatarColliderMesh(newMass);
}
}
//made if mass value to 1, so any collision will trigger the tween
if (camera.position.z !== 75 && newMass >= 1 && newMass <= 10 ){
//////////////////////Calling Tween on collision
tweenCam1.start();
console.log("camera newPos1");
} else if (camera.position.z !== 125 && newMass >= 50 && newMass <= 60 ){
tweenCam2.start();
tweenAvatar.start();
} else if (newMass <= 31 && camera.position.z !== 50){
tweenCamToOrig.start();
}
}
});
}
function avatarColliderMesh(avMass) {
//cannon avatar
//creating avatar physics shape and body
avatarPhysMaterial = new CANNON.Material();
avatarShape = new CANNON.Sphere(avMass);
avatarBody = new CANNON.RigidBody(avMass,avatarShape,avatarPhysMaterial);
avatarBody.position.set(0, 0, 0);
world.add(avatarBody);
colliderFunctions();
}
function createRandomPlanet() {
var mass = Math.floor((Math.random() * spawnMassLimit) + 1);
var posX = Math.floor((Math.random() * spawnDistanceLimit) + 1 - (spawnDistanceLimit / 2));
var posY = Math.floor((Math.random() * spawnDistanceLimit) + 1 - (spawnDistanceLimit / 2));
var posZ = Math.floor((Math.random() * spawnDistanceLimit) + 1 - (spawnDistanceLimit / 2));
var velX = (Math.random() * spawnSpeedLimit) - (spawnSpeedLimit / 2 );
var velY = (Math.random() * spawnSpeedLimit) - (spawnSpeedLimit / 2 );
var velZ = (Math.random() * spawnSpeedLimit) - (spawnSpeedLimit / 2 );
var i = createPlanet(mass, posX, posY, posZ, velX, velY, velZ);
return i;
}
function createPlanet(mass, posX, posY, posZ, velX, velY, velZ) {
planetsShape.push();
planets.push();
planetsMesh.push();
var i = planets.length;
var thisMass = mass /(10 * Math.PI);
//added THREE objects
planetGeometry = new THREE.SphereGeometry( thisMass ,20,10);
planetMaterial = new THREE.MeshNormalMaterial( /*{ shading: THREE.SmoothShading }*/ );
planetsMesh[i] = new THREE.Mesh(planetGeometry, planetMaterial);
planetsMesh[i].position.set(posX, posY, posZ-300);
//added CANNOn objects
planetPhysMaterial = new CANNON.Material();
planetsShape[i] = new CANNON.Sphere(thisMass );
//planetsShape[i].planetId = newGuid;
planets[i] = new CANNON.RigidBody(thisMass, planetsShape[i], planetPhysMaterial);
//planets[i].planetId = newGuid;
planets[i].lastCollidedWith = -1;
planets[i].position.set(posX, posY, posZ -300);
planets[i].velocity.set(velX, velY, velZ);
planets[i].linearDamping = 0.0;
planets[i].angularVelocity.set(Math.random()/2,Math.random(),Math.random()/2);
// We add the objects to the world to simulate them
world.add(planets[i]);
// And we add them to the scene to make them visible
planetsContainerArea1.add(planetsMesh[i]);
planets[i].preStep = function() {
planets[i].force.set(0,0,0.01);
//we need to juggle this and that as we are going through a loop...
that = this;
var thisSumForce = new CANNON.Vec3();
//console.log(that.position.x);
planets.forEach(function(thisPlanet) {
//for each(var thisPlanet in planets) {
if (that.planetId != thisPlanet.planetId) {
var thisPlanet_to_that = new CANNON.Vec3(thisPlanet.position.x - that.position.x, thisPlanet.position.y - that.position.y, thisPlanet.position.z - that.position.z);
var distance = that.position.distanceTo(thisPlanet.position);
//temp force.
var tempForce = new CANNON.Vec3();
// Now apply force on temp vector
thisPlanet_to_that.normalize();
thisPlanet_to_that.mult(thisPlanet.mass / Math.pow(distance, 2), tempForce);
thisSumForce.x += tempForce.x;
thisSumForce.y += tempForce.y;
thisSumForce.z += tempForce.z;
}
});
//now apply the resultant force to that.
that.force = thisSumForce;
};
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
/////////////Tween setup function
function tweenSetUp(){
//camera movement Tweening
var camPosition = { x : 0, y : 5, z : 50 };
var camTarget1 = { x : +0, y : +10, z : +75};
var camTarget2 = { x : +0, y : +100, z : +1000};
tweenCam1 = new TWEEN.Tween(camPosition)
.to(camTarget1, 2000)
.easing( TWEEN.Easing.Back.InOut )
.onUpdate(function(){
controls.freeze = true;
camera.position.x = camTarget1.x;
camera.position.y = camTarget1.y;
camera.position.z = camTarget1.z;
})
.onComplete(function(){
controls.freeze = false;
});
tweenCam2 = new TWEEN.Tween(camTarget1)
.to(camTarget2, 2000)
.easing( TWEEN.Easing.Back.InOut )
.onUpdate(function(){
//controls.freeze = true;
camera.position.x = camTarget2.x;
camera.position.y = camTarget2.y;
camera.position.z = camTarget2.z;
})
.onComplete(function(){
//controls.freeze = false;
});
var avatarPosOrig = {x : 0, y : -5, z : 0};
var newAvatarPos1 = {x:0, y:-10, z:0};
tweenAvatar = new TWEEN.Tween(avatarPosOrig)
.to(newAvatarPos1)
.easing(TWEEN.Easing.Back.InOut)
.onUpdate(function(){
avatar.position.x = newAvatarPos1.x;
avatar.position.y = newAvatarPos1.y;
avatar.position.z = newAvatarPos1.z;
});
tweenCamToOrig = new TWEEN.Tween(camTarget1)
.to(camPosition, 2000)
.easing( TWEEN.Easing.Back.InOut )
.onUpdate(function(){
//controls.freeze = true;
camera.position.x = camPosition.x;
camera.position.y = camPosition.y;
camera.position.z = camPosition.z;
})
.onComplete(function(){
//controls.freeze = false;
});
}
//Run each frame
function animate() {
requestAnimationFrame( animate );
controls.update(clock.getDelta() );
render();
stats.update();
}
function render() {
var time = Date.now() * 0.01;
//////////Tween update
TWEEN.update();
//updating the physic engine body positions = to three mesh position
//skybos position center = controller/avatar position
avatarPosition.setFromMatrixPosition(avatar.matrixWorld);
avatarBody.position.set(avatarPosition.x, avatarPosition.y, avatarPosition.z);
// Step the physics world
world.step(timeStep);
for (var i = 0, j = planets.length; i < j; i++){
planets[i].position.copy(planetsMesh[i].position);
//planets[i].quaternion.copy(planetsMesh[i].quaternion);
planetsMesh[i].quaternion.set(planets[i].quaternion.x, planets[i].quaternion.y, planets[i].quaternion.z, planets[i].quaternion.w);
}
renderer.render( scene, camera );
}
</script>
</body>
</html>