Skip to main content

Sphere

Live Editor

function Canvas() {
      const geo = sphere({ radius: 1 })
      const mat = rotate3dX(iDrag.y).mul(rotate3dY(iDrag.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} />
}
Result
Loading...

Sphere Props

radiusThe sphere radius. Default is 1.
widthSegmentsThe number of horizontal segments.
Minimum value is 3.
Default is 32.
heightSegmentsThe number of vertical segments.
Minimum value is 2.
Default is 16.
phiStartThe horizontal starting angle in radians.
Default is 0.
phiLengthThe horizontal sweep angle size.
Default is Math.PI*2.
thetaStartThe vertical starting angle in radians.
Default is 0.
thetaLengthThe vertical sweep angle size.
Default is Math.PI.

Spherical Coordinate Mathematics

The sphere buffer generates vertices using spherical coordinate transformations. Each point (x,y,z)(x,y,z) on the sphere surface follows the mathematical relationship:

x=rsin(θ)cos(ϕ)y=rcos(θ)z=rsin(θ)sin(ϕ)\begin{align} x &= r \sin(\theta) \cos(\phi) \\ y &= r \cos(\theta) \\ z &= r \sin(\theta) \sin(\phi) \end{align}

Where rr represents radius, θ\theta spans from thetaStart to thetaStart + thetaLength controlling vertical positioning, and ϕ\phi spans from phiStart to phiStart + phiLength controlling horizontal rotation.

The parametric equations enable precise control over sphere sections. Setting thetaLength to π/2\pi/2 generates hemispheres. Adjusting phiLength creates spherical wedges for architectural visualization or pie chart representations.

Segment Density Control

Triangle density follows the relationship T=2×widthSegments×heightSegmentsT = 2 \times \text{widthSegments} \times \text{heightSegments} for triangle count. Higher segment values produce smoother curvature but increase vertex buffer size.

The vertex grid structure creates (widthSegments+1)×(heightSegments+1)(widthSegments + 1) \times (heightSegments + 1) points. Each grid cell generates two triangles, except at polar regions where triangle fans prevent degenerate geometry.