Repository navigation
Math
math/vector2.mojo, math/vector3.mojo, math/matrix4.mojo and math/projection.mojo. Ported from three.js with the same conventions.
three.js: Vector2, Vector3, Matrix4, Matrix4.makePerspective, makeOrthographic, lookAt.
Vector2(x, y) and Vector3(x, y, z) hold Float32 components. Both are value types. Assignment copies.
| Vector3 member | Meaning |
|---|---|
dot(other) -> Float32 |
The dot product. |
length() -> Float32 |
The Euclidean length. |
add(other), sub(other)
|
Change self in place. |
cross(other) |
self = self × other. |
normalize() |
Scale to unit length. A zero vector stays zero. |
a + b, a - b, a * f, -a
|
Return a new vector. |
Column-major storage, as three.js and OpenGL. Element (row, col) is at col * 4 + row. The translation is at elements 12 to 14.
set takes its sixteen arguments in row-major order, so a matrix in source reads as it does on paper. That asymmetry is three.js's.
| Member | Meaning |
|---|---|
Matrix4() |
The identity. |
set(n11, n12, ..., n44) |
Every element, row by row. |
get(row, col), put(row, col, value)
|
One element. |
multiply(other) |
self = self * other. The right-hand matrix applies first. |
premultiply(other) |
self = other * self. |
transpose(), invert(), determinant()
|
As named. A singular matrix inverts to zeros. |
normal_matrix() -> Matrix4 |
The inverse transpose, for normals. |
extract_rotation() -> Matrix4 |
The rotation with scale and translation removed. |
transform_point(p) -> Vector3 |
Apply with w = 1 and divide by w. |
transform_w(p) -> Float32 |
The w that transform_point divides by. |
transform_direction(d) -> Vector3 |
Apply with w = 0. |
Builders: translation(x, y, z), scaling(x, y, z), rotation_x(angle), rotation_y(angle), rotation_z(angle). A rotation takes an Angle, so rotation_z(90.0) does not compile.
var m = translation(10, 0, 0)
m.multiply(rotation_z(Angle(90.0, DEGREE)))
m.multiply(translation(-10, 0, 0)) # a rotation about (10, 0, 0)| Function | Meaning |
|---|---|
perspective(left, right, top, bottom, near, far) |
Camera space to normalized device space, with perspective. |
orthographic(left, right, top, bottom, near, far) |
The same without perspective. w stays one. |
look_at(eye, target, up) |
The view matrix of a camera at eye. |
viewport(width, height) |
Normalized device space to pixels. Rows count down. |
Each raises for a degenerate volume, a camera at its own target, or an up vector along the view direction.
Normalized device space is unitless. World space is meters and screen space is pixels. The matrices meet in the middle.
Tutorials
How-to
Reference
- Scene graph
- Rotations
- Cameras
- Geometry
- Geometry addons
- Lofts and clipping groups
- Sculptor
- Procedural generators
- Meshes and assets
- Lines
- Points and sprites
- Helpers
- Scene objects
- Animated objects
- Raycasting
- Curves and paths
- Animation
- Skinning
- Materials
- Node materials
- TSL functions
- Lights
- Lighting addons
- Fog
- Textures
- Renderer
- Renderer hooks and material flags
- Rasterization
- GPU backend
- Render target
- Post-processing
- GPU computation
- Compute nodes
- Progressive light map
- Gaussian splats
- Voxel global illumination
- SVG renderer
- Image files
- Image decode queue
- Model files
- More model files
- Exporters
- Scene JSON
- Windowing and controls
- Math
- Norm consumers
- Math addons
- Units
- Coverage tool
- Commands
- Examples
- Benchmarks
- Extensions
- Shared physics
- Physics query snapshots
- Continuous collision
- Tick force restoration
- Water
- Streamline
- Animals
- Buildings
- CARLA
- Femur
- Tibia
- Fibula
- Patella
- Knee
- Muscles
- Vessels
- Lymph
- Nerves
- Integument
- Leg
- Foot
- Pelvis
- Torso
- Arm
- Hand
- Head
- Genome
- Game humanoid
- Humanoid fidelity
- Audio-aligned game faces
- Converted assets
- Mesh quality
- Segment inertia
Explanation
- Software rasterizer
- Scene graph as an array
- Linear color
- Normals are not colors
- Perspective correction
- Fixed-point coverage
- Per-fragment shading
- Mipmaps
- Sorted transparency
- Types and checks
- Render hooks as a trait
- Shared CPU and GPU code
- Bands and threads
- PNG structure
- Extensions beside the port
- CUDA teardown hang
- Mojo compiler hang