Hi there. I’m having a large issue with performance loss using PGraphics.
The situation is: I’m importing an svg with 860 child Polygons. I need to do a color sampling for each and every child shape based on it’s centroid. The final file is of large dimension(2400,1920).
What i have now: when i get everything running outside the PGr i get over 45 frames/second. if i encapsulate it in a PGr i get 9 and it starts to decrease the frameRate. So actualy i get no performance enhancement for offscreen buffer.
P.S. I have 10 images for colorsampling, the images are full scale(2400,1920). in the next phase it will be a video, a shader, or another interaction.
where did i get it worng?
here’s the code. PGr operations are commented
import java.util.Map;
ArrayList<Brick> bricks;
HashMap<Brick, Integer> colorMap;
PShape original;
PGraphics pg;
PImage[] images=new PImage[10];
int imageIndex=6;
PImage colorSampler;
int w=2400;
int h=1920;
void setup() {
size(1200, 960, P2D);
smooth(8);
frameRate(30);
pg=createGraphics(w, h);
colorSampler=createImage(w, h, RGB);
loadImages();
colorSampler=images[imageIndex];
bricks=new ArrayList<Brick>();
buildBrick();
colorMap=new HashMap<Brick, Integer>();
buildColorMap();
getColor();
}
void draw() {
image(colorSampler, 0, 0);
//pg.beginDraw();
//pg.clear();
getColor();
for (Brick b:bricks) {
b.update();
b.display();
}
//pg.endDraw();
fill(255, 0, 0, 100);
text(frameRate, 10, 10);
}
void buildBrick() {
original=loadShape("tijolos2.svg");
original.disableStyle();
int id=0;
int numChild=original.getChildCount();
println(numChild);
for (int i=0; i<numChild; i++) {
PShape tempFace=original.getChild(i);
int numBricks=tempFace.getChildCount();
for (int j=0; j<numBricks; j++) {
PShape temp=tempFace.getChild(j);
bricks.add(new Brick(id));
int vCount=temp.getVertexCount();
for (int k=0; k<vCount; k++) {
Brick b=bricks.get(bricks.size()-1);
PVector pv=temp.getVertex(k);
b.addPoint(pv);
}
Brick b=bricks.get(bricks.size()-1);
b.getCentroid();
b.buildShape();
id++;
}
}
}
void buildColorMap() {
for (Brick b : bricks) {
colorMap.put(b, b.c);
}
}
void getColor() {
colorSampler.loadPixels();
for (Brick b : bricks) {
color c=color(red(colorSampler.pixels[b.loc]), green(colorSampler.pixels[b.loc]), blue(colorSampler.pixels[b.loc]));
colorMap.put(b, c);
}
}
void loadImages() {
for (int loadCounter=1; loadCounter<=10; loadCounter++) {
images[loadCounter-1]=loadImage("sample0"+loadCounter+".jpg");
}
}
void keyPressed() {
if (key==CODED) {
if (keyCode==RIGHT) {
if (imageIndex<images.length-1) {
imageIndex++;
} else {
imageIndex=0;
}
println("RIGHT: "+imageIndex);
colorSampler=images[imageIndex];
}
if (keyCode==LEFT) {
if (imageIndex>0) {
imageIndex--;
} else {
imageIndex=images.length-1;
}
println("LEFT: "+imageIndex);
colorSampler=images[imageIndex];
}
}
}
Brick object
class Brick {
PShape s, centroid;
ArrayList<PVector> vertices;
PVector center;
int centerX, centerY;
int id;
color c;
int loc;
Brick(int _id) {
s=createShape();
vertices=new ArrayList<PVector>();
center=new PVector();
id=_id;
c=color(0);
}
void addPoint(PVector p) {
vertices.add(p);
}
void getCentroid() {
for (PVector p : vertices) {
centerX+=p.x;
centerY+=p.y;
}
centerX/=vertices.size();
centerY/=vertices.size();
loc=int(centerX+centerY*colorSampler.width);
}
void buildShape() {
s.beginShape();
s.fill(c, 120);
s.strokeWeight(1);
s.stroke(255);
for (PVector p : vertices) {
s.vertex(p.x, p.y);
}
s.endShape(CLOSE);
centroid=createShape(ELLIPSE, centerX, centerY, 8, 8);
}
void update() {
c=colorMap.get(this);
}
void display() {
/*
pg.beginDraw();
pg.strokeWeight(1);
s.setFill(color(c, 150));
pg.shape(s);
pg.strokeWeight(4);
centroid.setStroke(color(255));
centroid.setFill(color(c));
pg.shape(centroid);
pg.endDraw();
*/
strokeWeight(1);
s.setFill(color(c, 150));
shape(s);
strokeWeight(4);
centroid.setStroke(color(255));
centroid.setFill(color(c));
shape(centroid);
}
}