processing4 icon indicating copy to clipboard operation
processing4 copied to clipboard

Drawing PGraphics && reading pixel data from PGraphics in P2D renderer throws visual error

Open mheesakkers opened this issue 4 years ago • 3 comments

Description

When you want to read pixel data from a PGraphics with the P2D renderer specified the println result turns 0 and when you want to draw the pgraphcs with image() it turns out empty. There are two ways to reproduce this behaviour with the .get() method and with loadPixels().

Reproduce

PGraphics pg;

void setup () {
  size(256, 256, P2D);
  pg = createGraphics(256, 256, P2D);
  pg.beginDraw();
  pg.fill(255, 0, 0);
  pg.rect(0, 0, 256, 256);
  pg.endDraw();
}

void draw() {
  background(0);
  
  image(pg, 0, 0);
  
  // Reproduce 1:
  // Uncomment below and the pgraphics image doesn't get drawn or is empty?
  // println(pg.get(width / 2, height / 2));
  
  // Reproduce 2:
  // Uncomment below and the pgraphics image doesn't get drawn or is empty?
  //pg.loadPixels();
  //pg.updatePixels();
}

Specs

  • Processing version: 4
  • Operating System and OS version: MacOS BigSur

Possible Causes / Solutions

When removing the P2D in the size() and createGraphics() call the problem doesn't appear.

mheesakkers avatar Sep 04 '21 08:09 mheesakkers

Yeah I figured this out as well, here you can see the PGraphics becomes black after 2 seconds when the loadPixels is called. Removing P2D from mask = createGraphics(250, 250, P2D); fixes this, but that is more a workaround instead of a fix.

PGraphics mask;

void settings() {
  size(250, 250, P2D);
}

void setup() {

  mask = createGraphics(250, 250, P2D); 
  mask.beginDraw();
  mask.background(0);
  mask.fill(255);
  mask.ellipse(125, 125, 120, 120);
  mask.endDraw();
  mask.loadPixels();
}

void draw() {
  background(0);
  if (frameCount == 120) {
    mask.loadPixels();
  }
  image(mask, 0, 0);
}

clankill3r avatar Feb 09 '22 13:02 clankill3r

I figured out that if you load the pixels before you use beginDraw that then loadPixels will work again.

mask = createGraphics(250, 250, P2D);
mask.loadPixels();
mask.beginDraw();

clankill3r avatar Feb 15 '22 10:02 clankill3r

@clankill3r THANK YOU! I've been banging my head against the wall for hours trying to troubleshoot this issue. This is a simple workaround for a weird bug! 😊

finerrecliner avatar Feb 14 '23 22:02 finerrecliner