-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
170 lines (131 loc) · 4.23 KB
/
main.js
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
var particles = [],
particlePool = new particlePoolClass(100000),
stopRender = false,
scene = new THREE.Scene(),
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1,10000 ),
forceRegistry = new forceRegistry();
camera.position.set( 1500, 2400, 1500 );
camera.lookAt( scene.position );
// Renderer
var renderer = new THREE.WebGLRenderer();
renderer.shadowMapEnabled = true;
renderer.shadowMapSoft = true;
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// Ground
var groundGeo = new THREE.PlaneBufferGeometry( 5500, 5500 );
var groundMat = new THREE.MeshPhongMaterial( { ambient: 0xffffff, color: 0x0c0638, specular: 0x0c0638 } );
//groundMat.color.setHSL( 0.095, 1, 0.75 );
var ground = new THREE.Mesh( groundGeo, groundMat );
ground.rotation.x = -Math.PI/2;
ground.position.y = -33;
scene.add(ground);
ground.receiveShadow = true;
// // 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 );
light = new THREE.DirectionalLight(0xdfebff, .75);
light.position.set(0, 400, 0);
light.position.multiplyScalar(1.3);
light.castShadow = true;
// light.shadowCameraVisible = true;
light.shadowMapWidth = 2048;
light.shadowMapHeight = 2048;
var d = 2500;
light.shadowCameraLeft = -d;
light.shadowCameraRight = d;
light.shadowCameraTop = d;
light.shadowCameraBottom = -d;
light.shadowCameraFar = 1000;
light.shadowDarkness = 0.5;
light.shadowBias = -0.0001;
scene.add(light);
// mouse movement to camera
var mouseX = 0;
var mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var time = -1;
var render = function () {
if (stopRender) {
return false;
}
camera.lookAt(ground.position);
requestAnimationFrame( render );
if (time === -1) {
var difference = 0;
} else {
var temp = +new Date();
var difference = temp - time;
}
time = new Date();
// apply forces for each frame
forceRegistry.updateForces(difference/1000);
var indexesToRemove = [];
for (var i = particles.length - 1; i >= 0; i--) {
if (particles[i].canRemove()) indexesToRemove.push(i);
particles[i].update(difference/1000);
};
for (var i = 0; i < indexesToRemove.length; i++) {
// console.log(JSON.stringify(indexesToRemove));
particles.splice(indexesToRemove[i], 1);
};
renderer.render(scene, camera);
};
var mouse = {};
var raycaster = new THREE.Raycaster();
var velocityFactor = 650;
// click to create
document.addEventListener('mousedown', shoot, false);
function shoot(event) {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, camera );
// calculate objects intersecting the picking ray
var intersects = raycaster.intersectObject( ground );
if (intersects.length) {
var norm = intersects[0].point.normalize(),
velocityX = norm.x * velocityFactor,
velocityZ = norm.z * velocityFactor;
var geometry = new THREE.SphereGeometry( 22, 12, 12 );
var material = new THREE.MeshPhongMaterial({
ambient: 0x030303,
color: 0xED6F15,
specular: 0xED6F15,
shininess: 4,
shading: THREE.FlatShading
});
var mesh = new THREE.Mesh( geometry, material );
var fireball = new cyclone.particle(mesh);
var mass = Math.floor(Math.random() * 400);
fireball.setMass(.01); // 1.0kg - mostly blast damage
console.log('the mass for this fireball: ' + mass);
fireball.setVelocity(new THREE.Vector3(velocityX,60,velocityZ)); //
fireball.setAcceleration(new THREE.Vector3(0, -65.0, 0));
fireball.setDamping(0.9);
fireball.setPosition(0,80,0);
fireball.mesh.castShadow = true;
fireball.setEmitter(1,150);
var fireballDrag = new forceGenerator_particleDrag(0.001);
forceRegistry.add({
particle : fireball,
fg : fireballDrag
});
particles.push(fireball);
scene.add(fireball.mesh);
}
}
document.addEventListener("keydown", keyDownHandler, false);
function keyDownHandler(event) {
if (event.keyCode == 81) {
stopRender = true;
} else {
console.log('hit key: ' + event.keyCode);
}
}
// start the ap
render();