Initial Commit

This commit is contained in:
plane000
2018-04-20 10:15:15 +01:00
parent 49150ccfe4
commit 62101e8e61
2870 changed files with 520122 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
/**
* BeginEndContour
*
* How to cut a shape out of another using beginContour() and endContour()
*/
PShape s;
void setup() {
size(640, 360, P2D);
// Make a shape
s = createShape();
s.beginShape();
s.fill(0);
s.stroke(255);
s.strokeWeight(2);
// Exterior part of shape
s.vertex(-100,-100);
s.vertex(100,-100);
s.vertex(100,100);
s.vertex(-100,100);
// Interior part of shape
s.beginContour();
s.vertex(-10,-10);
s.vertex(-10,10);
s.vertex(10,10);
s.vertex(10,-10);
s.endContour();
// Finishing off shape
s.endShape(CLOSE);
}
void draw() {
background(52);
// Display shape
translate(width/2, height/2);
// Shapes can be rotated
s.rotate(0.01);
shape(s);
}

View File

@@ -0,0 +1,67 @@
/**
* GroupPShape
*
* How to group multiple PShapes into one PShape
*/
// A PShape that will group PShapes
PShape group;
void setup() {
size(640, 360, P2D);
// Create the shape as a group
group = createShape(GROUP);
// Make a polygon PShape
PShape star = createShape();
star.beginShape();
star.noFill();
star.stroke(255);
star.vertex(0, -50);
star.vertex(14, -20);
star.vertex(47, -15);
star.vertex(23, 7);
star.vertex(29, 40);
star.vertex(0, 25);
star.vertex(-29, 40);
star.vertex(-23, 7);
star.vertex(-47, -15);
star.vertex(-14, -20);
star.endShape(CLOSE);
// Make a path PShape
PShape path = createShape();
path.beginShape();
path.noFill();
path.stroke(255);
for (float a = -PI; a < 0; a += 0.1) {
float r = random(60, 70);
path.vertex(r*cos(a), r*sin(a));
}
path.endShape();
// Make a primitive (Rectangle) PShape
PShape rectangle = createShape(RECT,-10,-10,20,20);
rectangle.setFill(false);
rectangle.setStroke(color(255));
// Add them all to the group
group.addChild(star);
group.addChild(path);
group.addChild(rectangle);
}
void draw() {
// We can access them individually via the group PShape
PShape rectangle = group.getChild(2);
// Shapes can be rotated
rectangle.rotate(0.1);
background(52);
// Display the group PShape
translate(mouseX, mouseY);
shape(group);
}

View File

@@ -0,0 +1,82 @@
// An individual Particle
class Particle {
// Velocity
PVector center;
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();
part.beginShape(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.endShape();
// Initialize center vector
center = new PVector();
// 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
part.resetMatrix();
part.translate(x, y);
// Update center vector
center.set(x, y, 0);
}
// Is it off the screen, or its lifespan is over?
boolean isDead() {
if (center.x > width || center.x < 0 ||
center.y > height || center.y < 0 || lifespan < 0) {
return true;
}
else {
return false;
}
}
void update() {
// Decrease life
lifespan = lifespan - 1;
// Apply gravity
velocity.add(gravity);
part.setTint(color(255, lifespan));
// Move the particle according to its velocity
part.translate(velocity.x, velocity.y);
// and also update the center
center.add(velocity);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,41 @@
/**
* 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, P2D);
// 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 and display 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);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

@@ -0,0 +1,38 @@
/**
* PathPShape
*
* A simple path using PShape
*/
// A PShape object
PShape path;
void setup() {
size(640, 360, P2D);
// Create the shape
path = createShape();
path.beginShape();
// Set fill and stroke
path.noFill();
path.stroke(255);
path.strokeWeight(2);
float x = 0;
// Calculate the path as a sine wave
for (float a = 0; a < TWO_PI; a+=0.1) {
path.vertex(x,sin(a)*100);
x+= 5;
}
// The path is complete
path.endShape();
}
void draw() {
background(51);
// Draw the path at the mouse location
translate(mouseX, mouseY);
shape(path);
}

View File

@@ -0,0 +1,42 @@
/**
* PrimitivePShape.
*
* Using a PShape to display a custom polygon.
*/
// The PShape object
PShape star;
void setup() {
size(640, 360, P2D);
// First create the shape
star = createShape();
star.beginShape();
// You can set fill and stroke
star.fill(102);
star.stroke(255);
star.strokeWeight(2);
// Here, we are hardcoding a series of vertices
star.vertex(0, -50);
star.vertex(14, -20);
star.vertex(47, -15);
star.vertex(23, 7);
star.vertex(29, 40);
star.vertex(0, 25);
star.vertex(-29, 40);
star.vertex(-23, 7);
star.vertex(-47, -15);
star.vertex(-14, -20);
star.endShape(CLOSE);
}
void draw() {
background(51);
// We can use translate to move the PShape
translate(mouseX, mouseY);
// Display the shape
shape(star);
}

View File

@@ -0,0 +1,30 @@
/**
* PolygonPShapeOOP.
*
* Wrapping a PShape inside a custom class
*/
// A Star object
Star s1, s2;
void setup() {
size(640, 360, P2D);
// Make a new Star
s1 = new Star();
s2 = new Star();
}
void draw() {
background(51);
s1.display(); // Display the first star
s1.move(); // Move the first star
s2.display(); // Display the second star
s2.move(); // Move the second star
}

View File

@@ -0,0 +1,52 @@
// A class to describe a Star shape
class Star {
// The PShape object
PShape s;
// The location where we will draw the shape
float x, y;
float speed;
Star() {
x = random(100, width-100);
y = random(100, height-100);
speed = random(0.5, 3);
// First create the shape
s = createShape();
s.beginShape();
// You can set fill and stroke
s.fill(255, 204);
s.noStroke();
// Here, we are hardcoding a series of vertices
s.vertex(0, -50);
s.vertex(14, -20);
s.vertex(47, -15);
s.vertex(23, 7);
s.vertex(29, 40);
s.vertex(0, 25);
s.vertex(-29, 40);
s.vertex(-23, 7);
s.vertex(-47, -15);
s.vertex(-14, -20);
// The shape is complete
s.endShape(CLOSE);
}
void move() {
// Demonstrating some simple motion
x += speed;
if (x > width+100) {
x = -100;
}
}
void display() {
// Locating and drawing the shape
pushMatrix();
translate(x, y);
shape(s);
popMatrix();
}
}

View File

@@ -0,0 +1,34 @@
// A class to describe a Polygon (with a PShape)
class Polygon {
// The PShape object
PShape s;
// The location where we will draw the shape
float x, y;
// Variable for simple motion
float speed;
Polygon(PShape s_) {
x = random(width);
y = random(-500, -100);
s = s_;
speed = random(2, 6);
}
// Simple motion
void move() {
y += speed;
if (y > height+100) {
y = -100;
}
}
// Draw the object
void display() {
pushMatrix();
translate(x, y);
shape(s);
popMatrix();
}
}

View File

@@ -0,0 +1,53 @@
/**
* PolygonPShapeOOP.
*
* Wrapping a PShape inside a custom class
* and demonstrating how we can have a multiple objects each
* using the same PShape.
*/
// A list of objects
ArrayList<Polygon> polygons;
void setup() {
size(640, 360, P2D);
// Make a PShape
PShape star = createShape();
star.beginShape();
star.noStroke();
star.fill(0, 127);
star.vertex(0, -50);
star.vertex(14, -20);
star.vertex(47, -15);
star.vertex(23, 7);
star.vertex(29, 40);
star.vertex(0, 25);
star.vertex(-29, 40);
star.vertex(-23, 7);
star.vertex(-47, -15);
star.vertex(-14, -20);
star.endShape(CLOSE);
// Make an ArrayList
polygons = new ArrayList<Polygon>();
// Add a bunch of objects to the ArrayList
// Pass in reference to the PShape
// We coud make polygons with different PShapes
for (int i = 0; i < 25; i++) {
polygons.add(new Polygon(star));
}
}
void draw() {
background(255);
// Display and move them all
for (Polygon poly : polygons) {
poly.display();
poly.move();
}
}

View File

@@ -0,0 +1,34 @@
// A class to describe a Polygon (with a PShape)
class Polygon {
// The PShape object
PShape s;
// The location where we will draw the shape
float x, y;
// Variable for simple motion
float speed;
Polygon(PShape s_) {
x = random(width);
y = random(-500, -100);
s = s_;
speed = random(2, 6);
}
// Simple motion
void move() {
y+=speed;
if (y > height+100) {
y = -100;
}
}
// Draw the object
void display() {
pushMatrix();
translate(x, y);
shape(s);
popMatrix();
}
}

View File

@@ -0,0 +1,60 @@
/**
* PolygonPShapeOOP.
*
* Wrapping a PShape inside a custom class
* and demonstrating how we can have a multiple objects each
* using the same PShape.
*/
// A list of objects
ArrayList<Polygon> polygons;
// Three possible shapes
PShape[] shapes = new PShape[3];
void setup() {
size(640, 360, P2D);
shapes[0] = createShape(ELLIPSE,0,0,100,100);
shapes[0].setFill(color(255, 127));
shapes[0].setStroke(false);
shapes[1] = createShape(RECT,0,0,100,100);
shapes[1].setFill(color(255, 127));
shapes[1].setStroke(false);
shapes[2] = createShape();
shapes[2].beginShape();
shapes[2].fill(0, 127);
shapes[2].noStroke();
shapes[2].vertex(0, -50);
shapes[2].vertex(14, -20);
shapes[2].vertex(47, -15);
shapes[2].vertex(23, 7);
shapes[2].vertex(29, 40);
shapes[2].vertex(0, 25);
shapes[2].vertex(-29, 40);
shapes[2].vertex(-23, 7);
shapes[2].vertex(-47, -15);
shapes[2].vertex(-14, -20);
shapes[2].endShape(CLOSE);
// Make an ArrayList
polygons = new ArrayList<Polygon>();
for (int i = 0; i < 25; i++) {
int selection = int(random(shapes.length)); // Pick a random index
Polygon p = new Polygon(shapes[selection]); // Use corresponding PShape to create Polygon
polygons.add(p);
}
}
void draw() {
background(102);
// Display and move them all
for (Polygon poly : polygons) {
poly.display();
poly.move();
}
}

View File

@@ -0,0 +1,27 @@
/**
* PrimitivePShape.
*
* Using a PShape to display a primitive shape (in this case, ellipse).
*/
// The PShape object
PShape circle;
void setup() {
size(640, 360, P2D);
// Creating the PShape as an ellipse
circle = createShape(ELLIPSE, 0, 0, 100, 50);
}
void draw() {
background(51);
// We can dynamically set the stroke and fill of the shape
circle.setStroke(color(255));
circle.setStrokeWeight(4);
circle.setFill(color(map(mouseX, 0, width, 0, 255)));
// We can use translate to move the PShape
translate(mouseX, mouseY);
// Drawing the PShape
shape(circle);
}

View File

@@ -0,0 +1,22 @@
/**
* WigglePShape.
*
* How to move the individual vertices of a PShape
*/
// A "Wiggler" object
Wiggler w;
void setup() {
size(640, 360, P2D);
w = new Wiggler();
}
void draw() {
background(255);
w.display();
w.wiggle();
}

View File

@@ -0,0 +1,67 @@
// An object that wraps the PShape
class Wiggler {
// The PShape to be "wiggled"
PShape s;
// Its location
float x, y;
// For 2D Perlin noise
float yoff = 0;
// We are using an ArrayList to keep a duplicate copy
// of vertices original locations.
ArrayList<PVector> original;
Wiggler() {
x = width/2;
y = height/2;
// The "original" locations of the vertices make up a circle
original = new ArrayList<PVector>();
for (float a = 0; a < TWO_PI; a+=0.2) {
PVector v = PVector.fromAngle(a);
v.mult(100);
original.add(v);
}
// Now make the PShape with those vertices
s = createShape();
s.beginShape();
s.fill(127);
s.stroke(0);
s.strokeWeight(2);
for (PVector v : original) {
s.vertex(v.x, v.y);
}
s.endShape(CLOSE);
}
void wiggle() {
float xoff = 0;
// Apply an offset to each vertex
for (int i = 0; i < s.getVertexCount(); i++) {
// Calculate a new vertex location based on noise around "original" location
PVector pos = original.get(i);
float a = TWO_PI*noise(xoff,yoff);
PVector r = PVector.fromAngle(a);
r.mult(4);
r.add(pos);
// Set the location of each vertex to the new one
s.setVertex(i, r.x, r.y);
// increment perlin noise x value
xoff+= 0.5;
}
// Increment perlin noise y value
yoff += 0.02;
}
void display() {
pushMatrix();
translate(x, y);
shape(s);
popMatrix();
}
}