Three.js SDF Mesh Deformer
Deform meshes in Three.js with animated signed distance fields (sphere, rounded box or torus) in shrink-wrap dent, attract, repel and twist modes, with vertices colored by displacement.
Key topics: three.js mesh deformation, signed distance field, sdf, vertex displacement, sculpting, deformer
Game Over
What this code does
- Distance Field: each tool is an analytic SDF, giving signed distance to its surface from any point.
- Gradient: central differences of the SDF give the direction away from the tool surface.
- Dent: vertices inside the tool (d < 0) are pushed out to its surface, like pressing an object into clay.
- Attract / Repel: displacement scales with exp(−|d| / falloff), pulling vertices onto the tool or pushing them away.
- Twist: vertices rotate around the tool's axis, more strongly the closer they are.
- Controls: target mesh, tool, mode, strength, falloff, tool size, animation and tool visibility.
Related Demos
Continue Learning
JavaScript Implementation
// Push vertices out of a moving sphere SDF (shrink-wrap dent)
const geometry = new THREE.TorusKnotGeometry(0.8, 0.32, 300, 48)
const base = geometry.attributes.position.array.slice()
const mesh = new THREE.Mesh(geometry, new THREE.MeshStandardMaterial({ color: 0x60a5fa }))
scene.add(mesh)
const p = new THREE.Vector3()
function animate (time) {
requestAnimationFrame(animate)
const tool = new THREE.Vector3(Math.sin(time / 1600) * 1.1, 0, Math.cos(time / 1400) * 0.9)
const pos = geometry.attributes.position
for (let i = 0; i < pos.count; i++) {
p.fromArray(base, i * 3)
const offset = p.clone().sub(tool)
const d = offset.length() - 0.55 // sphere SDF
if (d < 0) p.addScaledVector(offset.normalize(), -d) // move to the surface
pos.setXYZ(i, p.x, p.y, p.z)
}
pos.needsUpdate = true
geometry.computeVertexNormals()
renderer.render(scene, camera)
}
animate(0)