ComputeSDFGenerator
new ComputeSDFGenerator(options : Object)GPU-accelerated SDF (Signed Distance Field) generator using mesh BVH.
Overview Generates a 3D texture containing signed distances from a mesh surface. It uses a BVH (Bounding Volume Hierarchy) acceleration structure to efficiently query the closest point on the mesh for each voxel.
Features
- Fast Generation: Uses compute shaders to generate SDFs in parallel.
- BVH Acceleration: Leverages
three-mesh-bvhfor O(log N) distance queries. - Dynamic Updates: Can update the SDF if the mesh deforms (provided the BVH is updated).
Usage The generated 3D texture can be used for:
- Volume Rendering: Raymarching clouds, smoke, or fluids.
- Collision Detection: GPU-based particle collisions.
- VFX: Attracting/repelling particles from a surface.
Constructor Parameters
optionsoptionalObjectresolutionoptionalnumberSDF grid resolution (resolution^3 voxels). Higher is more detailed but slower.
Default is64.marginoptionalnumberExtra margin around mesh bounds to capture the field.
Default is0.2.thresholdoptionalnumberDistance threshold (bias) applied to the SDF.
Default is0.0.boundsoptionalTHREE.Box3Custom bounds for the SDF volume. Auto-computed from geometry if not provided.workgroupSizeoptionalTHREE.Vector3Compute shader workgroup size. Tweak for performance.
Default isVector3(4,4,4).
Example
import { GenerateMeshBVHWorker } from 'three-mesh-bvh/worker';
import { ComputeSDFGenerator } from '@three-blocks/core';
// 1. Generate BVH (async worker recommended)
const worker = new GenerateMeshBVHWorker();
const bvh = await worker.generate(geometry, { maxLeafTris: 1 });
// 2. Create SDF Generator
const sdfGen = new ComputeSDFGenerator({
resolution: 64,
margin: 0.2
});
// 3. Generate SDF Texture
await sdfGen.generate(mesh, bvh, renderer);
const sdfTexture = sdfGen.sdfTexture;Properties
# .resolution : number
# .margin : number
# .threshold : number
# .customBounds : THREE.Box3|null
# .workgroupSize : THREE.Vector3
# .sdfTexture :
Gets the generated SDF texture.
# .boundsMatrix :
Gets the bounds transformation matrix (local to world).
# .inverseBoundsMatrix :
Gets the inverse bounds matrix (world to local).
# .bounds :
Gets the computed bounding box (includes margin).
# .geometryBounds :
Gets the tight geometry bounding box (without margin). Useful for focused sampling in ComputeBVHSampler.
Methods
generate#
generate(geometry : THREE.BufferGeometry, bvh : *, renderer : THREE.WebGPURenderer) : Promise<THREE.Storage3DTexture>Generates SDF texture from mesh and BVH.
Parameters
geometryTHREE.BufferGeometrybvh*rendererTHREE.WebGPURendererReturns
Promise<THREE.Storage3DTexture> — Generated SDF textureupdate#
update(geometry : THREE.BufferGeometry, bvh : *, renderer : THREE.WebGPURenderer) : Promise<THREE.Storage3DTexture>Updates SDF texture with potentially modified mesh/BVH. More efficient than full regeneration if structure hasn't changed.
Parameters
geometryTHREE.BufferGeometrybvh*rendererTHREE.WebGPURendererReturns
Promise<THREE.Storage3DTexture> — Updated SDF texturedispose#
dispose()Disposes GPU resources.