PShape vs PGraphics

can PShape do everything that a PGraphics object can?
if so, would it be faster in a multi-object context?

for example

for PGraphics, i get 29 FPS without beginDraw and endDraw for each draw() mostly due to PGraphics cannot be translated and instead image translates for us, thus beginDraw and endDraw are not required

however if i do use beginDraw and endDray then i get 7 FPS

PGraphics[] group;

void setup() {  
  size(200, 200, P3D);
  noStroke();
  group = new PGraphics[2500];
  for (int i = 1; i < 2500; i++) {
    group[i] = createGraphics(2, 2, P3D);
    group[i].beginDraw();
    group[i].background(255);
    group[i].endDraw();
  }
}

void draw() {
  background(0);
  for (int i = 1; i < 2500; i++) {
    image(group[i], random(width), random(height));
  }
  if(frameCount % 60 == 0) {
    println(frameRate);
  }
}

for PShape i get 60 FPS

PShape group;

void setup() {  
  size(200, 200, P3D);
  noStroke();
  group = createShape(GROUP);
  for (int i = 0; i < 2500; i++) {
    PShape s = createShape(BOX, 2);
    s.translate(random(width), random(height));
    group.addChild(s);
  }
}
void draw() {
  background(0);
  PShape[] shapes = group.getChildren(); 
  for (int i = 0; i < shapes.length; i++) {
    shapes[i].translate(random(-1, 1), random(-1, 1));
  }
  shape(group);
  if(frameCount % 60 == 0) {
    println(frameRate);
  }
}

nvm it cant, for

class Applications_Cube extends Window {

  @Override
    void draw() {
    graphics.lights();
    graphics.background(0);
    graphics.noStroke();
    graphics.translate(width/2, height/2);
    graphics.rotateX(frameCount/100.0);
    graphics.rotateY(frameCount/200.0);
    graphics.box(40);
  }
}

and

void draw() {
  background(0);
  PShape[] shapes = group.getChildren(); 
  for (int i = 0; i < shapes.length; i++) {
    shapes[i].background(0);
    shapes[i].noStroke();
    shapes[i].translate(width/2, height/2);
    shapes[i].rotateX(frameCount/100.0);
    shapes[i].rotateY(frameCount/200.0);
    shapes[i].box(40);
  }
  shape(group);
  if(frameCount % 60 == 0) {
    println(frameRate);
  }
}

background(int) does not exist, and box(int) does not exist

2 Likes

No, not at all. PShape is completely separate from PImage / PGraphics.

  • PGraphics is primarily a set of draw methods on pixel buffer – the methods depend on the renderer. It extends PImage, which is that base pixel buffer.
  • PShape is primarily a container for vertex data, which must then be rendered to a PGraphics using e.g. PGraphics.shape().

PShapes can be created on the fly or loaded from OBJ or SVG. PShape is also mostly a write-only class in practice – it lack significant features for reliable manipulation of its existing contents, and it is often easier / more reliable to edit a PShape by keepings it contents in data, edit the data, and create a completely new PShape each time it changes rather than attempt to change the existing one.

For these kinds of questions consider looking at the reference, then clicking through to the JavaDoc and looking up the class.

… and

Note that the JavaDoc also has separate PGraphics subclasses per renderer – so: PGraphics2D, PGraphics3D, PGraphicsFX2D, PGraphicsJava2D.

5 Likes