#mesh #voxel #graphics

block-mesh

从数组生成体素块网格的快速算法

2个不稳定版本

0.2.0 2022年4月17日
0.1.0 2022年1月13日

算法 中排名 #558

Download history 162/week @ 2024-04-22 161/week @ 2024-04-29 150/week @ 2024-05-06 98/week @ 2024-05-13 152/week @ 2024-05-20 115/week @ 2024-05-27 141/week @ 2024-06-03 121/week @ 2024-06-10 135/week @ 2024-06-17 127/week @ 2024-06-24 70/week @ 2024-07-01 92/week @ 2024-07-08 121/week @ 2024-07-15 120/week @ 2024-07-22 159/week @ 2024-07-29 216/week @ 2024-08-05

每月下载量 629
用于 4 crates

MIT/Apache

48KB
848

block-mesh

Crates.io Docs.rs

生成体素块网格的快速算法。

Mesh Examples

包含两种算法

基准测试表明,visible_block_faces 在2.5 GHz Intel Core i7的单核上每秒可以生成约4000万个四边形。假设球形输入数据,greedy_quads 可以用1/3的四边形生成更优的网格,但需要大约3倍的时间。要运行基准测试,请执行以下命令:cd bench/ && cargo bench

示例代码

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);

许可证:MIT OR Apache-2.0

依赖项

~3MB
~92K SLoC