Skip to content

Blendmode not respecting alpha in P3D under certain circumstances #843

@RectangleWorld

Description

@RectangleWorld

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:

  • Omitting the draw() function
  • Making sure to set the blend mode back to BLEND at the end of the generate() function
  • Drawing into a PGraphics buffer and then drawing this buffer to the screen as in this code:
void generate() {
  PGraphics pg = createGraphics(width, height, P3D);
  pg.beginDraw();
  pg.blendMode(BLEND);
  pg.background(0xFF000000);
  pg.noStroke();
  pg.blendMode(ADD);
  pg.fill(0x80FF0000);
  pg.circle(300,300,400);
  pg.fill(0x8000FF00);
  pg.circle(500,500,400);
  pg.endDraw();
  image(pg, 0, 0);
}

Environment

  • Processing version: 4.3
  • Operating System and OS version: Windows 11, version 22H2
  • Graphics: NVIDIA GeForce RTX 3050 Ti Laptop GPU

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions