Skip to content

Make surfaces and the faces of solids lit meshes on both renderers - #5175

Open
behackl wants to merge 7 commits into
refactor/unified-mobjectsfrom
refactor/meshes
Open

behackl wants to merge 7 commits into
refactor/unified-mobjectsfrom
refactor/meshes

Conversation

@behackl

@behackl behackl commented Nov 23, 2026

Copy link
Copy Markdown
Member

Stacked on #5174.

Overview

Surface was a VGroup with one VMobject per face, so a sphere was hundreds of paths that the renderer sorted and shaded one by one. It is now a ParametricSurface mesh (from #5174). Sphere, Dot3D, Torus, Cone, Cylinder, Line3D, Arrow3D and plot_surface build on it as before. The faces of Cube, Prism and the polyhedra become meshes too.

The Cairo renderer learns to draw meshes: one polygon per face, sorted far to near and lit per face. OpenGL draws them with a depth test and per-pixel lighting.

Breaking changes

  • A surface is one mobject. surface[i], iterating over its faces and list_of_faces are gone. Colour faces with set_fill_by_checkerboard, set_fill_by_value or set_vertex_colors.
  • Surface loses the arguments should_make_jagged, surface_piece_config and pre_function_handle_to_anchor_scale_factor.
  • The faces of Cube, Prism and polyhedra are PolygonFace meshes. cube[i] and polyhedron.faces[i] still work, as do set_fill, set_stroke, set_color and set_opacity. Path methods such as get_vertices or point_from_proportion are gone; a face's corners are its points.
  • The caps of Cylinder and the base of Cone are disc meshes (they were Circle paths).
  • Transforms between meshes need the same triangles: surfaces of the same resolution and kind, faces with as many corners. A mesh and a path cannot be transformed into each other; both raise a ValueError that says so.

Visible changes

  • Surfaces and solids are lit by their material under a ThreeDCamera: faces turned away from the light are darker than before, and surfaces have highlights. material=Unlit() shows the plain colours.
  • The sides of faces are straight lines between the sampled points (they were slightly curved).
  • Create shows a face of a solid at once instead of drawing its outline.

Control data is regenerated for 19 Cairo tests of 3D primitives, surface plots and polyhedra.

New

  • MeshMobject has the styling methods groups call (set_fill, set_stroke, set_style, ...) and face outlines (stroke_color, stroke_width), so meshes can sit in a VGroup.
  • PolygonFace; Cube(material=...) and faces_config={"material": ...}.

Docs

  • Upgrading guide: new sections "Surfaces are meshes" and "The faces of solids are meshes".

For reviewers

6 commits. The last two speed up bounding boxes, which every positioning method and the depth sort use. Also fixes a flaky ConvexHull3D test (face order depended on the hash seed).

# A fan of triangles from the first corner (the polygon is convex).
triangles = np.array([[0, i, i + 1] for i in range(1, count - 1)])
super().__init__(points=corners_array, triangles=triangles, **kwargs)
self.faces = np.arange(count)[None, :]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

False positive: faces is a versioned field (a data descriptor defined on MeshMobject), and this line assigns the instance's value through it, after MeshMobject.__init__ set the empty default. A polygon face has one face, its corners in order.

The Cairo renderer raised CapabilityError for MeshMobject and
ParametricSurface. It now draws a mesh as one filled polygon per face:
the faces become path snapshots sorted far to near, and under a
ThreeDCamera they are sorted with the other 3D paths of the scene.

- Faces: MeshMobject.faces holds the polygons a mesh is made of (quads
  for ParametricSurface, in order around each); empty means its
  triangles. Each face is colored by its corners' mean color or by the
  texture at its middle (renderer/_mesh.py, shared with OpenGL).
- Lighting: each face is lit at its middle by renderer/_lighting.shade,
  the function the OpenGL mesh shader computes per pixel, with the
  camera's light and eye (Camera._light, _eye, _view_rotation); not lit
  under a flat camera or when fixed in frame.
- Outlines: stroke colour and width for the faces (new fields, none by
  default). Without them, opaque faces get a one-pixel outline in their
  own colour, which closes the seams where antialiased edges of
  neighbouring faces meet.
- Keys: a face node's key is (mesh key, face index), or None if the
  mesh has none, so the path cache never reuses a face across a change
  of the mesh, its material included.

The mesh drawing tests run on both renderers now (translucency stays an
OpenGL test: Cairo shows the back faces of translucent meshes), and
Cairo's faces are compared with the lighting function.

Interpolating between meshes with the same outline colours and width
keeps their versions (equal widths are not mixed: that can round them).
A mesh with outlines (stroke_width > 0) is drawn by the OpenGL renderer
with a vertex per corner of each face, so that its fragment shader can
outline the faces: at the distance from the face's sides (quad or
barycentric coordinates per corner), as wide as a stroke on the Cairo
renderer, half inside each face; so the outlines take part in the depth
test and are lit with the face. Meshes without outlines keep shared
vertices and smooth lighting.

The faces come from renderer/_mesh.py, as on Cairo; the OpenGL package
does not import the Cairo package. A test checks that outlines land on
the same pixels as Cairo's strokes.
Surface was a VGroup of one VMobject per face, drawn as thousands of
paths; ParametricSurface a mesh. Surface is now a ParametricSurface with
its former API: faces in a checkerboard (checkerboard_colors,
set_fill_by_checkerboard), outlined (stroke_color, stroke_width), and
set_fill_by_value colouring each face, and its outline, by the value
halfway around it, with pivots as before. Sphere, Dot3D, Torus, Cone,
Cylinder, Line3D, Arrow3D and plot_surface build on it unchanged; on
OpenGL they are meshes with a depth test now. should_make_jagged,
surface_piece_config and pre_function_handle_to_anchor_scale_factor go;
color is the fill colour, as for other mobjects.

Surfaces are lit by their material, Pbr() by default, as meshes are:
under a ThreeDCamera, faces turned away from its light are darker than
the old shade_in_3d shading made them, and surfaces have highlights.
With a checkerboard (or after set_fill_by_value), every face has its
own corners and is lit flat; without, neighbouring faces share corners
and the surface is lit smoothly.

The end caps of Cylinder and the base of Cone are discs of the same
kind (ParametricSurface), with the surface's colour and material, so
they are lit as the side is; they were Circle paths with the old 3D
shading.

So that existing code keeps working with meshes in groups:
- Meshes may be submobjects of VMobjects and VGroups (the Dot3D
  vertices of polyhedra are in a Graph): Mobject._vmobject_like,
  true for VMobjects and MeshMobjects. VMobject methods that walk the
  family skip what only paths have (anchor modes, handles), and bounds
  use the points of meshes.
- MeshMobject has the styling methods groups call: set_fill,
  set_stroke (outline colours: one or one per face), set_style,
  fill_color/fill_opacity/stroke_color/stroke_opacity, and no-op
  set_sheen and color_using_background_image. Surface.set_color colours
  the outlines too, as for the faces before.
  set_color, set_fill, set_opacity, fade, set_stroke and set_style
  reach submobjects unless family=False, so a Cone's base, a
  Cylinder's caps and an Arrow3D's parts take the solid's colour.

ThreeDCamera sorts a whole mesh (on OpenGL; Cairo sorts its faces) by
its middle among the 3D paths, so the end caps of a Cylinder show.

ConvexHull3D took its faces, and their corners, from sets of points
whose iteration order depended on the hash seed, which made its test
flaky; they are sorted now, with corners in order around the outward
normal.

Control data regenerated deliberately (Cairo), the look approved
before: threed (Sphere, Dot3D, Cone, Cylinder, Line3D, Arrow3D, Torus,
SurfaceColorscale, Y_Direction), coordinate_system (plot_surface,
plot_surface_colorscale), and the polyhedra (their vertices are Dot3D
spheres, now lit).

The upgrading guide has a section on surfaces.
The faces of Cube, Prism and the polyhedra were Square and Polygon paths
shaded with shade_in_3d, the old 3D shading of vector mobjects. They are
now PolygonFace meshes (a flat polygon as a MeshMobject, its corners in
order around it), lit by a material like surfaces: Pbr() by default;
Cube(material=...) and faces_config={"material": ...} set another.

- Cube and Prism stay VGroups of six faces; Polyhedron keeps its faces
  in a VGroup and moves their corners with the graph's vertices.
- faces_config takes the styling arguments of the Polygon faces
  (fill_color, fill_opacity, stroke_color, stroke_width, stroke_opacity,
  color); polyhedron faces keep their outline, 4 wide in the face's
  colour, as the edges of the solid. shade_in_3d has no effect.

The faces are lit flat, and darker where they turn away from the light,
on both renderers; translucent solids show their back faces as before.
Control data regenerated deliberately (Cairo): threed Cube, CameraMove
and AmbientCameraMove. The polyhedra tests run in a Scene without a
light and keep their pixels.

The upgrading guide has a section on the faces of solids.
VMobject.get_points_defining_boundary built a Python list of every
anchor row of the family and converted it back to an array, and
get_critical_point then reduced it once per dimension. Both are now
array operations with the same results (and the same order of anchors).
get_critical_point keeps the per-dimension path for subclasses that
override get_extremum_along_dim.

Every positioning method goes through these (next_to, arrange, get_center,
...), as does the depth sorting of 3D faces and the axis shading of
ThreeDAxes, which asks for edge centers of whole axes in every frame.
_groups updated each group's pixel box with numpy calls for every path
that joined it, which cost more than the comparisons themselves.
Review of #5173, for the commits of this branch: the faces of a mesh
take its RNode.placement (they had z_index and the two fixed-placement
fields), and renderer/cairo/mesh_faces.py, a renderer internal, becomes
_mesh_faces.py.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants