Skip to content

Drawing low-alpha graphics over an opaque background alters the resulting alpha in a PGraphics with P2D/P3D #2807

@RectangleWorld

Description

@RectangleWorld

A PGraphics object and the way it handles alpha is behaving differently in the default renderer versus the P2D/P3D renderers. Example code and screencaps below.

In the example code, a PGraphics object is created, filled with opaque red, then low-alpha black lines are drawn over this. One should expect the result to be a completely opaque image (because of the initial red fill), with gray lines over a red background. When this PGraphics object is shown on the screen with image(), one can see that the alpha of the PGraphics image has actually been altered by drawing these lines over the top of the red. In the default renderer, the PGraphics image is completely opaque as expected.

So I believe the problem is that (in P2D/P3d) drawing low-alpha graphics in a PGraphics object is altering the alpha channel when it should be blending.

PGraphics pg;

void setup() {
  size(200,200,P2D);
  pg = createGraphics(100,100,P2D);

  //drawing an opaque red background, then random low-alpha black lines over the top.
  pg.beginDraw();
  pg.fill(0xFFFF0000);
  pg.noStroke();
  pg.rect(0,0,100,100);
  pg.stroke(0x10000000);
  pg.strokeWeight(2);
  pg.noFill();
  for(int i = 0; i < 1000; i++) {
    pg.line(random(100),random(100),random(100),random(100));
  }
  pg.endDraw();

  background(0xFF0088FF);
  image(pg, 50, 50);
}

Here is the screencap for the above sketch - note how the blue background is showing through:

screencap_p2d

Here is the screencap for the sketch when the default renderer is used by omitting the P2D variable in both size() and createGraphics() - note the lines are now low-alpha black as expected:

screencap_default

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions