P2D transparency issue

I’ve run into a problem with transparency in the P2D renderer. When rendering an image in PG, the transparent pixels somehow blend into the background. I know I can use blendMode(REPLACE), but then the transparent pixels replace all the non-transparent pixels. Is there a way to render images with transparency so that they don’t blend into the background?

Here’s an example of the code:

PGraphics canvas;
PGraphics image;


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

void setup(){
  canvas = createGraphics(720,720,P2D);
  image = createGraphics(360,360);
  
  image.beginDraw();
  image.noStroke();
  image.fill(0,128);
  image.rect(0,0,360,360);
  image.fill(0);
  image.rect(90,90,180,180);
  image.endDraw();
  
  canvas.beginDraw();
  canvas.background(255,0,0,0); // red but transparent
  canvas.image(image,180,180);
  canvas.endDraw();
}


void draw(){
  background(255);
  image(canvas,0,0);
}

As you can see transparent pixels blends with red. I’ve set a red background just for clarity; by default, the background colour is set to (0,0,0,0), so the image blends into it.

Hello @Abhyudaya_Singh ,

This may be related:

:)

Hi, thanks for replay. I know this isn’t an issue in JAVA2D. But the thing is, it’s very slow, and I’m also using shaders. Is there really no way to fix this problem without using third-party libraries?

Hello,

I can’t comment beyond what my research has told me.

This is discussed in the old repository :

This is stated at the top of the issues:

You can always open an issue in the new repository:

I have experimented with modifying the source code:

I tweaked some of the blend modes based on what was discussed in the issue. This was some time back and I was exploring; I remember a subtle difference in one of the tweaks but not a solution to this issue.

:)

1 Like