-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleSystem.js
More file actions
265 lines (226 loc) · 8.22 KB
/
ParticleSystem.js
File metadata and controls
265 lines (226 loc) · 8.22 KB
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
// Set the scene size.
const WIDTH = 600;
const HEIGHT = 600;
// Set some camera attributes.
const VIEW_ANGLE = 45;
const ASPECT = WIDTH / HEIGHT;
const NEAR = 0.1;
const FAR = 10000;
const numParticles = 3000;
let minLifetime;
let maxLifetime;
let minSize;
let maxSize;
let minSpeed;
let maxSpeed;
let startColor;
let endColor;
const objects = [];
var spawnDensity = document.getElementById("spawnDensity").value;
var lifetime = document.getElementById("lifetime").value;
var lifetimeVariation = document.getElementById("lifetimeVariation").value;
minLifetime = lifetime - lifetimeVariation / 2.0;
maxLifetime = parseFloat(lifetime) + parseFloat(lifetimeVariation / 2.0);
var size = document.getElementById("size").value;
if (size < 0.001){
size = 0.001;
}
var sizeVariation = document.getElementById("sizeVariation").value;
if(size - sizeVariation / 2 < 0.001){
minSize = 0.001;
}else{
minSize = size - sizeVariation / 2.0;
}
maxSize = parseFloat(size) + parseFloat(sizeVariation / 2.0);
var speed = document.getElementById("speed").value;
var speedVariation = document.getElementById("speedVariation").value;
minSpeed = speed - speedVariation / 2.0;
maxSpeed = parseFloat(speed) + parseFloat(speedVariation / 2.0);
var acceleration = document.getElementById("acceleration").value;
var startColorR = document.getElementById("startColorR").value;
var startColorG = document.getElementById("startColorG").value;
var startColorB = document.getElementById("startColorB").value;
var endColorR = document.getElementById("endColorR").value;
var endColorG = document.getElementById("endColorG").value;
var endColorB = document.getElementById("endColorB").value;
function updateTextBoxes() {
spawnDensity = document.getElementById("spawnDensity").value;
lifetime = document.getElementById("lifetime").value;
lifetimeVariation = document.getElementById("lifetimeVariation").value;
minLifetime = lifetime - lifetimeVariation / 2.0;
maxLifetime = parseFloat(lifetime) + parseFloat(lifetimeVariation / 2.0);
size = document.getElementById("size").value;
if (size < 0.001){
size = 0.001;
}
sizeVariation = document.getElementById("sizeVariation").value;
if(size - sizeVariation / 2.0 < 0.001){
minSize = 0.001;
}else{
minSize = size - sizeVariation / 2.0;
}
maxSize = parseFloat(size) + parseFloat(sizeVariation / 2.0);
speed = document.getElementById("speed").value;
speedVariation = document.getElementById("speedVariation").value;
minSpeed = speed - speedVariation / 2.0;
maxSpeed = parseFloat(speed) + parseFloat(speedVariation / 2.0);
acceleration = document.getElementById("acceleration").value;
this.startColor.setRGB(
document.getElementById("startColorR").value,
document.getElementById("startColorG").value,
document.getElementById("startColorB").value
);
this.endColor.setRGB(
document.getElementById("endColorR").value,
document.getElementById("endColorG").value,
document.getElementById("endColorB").value
);
}
var xSpeed = 3.0;
var ySpeed = 3.0;
var zSpeed = 3.0;
document.addEventListener("keydown", onDocumentKeyDown, false);
function onDocumentKeyDown(event) {
var keyCode = event.which;
if (keyCode === 87) {
//w
user.position.z -= zSpeed;
} else if (keyCode === 83) {
//s
user.position.z += zSpeed;
} else if (keyCode === 65) {
//a
user.position.x -= xSpeed;
} else if (keyCode === 68) {
//d
user.position.x += xSpeed;
} else if (keyCode === 82) {
//r
user.position.y += ySpeed;
} else if (keyCode === 70) {
//f
user.position.y -= ySpeed;
} else if (keyCode === 32) {
//space
user.position.set(0, 0, 0);
}
}
// Get the DOM element to attach to
const container =
document.querySelector('#container');
// Create a WebGL renderer, camera
// and a scene
const renderer = new THREE.WebGLRenderer();
const camera =
new THREE.PerspectiveCamera(
VIEW_ANGLE,
ASPECT,
NEAR,
FAR
);
const scene = new THREE.Scene();
// Add the camera to the scene.
scene.add(camera);
// Start the renderer.
renderer.setSize(WIDTH, HEIGHT);
// Attach the renderer-supplied
// DOM element.
container.appendChild(renderer.domElement);
// create a point light
const pointLight =
new THREE.PointLight(0xFFFFFF);
// set its position
pointLight.position.set(10,50,100);
// add to the scene
scene.add(pointLight);
// create the sphere's material
const sphereMaterial =
new THREE.MeshPhongMaterial(
{
color: new THREE.Color(0xFF0000)
});
// Set up the sphere vars
const RADIUS = 10;
const SEGMENTS = 16;
const RINGS = 16;
const user = new THREE.Mesh(
new THREE.SphereGeometry(
RADIUS,
SEGMENTS,
RINGS),
sphereMaterial);
user.position.z = -300;
scene.add(user);
for (let i = 0; i < numParticles; i++) {
let particleMaterial =
new THREE.PointsMaterial(
{
color: new THREE.Color(0xFFFFFF)
});
// let particleMesh = new THREE.Mesh(
// // new THREE.SphereGeometry(
// // 0.5,
// // RADIUS,
// // RINGS),
// new THREE.PlaneGeometry(
// 1.0,
// 1.0,
// 1.0),
var vertices = new Float32Array( [
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0
] );
var geometry = new THREE.BufferGeometry();
geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ).setDynamic(true) );
let particleMesh = new THREE.Mesh(geometry, particleMaterial);
particleMesh.position.set(
user.position.x + parseFloat(Math.random() * spawnDensity - spawnDensity / 2.0),
user.position.y + parseFloat(Math.random() * spawnDensity - spawnDensity / 2.0),
user.position.z + parseFloat(Math.random() * spawnDensity - spawnDensity / 2.0)
);
particleMesh.scale.setScalar(parseFloat(Math.random() * (maxSize - minSize)) + parseFloat(minSize));
this.startColor = new THREE.Color(this.startColorR, this.startColorG, this.startColorB);
this.endColor = new THREE.Color(this.endColorR, this.endColorG, this.endColorB);
let particle =
new ParticleObject(
particleMesh, //Mesh
lifetime * Math.random(), //Lifetime. If there is no random, all the particle will spawn and die at the same time
parseFloat(Math.random() * (maxSpeed - minSpeed)) + parseFloat(minSpeed), //Speed
acceleration, //Acceleration
new Vector3([1 - (2 * Math.random()), 1 - (2 *Math.random()), 1 - (2 * Math.random())]).normalize(), //Direction
new THREE.Color(this.startColor), //StartColor
new THREE.Color(this.endColor) //EndColor
);
objects.push(particle);
scene.add(particle.mesh);
}
function update() {
updateTextBoxes();
renderer.render(scene, camera);
let particle;
for (let i = 0; i < objects.length; i++) {
particle = objects[i];
// objects[i].position.x = Math.random() * 250 - 175;
if (particle.getLifeLeft() < 0) {
let spawnX = user.position.x + parseFloat(Math.random() * spawnDensity - spawnDensity / 2.0);
let spawnY = user.position.y + parseFloat(Math.random() * spawnDensity - spawnDensity / 2.0);
let spawnZ = user.position.z + parseFloat(Math.random() * spawnDensity - spawnDensity / 2.0);
particle.setPosition(spawnX, spawnY, spawnZ);
particle.setSize(parseFloat(Math.random() * (maxSize - minSize)) + parseFloat(minSize));
particle.setLifetime(parseFloat(Math.random() * (maxLifetime - minLifetime)) + parseFloat(minLifetime));
particle.resetColor();
particle.setSpeed(parseFloat(Math.random() * (maxSpeed - minSpeed)) + parseFloat(minSpeed));
particle.setAcceleration(this.acceleration);
particle.swapColor(this.startColor, this.endColor);
} else {
particle.update();
}
}
requestAnimationFrame(update);
}
// Schedule the first frame.
requestAnimationFrame(update);