Crates.io | binary-greedy-meshing |
lib.rs | binary-greedy-meshing |
version | 0.3.6 |
source | src |
created_at | 2024-08-01 08:45:42.54602 |
updated_at | 2024-10-18 09:26:55.687947 |
description | A port of https://github.com/cgerikj/binary-greedy-meshing to Rust. |
homepage | |
repository | https://github.com/Inspirateur/binary-greedy-meshing |
max_upload_size | |
id | 1321782 |
size | 143,183 |
Originally a port of Binary Greedy Meshing v2 to Rust, with additional improvements such as support for transparent blocks.
This crate is used in the Bevy voxel game Riverbed, you can check out the code for usage examples.
use binary_greedy_meshing as bgm;
use std::collections::BTreeSet;
fn main() {
let mut voxels = [0; bgm::CS_P3];
// Add 2 voxels at position 0;0;0 and 0;1;0
voxels[bgm::pad_linearize(0, 0, 0)] = 1;
voxels[bgm::pad_linearize(0, 1, 0)] = 1;
// Contain useful buffers that can be cached and cleared
// with mesh_data.clear() to avoid re-allocation
let mut mesh_data = bgm::MeshData::new();
// Does the meshing, mesh_data.quads is the output
// transparent block values are signaled by putting them in the BTreeSet
bgm::mesh(&voxels, &mut mesh_data, BTreeSet::default());
}
mesh_data.quads
mesh_data.quads
is a [Vec<u64>; 6]
, 1 Vec
(v_type << 32) | (h << 24) | (w << 18) | (z << 12) | (y << 6) | x
The face groups correspond to Up, Down, Right, Left, Front, Back, in this order. (assuming right handed Y up)
The fastest way of rendering quads is using instancing (check this video to learn more about the topic), but if it's not available you can still convert the quads to vertices and indices making a regular mesh, see this Riverbed files for an example of this:
Benching the crate on Intel(R) Xeon(R) CPU E5-1650 v3 @ 3.50GHz:
This is coherent with the 50-200μs range (without transparency) reported from the original C version of the library, as transparency incurrs a significant cost in the hidden face culling phase.
The meshing is also ~7x faster than block-mesh-rs which took ~4.5ms to greedy mesh a chunk on my machine.
chunk sizes are 62^3 (64^3 with padding), this crate doesn't support other sizes.