veloren/common/src/terrain/structure.rs

111 lines
2.8 KiB
Rust
Raw Normal View History

use super::Block;
use crate::{
assets::{self, Asset},
vol::{BaseVol, ReadVol, Vox, WriteVol},
volumes::dyna::{Dyna, DynaErr},
};
use dot_vox::DotVoxData;
use vek::*;
2019-06-12 20:22:16 +00:00
#[derive(Copy, Clone)]
pub enum StructureBlock {
TemperateLeaves,
PineLeaves,
PalmLeaves,
Block(Block),
}
impl Vox for StructureBlock {
fn empty() -> Self {
StructureBlock::Block(Block::empty())
}
fn is_empty(&self) -> bool {
match self {
StructureBlock::Block(block) => block.is_empty(),
_ => false,
}
}
}
#[derive(Debug)]
pub enum StructureError {}
#[derive(Clone)]
pub struct Structure {
center: Vec3<i32>,
2019-06-12 20:22:16 +00:00
vol: Dyna<StructureBlock, ()>,
empty: StructureBlock,
}
impl Structure {
pub fn with_center(mut self, center: Vec3<i32>) -> Self {
self.center = center;
self
}
}
impl BaseVol for Structure {
2019-06-12 20:22:16 +00:00
type Vox = StructureBlock;
type Err = StructureError;
}
impl ReadVol for Structure {
#[inline(always)]
2019-06-12 20:22:16 +00:00
fn get(&self, pos: Vec3<i32>) -> Result<&Self::Vox, StructureError> {
match self.vol.get(pos + self.center) {
Ok(block) => Ok(block),
Err(DynaErr::OutOfBounds) => Ok(&self.empty),
}
}
}
impl Asset for Structure {
fn load(specifier: &str) -> Result<Self, assets::Error> {
let dot_vox_data = DotVoxData::load(specifier)?;
if let Some(model) = dot_vox_data.models.get(0) {
let palette = dot_vox_data
.palette
.iter()
.map(|col| Rgba::from(col.to_ne_bytes()).into())
.collect::<Vec<_>>();
let mut vol = Dyna::filled(
Vec3::new(model.size.x, model.size.y, model.size.z),
2019-06-12 20:22:16 +00:00
StructureBlock::empty(),
(),
);
for voxel in &model.voxels {
2019-06-12 20:22:16 +00:00
let block = match voxel.i {
0 => StructureBlock::TemperateLeaves,
1 => StructureBlock::PineLeaves,
2 => StructureBlock::PalmLeaves,
index => {
let color = palette.get(index as usize).copied().unwrap_or(Rgb::broadcast(0));
StructureBlock::Block(Block::new(1, color))
}
};
let _ = vol.set(
Vec3::new(voxel.x, voxel.y, voxel.z).map(|e| e as i32),
block
);
}
Ok(Structure {
center: Vec3::zero(),
vol,
2019-06-12 20:22:16 +00:00
empty: StructureBlock::empty(),
})
} else {
Ok(Self {
center: Vec3::zero(),
2019-06-12 20:22:16 +00:00
vol: Dyna::filled(Vec3::zero(), StructureBlock::empty(), ()),
empty: StructureBlock::empty(),
})
}
}
}