How Constructive Solid Geometry (CSG) works in React
mainThe library provides a React abstraction for Constructive Solid Geometry using three-bvh-csg.
To use it, you wrap your operations inside a <Geometry /> component. The <Geometry /> component produces a regular THREE.BufferGeometry which must be paired with a <mesh /> or another object that consumes geometry (like physics rigid bodies).
Core Workflow:
- Define a Base: Every
<Geometry />must start with a<Base />component. This serves as the foundation for all subsequent operations. - Chain Operations: Add boolean operations as children of the
<Geometry />. Order matters as each operation is performed on the result of the previous one. - Transformations: All components (
Base,Addition,Subtraction, etc.) behave like regular meshes; they can be nested, grouped, and transformed using props likeposition,scale, androtation.
import { Geometry, Base, Addition, Subtraction } from '@react-three/csg'
function Cross() {
return (
<mesh>
<meshStandardMaterial />
<Geometry>
<Base scale={[2, 0.5, 0.5]}>
<boxGeometry />
</Base>
<Addition scale={[0.5, 2, 0.5]}>
<boxGeometry />
</Addition>
</Geometry>
</mesh>
)
}