concinnity_core/components/
voxel_chunk.rs1use crate::ecs::PayloadLocator;
4use crate::ecs::asset_id::AssetId;
5use alloc::vec::Vec;
6
7#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
19#[serde(default)]
20pub struct VoxelChunk {
21 #[serde(skip)]
23 pub asset_id: AssetId,
24 pub palette: Vec<AssetId>,
26 pub dim: [u32; 3],
28 pub block_size: f32,
30 pub blocks: Vec<u32>,
32 pub lod_levels: u32,
35 #[serde(default)]
38 pub lod_distances: Vec<f32>,
39 #[serde(skip)]
41 pub locator: Option<PayloadLocator>,
42}
43
44impl Default for VoxelChunk {
45 fn default() -> Self {
46 Self {
47 asset_id: AssetId::default(),
48 palette: Vec::new(),
49 dim: [0, 0, 0],
50 block_size: 1.0,
51 blocks: Vec::new(),
52 lod_levels: 1,
53 lod_distances: Vec::new(),
54 locator: None,
55 }
56 }
57}
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn a_blank_chunk_is_empty_with_metre_sized_blocks() {
65 let c = VoxelChunk::default();
66 assert_eq!(c.dim, [0, 0, 0]);
67 assert_eq!(c.block_size, 1.0);
68 assert!(c.blocks.is_empty());
69 assert!(c.palette.is_empty());
70 assert_eq!(c.lod_levels, 1);
71 assert!(c.lod_distances.is_empty());
72 assert!(c.locator.is_none());
73 }
74
75 #[test]
76 fn an_authored_chunk_parses_and_round_trips_through_postcard() {
77 crate::test_support::install_resolvers();
78 let c: VoxelChunk = serde_json::from_str(
79 r#"{"palette":["air","stone"],"dim":[2,1,2],"block_size":0.5,
80 "blocks":[0,1,1,0],"lod_levels":2,"lod_distances":[16]}"#,
81 )
82 .unwrap();
83 assert_eq!(c.palette, [AssetId(3), AssetId(5)]);
84 assert_eq!(c.blocks.len() as u32, c.dim[0] * c.dim[1] * c.dim[2]);
86
87 let bytes = postcard::to_allocvec(&c).unwrap();
88 let back: VoxelChunk = postcard::from_bytes(&bytes).unwrap();
89 assert_eq!(back.dim, [2, 1, 2]);
90 assert_eq!(back.block_size, 0.5);
91 assert_eq!(back.blocks, [0, 1, 1, 0]);
92 assert_eq!(back.lod_levels, 2);
93 assert_eq!(back.lod_distances, [16.0]);
94 assert_eq!(back.asset_id, AssetId::default());
95 }
96}