Box
function Canvas() { const geo = box({ width: 1, height: 1, depth: 1 }) const mat = rotate3dX(iMouse.y.negate()).mul(rotate3dY(iMouse.x)) const gl = useGL({ isWebGL: true, isDepth: true, count: geo.count, vertex: vec4(mat.mul(geo.vertex), 1), fragment: vec4(varying(geo.normal), 1), }) return <canvas ref={gl.ref} /> }
Box Props
| width | The width dimension parallel to X-axis. Default is 1. |
|---|---|
| height | The height dimension parallel to Y-axis. Default is 1. |
| depth | The depth dimension parallel to Z-axis. Default is 1. |
| widthSegments | Subdivision count along width. Default is 1. |
| heightSegments | Subdivision count along height. Default is 1. |
| depthSegments | Subdivision count along depth. Default is 1. |
Cartesian Cuboid Generation
Box function generates a rectangular parallelepiped centered at the origin. Each face consists of two triangles forming a quadrilateral surface. The parametric representation follows the mathematical definition of a rectangular prism with dimensions where vertices are positioned at coordinates:
The face normal vectors are axis-aligned unit vectors corresponding to each orthogonal face direction. This generates six planar surfaces with consistent winding order for proper backface culling.
Subdivision Mathematics
Segment parameters control tessellation density on each axis. For width segments , height segments , and depth segments , the total vertex count becomes:
Each face subdivides into a regular grid where segment boundaries create additional vertices for increased geometric detail. The subdivision creates uniform spacing across face dimensions, enabling consistent level-of-detail control.
Face Construction Algorithm
Six faces are constructed using a plane-building function that maps 2D grid coordinates to 3D space. Each plane is defined by three orthogonal vectors where and span the face surface and defines the normal direction. The mapping transforms grid coordinates to world space:
where and are directional multipliers and face dimensions determine scaling factors.
Geometric Applications
Box geometry serves as fundamental building blocks for architectural modeling, voxel-based systems, and spatial partitioning algorithms. The uniform subdivision enables procedural detail addition through displacement mapping or fractal subdivision. Multiple boxes can be combined through boolean operations to create complex shapes while maintaining computational simplicity.
The axis-aligned nature makes box geometry computationally efficient for collision detection, spatial indexing, and level-of-detail systems. Texture coordinate generation follows standard planar projection with consistent UV mapping across all faces.