martini
RTIN (Right-Triangulated Irregular Network) mesh generation for terrain data in Rust.
Based on the Martini algorithm by Mapbox.
Features
- Fast terrain mesh generation from heightmap data
- Level-of-detail control via error threshold
- Pre-computed triangle coordinates for efficient reuse
- Outputs vertices, indices, and UV coordinates
Installation
Add to your Cargo.toml:
[]
= "0.1"
Usage
use Martini;
// Create a Martini instance for a 257x257 grid (2^8 + 1)
let mut martini = new;
// Create terrain from height data
// Height data should be a 257x257 grid
let heightmap: = vec!;
let tile = martini.create_terrain;
// Generate mesh with maximum error threshold
// Lower error = more triangles = higher detail
let = tile.construct_mesh;
println!;
Grid Size
The grid size must be 2^n + 1:
- 3, 5, 9, 17, 33, 65, 129, 257, 513, 1025, ...
Common sizes:
- 257 (256x256 tiles) - Good for web terrain
- 513 (512x512 tiles) - Higher detail
- 1025 (1024x1024 tiles) - Very high detail
Error Threshold
The max_error parameter controls mesh detail:
| Error | Result |
|---|---|
| High (100+) | Fewer triangles, lower quality |
| Medium (10) | Balanced quality/performance |
| Low (1) | More triangles, higher quality |
| 0 | Maximum detail (may be slow) |
Output Format
- vertices:
Vec<f32>- Flat array[x, y, z, x, y, z, ...] - indices:
Vec<u32>- Triangle indices - uvs:
Vec<f32>- Flat array[u, v, u, v, ...](0-1 range)
Reusing Martini Instance
The Martini struct pre-computes triangle coordinates. Reuse it for multiple tiles of the same size:
let mut martini = new;
// Process multiple tiles
for heightmap in heightmaps
Algorithm
The Martini algorithm uses a right-triangulated irregular network (RTIN) to create adaptive terrain meshes:
- Start with two triangles covering the entire tile
- For each triangle, compute the error at the midpoint of its longest edge
- If error exceeds threshold, split the triangle into two smaller triangles
- Repeat until all triangles meet the error threshold
This produces meshes that adapt to terrain complexity - flat areas get fewer triangles, complex areas get more.
References
- mapbox/martini - Original JavaScript implementation
- Right-Triangulated Irregular Networks - Paper by Will Evans et al.
License
MIT OR Apache-2.0