2019-01-23 20:01:58 +00:00
|
|
|
// Library
|
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
// Project
|
|
|
|
use common::{
|
2019-01-23 22:21:47 +00:00
|
|
|
terrain::Block,
|
2019-05-17 17:44:30 +00:00
|
|
|
vol::{BaseVol, ReadVol, SizedVol, VolSize, Vox},
|
|
|
|
volumes::{dyna::Dyna, vol_map_2d::VolMap2d, vol_map_3d::VolMap3d},
|
2019-01-23 20:01:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Crate
|
|
|
|
use crate::{
|
2019-04-28 21:34:58 +00:00
|
|
|
mesh::{vol, Meshable},
|
2019-04-28 14:29:54 +00:00
|
|
|
render::{self, Mesh, Quad, TerrainPipeline},
|
2019-01-23 20:01:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type TerrainVertex = <TerrainPipeline as render::Pipeline>::Vertex;
|
|
|
|
|
|
|
|
impl<M> Meshable for Dyna<Block, M> {
|
|
|
|
type Pipeline = TerrainPipeline;
|
|
|
|
type Supplement = ();
|
|
|
|
|
|
|
|
fn generate_mesh(&self, _: Self::Supplement) -> Mesh<Self::Pipeline> {
|
|
|
|
let mut mesh = Mesh::new();
|
|
|
|
|
|
|
|
for pos in self
|
|
|
|
.iter_positions()
|
|
|
|
.filter(|pos| pos.map(|e| e >= 1).reduce_and())
|
2019-04-28 14:29:54 +00:00
|
|
|
.filter(|pos| {
|
|
|
|
pos.map2(self.get_size(), |e, sz| e < sz as i32 - 1)
|
|
|
|
.reduce_and()
|
|
|
|
})
|
2019-01-23 20:01:58 +00:00
|
|
|
{
|
2019-04-24 11:17:45 +00:00
|
|
|
let offs = pos.map(|e| e as f32 - 1.0);
|
|
|
|
|
2019-04-28 14:29:54 +00:00
|
|
|
if let Some(col) = self.get(pos).ok().and_then(|vox| vox.get_color()) {
|
2019-01-23 20:01:58 +00:00
|
|
|
let col = col.map(|e| e as f32 / 255.0);
|
|
|
|
|
2019-05-13 12:08:17 +00:00
|
|
|
vol::push_vox_verts(&mut mesh, self, pos, offs, col, TerrainVertex::new, true);
|
2019-01-23 20:01:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh
|
|
|
|
}
|
|
|
|
}
|
2019-05-12 18:33:39 +00:00
|
|
|
|
2019-05-17 17:54:56 +00:00
|
|
|
impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap2d<V, S> {
|
2019-05-17 17:44:30 +00:00
|
|
|
type Pipeline = TerrainPipeline;
|
|
|
|
type Supplement = Aabb<i32>;
|
|
|
|
|
|
|
|
fn generate_mesh(&self, range: Self::Supplement) -> Mesh<Self::Pipeline> {
|
|
|
|
let mut mesh = Mesh::new();
|
|
|
|
|
2019-05-17 21:19:32 +00:00
|
|
|
for x in range.min.x + 1..range.max.x - 1 {
|
|
|
|
for y in range.min.y + 1..range.max.y - 1 {
|
|
|
|
for z in range.min.z..range.max.z {
|
2019-05-17 17:44:30 +00:00
|
|
|
let pos = Vec3::new(x, y, z);
|
2019-05-21 22:31:38 +00:00
|
|
|
let offs = (pos - range.min * Vec3::new(1, 1, 0)).map(|e| e as f32)
|
|
|
|
- Vec3::new(1.0, 1.0, 0.0);
|
2019-05-17 17:44:30 +00:00
|
|
|
|
2019-05-17 21:19:32 +00:00
|
|
|
if let Some(col) = self.get(pos).ok().and_then(|vox| vox.get_color()) {
|
|
|
|
let col = col.map(|e| e as f32 / 255.0);
|
|
|
|
|
2019-05-21 22:31:38 +00:00
|
|
|
vol::push_vox_verts(
|
|
|
|
&mut mesh,
|
|
|
|
self,
|
|
|
|
pos,
|
|
|
|
offs,
|
|
|
|
col,
|
|
|
|
TerrainVertex::new,
|
|
|
|
false,
|
|
|
|
);
|
2019-05-17 17:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mesh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 17:54:56 +00:00
|
|
|
impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap3d<V, S> {
|
2019-05-12 18:33:39 +00:00
|
|
|
type Pipeline = TerrainPipeline;
|
|
|
|
type Supplement = Aabb<i32>;
|
|
|
|
|
|
|
|
fn generate_mesh(&self, range: Self::Supplement) -> Mesh<Self::Pipeline> {
|
|
|
|
let mut mesh = Mesh::new();
|
|
|
|
|
|
|
|
let mut last_chunk_pos = self.pos_key(range.min);
|
|
|
|
let mut last_chunk = self.get_key(last_chunk_pos);
|
|
|
|
|
|
|
|
let size = range.max - range.min;
|
2019-05-13 12:08:17 +00:00
|
|
|
for x in 1..size.x - 1 {
|
|
|
|
for y in 1..size.y - 1 {
|
|
|
|
for z in 1..size.z - 1 {
|
2019-05-12 18:33:39 +00:00
|
|
|
let pos = Vec3::new(x, y, z);
|
|
|
|
|
|
|
|
let new_chunk_pos = self.pos_key(range.min + pos);
|
|
|
|
if last_chunk_pos != new_chunk_pos {
|
|
|
|
last_chunk = self.get_key(new_chunk_pos);
|
|
|
|
last_chunk_pos = new_chunk_pos;
|
|
|
|
}
|
|
|
|
let offs = pos.map(|e| e as f32 - 1.0);
|
2019-05-12 20:58:37 +00:00
|
|
|
if let Some(chunk) = last_chunk {
|
|
|
|
let chunk_pos = Self::chunk_offs(range.min + pos);
|
|
|
|
if let Some(col) = chunk.get(chunk_pos).ok().and_then(|vox| vox.get_color())
|
|
|
|
{
|
|
|
|
let col = col.map(|e| e as f32 / 255.0);
|
|
|
|
|
|
|
|
vol::push_vox_verts(
|
|
|
|
&mut mesh,
|
|
|
|
self,
|
2019-05-12 22:46:36 +00:00
|
|
|
range.min + pos,
|
2019-05-12 20:58:37 +00:00
|
|
|
offs,
|
|
|
|
col,
|
|
|
|
TerrainVertex::new,
|
2019-05-13 12:08:17 +00:00
|
|
|
false,
|
2019-05-12 20:58:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let Some(col) = self
|
|
|
|
.get(range.min + pos)
|
|
|
|
.ok()
|
|
|
|
.and_then(|vox| vox.get_color())
|
|
|
|
{
|
|
|
|
let col = col.map(|e| e as f32 / 255.0);
|
|
|
|
|
|
|
|
vol::push_vox_verts(
|
|
|
|
&mut mesh,
|
|
|
|
self,
|
2019-05-12 22:46:36 +00:00
|
|
|
range.min + pos,
|
2019-05-12 20:58:37 +00:00
|
|
|
offs,
|
|
|
|
col,
|
|
|
|
TerrainVertex::new,
|
2019-05-13 12:08:17 +00:00
|
|
|
false,
|
2019-05-12 20:58:37 +00:00
|
|
|
);
|
|
|
|
}
|
2019-05-12 18:33:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mesh
|
|
|
|
}
|
|
|
|
}
|