Skip to content

Documentation / @ripl/3d

@ripl/3d ​

Experimental 3D rendering for Ripl — a unified API for 2D graphics rendering in the browser.

Note: This package is experimental and its API may change between releases.

Installation ​

bash
npm install @ripl/3d

Overview ​

Extends Ripl's rendering pipeline with 3D capabilities. Projects 3D geometry onto Ripl's 2D canvas context using a virtual camera, depth sorting, and flat shading.

Features ​

  • 3D math — Vectors, matrices, and transformations
  • Camera — Configurable virtual camera with projection
  • Shading — Flat shading with directional lighting
  • Depth sorting — Automatic painter's algorithm for correct draw order
  • 3D shapes — Built-in 3D shape primitives
  • Interpolation — 3D-aware interpolators for smooth animation

Usage ​

typescript
import {
    createContext,
} from '@ripl/web';
import {
    createCamera, createCube,
} from '@ripl/3d';

const context = createContext('.mount-element');
const camera = createCamera({ /* options */ });
const cube = createCube({ /* options */ });

Documentation ​

Full documentation is available at ripl.rocks.

License ​

MIT

Classes ​

ClassDescription
CameraAn interactive camera controlling the 3D context's view and projection, with mouse/touch orbit, pan, and zoom.
CanvasContext3DCanvas 2D–backed 3D rendering context with face buffer and painter's algorithm sorting.
ConeA 3D cone shape with configurable radius, height, and segment resolution.
Context3DBase 3D rendering context providing view/projection matrices, camera, lighting, and projection. Subclassed by CanvasContext3D and WebGPUContext3D.
CubeA 3D cube shape with uniform edge size.
CylinderA 3D cylinder shape with independent top and bottom radii for truncated cones.
PlaneA flat rectangular 3D plane oriented along the XY plane.
Shape3DBase class for 3D shapes, handling model transforms, face projection, shading, and hit testing.
SphereA 3D sphere shape tessellated with configurable segments and rings.
TorusA 3D torus (donut) shape with configurable major radius, tube radius, and tessellation.

Interfaces ​

InterfaceDescription
CameraInteractionConfigFine-grained configuration for a single camera interaction.
CameraInteractionsConfigures which camera interactions (zoom, pivot, pan) are enabled.
CameraOptionsOptions for constructing a camera, including position, projection type, and interaction config.
ConeStateState interface for a cone, defining radius, height, and segment count.
Context3DMetaTyped metadata for 3D contexts.
Context3DOptionsOptions for the 3D rendering context, extending the base context options with camera parameters.
CubeStateState interface for a cube, defining uniform edge size.
CylinderStateState interface for a cylinder, defining top/bottom radii, height, and segment count.
Face3DA single face of a 3D mesh, defined by its vertices and an optional precomputed normal.
MeshSubmissionA mesh submission queued for a single frame.
PlaneStateState interface for a plane, defining width and height.
ProjectedFace3DA projected face ready for 2D rendering with screen-space points, fill/stroke styles, and depth.
Shape3DStateState interface for a 3D shape, defining position and rotation around each axis.
SphereStateState interface for a sphere, defining radius, longitudinal segments, and latitudinal rings.
TorusStateState interface for a torus, defining major radius, tube radius, and segment counts.
ViewportViewport dimensions used for projection.

Type Aliases ​

Type AliasDescription
CameraInteractionOptionA camera interaction can be enabled/disabled with a boolean or configured with sensitivity.
LightModeDetermines whether the light direction is fixed in world space or follows the camera.
Matrix4A column-major 4×4 matrix stored as a 16-element Float64Array.
ProjectedPointA 2D screen-space point with a depth component for z-ordering.
RenderStrategyThe rendering strategy used by a 3D context.
Shape3DOptionsOptions for constructing a 3D shape, with all state properties optional.
Vector3A 3-component vector represented as a labeled tuple [x, y, z].

Variables ​

VariableDescription
interpolateVector3Interpolator factory for Vector3 values, using component-wise linear interpolation.
LIGHT_DIRECTIONPre-normalised light direction vectors for common light positions.

Functions ​

FunctionDescription
computeFaceBrightnessComputes a 0–1 brightness value for a face given its normal and a light direction.
computeFaceNormalComputes the surface normal of a face from its first three vertices via the cross product.
createCameraFactory function that creates a new Camera bound to a 3D scene.
createConeFactory function that creates a new Cone instance.
createContextCreates a Canvas 2D–backed 3D rendering context attached to the given DOM target.
createCubeFactory function that creates a new Cube instance.
createCylinderFactory function that creates a new Cylinder instance.
createPlaneFactory function that creates a new Plane instance.
createShape3DFactory function that creates a new Shape3D instance.
createSphereFactory function that creates a new Sphere instance.
createTorusFactory function that creates a new Torus instance.
elementIsConeType guard that checks whether a value is a Cone instance.
elementIsCubeType guard that checks whether a value is a Cube instance.
elementIsCylinderType guard that checks whether a value is a Cylinder instance.
elementIsPlaneType guard that checks whether a value is a Plane instance.
elementIsShape3DType guard that checks whether a value is a Shape3D instance.
elementIsSphereType guard that checks whether a value is a Sphere instance.
elementIsTorusType guard that checks whether a value is a Torus instance.
mat4CloneReturns a copy of the given matrix.
mat4CreateCreates a zeroed 4×4 matrix.
mat4IdentityCreates a 4×4 identity matrix.
mat4LookAtConstructs a view matrix looking from eye toward target with the given up direction.
mat4MultiplyMultiplies two 4×4 matrices.
mat4OrthographicConstructs an orthographic projection matrix.
mat4PerspectiveConstructs a perspective projection matrix.
mat4RotateXApplies a rotation around the X axis to a matrix.
mat4RotateYApplies a rotation around the Y axis to a matrix.
mat4RotateZApplies a rotation around the Z axis to a matrix.
mat4ScaleApplies a scale transform to a matrix.
mat4TransformDirectionTransforms a direction vector by the upper-3×3 of a 4×4 matrix, ignoring translation.
mat4TransformPointTransforms a 3D point by a 4×4 matrix, performing the perspective divide.
mat4TranslateApplies a translation to a matrix.
projectPointProjects a 3D world-space point onto 2D screen-space via a view-projection matrix and viewport.
shadeFaceColorShades a color by a brightness factor (0–1), darkening or lightening the RGB channels.
typeIsVector3Type guard that checks whether a value is a Vector3 tuple.
vec3AddReturns the component-wise sum of two vectors.
vec3CrossComputes the cross product of two vectors.
vec3DistanceReturns the Euclidean distance between two points.
vec3DotComputes the dot product of two vectors.
vec3LengthReturns the Euclidean length of a vector.
vec3LerpLinearly interpolates between two vectors by factor t.
vec3NegateNegates all components of a vector.
vec3NormalizeReturns the unit-length direction of a vector, or the zero vector if length is 0.
vec3ScaleScales a vector by a scalar.
vec3SubReturns the component-wise difference of two vectors.