MCPcopy Index your code
hub / github.com/bonsairobo/block-mesh-rs

github.com/bonsairobo/block-mesh-rs @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
97 symbols 172 edges 13 files 13 documented · 13%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

block-mesh

Crates.io Docs.rs

Fast algorithms for generating voxel block meshes.

Mesh Examples

Two algorithms are included: - visible_block_faces: very fast but suboptimal meshes - greedy_quads: not quite as fast, but far fewer triangles are generated

Benchmarks show that visible_block_faces generates about 40 million quads per second on a single core of a 2.5 GHz Intel Core i7. Assuming spherical input data, greedy_quads can generate a more optimal version of the same mesh with 1/3 of the quads, but it takes about 3 times longer. To run the benchmarks yourself, cd bench/ && cargo bench.

Example Code

use block_mesh::ndshape::{ConstShape, ConstShape3u32};
use block_mesh::{greedy_quads, GreedyQuadsBuffer, MergeVoxel, Voxel, VoxelVisibility, RIGHT_HANDED_Y_UP_CONFIG};

#[derive(Clone, Copy, Eq, PartialEq)]
struct BoolVoxel(bool);

const EMPTY: BoolVoxel = BoolVoxel(false);
const FULL: BoolVoxel = BoolVoxel(true);

impl Voxel for BoolVoxel {
    fn get_visibility(&self) -> VoxelVisibility {
        if *self == EMPTY {
            VoxelVisibility::Empty
        } else {
            VoxelVisibility::Opaque
        }
    }
}

impl MergeVoxel for BoolVoxel {
    type MergeValue = Self;
    type MergeValueFacingNeighbour = Self;

    fn merge_value(&self) -> Self::MergeValue {
        *self
    }

    fn merge_value_facing_neighbour(&self) -> Self::MergeValueFacingNeighbour {
        *self
    }
}

// A 16^3 chunk with 1-voxel boundary padding.
type ChunkShape = ConstShape3u32<18, 18, 18>;

// This chunk will cover just a single octant of a sphere SDF (radius 15).
let mut voxels = [EMPTY; ChunkShape::SIZE as usize];
for i in 0..ChunkShape::SIZE {
    let [x, y, z] = ChunkShape::delinearize(i);
    voxels[i as usize] = if ((x * x + y * y + z * z) as f32).sqrt() < 15.0 {
        FULL
    } else {
        EMPTY
    };
}

let mut buffer = GreedyQuadsBuffer::new(voxels.len());
greedy_quads(
    &voxels,
    &ChunkShape {},
    [0; 3],
    [17; 3],
    &RIGHT_HANDED_Y_UP_CONFIG.faces,
    &mut buffer
);

// Some quads were generated.
assert!(buffer.quads.num_quads() > 0);

License: MIT OR Apache-2.0

Extension points exported contracts — how you extend this code

Voxel (Interface)
Implement on your voxel types to inform the library how to generate geometry for this voxel. [6 implementers]
src/lib.rs
MergeVoxel (Interface)
(no doc) [4 implementers]
src/greedy.rs
MergeStrategy (Interface)
A strategy for merging cube faces into quads. [1 implementers]
src/greedy/merge_strategy.rs

Core symbols most depended-on inside this repo

greedy_quads
called by 8
src/greedy.rs
visible_block_faces
called by 7
src/simple.rs
num_quads
called by 6
src/buffer.rs
get_visibility
called by 3
src/greedy.rs
get_visibility
called by 3
src/simple.rs
index
called by 3
src/geometry/axis.rs
get_unit_vector
called by 3
src/geometry/axis.rs
axes
called by 3
src/geometry/axis.rs

Shape

Method 42
Function 30
Class 17
Enum 5
Interface 3

Languages

Rust100%

Modules by API surface

src/greedy.rs14 symbols
src/geometry/axis.rs13 symbols
src/geometry/face.rs12 symbols
examples-crate/uv_mapping/main.rs12 symbols
examples-crate/render/main.rs11 symbols
bench/src/bench.rs9 symbols
src/simple.rs6 symbols
src/lib.rs5 symbols
src/greedy/merge_strategy.rs5 symbols
src/buffer.rs5 symbols
src/geometry/quad.rs3 symbols
src/geometry.rs1 symbols

For agents

$ claude mcp add block-mesh-rs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact