3D scenes

Place 3D objects inside a Scene3D to give them a shared camera and depth buffer. Position the resulting layer in the canvas with the same layout and animation controls as a Box.

Loading Ebi…
let block = Cube(84pt, fill=Color("blue2"))
let ball = Sphere(radius=39pt, samples=28, smooth=true, fill=Color("orange2"))
let ring = Torus(radius=62pt, tube_radius=12pt, samples=28, smooth=false, fill=Color("green3"))
let center = [72pt, 0pt, 0pt]
mut spin = 0deg
mut heading = -75deg
sig camera = Camera3D(azimuth=heading, elevation=22deg, zoom=0.82, perspective=true)
<Scene3D camera={camera}>
    {Transform3D(block, translation=[-101pt, 0pt, 0pt], rotation=[0deg, spin, spin])}
    {Transform3D(ball, translation=center)}
    {Transform3D(ring, translation=center, rotation=[68deg + spin, 0deg, 0deg])}
</Scene3D>
<Animation flow="parallel">
    {animate(spin, to=360deg, duration=6s, ease="linear")}
    <Animation flow="sequence">
        {animate(heading, to=-105deg, duration=3s, ease="smootherstep")}
        {animate(heading, to=-75deg, duration=3s, ease="smootherstep")}
    </Animation>
</Animation>

Transform3D applies uniform scale, x/y/z rotation, then translation. Use a signal for any transform input. The camera accepts azimuth, elevation, roll, target, zoom, and focal_distance. Enable perspective for perspective projection.

Geometry

UseTo build
Cube(size), Prism([width, length, height])Box-shaped solids.
Sphere, Torus, Cylinder, ConeRound solids with a configurable radius and sampling resolution.
Line3D(start, end), Dot3D(point)A cylindrical segment or a spherical marker at a world point.
Arrow3D(start, end)A depth-tested arrow with a screen-facing head.
Mesh3D(points, triangles)A mesh with zero-based triangle indices. Each point is [x, y, z] in lengths.
SurfaceGrid(points, columns)A surface from rows of points, with a fixed number of columns.
Path3D, Scatter3D, WireframeScreen-width paths, markers, and mesh edges.

Solids and indexed meshes are shaded by default. Use smooth=true on Sphere, Torus, or Dot3D to interpolate lighting across the surface. A Mesh3D can supply one direction in normals for each vertex. The demo centers a smooth-shaded ball in the opening of a flat-shaded torus. The cube is also flat-shaded. Pass lit=false to keep the specified fill color. Use a color with alpha for translucency. WebGPU resolves opaque visibility per sample and composites translucent geometry using weighted blending.

Place plots and annotations in a scene

ThreeDAxes.frame(), ax.plot_surface(f), and ax.plot_parametric_curve(f) produce objects for the same Scene3D. Use ax.c2p([x, y, z]) to place solids at data coordinates. Plot shows a face-on view that tilts and gains depth. Flow fields shows the Lorenz attractor.

To make an upright label follow a point in the scene, use project3d(point, camera, depth=depth) for its 2D position. Place a label directly in the canvas to keep its position independent of the camera.