Weighted, Blended Order Independent Transparency (paper, blog) for use with three.js. This implementation is designed as a rendering Pass. It can be used as a stand-alone replacement for a traditional render pass (i.e. renderer.render
), or used as part of a larger rendering stack within Effect Composer.
- Stand-Alone
- Scene Composer
- Three Versions
There are several common techniques available for order independent transparency. This implementation uses Weighted, Blended Order-Indepent Transparency (WBOIT), both for it's high performance and also compatibility on slower hardware. This implementation is WebGL 1 compatible and mobile friendly.
One of the biggest advantages of order independent transparency is for the rendering of detailed transparent models. Typically when rendering such a model, it is common for some faces to be depth culled. When rendering with WBOIT, all faces will be visible. WBOIT is approximate, though, and while it provides good results it may not be appropriate for all use cases.
There are a variety of weight functions available when rendering with WBOIT. This is partially due to inconsistencies in rendering overlapping pixels at varying depths. Some weight functions are better at incorporating camera near / far planes, some are better at handling larger groups of overlapping triangles. This implementation includes a weight
modifier within MeshWboitMaterial
that attempts to adjust the weight function for both opacity and color depending on the depth of the fragments.
The biggest downside of this method is that due to the blending method used, as opacity
approaches 1.0
objects still retain an artifically high amount of transparency. WBOIT enabled materials can be made opaque during the render pass by setting the material property transparent
to false
.
- Option 1: Copy files from
src
directory into project, import from files...
import { MeshWboitMaterial } from './materials/MeshWboitMaterial.js';
import { WboitPass } from './WboitPass.js';
- Option 2: Install from npm, import from 'three-wboit'...
npm install three-wboit
import { MeshWboitMaterial, WboitPass } from 'three-wboit';
- Option 3: Import directly from CDN...
import { MeshWboitMaterial, WboitPass } from 'https://unpkg.com/three-wboit/build/index.module.js';
To setup your scene to use WBOIT, create an instance of WboitPass
.
When creating Mesh
objects intended to be transparent, use the included material MeshWboitMaterial
. Objects using this material have WBOIT enabled by default. WBOIT can be turned on / off on each object by setting the material's transparent
property. The material is functionaly equivalent to MeshBasicMaterial
, and supports all it's methods and properties.
When rendering your scene, instead of calling renderer.render()
, call wboitPass.render( renderer )
. Enjoy.
import * as THREE from 'three';
import { MeshWboitMaterial, WboitPass } from 'three-wboit';
const renderer = new THREE.WebGLRenderer( { preserveDrawingBuffer: true } );
const camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.10, 100 );
const scene = new THREE.Scene();
scene.add( new THREE.BoxGeometry(), new MeshWboitMaterial( { opacity: 0.5 } ) );
const wboitPass = new WboitPass( renderer, scene, camera, 0 /* optional clear color */, 1.0 /* optional clear alpha */);
...
render() {
// OLD:
// renderer.render( scene, camera );
// NEW:
wboitPass.render( renderer );
}
To use WboitPass
with any existing material, use the included utility function WboitUtils.patch()
import * as THREE from 'three';
import { WboitUtils } from 'three-wboit';
const material = new THREE.MeshStandardMaterial();
WboitUtils.patch( material );
const myMesh = new THREE.Mesh( new THREE.BoxGeometry(), material );
NOTE: Three.js has made some changes to the renderer from versions v148 through v152, including how it renders transparent objects and how it deals with color space. For versions prior to 152 it is recommended to use the version of this library v1.0.13
.