Skip to content

Geometry

github-actions[bot] edited this page Sep 15, 2026 · 25 revisions

Geometry

core/buffer_geometry.mojo, core/buffer_attribute.mojo, core/geometry_store.mojo and geometries/. A BufferGeometry holds named vertex attributes and an optional index. Three builders make a box, a sphere and a plane.

three.js: BufferGeometry, BufferAttribute, BoxGeometry, SphereGeometry, PlaneGeometry.

BufferAttribute

A flat List[Float32] with an item size. BufferAttribute(data, 3) holds vectors of three floats.

Member Meaning
count() -> Int The number of items.
component(index, offset) -> Float32 One float of one item.
vector3(index) -> Vector3 One item as a vector. Item size must be three.

BufferGeometry

Member Meaning
set_attribute(name, attribute) Add or replace an attribute.
attribute_view(name) -> ref BufferAttribute Borrow an attribute. Bind it with ref.
clone_attribute(name) -> BufferAttribute Copy an attribute.
has_attribute(name) -> Bool Whether the attribute exists.
set_index(index) Set the triangle index. An empty list clears it.
vertex_count() -> Int The number of vertices in position.
triangle_count() -> Int The number of triangles.
corner(triangle, corner) -> Vector3 A corner position.
corner_index(triangle, corner) -> Int Which vertex that corner is.

Attribute names are the constants POSITION, NORMAL and UV. A geometry needs position. It needs normal for smooth shading and uv for a texture.

GeometryStore

assets.geometries.add(geometry) stores a geometry and returns a GeometryId. get(id) borrows it. Many meshes share one geometry. See Meshes and assets.

Box

var solid = cube(Length(1.0, METRE))
var brick = box(Length(2.0, METRE), Length(1.0, METRE), Length(0.5, METRE))

A box has twenty-four vertices, four per face. Each face carries its own normal and texture coordinates. The whole image covers each face once.

Sphere

var ball = sphere(Length(1.0, METRE), 24, 16)   # segments around, rings down

A latitude and longitude sphere. Each normal points away from the centre, so shading is smooth. u runs once around the equator. v runs from one at the north pole to zero at the south pole. It needs at least three segments and two rings.

Plane

var sheet = plane(Length(2.0, METRE), Length(1.0, METRE))        # one quad
var grid = plane(Length(20.0, METRE), Length(20.0, METRE), 4, 4)  # sixteen quads

A rectangle in the xy plane, facing +z, centred on the origin. The vertex order, winding and texture coordinates match three.js. Turn the node a quarter turn about x to make a floor.

The texture coordinates run once across the rectangle. To tile an image, rewrite the uv attribute, as examples/floor.mojo does.

Errors

  • A negative or zero extent raises.
  • A sphere with too few segments or rings raises.
  • A plane with fewer than one segment raises.
  • An index entry beyond the last vertex raises.

Home

Tutorials

How-to

Reference

Explanation

Clone this wiki locally