three-impostor
v0.4.2
Published
Box and octahedral impostors for three.js WebGPU
Maintainers
Readme
three-impostor
Impostors for three.js WebGPURenderer.
An object is captured once into textures. It can then be drawn as a box of 12 triangles whose shader reconstructs the object from those textures and shades it through the usual three.js lighting. Each pixel writes its depth, so impostors intersect each other and the rest of the scene.
There are two layouts:
'box'captures six views and ray marches them. It gives true depth, looks the same from every angle and holds up at closer range.'octahedral'captures a grid of views from around the object and blends the three nearest. It's cheaper per pixel and suits objects further away.
npm install three-impostorimport * as THREE from 'three/webgpu';
import { ImpostorBaker, ImpostorNodeMaterial } from 'three-impostor';
const baker = new ImpostorBaker( renderer );
const near = await baker.bake( mesh );
const far = await baker.bake( mesh, { layout: 'octahedral' } );
const lod = new THREE.LOD();
lod.addLevel( mesh, 0 );
lod.addLevel( new THREE.Mesh( near.geometry, new ImpostorNodeMaterial( near ) ), 20 );
lod.addLevel( new THREE.Mesh( far.geometry, new ImpostorNodeMaterial( far ) ), 60 );Many copies work with InstancedMesh:
const forest = new THREE.InstancedMesh( far.geometry, new ImpostorNodeMaterial( far ), 5000 );API
new ImpostorBaker( renderer )
bake( object, options ) captures object in its local space and returns a promise for an
ImpostorData. The object's own position, rotation and scale are ignored, so give the impostor the
same transform as the object.
layout:'box'(default) or'octahedral'resolution: the size in pixels of each view (default256for box,128for octahedral)frames: octahedral only, the number of views along each side of the grid (default12)hemisphere: octahedral only, capture views from above only, for objects never seen from below (defaultfalse)maps: the material properties to capture besides colour and normals, any of'roughness','metalness','ao'and'emissive'(default all four). Leave out the ones you don't need to save memory;[]captures none.
ImpostorData
layout: the layout it was captured withbox: the box the object was captured in, in local spacegeometry: a box geometry to draw the impostor withmaps: the material properties that were captured.'emissive'is left out when nothing on the object is emissive.colorMap,normalMap,materialMap,emissiveMap,intervalMap,rangeMap,distanceMap: the captured textures.materialMapholds roughness, metalness and ambient occlusion andemissiveMapemissive light, when captured;intervalMap,rangeMapanddistanceMapare box only.dispose()
new ImpostorNodeMaterial( impostor, parameters )
A MeshStandardNodeMaterial, so color, roughness, metalness and the other standard parameters
apply. color tints the captured colour, and roughness and metalness scale the captured values like
they do a texture map. When a property wasn't captured, the material's own value is used.
steps: box only, the largest number of ray steps for a pixel (default128). A step either crosses empty space, up to 13 blocks of 8 texels at once, or moves a texel and a half through a block that may hold a surface.
Notes
- Requires three.js r186 or later. Works with both the WebGPU and WebGL 2 backends of
WebGPURenderer. - The capture records base colour and normals, plus whichever of roughness, metalness, ambient occlusion
and emissive light
mapsasks for. Roughness, metalness and ambient occlusion share one 8-bit atlas, so leaving out only some of them saves no memory; emissive light has its own. - Box: each axis keeps the first gap inside the object, so a part in front of another, like a leg in front of a body, stays separate. Deeper hollows that can't be seen from any of the six sides are filled in. Gaps are found from back faces, so they work best with closed meshes. Where a mesh is open, like a scan with no base, the object is taken to run on to the side of the box it opens towards.
- GPU memory at the default sizes: box about 8 MB, plus 1.5 MB for roughness, metalness and ambient occlusion and 3 MB for emissive light; octahedral about 38 MB, plus 9 MB and 19 MB.
- Octahedral: silhouettes are slightly softer.
- Impostors don't cast shadows yet.
