/
popullution.js
321 lines (253 loc) · 10.7 KB
/
popullution.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
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
// set the scene size
var WIDTH = $('#container').width(),//1100,
HEIGHT = $('#container').height();//600;
var scene, camera, renderer, particleSystem, particles;
var container;
var mouseDown = false;
var particleCount= 2500;
var particleStartAreaSize = 50;
var lifespan = 2000;
var colors = new Array();
var pMaterial;
var Pool;
var POPULLUTION = {
maxGen: 1
};
POPULLUTION.pleb = function() {
this.energy = Infinity;//KUTIL.rnd(lifespan, 150);//Number.POSITIVE_INFINITY;
this.spawns = 3;
this.generation = 1;
this.particle = undefined;
}
function normalizePosition(particle) {
if(particle.position.x > cellcube.normalizer) {
particle.position.x = cellcube.normalizer;
} else if(particle.position.x < -cellcube.normalizer) {
particle.position.x = -cellcube.normalizer;
}
if(particle.position.y > cellcube.normalizer) {
particle.position.y = cellcube.normalizer;
} else if(particle.position.y < -cellcube.normalizer) {
particle.position.y = -cellcube.normalizer;
}
if(particle.position.z > cellcube.normalizer) {
particle.position.z = cellcube.normalizer;
} else if(particle.position.z < -cellcube.normalizer) {
particle.position.z = -cellcube.normalizer;
}
}
POPULLUTION.plebs = new Array();
init();
animate();
var cellcube;
function init() {
var VIEW_ANGLE = 45,
ASPECT = WIDTH / HEIGHT,
NEAR = 0.1,
FAR = 10000;
renderer = new THREE.WebGLRenderer();
camera =
new THREE.PerspectiveCamera(
VIEW_ANGLE,
ASPECT,
NEAR,
FAR);
scene = new THREE.Scene();
camera.position.z = settings.cameraPosition;
scene.add(camera);
particles = new THREE.Geometry();
pMaterial =
new THREE.ParticleBasicMaterial({
color: 0xFFFFFF,
size: 1,
opacity: 0.9,
vertexColors: true,
blending: THREE.AdditiveBlending
});
//// PARTICLE POOL
function newpos(x, y, z) {
return new THREE.Vertex(
new THREE.Vector3(x, y, z)
);
}
Pool = {
__pools: [],
// Get a new particle
get: function() {
if (this.__pools.length>0) {
return this.__pools.pop();
}
console.log("pool ran out!")
return null;
},
// Release a particle back into the pool
add: function(v) {
this.__pools.push(v);
}
};
for ( i = 0; i < 20000; i++ ) {
particles.vertices.push(newpos(Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY));
colors.push(new THREE.Color( 0xffffff ));
Pool.add(i);
}
// now create the individual particles
for(var p = 0; p < particleCount; p++) {
var pX = Math.random() * particleStartAreaSize - particleStartAreaSize/2,
pY = Math.random() * particleStartAreaSize - particleStartAreaSize/2,
pZ = Math.random() * particleStartAreaSize - particleStartAreaSize/2,
particleIndex = Pool.get();
particle = particles.vertices[particleIndex];
particle.position.set(pX, pY, pZ);
// particle.position.set(pX, pX, pX);
// colors[ particleIndex ].setRGB(pRed, pGreen, pYellow);
colors[ particleIndex ].setHSV(1/POPULLUTION.maxGen,1,1);
pleb = new POPULLUTION.pleb();
pleb.particle = particleIndex;
POPULLUTION.plebs.push(pleb);
}
particles.colors = colors;
// create the particle system
particleSystem =
new THREE.ParticleSystem(
particles,
pMaterial);
// helper function to compute bounding sphere ignoring particles at position Infinity
particleSystem.geometry.computeFiniteBoundingSphere = function (){
for(var a,b=0,c=0,d=this.vertices.length;c<d;c++)a=this.vertices[c].position.length(),(a != Number.POSITIVE_INFINITY && a>b)&&(b=a);
this.finiteBoundingSphere={
radius:b
}
}
particleSystem.sortParticles = false;
// space partioning
cellcube = new CELLLIST.Cube(100);
cellcube.computeNeighbors();
for(p = 0; p < particleCount; p++) {
position = particles.vertices[POPULLUTION.plebs[p].particle].position;
cX = position.x > 0 ? Math.floor(position.x) : Math.ceil(position.x);
cY = position.y > 0 ? Math.floor(position.y) : Math.ceil(position.y);
cZ = position.z > 0 ? Math.floor(position.z) : Math.ceil(position.z);
cellcube.add(cX,cY,cZ,POPULLUTION.plebs[p]);
}
scene.add(particleSystem);
renderer.setSize(WIDTH, HEIGHT);
// attach the render-supplied DOM element
container = document.getElementById( 'container' );
container.style.width = WIDTH+"px";
container.style.height = HEIGHT+"px";
container.appendChild( renderer.domElement );
addEventListeners();
// add stats widget
var stats = new Stats();
statsContainer = document.getElementById( 'statsContainer' );
statsContainer.appendChild( stats.getDomElement() );
setInterval( function () {
stats.update();
}, 1000 / 60 );
$('#census').text(POPULLUTION.plebs.length);
renderer.render(scene, camera);
}
function animate() {
particleSystem.rotation.y += 0.001;
// particleSystem.rotation.x += 0.001;
// particleSystem.rotation.z += 0.001;
if(settings.run) {
if(POPULLUTION.plebs.length >= settings.maxPop) {
settings.run = false;
$('#message').text("overkill!");
} else if (POPULLUTION.plebs.length == 0) {
settings.run = false;
$('#message').text("wipeout!");
}
if(settings.move) {
move();
}
if(settings.collide) {
collide();
}
particleSystem.geometry. __dirtyVertices = true;
particleSystem.geometry. __dirtyColors = true;
$('#census').text(POPULLUTION.plebs.length);
}
if(settings.animate) {
requestAnimationFrame( animate );
}
render();
}
function collide() {
var exhausted = new Array();
for(cellIndex in cellcube.activeCells) {
for(plebIndex in cellcube.activeCells[cellIndex].members) {
plebOne = cellcube.activeCells[cellIndex].members[plebIndex];
if(exhausted[plebOne.particle])
continue;
particle = particles.vertices[plebOne.particle];
for( neighborIndex in cellcube.activeCells[cellIndex].neighbors) {
if(cellcube.activeCells[cellIndex].neighbors[neighborIndex].active) {
for(neighborPlebIndex in cellcube.activeCells[cellIndex].neighbors[neighborIndex].members) {
plebTwo = cellcube.activeCells[cellIndex].neighbors[neighborIndex].members[neighborPlebIndex];
if(neighborPlebIndex == plebIndex || exhausted[plebTwo.particle])
continue;
candiatePlebParticleIndex = plebTwo.particle;
collisionCandidate = particles.vertices[candiatePlebParticleIndex];
var distance = particle.position.distanceTo(collisionCandidate.position);
if(distance <1) {
if(plebOne.spawns > 0 && plebTwo.spawns > 0) {
plebOne.spawns--;
plebTwo.spawns--;
exhausted[POPULLUTION.plebs.indexOf(plebOne)] = true;
exhausted[POPULLUTION.plebs.indexOf(plebOne)] = true;
particleIndex = Pool.get();
childParticle = particles.vertices[particleIndex];
childParticle.position.set(
particle.position.x+(Math.random()*settings.spawningAreaSize*2-settings.spawningAreaSize),
particle.position.y+(Math.random()*settings.spawningAreaSize*2-settings.spawningAreaSize),
particle.position.z+(Math.random()*settings.spawningAreaSize*2-settings.spawningAreaSize));
normalizePosition(childParticle);
pleb = new POPULLUTION.pleb();
pleb.particle = particleIndex;
pleb.generation = plebOne.generation+1,(pleb.generation > POPULLUTION.maxGen)&&(POPULLUTION.maxGen = pleb.generation);
color = KCOLORS.RtoB(pleb.generation, POPULLUTION.maxGen);
colors[ particleIndex ].setRGB( color.r, color.g, color.b);
POPULLUTION.plebs.push(pleb);
}
}
}
}
}
}
}
}
function move() {
for(var pCount in POPULLUTION.plebs) {
// get the particle
var plebs = POPULLUTION.plebs[pCount];
plebsParticleIndex = POPULLUTION.plebs[pCount].particle
particle = particles.vertices[plebsParticleIndex];
cX = particle.position.x > 0 ? Math.floor(particle.position.x) : Math.ceil(particle.position.x);
cY = particle.position.y > 0 ? Math.floor(particle.position.y) : Math.ceil(particle.position.y);
cZ = particle.position.z > 0 ? Math.floor(particle.position.z) : Math.ceil(particle.position.z);
cellcube.remove(cX,cY,cZ, POPULLUTION.plebs[pCount]);
if(--POPULLUTION.plebs[pCount].energy <= 0) {
particle.position.set(Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
colors[plebsParticleIndex].setRGB(0,0,0);
Pool.add(plebsParticleIndex);
POPULLUTION.plebs.splice(pCount,1);
} else {
// color
colors[ plebsParticleIndex ].setHSV(POPULLUTION.plebs[pCount].generation/POPULLUTION.maxGen,1,1);
// random step of size stepsize
particle.position.addSelf(
settings.movementMatrix[Math.floor(Math.random()*6)]); // TODO: multiply with gauss scalar
normalizePosition(particle);
cX = particle.position.x > 0 ? Math.floor(particle.position.x) : Math.ceil(particle.position.x);
cY = particle.position.y > 0 ? Math.floor(particle.position.y) : Math.ceil(particle.position.y);
cZ = particle.position.z > 0 ? Math.floor(particle.position.z) : Math.ceil(particle.position.z);
cellcube.add(cX,cY,cZ, POPULLUTION.plebs[pCount])
}
}
}
function render() {
renderer.clear();
renderer.render( scene, camera );
}