Skip to content

07 Mesh And 3D

Tagin T edited this page Nov 24, 2026 · 2 revisions

English | 中文

07 Meshes & 3D

Why meshes exist

The painter is a 2D pipeline that covers in submission order. For a self-intersecting surface, a rim crossing its own edge, or coplanar grid cells, sorting alone cannot decide per pixel what is in front. RTM's mesh path hands geometry to the GPU depth buffer:

mesh (depth write)
  ↓
painter (depth test, no write)
  ↓
text / images / 2D overlays

Two ways to store a MeshBatch

Explicit quads

batch = MeshBatch()
batch.append(quads, colors)
  • quads: (Q, 4, 3), world-space positions of the four corners;
  • colors: (Q, 4, 4), per-corner RGBA.

Unique vertices + indices

batch = MeshBatch()
batch.append_indexed(vertices, quads, colors)
  • vertices: (N, 3) unique vertices;
  • quads: (Q, 4) integer indices;
  • shared corners are projected once, then gathered per quad.

MeshBatch.from_surface(surface) bakes an ordinary Surface into an indexed batch; MeshMobject.from_surface(...) turns the mesh into a scene mobject, so shift/rotate/scale/.animate really move it.

Native indexed submission

The current native path submits 4 vertices + 6 uint32 indices per quad:

AddMeshIndexed(vertex_count, x,y,z,rgba, index_count, indices)

The old path submitted 6 expanded vertices; indexing removes duplicates and raises the capacity to:

  • MAX_MESH_VERTS = 524,288
  • indexed path: 131,072 quads
  • legacy six-vertex fallback: 87,381 quads

MeshGroup(*batches) / win.set_mesh([a, b]) merge independent batches into the frame's single native mesh; list order is draw order. It changes ownership and ordering, not total capacity.

Multiple meshes and the scene

  • win.set_mesh(batch): an explicit frame-level mesh that does not follow scene transforms.
  • MeshMobject: a scene mobject; sync collects every MeshMobject (including nested ones) and merges them with explicit batches into the frame's single mesh.
  • MeshGroup: a merge of several batches; each batch can still update its arrays independently.

Lit meshes

Two mutually exclusive paths:

CPU baking

MeshMobject.from_surface(surf, shade=True)
  • bakes ManimCE's two-sided shading into per-corner colours;
  • re-shades as geometry moves and the world light changes;
  • does not depend on win.set_mesh_light.

GPU lighting

MeshBatch.from_surface(surf, lit=True)
# or MeshBatch(lit=True).append(quads, colors)
win.set_mesh_light(ambient=0.3, diffuse=0.8, specular=0.25, shininess=24)
  • vertices carry normals: x,y,z,nx,ny,nz,r,g,b,a;
  • the fragment stage does Lambert + Blinn-Phong;
  • ambient=1, diffuse=0, specular=0 is pixel-identical to unlit output;
  • append_indexed(normals=...) accepts per-vertex smooth normals (N,3) or per-corner face normals (Q,4,3).

Painter depth

Text, math and images are always on top by default. To let a 3D primitive be occluded by meshes:

label.set_shade_in_3d(True)
img.set_shade_in_3d(True)

Its paint unit depth maps into the mesh's [0,1] depth range and takes part in depth testing; unmarked 2D content keeps depth 0 and the overlay semantics.

Automatic depth

With RTM_AUTO_DEPTH=1, depth_mesh.py tries to route Surface roots that are "opaque, flat-shaded, no visible stroke" into the depth channel; if any condition fails, the whole frame falls back to painter to avoid cross-object ordering mistakes.

Limits

  • quads only; use a degenerate quad for triangles, or go through from_surface.
  • no per-pixel transparency sorting; translucent coplanar faces still blend in submission order.
  • one native mesh per frame; append raises ValueError past capacity, and writing the public arrays directly truncates to whole quads with a single RuntimeWarning.
  • MeshGroup's per-batch ownership does not change the frame budget; the total is still capped at 131,072 quads.
  • lit=True and shade=True cannot be mixed on one mesh.

Key files

  • real_time_manim/mesh.py: MeshBatch, MeshGroup, from_surface, indexed/lit construction
  • real_time_manim/mesh_mobject.py: MeshMobject, scene transforms
  • real_time_manim/window/depth_mesh.py: RTM_AUTO_DEPTH
  • native/vulkan_draw.c, native/platform.c: AddMesh* and the lit pipeline

Clone this wiki locally