Documentation / @ripl/webgpu
@ripl/webgpu ​
A WebGPU backend for Ripl 3D: the same
Shape3Delements as@ripl/3d, rasterized on the GPU instead of face-sorted on a 2D canvas.
Features ​
- Hardware depth testing — a real depth buffer replaces the painter's algorithm, so intersecting and self-occluding geometry resolves per fragment rather than per face.
- WGSL shaders — the fragment shader mirrors
@ripl/3d'sshadeSurfaceterm for term (ambient, hemisphere, directional, point and spot lights, Blinn-Phong specular, emissive, fog), so a scene shades identically on either backend. The uniform structs are generated from the same descriptor the CPU-side packer writes, so the two cannot disagree about the bytes. - 4× MSAA by default, configurable through
sampleCount. - Its own texture path —
TextureManageruploadsTextureimages to the GPU and caches their bind groups, mapping Ripl's wrap modes toclamp-to-edge/repeat/mirror-repeatand its filters tonearest/linear. Untextured meshes bind a 1×1 white fallback, which keeps the whole backend on a single pipeline rather than one permutation per material. - Drop-in swap — shapes, camera, lights, materials, fog and raycasting all come from
@ripl/3dunchanged; only thecreateContextimport differs. - Configurable clear colour — a straight (non-premultiplied) RGBA
clearColor, transparent by default.
Installation ​
bash
# npm
npm install @ripl/webgpu @ripl/3d @ripl/web
# yarn
yarn add @ripl/webgpu @ripl/3d @ripl/web
# pnpm
pnpm add @ripl/webgpu @ripl/3d @ripl/webWebGPU needs a browser that supports it (Chrome 113+, Edge 113+, Firefox Nightly). The Canvas Context3D from @ripl/3d stays available as a fallback and takes the same scene.
Quick start ​
typescript
import {
createContext,
} from '@ripl/webgpu';
import {
createCamera,
createCube,
createDirectionalLight,
createSphere,
} from '@ripl/3d';
import {
createRenderer,
createScene,
} from '@ripl/web';
const context = await createContext('.mount-element');
const scene = createScene(context);
createCamera(context, {
position: [0, 1.5, 5],
target: [0, 0, 0],
interactions: true,
});
context.lights.add(createDirectionalLight({
direction: [-1, -1, -0.5],
intensity: 0.8,
}));
scene.add(createCube({
size: 1,
x: -1.2,
fill: '#3a86ff',
}));
scene.add(createSphere({
radius: 0.6,
x: 1.2,
fill: '#ff006e',
}));
createRenderer(scene, {
autoStop: false,
});createContext is async — it negotiates a GPU adapter and device before returning.
Key API ​
| Export | What it does |
|---|---|
createContext | Async factory returning a WebGPUContext3D |
WebGPUContext3D | The context itself, a Context3D subclass |
requestDevice | Adapter and device negotiation, for reuse or capability checks |
createPipeline | The render pipeline, bind group layouts and vertex buffer layout |
VERTEX_SHADER / FRAGMENT_SHADER | The WGSL source, if you need to read or extend it |
Related packages ​
@ripl/3d— the shapes, camera, lights, materials and textures this backend draws@ripl/web— the scene, renderer and animation@ripl/core— the element and context model underneath both
Documentation ​
Guides, a live demo and the full API reference are at ripl.run/docs/3d/contexts/webgpu.
License ​
Classes ​
| Class | Description |
|---|---|
| GeometryManager | Manages GPU buffer allocation and per-frame mesh accumulation. |
| TextureManager | Uploads Texture images to the GPU and caches the bind groups built from them. |
| WebGPUContext3D | WebGPU-backed 3D rendering context with hardware depth testing and WGSL shaders. |
Interfaces ​
| Interface | Description |
|---|---|
| DrawCommand | A single draw call within a flush result. |
| FlushResult | Result of flushing all queued meshes: buffers and per-mesh draw commands. |
| PipelineOptions | Options for creating the render pipeline. |
| PipelineState | Holds all GPU pipeline objects and layouts needed for rendering. |
| WebGPUContextOptions | Options for constructing a WebGPU 3D context. |
Variables ​
| Variable | Description |
|---|---|
| FRAGMENT_SHADER | WGSL fragment shader resolving the shared lighting model across every light in the scene. |
| MODEL_BIND_GROUP_LAYOUT_ENTRIES | Bind group layout entries for the per-model uniforms (group 1). |
| MODEL_UNIFORM_SIZE | Size in bytes of the model uniform buffer, derived from the layout descriptor in @ripl/3d. |
| SCENE_BIND_GROUP_LAYOUT_ENTRIES | Bind group layout entries for the scene-level uniforms (group 0). |
| SCENE_UNIFORM_SIZE | Size in bytes of the scene uniform buffer, derived from the layout descriptor in @ripl/3d. |
| TEXTURE_BIND_GROUP_LAYOUT_ENTRIES | Bind group layout entries for the material texture and its sampler (group 2). |
| VERTEX_BUFFER_LAYOUT | Vertex buffer layout describing position, normal, and color attributes. |
| VERTEX_SHADER | WGSL vertex shader for 3D mesh rendering with per-vertex color and normal. |
| VERTEX_STRIDE | Byte stride for a single vertex, derived from the interleaved layout in @ripl/3d. |
Functions ​
| Function | Description |
|---|---|
| createContext | Creates a WebGPU 3D rendering context attached to the given DOM target. |
| createPipeline | Creates the render pipeline and associated layouts on the given device. |
| requestDevice | Requests a WebGPU adapter and device, throwing if unsupported. |