Added dynamic/static versions of the particle system example, removed original one

This commit is contained in:
codeanticode
2012-01-09 20:21:21 +00:00
parent dcdab48e2a
commit 0529f20716
4 changed files with 263 additions and 14 deletions
@@ -0,0 +1,117 @@
PImage sprite;
int npartTotal = 10000;
int npartPerFrame = 25;
float speed = 1.0;
float gravity = 0.05;
float partSize = 20;
int partLifetime;
PVector positions[];
PVector velocities[];
int lifetimes[];
int fcount, lastm;
float frate;
int fint = 3;
void setup() {
size(640, 480, P3D);
frameRate(240);
sprite = loadImage("sprite.png");
partLifetime = npartTotal / npartPerFrame;
initPositions();
initVelocities();
initLifetimes();
// Writing to the depth buffer is disabled to avoid rendering
// artifacts due to the fact that the particles are semi-transparent
// but not z-sorted.
hint(DISABLE_DEPTH_MASK);
// Testing some hints
//hint(DISABLE_TRANSFORM_CACHE);
//hint(ENABLE_ACCURATE_2D);
}
void draw () {
background(0);
for (int n = 0; n < npartTotal; n++) {
lifetimes[n]++;
if (lifetimes[n] == partLifetime) {
lifetimes[n] = 0;
}
if (0 <= lifetimes[n]) {
float opacity = 1.0 - float(lifetimes[n]) / partLifetime;
if (lifetimes[n] == 0) {
// Re-spawn dead particle
positions[n].x = mouseX;
positions[n].y = mouseY;
float angle = random(0, TWO_PI);
float s = random(0.5 * speed, 0.5 * speed);
velocities[n].x = s * cos(angle);
velocities[n].y = s * sin(angle);
} else {
positions[n].x += velocities[n].x;
positions[n].y += velocities[n].y;
velocities[n].y += gravity;
}
drawParticle(positions[n], opacity);
}
}
fcount += 1;
int m = millis();
if (m - lastm > 1000 * fint) {
frate = float(fcount) / fint;
fcount = 0;
lastm = m;
println("fps: " + frate);
}
}
void drawParticle(PVector center, float opacity) {
beginShape(QUAD);
noStroke();
tint(255, opacity * 255);
texture(sprite);
vertex(center.x - partSize/2, center.y - partSize/2, 0, 0);
vertex(center.x + partSize/2, center.y - partSize/2, sprite.width, 0);
vertex(center.x + partSize/2, center.y + partSize/2, sprite.width, sprite.height);
vertex(center.x - partSize/2, center.y + partSize/2, 0, sprite.height);
endShape();
}
void initPositions() {
positions = new PVector[npartTotal];
for (int n = 0; n < positions.length; n++) {
positions[n] = new PVector();
}
}
void initVelocities() {
velocities = new PVector[npartTotal];
for (int n = 0; n < velocities.length; n++) {
velocities[n] = new PVector();
}
}
void initLifetimes() {
// Initializing particles with negative lifetimes so they are added
// progressively into the screen during the first frames of the sketch
lifetimes = new int[npartTotal];
int t = -1;
for (int n = 0; n < lifetimes.length; n++) {
if (n % npartPerFrame == 0) {
t++;
}
lifetimes[n] = -t;
}
}
@@ -1,19 +1,23 @@
PShape particles;
PImage sprite;
int npartTotal = 1000;
int npartPerFrame = 10;
int npartTotal = 10000;
int npartPerFrame = 25;
float speed = 1.0;
float gravity = 0.05;
float partSize = 20;
int partLifetime;
PVector velocities[];
int lifetimes[];
int lifetimes[];
int fcount, lastm;
float frate;
int fint = 3;
void setup() {
size(640, 480, P3D);
frameRate(120);
frameRate(240);
particles = createShape(PShape.GROUP);
sprite = loadImage("sprite.png");
@@ -52,29 +56,35 @@ void draw () {
}
if (0 <= lifetimes[n]) {
float opacity = 1.0 - float(lifetimes[n]) / partLifetime;
part.tint(255, opacity * 255);
if (lifetimes[n] == 0) {
// Re-spawn dead particle
part.center(mouseX, mouseY);
float a = random(0, TWO_PI);
float angle = random(0, TWO_PI);
float s = random(0.5 * speed, 0.5 * speed);
velocities[n].x = s * cos(a);
velocities[n].y = s * sin(a);
}
else {
velocities[n].x = s * cos(angle);
velocities[n].y = s * sin(angle);
} else {
part.translate(velocities[n].x, velocities[n].y);
velocities[n].y += gravity;
}
float a = 1.0 - float(lifetimes[n]) / partLifetime;
part.fill(255, a * 255);
} else {
part.fill(0, 0);
part.tint(0, 0);
}
}
shape(particles);
if (frameCount % 10 == 0) println(frameRate);
fcount += 1;
int m = millis();
if (m - lastm > 1000 * fint) {
frate = float(fcount) / fint;
fcount = 0;
lastm = m;
println("fps: " + frate);
}
}
void initVelocities() {
@@ -0,0 +1,64 @@
PImage sprite;
int npartTotal = 50000;
float partSize = 20;
PVector positions[];
int fcount, lastm;
float frate;
int fint = 3;
void setup() {
size(800, 600, P3D);
frameRate(240);
sprite = loadImage("sprite.png");
initPositions();
// Writing to the depth buffer is disabled to avoid rendering
// artifacts due to the fact that the particles are semi-transparent
// but not z-sorted.
hint(DISABLE_DEPTH_MASK);
}
void draw () {
background(0);
translate(width/2, height/2);
rotateY(frameCount * 0.01);
for (int n = 0; n < npartTotal; n++) {
drawParticle(positions[n]);
}
fcount += 1;
int m = millis();
if (m - lastm > 1000 * fint) {
frate = float(fcount) / fint;
fcount = 0;
lastm = m;
println("fps: " + frate);
}
}
void drawParticle(PVector center) {
beginShape(QUAD);
noStroke();
tint(255);
texture(sprite);
vertex(center.x - partSize/2, center.y - partSize/2, center.z, 0, 0);
vertex(center.x + partSize/2, center.y - partSize/2, center.z, sprite.width, 0);
vertex(center.x + partSize/2, center.y + partSize/2, center.z, sprite.width, sprite.height);
vertex(center.x - partSize/2, center.y + partSize/2, center.z, 0, sprite.height);
endShape();
}
void initPositions() {
positions = new PVector[npartTotal];
for (int n = 0; n < positions.length; n++) {
positions[n] = new PVector(random(-500, +500), random(-500, +500), random(-500, +500));
}
}
@@ -0,0 +1,58 @@
PShape particles;
PImage sprite;
int npartTotal = 50000;
float partSize = 20;
int fcount, lastm;
float frate;
int fint = 3;
void setup() {
size(800, 600, P3D);
frameRate(240);
particles = createShape(PShape.GROUP);
sprite = loadImage("sprite.png");
for (int n = 0; n < npartTotal; n++) {
float cx = random(-500, +500);
float cy = random(-500, +500);
float cz = random(-500, +500);
PShape part = createShape(QUAD);
part.noStroke();
part.tint(255);
part.texture(sprite);
part.vertex(cx - partSize/2, cy - partSize/2, cz, 0, 0);
part.vertex(cx + partSize/2, cy - partSize/2, cz, sprite.width, 0);
part.vertex(cx + partSize/2, cy + partSize/2, cz, sprite.width, sprite.height);
part.vertex(cx - partSize/2, cy + partSize/2, cz, 0, sprite.height);
part.end();
particles.addShape(part);
}
// Writing to the depth buffer is disabled to avoid rendering
// artifacts due to the fact that the particles are semi-transparent
// but not z-sorted.
hint(DISABLE_DEPTH_MASK);
}
void draw () {
background(0);
translate(width/2, height/2);
rotateY(frameCount * 0.01);
shape(particles);
fcount += 1;
int m = millis();
if (m - lastm > 1000 * fint) {
frate = float(fcount) / fint;
fcount = 0;
lastm = m;
println("fps: " + frate);
}
}