A comprehensive Three.js library for tetrahedralizing geometries and running real-time softbody simulations using WebGPU compute shaders.
- Tetrahedralization: Convert any Three.js geometry into a tetrahedral mesh using Delaunay tetrahedralization (Bowyer-Watson algorithm)
- Real-time Softbody Physics: GPU-accelerated FEM (Finite Element Method) physics simulation
- WebGPU Compute Shaders: Massively parallel physics computation for high performance
- SDF Colliders: Sphere, box, capsule, plane, and mesh colliders
- Interactive Controls: Mouse dragging and vertex anchoring
- Debug Visualization: Strain visualization and tetrahedral mesh inspection
- Geometry Generators: Pre-built tube, sphere, box, torus, cylinder, and cone generators
npm install tetrament three three-mesh-bvh
- Three.js 0.182.0 or higher (with WebGPU support)
- three-mesh-bvh 0.7.0 or higher
- WebGPU-enabled browser (Chrome 113+, Edge 113+)
import * as THREE from 'three';
import { tetrahedralize, TetVisualizer } from 'tetrament';
// Create a geometry
const geometry = new THREE.SphereGeometry(1, 16, 12);
// Tetrahedralize it
const result = tetrahedralize(geometry, {
resolution: 10, // Interior sampling resolution
minQuality: 0.001 // Minimum tet quality
});
console.log(`Created ${result.tetCount} tetrahedra`);
// Visualize the result
const visualizer = new TetVisualizer();
const tetMesh = visualizer.createVisualization(result.tetVerts, result.tetIds, {
showWireframe: true,
showFaces: true,
colorByQuality: true
});
scene.add(tetMesh);
import * as THREE from 'three/webgpu';
import {
SoftbodySimulation,
generateTube,
PlaneCollider,
DragControl
} from 'tetrament';
// Create WebGPU renderer
const renderer = new THREE.WebGPURenderer();
await renderer.init();
// Create simulation
const simulation = new SoftbodySimulation(renderer, {
stepsPerSecond: 180,
gravity: new THREE.Vector3(0, -19.62, 0)
});
// Add ground collider
simulation.addCollider(PlaneCollider(new THREE.Vector3(0, 1, 0), 0));
// Generate a tube model
const tubeModel = generateTube(15, { radius: 0.1 });
// Add geometry and create instances
const geometry = simulation.addGeometry(tubeModel);
const instance = simulation.addInstance(geometry);
// Add to scene
scene.add(simulation.object);
// Initialize simulation
await simulation.bake();
// Spawn the softbody
await instance.spawn(
new THREE.Vector3(0, 3, 0),
new THREE.Quaternion(),
new THREE.Vector3(1, 1, 1)
);
// Enable mouse interaction
const dragControl = new DragControl(simulation, camera, renderer.domElement);
// Animation loop
function animate() {
requestAnimationFrame(animate);
simulation.update(deltaTime);
renderer.render(scene, camera);
}
tetrahedralize(geometry, options)- Tetrahedralize a BufferGeometrytetrahedralizePoints(points, options)- Tetrahedralize a point cloudTetrahedralizerclass - Full control over tetrahedralization
SoftbodySimulation- Main physics simulationSoftbodyGeometry- Manages softbody renderingSoftbodyInstance- Individual softbody instance
PlaneCollider- Infinite planeSphereCollider- Sphere (static)BoxCollider- Axis-aligned boxCapsuleCollider- CapsuleMeshCollider- Complex mesh shapes (CPU-based)
generateTube(segments, options)- Tube/capsule shapesgenerateSphere(radius, options)- SpheresgenerateIcosphere(radius, options)- Icosphere variantgenerateBox(w, h, d, options)- BoxesgenerateTorus(options)- Torus shapesgenerateTorusKnot(options)- Torus knotsgenerateCylinder(options)- CylindersgenerateCone(options)- Cones
processGeometry(geometry)- Process BufferGeometry for softbody useprocessTetGeometry(tetVerts, tetIds, geometry)- Process pre-tetrahedralized geometryloadModelFromMsh(mshData)- Load from MSH formatloadModelFromGeometry(geometry, options)- Load from BufferGeometry with tetrahedralization
DragControl- Mouse interactionAnchorControl- Pin vertices to transforms
TetVisualizer- Visualize tetrahedral meshStrainVisualizer- Show compression/tension
/examples/tetrahedralization/- Interactive tetrahedralization demo/examples/softbody-basic/- Basic softbody simulation
# Install dependencies
npm install
# Build library
npm run build
# Build with watch mode
npm run dev
The softbody simulation is based on the WebGL implementation in TetSim. Reimplemented in three.js TSL with collision detection by holtsetio.
Tetrahedralization algorithm based on Ten Minute Physics by Matthias Mueller.
MIT