Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL blending modes, creating additive transparency

I have a little shader that looks like this, and has a neat effect.

uniform float beatScale;
varying vec3 vPos;

void main() {

  vec3 normPos = (normalize(vPos) + vec3(1, 1, 1)) / vec3(2, 2, 2);
  float alpha;

  if (normPos.y < beatScale - 0.1) {
    alpha = 0.0;
  } else if (normPos.y < beatScale) {
    alpha = 1.0 - (beatScale - normPos.y) / 0.1;
  } else {
    alpha = 1.0;
  }

  gl_FragColor = vec4(normPos, alpha);    
}

But the transparent parts of my objects in front block the objects behind, knocking out pixels that should have been rendered.

My sorry attempt was:

gl_FragColor = gl_FragColor + vec4(normPos, alpha);

But that didn't seem to change it. I was hoping gl_FragColor had the previous pixel color, but no dice.

How can I add the computed color the color that was there before? I don't care about what's on top, this is all glowy stuff I can simply add together.

like image 810
Alex Wayne Avatar asked Feb 12 '26 03:02

Alex Wayne


1 Answers

If you're doing additive blending, then you should turn off depth writes.

like image 72
Nicol Bolas Avatar answered Feb 15 '26 12:02

Nicol Bolas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!