new PShape examples

This commit is contained in:
shiffman
2012-05-22 02:05:57 +00:00
parent ea62e872a1
commit 2fb7155d95
16 changed files with 718 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
PShape s;
void setup() {
size(640, 360, P2D);
smooth();
s = createShape();
s.noFill();
s.stroke(0);
s.beginContour();
for (float a = -PI; a < 0; a += 0.1) {
float r = random(45, 55);
s.vertex(-60+r*cos(a), r*sin(a));
}
s.endContour();
s.beginContour();
for (float a = 0; a < PI; a += 0.1) {
float r = random(45, 55);
s.vertex(60+r*cos(a), r*sin(a));
}
s.endContour();
s.end();
}
void draw() {
background(255);
translate(width/2, height/2);
s.rotate(0.01);
shape(s);
}

View File

@@ -0,0 +1,69 @@
/**
* GroupPShape
*
* How to group multiple PShapes into one PShape
*/
// A PShape that will group PShapes
PShape group;
void setup() {
size(640, 360, P2D);
smooth();
// Create the shape as a group
group = createShape(GROUP);
// Make a polygon PShape
PShape star = createShape();
star.fill(102);
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.end(CLOSE);
// Make a path PShape
PShape path = createShape();
path = createShape();
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.end();
// Make a primitive (Rectangle) PShape
PShape rectangle = createShape(RECT,-10,-10,20,20);
rectangle.noFill();
rectangle.stroke(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,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);
}
}

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

View File

@@ -0,0 +1,37 @@
/**
* PathPShape
*
* A simple path using PShape
*/
// A PShape object
PShape path;
void setup() {
size(640, 360, P3D);
smooth();
// Create the shape
path = createShape();
// Set fill and stroke
path.noFill();
path.stroke(0);
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.end();
}
void draw() {
background(255);
// Draw the path at the mouse location
translate(mouseX, mouseY);
shape(path);
}

View File

@@ -0,0 +1,40 @@
/**
* PrimitivePShape.
*
* Using a PShape to display a custom polygon.
*/
// The PShape object
PShape star;
void setup() {
size(640, 360,P3D);
smooth();
// First create the shape
star = createShape();
// You can set fill and stroke
star.fill(102);
star.stroke(255);
// 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.end(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,26 @@
/**
* PolygonPShapeOOP.
*
* Wrapping a PShape inside a custom class
*/
// A Star object
Star s;
void setup() {
size(640, 360, P2D);
smooth();
// Make a new Star
s = new Star();
}
void draw() {
background(51);
// Display the star
s.display();
// Move the star
s.move();
}

View File

@@ -0,0 +1,49 @@
// 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;
Star() {
x = 0;
y = height/2;
// First create the shape
s = createShape();
// You can set fill and stroke
s.fill(102);
s.stroke(255);
// 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.end(CLOSE);
}
void move() {
// Demonstrating some simple motion
x++;
if (x > width) {
x = 0;
}
}
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,52 @@
/**
* 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, P3D);
smooth();
// Make a PShape
PShape star = createShape(POLYGON);
star.fill(0,127);
star.stroke(0);
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.end(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,29 @@
/**
* PrimitivePShape.
*
* Using a PShape to display a primitive shape (in this case, ellipse).
*/
// The PShape object
PShape circle;
void setup() {
size(640, 360, P3D);
smooth();
// Creating the PShape as an ellipse
// The corner is -50,-50 so that the center is at 0,0
circle = createShape(ELLIPSE,-50,-50,100,100);
}
void draw() {
background(51);
// We can dynamically set the stroke and fill of the shape
circle.stroke(255);
circle.fill(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,26 @@
/**
* SVGPShape
*
* How to load an SVG into a PShape
*/
// PShapoe object
PShape svg;
void setup() {
size(640, 360);
// Looks weird with P3D renderer?
// size(640, 360,P3D);
smooth();
// Load the SVG
svg = loadShape("star.svg");
}
void draw() {
background(255);
// Draw PShape at mouse location
translate(mouseX, mouseY);
shapeMode(CENTER);
shape(svg);
}

View File

@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://web.resource.org/cc/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="293.89999"
height="282.98001"
id="svg2"
sodipodi:version="0.32"
inkscape:version="0.45.1"
version="1.0"
sodipodi:docbase="/home/uwe/Desktop"
sodipodi:docname="star.svg"
inkscape:output_extension="org.inkscape.output.svg.inkscape">
<defs
id="defs4" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="2"
inkscape:cx="149.41261"
inkscape:cy="100"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showguides="false"
showgrid="true"
height="282.98px"
width="293.9px"
inkscape:showpageshadow="true"
borderlayer="false"
showborder="true"
inkscape:window-width="872"
inkscape:window-height="624"
inkscape:window-x="71"
inkscape:window-y="47" />
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Ebene 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-156.02455,-156.34725)">
<path
sodipodi:type="star"
style="fill:#000000;fill-opacity:1;stroke:#000000;stroke-width:12.73799992;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="path2160"
sodipodi:sides="5"
sodipodi:cx="197.14285"
sodipodi:cy="155.21933"
sodipodi:r1="50.377609"
sodipodi:r2="132.57266"
sodipodi:arg1="0.9229849"
sodipodi:arg2="1.5513034"
inkscape:flatsided="false"
inkscape:rounded="3.1225023e-17"
inkscape:randomized="0"
d="M 227.54285,195.39076 L 199.72692,287.76681 L 168.33167,196.5451 L 71.881232,198.63634 L 148.93656,140.58863 L 117.14285,49.505045 L 196.16091,104.85129 L 272.96175,46.467294 L 244.74227,138.72087 L 324.00151,193.72116 L 227.54285,195.39076 z "
transform="matrix(0.8199778,-0.5723954,0.5723954,0.8199778,53.582246,297.32687)" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

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

View File

@@ -0,0 +1,65 @@
// 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.fill(127);
s.stroke(0);
for (PVector v : original) {
s.vertex(v.x, v.y);
}
s.end(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, r.z);
// 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();
}
}