Three.js Order-Independent Transparency (Weighted Blended OIT)
Weighted blended order-independent transparency in Three.js (McGuire & Bavoil), with accumulation and revealage render targets composited over the opaque scene, compared with sorted alpha blending on intersecting glass.
Key topics: three.js oit, order independent transparency, weighted blended, alpha blending, transparency sorting
Game Over
What this code does
- Sorting Problem: three.js sorts transparent objects by centre, which fails for intersecting or nested surfaces.
- Accumulation: each fragment adds (color·α, α) × w with One/One blending, where w favours near, opaque fragments.
- Revealage: a second target multiplies (1 − α) per fragment, leaving the total coverage Π(1 − α).
- Composite: color = accum.rgb / accum.a, alpha = 1 − revealage, blended over the opaque image.
- Depth Pre-pass: opaque depth is drawn first into both targets so glass behind solid objects is rejected.
- WebGL2 Note: each target is a separate pass because per-attachment blend modes need an extension.
- Controls: technique, opacity, depth-weight scale and spin.
Related Demos
Continue Learning
JavaScript Implementation
// Weighted blended OIT (McGuire & Bavoil 2013)
// accumulation pass — blending One / One:
// float w = clamp(pow(min(1.0, a * 10.0) + 0.01, 3.0) * 1e8 * pow(1.0 - gl_FragCoord.z * 0.9, 3.0), 1e-2, 3e3);
// gl_FragColor = vec4(color * a, a) * w;
// revealage pass — blending Zero / OneMinusSrcColor, cleared to 1:
// gl_FragColor = vec4(a); // target ends up holding Π(1 − a)
// composite over the opaque image:
const composite = `
vec4 accum = texture2D(uAccum, vUv);
float reveal = texture2D(uReveal, vUv).r;
gl_FragColor = vec4(accum.rgb / max(accum.a, 1e-5), 1.0 - reveal); // blended with SrcAlpha / OneMinusSrcAlpha`