Former-commit-id: 71912bdd2b8f40020153c5696cd5cc1af3f154cc
This commit is contained in:
Joshua Barretto 2019-05-17 18:54:56 +01:00
parent 91184356e7
commit 7174830279
7 changed files with 69 additions and 96 deletions

View File

@ -1,19 +1,10 @@
use vek::*;
use serde_derive::{Deserialize, Serialize};
use super::{block::Block, TerrainChunkMeta, TerrainChunkSize};
use crate::{
vol::{
BaseVol,
ReadVol,
WriteVol,
VolSize,
},
vol::{BaseVol, ReadVol, VolSize, WriteVol},
volumes::chunk::{Chunk, ChunkErr},
};
use super::{
block::Block,
TerrainChunkSize,
TerrainChunkMeta,
};
use serde_derive::{Deserialize, Serialize};
use vek::*;
#[derive(Debug)]
pub enum ChonkError {
@ -51,14 +42,15 @@ impl BaseVol for Chonk {
type Err = ChonkError;
}
impl ReadVol for Chonk {
#[inline(always)]
fn get(&self, pos: Vec3<i32>) -> Result<&Block, ChonkError> {
if pos.z < self.z_offset {
// Below the terrain
Ok(&self.below)
} else if pos.z >= self.z_offset + TerrainChunkSize::SIZE.z as i32 * self.sub_chunks.len() as i32 {
} else if pos.z
>= self.z_offset + TerrainChunkSize::SIZE.z as i32 * self.sub_chunks.len() as i32
{
// Above the terrain
Ok(&self.above)
} else {
@ -66,17 +58,16 @@ impl ReadVol for Chonk {
let sub_chunk_idx = self.sub_chunk_idx(pos.z);
match &self.sub_chunks[sub_chunk_idx] { // Can't fail
match &self.sub_chunks[sub_chunk_idx] {
// Can't fail
SubChunk::Homogeneous(block) => Ok(block),
SubChunk::Heterogeneous(chunk) => {
let rpos = pos - Vec3::unit_z() * (
self.z_offset +
sub_chunk_idx as i32 * TerrainChunkSize::SIZE.z as i32
);
chunk
.get(rpos)
.map_err(|err| ChonkError::ChunkError(err))
},
let rpos = pos
- Vec3::unit_z()
* (self.z_offset
+ sub_chunk_idx as i32 * TerrainChunkSize::SIZE.z as i32);
chunk.get(rpos).map_err(|err| ChonkError::ChunkError(err))
}
}
}
}
@ -94,30 +85,26 @@ impl WriteVol for Chonk {
self.sub_chunks.push(SubChunk::Homogeneous(self.above));
}
let rpos = pos - Vec3::unit_z() * (
self.z_offset +
sub_chunk_idx as i32 * TerrainChunkSize::SIZE.z as i32
);
let rpos = pos
- Vec3::unit_z()
* (self.z_offset + sub_chunk_idx as i32 * TerrainChunkSize::SIZE.z as i32);
match &mut self.sub_chunks[sub_chunk_idx] { // Can't fail
match &mut self.sub_chunks[sub_chunk_idx] {
// Can't fail
SubChunk::Homogeneous(cblock) if *cblock == block => Ok(()),
SubChunk::Homogeneous(cblock) => {
let mut new_chunk = Chunk::filled(*cblock, ());
match new_chunk
.set(rpos, block)
.map_err(|err| {
println!("Error!! {:?}", rpos);
ChonkError::ChunkError(err)
})
{
match new_chunk.set(rpos, block).map_err(|err| {
println!("Error!! {:?}", rpos);
ChonkError::ChunkError(err)
}) {
Ok(()) => {
self.sub_chunks[sub_chunk_idx] = SubChunk::Heterogeneous(new_chunk);
Ok(())
},
}
Err(err) => Err(err),
}
},
}
SubChunk::Heterogeneous(chunk) => chunk
.set(rpos, block)
.map_err(|err| ChonkError::ChunkError(err)),

View File

@ -1,4 +1,4 @@
pub mod chunk;
pub mod dyna;
pub mod vol_map_3d;
pub mod vol_map_2d;
pub mod vol_map_3d;

View File

@ -1,9 +1,3 @@
use std::{
collections::HashMap,
sync::Arc,
marker::PhantomData,
};
use vek::*;
use crate::{
terrain::TerrainChunkMeta,
vol::{BaseVol, ReadVol, SampleVol, SizedVol, VolSize, Vox, WriteVol},
@ -12,6 +6,8 @@ use crate::{
dyna::{Dyna, DynaErr},
},
};
use std::{collections::HashMap, marker::PhantomData, sync::Arc};
use vek::*;
#[derive(Debug)]
pub enum VolMap2dErr<V: BaseVol> {
@ -134,11 +130,7 @@ impl<V: BaseVol, S: VolSize> VolMap2d<V, S> {
S::SIZE.into()
}
pub fn insert(
&mut self,
key: Vec2<i32>,
chunk: Arc<V>,
) -> Option<Arc<V>> {
pub fn insert(&mut self, key: Vec2<i32>, chunk: Arc<V>) -> Option<Arc<V>> {
self.chunks.insert(key, chunk)
}

View File

@ -1,9 +1,3 @@
use std::{
collections::HashMap,
sync::Arc,
marker::PhantomData,
};
use vek::*;
use crate::{
terrain::TerrainChunkMeta,
vol::{BaseVol, ReadVol, SampleVol, SizedVol, VolSize, Vox, WriteVol},
@ -12,6 +6,8 @@ use crate::{
dyna::{Dyna, DynaErr},
},
};
use std::{collections::HashMap, marker::PhantomData, sync::Arc};
use vek::*;
#[derive(Debug)]
pub enum VolMap3dErr<V: BaseVol> {
@ -134,11 +130,7 @@ impl<V: BaseVol, S: VolSize> VolMap3d<V, S> {
S::SIZE
}
pub fn insert(
&mut self,
key: Vec3<i32>,
chunk: Arc<V>,
) -> Option<Arc<V>> {
pub fn insert(&mut self, key: Vec3<i32>, chunk: Arc<V>) -> Option<Arc<V>> {
self.chunks.insert(key, chunk)
}

View File

@ -44,7 +44,7 @@ impl<M> Meshable for Dyna<Block, M> {
}
}
impl<V: BaseVol<Vox=Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap2d<V, S> {
impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap2d<V, S> {
type Pipeline = TerrainPipeline;
type Supplement = Aabb<i32>;
@ -108,7 +108,7 @@ impl<V: BaseVol<Vox=Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap2d<
}
}
impl<V: BaseVol<Vox=Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap3d<V, S> {
impl<V: BaseVol<Vox = Block> + ReadVol, S: VolSize + Clone> Meshable for VolMap3d<V, S> {
type Pipeline = TerrainPipeline;
type Supplement = Aabb<i32>;

View File

@ -1,11 +1,11 @@
use std::{collections::HashMap, sync::mpsc, time::Duration};
use vek::*;
use client::Client;
use common::{terrain::TerrainMap, vol::SampleVol, volumes::vol_map_2d::VolMap2dErr};
use crate::{
mesh::Meshable,
render::{Consts, Globals, Mesh, Model, Renderer, TerrainLocals, TerrainPipeline},
};
use client::Client;
use common::{terrain::TerrainMap, vol::SampleVol, volumes::vol_map_2d::VolMap2dErr};
use std::{collections::HashMap, sync::mpsc, time::Duration};
use vek::*;
struct TerrainChunk {
// GPU data
@ -174,12 +174,12 @@ impl Terrain {
.expect("Failed to upload chunk mesh to the GPU"),
locals: renderer
.create_consts(&[TerrainLocals {
model_offs: Vec3::from(response
.pos
.map2(TerrainMap::chunk_size(), |e, sz| {
model_offs: Vec3::from(
response.pos.map2(TerrainMap::chunk_size(), |e, sz| {
e as f32 * sz as f32
}))
.into_array(),
}),
)
.into_array(),
}])
.expect("Failed to upload chunk locals to the GPU"),
},

View File

@ -4,8 +4,8 @@ use vek::*;
// Project
use common::{
terrain::{Block, TerrainChunk, TerrainChunkSize, TerrainChunkMeta},
vol::{SizedVol, Vox, WriteVol, VolSize},
terrain::{Block, TerrainChunk, TerrainChunkMeta, TerrainChunkSize},
vol::{SizedVol, VolSize, Vox, WriteVol},
};
#[derive(Debug)]
@ -39,7 +39,8 @@ impl World {
for y in 0..TerrainChunkSize::SIZE.y as i32 {
for z in 0..256 {
let lpos = Vec3::new(x, y, z);
let wpos = lpos + Vec3::from(chunk_pos) * TerrainChunkSize::SIZE.map(|e| e as i32);
let wpos =
lpos + Vec3::from(chunk_pos) * TerrainChunkSize::SIZE.map(|e| e as i32);
let wposf = wpos.map(|e| e as f64);
let chaos_freq = 1.0 / 100.0;
@ -54,27 +55,28 @@ impl World {
.max(0.0)
+ 0.5;
let height = perlin_nz.get(Vec2::from(wposf * freq).into_array()) * ampl * chaos
+ perlin_nz.get((wposf * small_freq).into_array())
* small_ampl
* 3.0
* chaos.powf(2.0)
+ offs;
let temp = (temp_nz.get(Vec2::from(wposf * (1.0 / 64.0)).into_array()) + 1.0) * 0.5;
let height =
perlin_nz.get(Vec2::from(wposf * freq).into_array()) * ampl * chaos
+ perlin_nz.get((wposf * small_freq).into_array())
* small_ampl
* 3.0
* chaos.powf(2.0)
+ offs;
let temp =
(temp_nz.get(Vec2::from(wposf * (1.0 / 64.0)).into_array()) + 1.0) * 0.5;
let _ = chunk
.set(
lpos,
if wposf.z < height - 4.0 {
stone
} else if wposf.z < height - 2.0 {
dirt
} else if wposf.z < height {
Block::new(2, Rgb::new(10 + (150.0 * temp) as u8, 150, 0))
} else {
air
},
);
let _ = chunk.set(
lpos,
if wposf.z < height - 4.0 {
stone
} else if wposf.z < height - 2.0 {
dirt
} else if wposf.z < height {
Block::new(2, Rgb::new(10 + (150.0 * temp) as u8, 150, 0))
} else {
air
},
);
}
}
}