#mesh #voxel #graphics

block-mesh

Fast algorithms for generating voxel block meshes from arrays

2 unstable releases

0.2.0 Apr 17, 2022
0.1.0 Jan 13, 2022

#610 in Algorithms

Download history 162/week @ 2023-12-15 89/week @ 2023-12-22 51/week @ 2023-12-29 121/week @ 2024-01-05 170/week @ 2024-01-12 115/week @ 2024-01-19 63/week @ 2024-01-26 44/week @ 2024-02-02 107/week @ 2024-02-09 119/week @ 2024-02-16 117/week @ 2024-02-23 95/week @ 2024-03-01 140/week @ 2024-03-08 116/week @ 2024-03-15 129/week @ 2024-03-22 150/week @ 2024-03-29

549 downloads per month
Used in 4 crates

MIT/Apache

48KB
848 lines

block-mesh

Crates.io Docs.rs

Fast algorithms for generating voxel block meshes.

Mesh Examples

Two algorithms are included:

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;

    fn merge_value(&self) -> Self::MergeValue {
        *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

Dependencies

~3MB
~88K SLoC