Capsule
function Canvas() { const geo = capsule({ radius: 0.5, height: 1, capSegments: 8, radialSegments: 16 }) 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} /> }
Capsule Props
| radius | The capsule radius. Default is 1. |
|---|---|
| height | Height of the middle cylindrical section. Default is 1. |
| capSegments | Number of curve segments for each hemispherical cap. Minimum value is 1.Default is 4. |
| radialSegments | Number of segmented faces around the circumference. Minimum value is 3.Default is 8. |
| heightSegments | Number of rows of faces along the cylindrical height. Minimum value is 1.Default is 1. |
Capsule Mathematical Foundation
A capsule represents a compound geometric primitive consisting of three parametrically defined sections. The mathematical foundation follows cylindrical coordinates with hemispherical terminations.
For the bottom hemisphere section where :
The cylindrical midsection maintains constant radius while varying height linearly:
The top hemisphere section where :
Surface Normal Computation
The normal vector calculation differs across the three regions. For hemispherical caps, normals point radially outward from the sphere centers. The cylindrical section maintains horizontal radial normals with zero vertical component.
Normal vectors are computed per vertex and normalized to unit length:
where varies by geometric section.
Parametric Surface Segmentation
The capsule generation algorithm divides the surface into discrete segments based on the input parameters. Total vertical segments equal:
Arc length parameterization ensures uniform texture coordinate distribution across the compound surface. The total arc length encompasses both hemispherical caps and the cylindrical section:
This parameterization maintains proportional spacing between vertices regardless of the height-to-radius ratio.