geometry3d

This manual documents version 0.5.2 of Luna-Flow/geometry3d as it stands on main after its migration to MoonBit 0.10.

Overview

Luna-Flow/geometry3d is a small 3D rendering pipeline for MoonBit, built on Luna-Flow/linear-algebra. It takes quad meshes through homogeneous transforms, a look-at camera with a physical lens model, perspective projection, back-face culling, Lambert shading and shadow mapping, and draws the result in a terminal, on an HTML canvas or as animated SVG. It is deliberately not a game engine: every stage is short enough to read and is documented with the mathematics it implements.

  • Points are (p,1)(p, 1) and directions (d,0)(d, 0) in homogeneous coordinates; transforms are 4×4 matrices composed in application order.
  • Camera space has xx right, yy up and zz forward (the left-handed convention of Direct3D), and depth is the camera-space zz.
  • The frontend hands every backend the same list of projected, shaded triangles; the terminal and canvas backends resolve occlusion with a depth buffer, the SVG backend by drawing order.

Install

moon add Luna-Flow/geometry3d@0.5.2
moon add Luna-Flow/linear-algebra@0.4.7

Then import the packages you use in your moon.pkg:

import {
  "Luna-Flow/geometry3d/core",
  "Luna-Flow/geometry3d/view",
  "Luna-Flow/geometry3d/frontend",
  "Luna-Flow/geometry3d/backend/tui",
  "Luna-Flow/linear-algebra/mutable" @la,
}

linear-algebra is needed in your own module only if you name its types, for example @la.Vector[Double] in a signature. The browser backends also need moonbit-community/rabbita@0.12.4. The module requires MoonBit with moonc 0.10.20 or later. core, view, frontend and backend/tui build on every target (wasm-gc, wasm, js, native). backend/canvas, backend/gsap, demo_canvas and demo_gsap build only for js, and the terminal demo is meant for native.

Pages

The library packages form a chain: each depends only on the ones above it. The executables at the bottom are demos; their API pages describe a command line or an HTML page contract instead of MoonBit items. Package paths are Luna-Flow/geometry3d/<package>.

PartTutorialAPIDesign
core: vectors, quad meshes and generators, face normals, visibility, Lambert intensity, 4×4 transformstutorialAPIdesign
view: look-at camera, perspective and orthographic projection, perspective-correct depth, sensor and lens modeltutorialAPIdesign
frontend: scenes, the backend-neutral DrawList, shadow mapping, the luma depth buffer, exposure, optical flow, timelinestutorialAPIdesign
backend/tui: character frame buffer, shade ramp, terminal aspect correction, .tuiimg and .tui3d filestutorialAPIdesign
backend/canvas: HTML canvas output with exact occlusion (js only)tutorialAPIdesign
backend/gsap: SVG polygons in painter’s order and a GSAP playback controller (js only)tutorialAPIdesign
demo: terminal demo with scenes, exposure effects, recording and playback (native)tutorialCLIdesign
demo_canvas: browser demo of the Canvas backend (js)tutorialpagedesign
demo_gsap: browser player for the GSAP SVG backend (js)tutorialpagedesign
Guidesmigration from v0.1conventionsarchitecture

Exported items

Geometry (core)

  • Vectors: vec3, vec4, sub_vec, cross_vec, vec_length, normalize_vec, min3, max3, DEPTH_EPSILON
  • Meshes: Mesh, QuadFace, TriangleFace, the generators cube_mesh, sphere_mesh, cylinder_mesh, cone_mesh, triangular_pyramid_mesh, torus_mesh, and face_vertices, triangulate_quad
  • Faces: face_center, face_normal, face_is_visible, face_intensity
  • Transforms: Transform3 and the matrix builders identity_matrix4, translation_matrix, scale_matrix, rotation_x, rotation_y, rotation_z, rotation_matrix

Camera and projection (view)

  • Camera3, Viewport, ProjectedVertex, PerspectiveProjection, OrthographicProjection, interpolate_perspective_depth
  • The physical camera: ScientificCamera, SensorSpec, LensSpec, WorldUnit

Rendering (frontend and the backends)

  • Scenes and draw lists: Scene, SceneObject, Light, RenderView, build_draw_list, DrawList, DrawTriangle
  • Buffers and time: LumaBuffer, ExposureSettings, ShutterSpeed, optical flow, Timeline, ScalarTrack
  • Output: render_scene and the file formats of backend/tui, the Canvas renderer, and the GSAP SVG renderer and player

The architecture guide follows one frame through the packages. The design pages of core, view and frontend derive every formula from first principles, and each backend design states what its output guarantees.

Luna-Flow/linear-algebra provides the vector and matrix types (https://lunaflow.cn/en/linear-algebra/), and Luna-Flow/luna-generic and Luna-Flow/arithmetic provide the numeric traits used internally (https://lunaflow.cn/en/luna-generic/, https://lunaflow.cn/en/arithmetic/).

Validation

Recommended release checks:

moon check --target all
bash ./run_test.sh

run_test.sh runs the test suite on every target and smoke-tests the demo.