mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
new PShape examples
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
// An individual Particle
|
||||
|
||||
class Particle {
|
||||
|
||||
// Velocity
|
||||
PVector velocity;
|
||||
// Lifespane is tied to alpha
|
||||
float lifespan;
|
||||
|
||||
// The particle PShape
|
||||
PShape part;
|
||||
// The particle size
|
||||
float partSize;
|
||||
|
||||
// A single force
|
||||
PVector gravity = new PVector(0, 0.1);
|
||||
|
||||
Particle() {
|
||||
partSize = random(10, 60);
|
||||
// The particle is a textured quad
|
||||
part = createShape(QUAD);
|
||||
part.noStroke();
|
||||
part.texture(sprite);
|
||||
part.normal(0, 0, 1);
|
||||
part.vertex(-partSize/2, -partSize/2, 0, 0);
|
||||
part.vertex(+partSize/2, -partSize/2, sprite.width, 0);
|
||||
part.vertex(+partSize/2, +partSize/2, sprite.width, sprite.height);
|
||||
part.vertex(-partSize/2, +partSize/2, 0, sprite.height);
|
||||
part.end();
|
||||
|
||||
// Set the particle starting location
|
||||
rebirth(width/2, height/2);
|
||||
}
|
||||
|
||||
PShape getShape() {
|
||||
return part;
|
||||
}
|
||||
|
||||
void rebirth(float x, float y) {
|
||||
float a = random(TWO_PI);
|
||||
float speed = random(0.5, 4);
|
||||
// A velocity with random angle and magnitude
|
||||
velocity = PVector.fromAngle(a);
|
||||
velocity.mult(speed);
|
||||
// Set lifespan
|
||||
lifespan = 255;
|
||||
// Set location using translate
|
||||
PVector center = part.getCenter();
|
||||
part.translate(x - center.x, y - center.y);
|
||||
}
|
||||
|
||||
// Is it off the screen?
|
||||
boolean isDead() {
|
||||
PVector center = part.getCenter();
|
||||
if (center.y > height+50) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void update() {
|
||||
// Decrease life
|
||||
lifespan = lifespan - 4;
|
||||
// Apply gravity
|
||||
velocity.add(gravity);
|
||||
part.tint(255, lifespan);
|
||||
// Move the particle according to its velocity
|
||||
part.translate(velocity.x, velocity.y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
// The Particle System
|
||||
|
||||
class ParticleSystem {
|
||||
// It's just an ArrayList of particle objects
|
||||
ArrayList<Particle> particles;
|
||||
|
||||
// The PShape to group all the particle PShapes
|
||||
PShape particleShape;
|
||||
|
||||
ParticleSystem(int n) {
|
||||
particles = new ArrayList<Particle>();
|
||||
// The PShape is a group
|
||||
particleShape = createShape(GROUP);
|
||||
|
||||
// Make all the Particles
|
||||
for (int i = 0; i < n; i++) {
|
||||
Particle p = new Particle();
|
||||
particles.add(p);
|
||||
// Each particle's PShape gets added to the System PShape
|
||||
particleShape.addChild(p.getShape());
|
||||
}
|
||||
}
|
||||
|
||||
void update() {
|
||||
for (Particle p : particles) {
|
||||
p.update();
|
||||
}
|
||||
}
|
||||
|
||||
void setEmitter(float x, float y) {
|
||||
for (Particle p : particles) {
|
||||
// Each particle gets reborn at the emitter location
|
||||
if (p.isDead()) {
|
||||
p.rebirth(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void display() {
|
||||
shape(particleShape);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* ParticleSystemPShape
|
||||
*
|
||||
* A particle system optimized for drawing using PShape
|
||||
*/
|
||||
|
||||
// Particle System object
|
||||
ParticleSystem ps;
|
||||
// A PImage for particle's texture
|
||||
PImage sprite;
|
||||
|
||||
void setup() {
|
||||
size(640, 360, P3D);
|
||||
// Load the image
|
||||
sprite = loadImage("sprite.png");
|
||||
// A new particle system with 10,000 particles
|
||||
ps = new ParticleSystem(10000);
|
||||
|
||||
// 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);
|
||||
// Update an dipsplay system
|
||||
ps.update();
|
||||
ps.display();
|
||||
|
||||
// Set the particle system's emitter location to the mouse
|
||||
ps.setEmitter(mouseX,mouseY);
|
||||
|
||||
// Display frame rate
|
||||
fill(255);
|
||||
textSize(16);
|
||||
text("Frame rate: " + int(frameRate),10,20);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user