benfry / processing4

Processing 4.x releases for Java 17
https://processing.org
Other
1.31k stars 236 forks source link

Blendmode not respecting alpha in P3D under certain circumstances #843

Open RectangleWorld opened 1 week ago

RectangleWorld commented 1 week ago

Description

The additive blendmode does not respect alpha under certain circumstances.

When the following code is run, the two circles drawn have 100% alpha, but we should expect them to have 50% alpha (set by 0x80). There are workarounds, explained below.

void setup() {
  size(800, 800, P3D);
  generate();
}

/*
If the draw() function is removed, blending honors alpha, 
whether or not the last line of generate() is included.
*/
void draw() {
  //
}

void generate() {
  blendMode(BLEND);
  background(0xFF000000);
  noStroke();
  blendMode(ADD);
  fill(0x80FF0000);
  circle(300,300,400);
  fill(0x8000FF00);
  circle(500,500,400);
  // If this last line is omitted, blending doesn't honor alpha.
  //blendMode(BLEND);
}

Workarounds

Blending will honor alpha as expected if one of these things is done:

Environment