I’m experimenting with depth peeling using three.js.
I’ve struggled a bit with achieving the correct blending. I have two examples:
The first one, uses 4 buffers and composes two layers in the fragment shader in its own pass. Here i was a bit confused about alpha multiplication, but i ended up getting the correct blending with the background. Ie. when i switch between the normal mode and depth peel mode, boxes blend the same with the background and opaque objects underneath.
https://raw.githack.com/pailhead/three.js/depth-peel-example/examples/webgl_materials_depthpeel.html
Left shows default transparency, to the right is my formula which i got off of wikipedia. I’m not sure what’s going on, but does looks slightly less correct on the left? It seems bright.
I got another version now where i’m trying to blend without the shader, switching up the dst/src and drawing each layer front to back in another offscreen buffer.
https://raw.githack.com/pailhead/three.js/depth-peel-stencil/examples/webgl_materials_depthpeel.html
This is the blending func for my full screen quad:
compositeMaterial.blendSrc = THREE.OneMinusDstAlphaFactor
compositeMaterial.blendDst = THREE.OneFactor
And i do this with premultipliedAlpha: false
in all the transparent materials.
gl_FragColor.xyz *= gl_FragColor.a;
Again, blending with the background looks the same, but the transparent layers different.
Quastions:
- Is there some blending “standard” that i can compare to, ie. given this scene, expect most engines to output this image.
- Is three’s blending correct? I can’t tell which one seems more plausible.
- What could i be doing wrong if this is wrong?
- What am i even looking at, is the transparency accumulating differently, and the brighter one is just more solid? Could also be that im adding a bit of black with each peel.