veloren/voxygen/src/scene/terrain.rs

1215 lines
43 KiB
Rust
Raw Normal View History

2019-01-15 15:13:11 +00:00
use crate::{
2019-01-23 20:01:58 +00:00
mesh::Meshable,
render::{
2019-09-25 12:00:00 +00:00
Consts, FluidPipeline, Globals, Instances, Light, Mesh, Model, Renderer, Shadow,
2019-10-17 16:11:55 +00:00
SpriteInstance, SpritePipeline, TerrainLocals, TerrainPipeline, Texture,
},
2019-01-15 15:13:11 +00:00
};
2019-09-01 19:04:03 +00:00
use client::Client;
use common::{
2019-08-19 20:09:35 +00:00
assets,
figure::Segment,
terrain::{Block, BlockKind, TerrainChunk},
vol::{BaseVol, ReadVol, RectRasterableVol, SampleVol, Vox},
volumes::vol_grid_2d::{VolGrid2d, VolGrid2dError},
};
use crossbeam::channel;
2019-08-19 20:09:35 +00:00
use dot_vox::DotVoxData;
2019-06-06 09:04:37 +00:00
use frustum_query::frustum::Frustum;
use hashbrown::{hash_map::Entry, HashMap};
use std::{f32, fmt::Debug, i32, marker::PhantomData, ops::Mul, time::Duration};
use vek::*;
2019-01-15 15:13:11 +00:00
struct TerrainChunkData {
2019-01-15 15:13:11 +00:00
// GPU data
2019-09-24 15:43:51 +00:00
load_time: f32,
opaque_model: Model<TerrainPipeline>,
fluid_model: Option<Model<FluidPipeline>>,
2019-08-21 17:22:05 +00:00
sprite_instances: HashMap<(BlockKind, usize), Instances<SpriteInstance>>,
2019-01-15 15:13:11 +00:00
locals: Consts<TerrainLocals>,
2019-08-19 20:09:35 +00:00
2019-06-06 09:04:37 +00:00
visible: bool,
z_bounds: (f32, f32),
2019-01-15 15:13:11 +00:00
}
2019-01-23 20:01:58 +00:00
struct ChunkMeshState {
pos: Vec2<i32>,
2019-01-23 20:01:58 +00:00
started_tick: u64,
active_worker: Option<u64>,
2019-01-23 20:01:58 +00:00
}
/// A type produced by mesh worker threads corresponding to the position and mesh of a chunk.
2019-01-23 20:01:58 +00:00
struct MeshWorkerResponse {
pos: Vec2<i32>,
2019-06-06 09:04:37 +00:00
z_bounds: (f32, f32),
opaque_mesh: Mesh<TerrainPipeline>,
fluid_mesh: Mesh<FluidPipeline>,
2019-08-21 17:22:05 +00:00
sprite_instances: HashMap<(BlockKind, usize), Vec<SpriteInstance>>,
2019-01-23 20:01:58 +00:00
started_tick: u64,
}
struct SpriteConfig {
2019-08-21 17:22:05 +00:00
variations: usize,
wind_sway: f32, // 1.0 is normal
}
fn sprite_config_for(kind: BlockKind) -> Option<SpriteConfig> {
match kind {
2019-08-21 17:22:05 +00:00
BlockKind::LargeCactus => Some(SpriteConfig {
variations: 1,
wind_sway: 0.0,
}),
BlockKind::BarrelCactus => Some(SpriteConfig {
variations: 1,
wind_sway: 0.0,
}),
2019-09-01 19:04:03 +00:00
BlockKind::RoundCactus => Some(SpriteConfig {
variations: 1,
wind_sway: 0.0,
}),
BlockKind::ShortCactus => Some(SpriteConfig {
variations: 1,
wind_sway: 0.0,
}),
BlockKind::MedFlatCactus => Some(SpriteConfig {
variations: 1,
wind_sway: 0.0,
}),
BlockKind::ShortFlatCactus => Some(SpriteConfig {
variations: 1,
wind_sway: 0.0,
}),
2019-08-21 17:22:05 +00:00
BlockKind::BlueFlower => Some(SpriteConfig {
variations: 7,
2019-09-01 19:04:03 +00:00
wind_sway: 0.1,
2019-08-21 17:22:05 +00:00
}),
BlockKind::PinkFlower => Some(SpriteConfig {
2019-09-01 19:04:03 +00:00
variations: 4,
wind_sway: 0.1,
2019-08-21 17:22:05 +00:00
}),
BlockKind::RedFlower => Some(SpriteConfig {
variations: 3,
2019-09-01 19:04:03 +00:00
wind_sway: 0.1,
2019-08-21 17:22:05 +00:00
}),
BlockKind::WhiteFlower => Some(SpriteConfig {
2019-10-02 10:05:17 +00:00
variations: 2,
2019-09-01 19:04:03 +00:00
wind_sway: 0.1,
2019-08-21 17:22:05 +00:00
}),
BlockKind::YellowFlower => Some(SpriteConfig {
variations: 1,
2019-09-01 19:04:03 +00:00
wind_sway: 0.1,
2019-08-21 17:22:05 +00:00
}),
BlockKind::Sunflower => Some(SpriteConfig {
variations: 2,
2019-09-01 19:04:03 +00:00
wind_sway: 0.1,
2019-08-21 17:22:05 +00:00
}),
BlockKind::LongGrass => Some(SpriteConfig {
2019-09-01 19:04:03 +00:00
variations: 7,
wind_sway: 0.8,
2019-08-21 17:22:05 +00:00
}),
BlockKind::MediumGrass => Some(SpriteConfig {
variations: 5,
2019-09-01 19:04:03 +00:00
wind_sway: 0.5,
2019-08-21 17:22:05 +00:00
}),
BlockKind::ShortGrass => Some(SpriteConfig {
variations: 5,
2019-09-01 19:04:03 +00:00
wind_sway: 0.1,
2019-08-21 17:22:05 +00:00
}),
BlockKind::Apple => Some(SpriteConfig {
variations: 1,
wind_sway: 0.0,
}),
2019-09-01 19:04:03 +00:00
BlockKind::Mushroom => Some(SpriteConfig {
variations: 11,
2019-09-01 19:04:03 +00:00
wind_sway: 0.0,
}),
BlockKind::Liana => Some(SpriteConfig {
variations: 2,
wind_sway: 0.05,
}),
2019-09-25 20:22:39 +00:00
BlockKind::Velorite => Some(SpriteConfig {
variations: 1,
wind_sway: 0.0,
}),
BlockKind::VeloriteFrag => Some(SpriteConfig {
variations: 10,
wind_sway: 0.0,
}),
2019-10-09 19:28:05 +00:00
BlockKind::Chest => Some(SpriteConfig {
variations: 4,
wind_sway: 0.0,
}),
_ => None,
}
}
/// Function executed by worker threads dedicated to chunk meshing.
fn mesh_worker<V: BaseVol<Vox = Block> + RectRasterableVol + ReadVol + Debug>(
pos: Vec2<i32>,
2019-06-06 09:04:37 +00:00
z_bounds: (f32, f32),
2019-01-23 20:01:58 +00:00
started_tick: u64,
volume: <VolGrid2d<V> as SampleVol<Aabr<i32>>>::Sample,
range: Aabb<i32>,
2019-01-23 20:01:58 +00:00
) -> MeshWorkerResponse {
let (opaque_mesh, fluid_mesh) = volume.generate_mesh(range);
2019-01-23 20:01:58 +00:00
MeshWorkerResponse {
pos,
2019-06-06 09:04:37 +00:00
z_bounds,
opaque_mesh,
fluid_mesh,
2019-08-19 20:09:35 +00:00
// Extract sprite locations from volume
sprite_instances: {
let mut instances = HashMap::new();
2019-08-19 20:09:35 +00:00
for x in 0..V::RECT_SIZE.x as i32 {
for y in 0..V::RECT_SIZE.y as i32 {
2019-08-19 20:09:35 +00:00
for z in z_bounds.0 as i32..z_bounds.1 as i32 + 1 {
let wpos = Vec3::from(pos * V::RECT_SIZE.map(|e: u32| e as i32))
+ Vec3::new(x, y, z);
2019-08-19 20:09:35 +00:00
let kind = volume.get(wpos).unwrap_or(&Block::empty()).kind();
if let Some(cfg) = sprite_config_for(kind) {
2019-09-01 19:04:03 +00:00
let seed = wpos.x * 3 + wpos.y * 7 + wpos.x * wpos.y; // Awful PRNG
2019-08-21 15:22:49 +00:00
let instance = SpriteInstance::new(
Mat4::identity()
.rotated_z(f32::consts::PI * 0.5 * (seed % 4) as f32)
.translated_3d(
wpos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0),
),
2019-08-21 15:22:49 +00:00
Rgb::broadcast(1.0),
cfg.wind_sway,
);
2019-08-21 15:22:49 +00:00
instances
2019-08-21 17:22:05 +00:00
.entry((kind, seed as usize % cfg.variations))
2019-08-21 15:22:49 +00:00
.or_insert_with(|| Vec::new())
.push(instance);
2019-08-19 20:09:35 +00:00
}
}
}
}
instances
},
2019-01-23 20:01:58 +00:00
started_tick,
}
}
pub struct Terrain<V: RectRasterableVol> {
chunks: HashMap<Vec2<i32>, TerrainChunkData>,
2019-01-23 20:01:58 +00:00
// The mpsc sender and receiver used for talking to meshing worker threads.
// We keep the sender component for no reason other than to clone it and send it to new workers.
mesh_send_tmp: channel::Sender<MeshWorkerResponse>,
mesh_recv: channel::Receiver<MeshWorkerResponse>,
mesh_todo: HashMap<Vec2<i32>, ChunkMeshState>,
2019-08-19 20:09:35 +00:00
// GPU data
2019-08-21 17:22:05 +00:00
sprite_models: HashMap<(BlockKind, usize), Model<SpritePipeline>>,
2019-11-17 22:41:00 +00:00
waves: Texture,
phantom: PhantomData<V>,
2019-01-15 15:13:11 +00:00
}
impl<V: RectRasterableVol> Terrain<V> {
2019-08-19 20:09:35 +00:00
pub fn new(renderer: &mut Renderer) -> Self {
2019-01-23 20:01:58 +00:00
// Create a new mpsc (Multiple Produced, Single Consumer) pair for communicating with
// worker threads that are meshing chunks.
let (send, recv) = channel::unbounded();
2019-01-23 20:01:58 +00:00
2019-09-01 19:04:03 +00:00
let mut make_model = |s, offset| {
renderer
.create_model(
&Meshable::<SpritePipeline, SpritePipeline>::generate_mesh(
&Segment::from(assets::load_expect::<DotVoxData>(s).as_ref()),
2019-09-01 19:04:03 +00:00
offset,
)
.0,
)
.unwrap()
};
2019-08-19 20:09:35 +00:00
2019-01-15 15:13:11 +00:00
Self {
chunks: HashMap::default(),
2019-01-23 20:01:58 +00:00
mesh_send_tmp: send,
mesh_recv: recv,
mesh_todo: HashMap::default(),
sprite_models: vec![
2019-08-21 17:22:05 +00:00
// Cacti
(
(BlockKind::LargeCactus, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.cacti.large_cactus",
2019-10-04 18:27:12 +00:00
Vec3::new(-13.5, -5.5, 0.0),
2019-09-01 19:04:03 +00:00
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::BarrelCactus, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.cacti.barrel_cactus",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::RoundCactus, 0),
make_model(
"voxygen.voxel.sprite.cacti.cactus_round",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::ShortCactus, 0),
make_model(
"voxygen.voxel.sprite.cacti.cactus_short",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::MedFlatCactus, 0),
make_model(
"voxygen.voxel.sprite.cacti.flat_cactus_med",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::ShortFlatCactus, 0),
make_model(
"voxygen.voxel.sprite.cacti.flat_cactus_short",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
// Fruit
(
(BlockKind::Apple, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.fruit.apple",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
// Flowers
(
(BlockKind::BlueFlower, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.flowers.flower_blue_1",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::BlueFlower, 1),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.flowers.flower_blue_2",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::BlueFlower, 2),
make_model(
"voxygen.voxel.sprite.flowers.flower_blue_3",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::BlueFlower, 3),
make_model(
"voxygen.voxel.sprite.flowers.flower_blue_4",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::BlueFlower, 4),
make_model(
"voxygen.voxel.sprite.flowers.flower_blue_5",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
2019-10-04 18:27:12 +00:00
(
(BlockKind::BlueFlower, 5),
make_model(
"voxygen.voxel.sprite.flowers.flower_blue_6",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::BlueFlower, 6),
make_model(
"voxygen.voxel.sprite.flowers.flower_blue_7",
Vec3::new(-6.0, -6.0, 0.0),
),
),
2019-08-21 17:22:05 +00:00
(
(BlockKind::PinkFlower, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.flowers.flower_pink_1",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::PinkFlower, 1),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.flowers.flower_pink_2",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::PinkFlower, 2),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.flowers.flower_pink_3",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::PinkFlower, 3),
make_model(
"voxygen.voxel.sprite.flowers.flower_pink_4",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::PurpleFlower, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.flowers.flower_purple_1",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::RedFlower, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.flowers.flower_red_1",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::RedFlower, 1),
make_model(
"voxygen.voxel.sprite.flowers.flower_red_2",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::RedFlower, 2),
make_model(
"voxygen.voxel.sprite.flowers.flower_red_3",
Vec3::new(-6.0, -6.0, 0.0),
),
),
2019-08-21 17:22:05 +00:00
(
(BlockKind::WhiteFlower, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.flowers.flower_white_1",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
2019-10-02 10:05:17 +00:00
(
(BlockKind::WhiteFlower, 1),
make_model(
"voxygen.voxel.sprite.flowers.flower_white_2",
Vec3::new(-6.0, -6.0, 0.0),
),
),
2019-08-21 17:22:05 +00:00
(
(BlockKind::YellowFlower, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.flowers.flower_purple_1",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::Sunflower, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.flowers.sunflower_1",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::Sunflower, 1),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.flowers.sunflower_2",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
// Grass
(
(BlockKind::LongGrass, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_long_1",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::LongGrass, 1),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_long_2",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::LongGrass, 2),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_long_3",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::LongGrass, 3),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_long_4",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::LongGrass, 4),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_long_5",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::LongGrass, 5),
make_model(
"voxygen.voxel.sprite.grass.grass_long_6",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::LongGrass, 6),
make_model(
"voxygen.voxel.sprite.grass.grass_long_7",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::MediumGrass, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_med_1",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::MediumGrass, 1),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_med_2",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::MediumGrass, 2),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_med_3",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::MediumGrass, 3),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_med_4",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::MediumGrass, 4),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_med_5",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::ShortGrass, 0),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_short_1",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::ShortGrass, 1),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_short_2",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
(BlockKind::ShortGrass, 2),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_short_3",
Vec3::new(-6.0, -6.0, 0.0),
),
2019-08-21 17:22:05 +00:00
),
(
2019-08-21 17:22:05 +00:00
(BlockKind::ShortGrass, 3),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_short_4",
2019-09-01 19:04:03 +00:00
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
2019-08-21 17:22:05 +00:00
(BlockKind::ShortGrass, 4),
2019-09-01 19:04:03 +00:00
make_model(
"voxygen.voxel.sprite.grass.grass_short_5",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::Mushroom, 0),
make_model(
"voxygen.voxel.sprite.mushrooms.mushroom-0",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::Mushroom, 1),
make_model(
"voxygen.voxel.sprite.mushrooms.mushroom-1",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::Mushroom, 2),
make_model(
"voxygen.voxel.sprite.mushrooms.mushroom-2",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::Mushroom, 3),
make_model(
"voxygen.voxel.sprite.mushrooms.mushroom-3",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::Mushroom, 4),
make_model(
"voxygen.voxel.sprite.mushrooms.mushroom-4",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::Mushroom, 5),
make_model(
"voxygen.voxel.sprite.mushrooms.mushroom-5",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::Mushroom, 6),
make_model(
"voxygen.voxel.sprite.mushrooms.mushroom-6",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::Mushroom, 7),
make_model(
"voxygen.voxel.sprite.mushrooms.mushroom-7",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::Mushroom, 8),
make_model(
"voxygen.voxel.sprite.mushrooms.mushroom-8",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::Mushroom, 9),
make_model(
"voxygen.voxel.sprite.mushrooms.mushroom-9",
Vec3::new(-6.0, -6.0, 0.0),
),
),
(
(BlockKind::Mushroom, 10),
make_model(
"voxygen.voxel.sprite.mushrooms.mushroom-10",
Vec3::new(-6.0, -6.0, 0.0),
),
),
2019-09-01 19:04:03 +00:00
(
(BlockKind::Liana, 0),
make_model(
"voxygen.voxel.sprite.lianas.liana-0",
Vec3::new(-1.5, -0.5, -88.0),
),
),
(
(BlockKind::Liana, 1),
make_model(
"voxygen.voxel.sprite.lianas.liana-1",
Vec3::new(-1.0, -0.5, -55.0),
),
),
2019-09-25 20:22:39 +00:00
(
(BlockKind::Velorite, 0),
make_model(
2019-10-02 10:05:17 +00:00
"voxygen.voxel.sprite.velorite.velorite_ore",
Vec3::new(-5.0, -5.0, -5.0),
2019-09-25 20:22:39 +00:00
),
),
(
(BlockKind::VeloriteFrag, 0),
make_model(
"voxygen.voxel.sprite.velorite.velorite_1",
Vec3::new(-3.0, -5.0, 0.0),
),
),
(
(BlockKind::VeloriteFrag, 1),
make_model(
"voxygen.voxel.sprite.velorite.velorite_2",
Vec3::new(-3.0, -5.0, 0.0),
),
),
(
(BlockKind::VeloriteFrag, 2),
make_model(
"voxygen.voxel.sprite.velorite.velorite_3",
Vec3::new(-3.0, -5.0, 0.0),
),
),
(
(BlockKind::VeloriteFrag, 3),
make_model(
"voxygen.voxel.sprite.velorite.velorite_4",
Vec3::new(-3.0, -5.0, 0.0),
),
),
(
(BlockKind::VeloriteFrag, 4),
make_model(
"voxygen.voxel.sprite.velorite.velorite_5",
Vec3::new(-3.0, -5.0, 0.0),
),
),
(
(BlockKind::VeloriteFrag, 5),
make_model(
"voxygen.voxel.sprite.velorite.velorite_6",
Vec3::new(-3.0, -5.0, 0.0),
),
),
(
(BlockKind::VeloriteFrag, 6),
make_model(
"voxygen.voxel.sprite.velorite.velorite_7",
Vec3::new(-3.0, -5.0, 0.0),
),
),
(
(BlockKind::VeloriteFrag, 7),
make_model(
"voxygen.voxel.sprite.velorite.velorite_8",
Vec3::new(-3.0, -5.0, 0.0),
),
),
(
(BlockKind::VeloriteFrag, 8),
make_model(
"voxygen.voxel.sprite.velorite.velorite_9",
Vec3::new(-3.0, -5.0, 0.0),
),
),
(
(BlockKind::VeloriteFrag, 9),
make_model(
"voxygen.voxel.sprite.velorite.velorite_10",
Vec3::new(-3.0, -5.0, 0.0),
),
),
2019-10-09 19:28:05 +00:00
(
(BlockKind::Chest, 0),
make_model(
"voxygen.voxel.sprite.chests.chest",
Vec3::new(-7.0, -5.0, -0.0),
),
),
(
(BlockKind::Chest, 1),
make_model(
"voxygen.voxel.sprite.chests.chest_gold",
Vec3::new(-7.0, -5.0, -0.0),
),
),
(
(BlockKind::Chest, 2),
make_model(
"voxygen.voxel.sprite.chests.chest_dark",
Vec3::new(-7.0, -5.0, -0.0),
),
),
(
(BlockKind::Chest, 3),
make_model(
"voxygen.voxel.sprite.chests.chest_vines",
Vec3::new(-7.0, -5.0, -0.0),
),
),
]
.into_iter()
.collect(),
2019-10-17 16:11:55 +00:00
waves: renderer
.create_texture(
&assets::load_expect("voxygen.texture.waves"),
2019-11-05 15:08:54 +00:00
Some(gfx::texture::FilterMethod::Trilinear),
2019-10-17 16:11:55 +00:00
Some(gfx::texture::WrapMode::Tile),
)
.expect("Failed to create wave texture"),
phantom: PhantomData,
2019-01-15 15:13:11 +00:00
}
}
2019-01-23 20:01:58 +00:00
/// Maintain terrain data. To be called once per tick.
2019-06-06 09:04:37 +00:00
pub fn maintain(
&mut self,
renderer: &mut Renderer,
client: &Client,
focus_pos: Vec3<f32>,
loaded_distance: f32,
view_mat: Mat4<f32>,
proj_mat: Mat4<f32>,
) {
2019-01-23 20:01:58 +00:00
let current_tick = client.get_tick();
2019-09-24 15:43:51 +00:00
let current_time = client.state().get_time();
2019-01-23 20:01:58 +00:00
// Add any recently created or changed chunks to the list of chunks to be meshed.
for (modified, pos) in client
.state()
.terrain_changes()
.modified_chunks
.iter()
.map(|c| (true, c))
2019-07-01 13:38:45 +00:00
.chain(
client
.state()
.terrain_changes()
2019-07-01 13:38:45 +00:00
.new_chunks
.iter()
.map(|c| (false, c)),
)
2019-01-23 20:01:58 +00:00
{
// TODO: ANOTHER PROBLEM HERE!
// What happens if the block on the edge of a chunk gets modified? We need to spawn
// a mesh worker to remesh its neighbour(s) too since their ambient occlusion and face
// elision information changes too!
for i in -1..2 {
for j in -1..2 {
let pos = pos + Vec2::new(i, j);
if !self.chunks.contains_key(&pos) || modified {
let mut neighbours = true;
for i in -1..2 {
for j in -1..2 {
neighbours &= client
.state()
.terrain()
.get_key(pos + Vec2::new(i, j))
.is_some();
}
}
if neighbours {
2019-07-01 13:38:45 +00:00
self.mesh_todo.insert(
pos,
2019-07-01 13:38:45 +00:00
ChunkMeshState {
pos,
started_tick: current_tick,
active_worker: None,
2019-07-01 13:38:45 +00:00
},
);
}
}
}
2019-01-23 20:01:58 +00:00
}
}
// Add the chunks belonging to recently changed blocks to the list of chunks to be meshed
for pos in client
.state()
.terrain_changes()
.modified_blocks
.iter()
.map(|(p, _)| *p)
{
let chunk_pos = client.state().terrain().pos_key(pos);
let new_mesh_state = ChunkMeshState {
pos: chunk_pos,
started_tick: current_tick,
active_worker: None,
};
// Only mesh if this chunk has all its neighbors
// If it does have all its neighbors either it should have already been meshed or is in
// mesh_todo
match self.mesh_todo.entry(chunk_pos) {
Entry::Occupied(mut entry) => {
entry.insert(new_mesh_state);
}
Entry::Vacant(entry) => {
if self.chunks.contains_key(&chunk_pos) {
entry.insert(new_mesh_state);
}
}
}
// Handle block changes on chunk borders
for x in -1..2 {
for y in -1..2 {
let neighbour_pos = pos + Vec3::new(x, y, 0);
let neighbour_chunk_pos = client.state().terrain().pos_key(neighbour_pos);
if neighbour_chunk_pos != chunk_pos {
let new_mesh_state = ChunkMeshState {
pos: neighbour_chunk_pos,
started_tick: current_tick,
active_worker: None,
};
// Only mesh if this chunk has all its neighbors
match self.mesh_todo.entry(neighbour_chunk_pos) {
Entry::Occupied(mut entry) => {
entry.insert(new_mesh_state);
}
Entry::Vacant(entry) => {
if self.chunks.contains_key(&neighbour_chunk_pos) {
entry.insert(new_mesh_state);
}
}
}
}
// TODO: Remesh all neighbours because we have complex lighting now
/*self.mesh_todo.insert(
neighbour_chunk_pos,
ChunkMeshState {
pos: chunk_pos + Vec2::new(x, y),
started_tick: current_tick,
active_worker: None,
},
);
*/
}
}
}
// Remove any models for chunks that have been recently removed.
for pos in &client.state().terrain_changes().removed_chunks {
2019-01-23 20:01:58 +00:00
self.chunks.remove(pos);
self.mesh_todo.remove(pos);
2019-01-23 20:01:58 +00:00
}
2019-07-04 17:13:29 +00:00
for todo in self
.mesh_todo
.values_mut()
.filter(|todo| {
todo.active_worker
.map(|worker_tick| worker_tick < todo.started_tick)
.unwrap_or(true)
})
2019-07-04 18:03:44 +00:00
.min_by_key(|todo| todo.active_worker.unwrap_or(todo.started_tick))
{
2019-07-12 18:51:22 +00:00
if client.thread_pool().queued_jobs() > 0 {
break;
}
// Find the area of the terrain we want. Because meshing needs to compute things like
// ambient occlusion and edge elision, we also need the borders of the chunk's
// neighbours too (hence the `- 1` and `+ 1`).
let aabr = Aabr {
min: todo
.pos
.map2(VolGrid2d::<V>::chunk_size(), |e, sz| e * sz as i32 - 1),
max: todo.pos.map2(VolGrid2d::<V>::chunk_size(), |e, sz| {
(e + 1) * sz as i32 + 1
}),
};
// Copy out the chunk data we need to perform the meshing. We do this by taking a
// sample of the terrain that includes both the chunk we want and its neighbours.
let volume = match client.state().terrain().sample(aabr) {
Ok(sample) => sample,
// Either this chunk or its neighbours doesn't yet exist, so we keep it in the
// queue to be processed at a later date when we have its neighbours.
common: Rework volume API See the doc comments in `common/src/vol.rs` for more information on the API itself. The changes include: * Consistent `Err`/`Error` naming. * Types are named `...Error`. * `enum` variants are named `...Err`. * Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation to an upcoming change where a “map” in the game related sense will be added. * Add volume iterators. There are two types of them: * _Position_ iterators obtained from the trait `IntoPosIterator` using the method `fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...` which returns an iterator over `Vec3<i32>`. * _Volume_ iterators obtained from the trait `IntoVolIterator` using the method `fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...` which returns an iterator over `(Vec3<i32>, &Self::Vox)`. Those traits will usually be implemented by references to volume types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some type which usually implements several volume traits, such as `Chunk`). * _Position_ iterators iterate over the positions valid for that volume. * _Volume_ iterators do the same but return not only the position but also the voxel at that position, in each iteration. * Introduce trait `RectSizedVol` for the use case which we have with `Chonk`: A `Chonk` is sized only in x and y direction. * Introduce traits `RasterableVol`, `RectRasterableVol` * `RasterableVol` represents a volume that is compile-time sized and has its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen because such a volume can be used with `VolGrid3d`. * `RectRasterableVol` represents a volume that is compile-time sized at least in x and y direction and has its lower bound at `(0, 0, z)`. There's no requirement on he lower bound or size in z direction. The name `RectRasterableVol` was chosen because such a volume can be used with `VolGrid2d`.
2019-09-03 22:23:29 +00:00
Err(VolGrid2dError::NoSuchChunk) => return,
_ => panic!("Unhandled edge case"),
};
// The region to actually mesh
2019-06-04 17:19:40 +00:00
let min_z = volume
.iter()
2019-06-04 17:19:40 +00:00
.fold(i32::MAX, |min, (_, chunk)| chunk.get_min_z().min(min));
let max_z = volume
.iter()
2019-06-04 17:19:40 +00:00
.fold(i32::MIN, |max, (_, chunk)| chunk.get_max_z().max(max));
let aabb = Aabb {
2019-06-04 17:19:40 +00:00
min: Vec3::from(aabr.min) + Vec3::unit_z() * (min_z - 1),
max: Vec3::from(aabr.max) + Vec3::unit_z() * (max_z + 1),
};
// Clone various things so that they can be moved into the thread.
let send = self.mesh_send_tmp.clone();
let pos = todo.pos;
// Queue the worker thread.
let started_tick = todo.started_tick;
client.thread_pool().execute(move || {
2019-06-06 09:04:37 +00:00
let _ = send.send(mesh_worker(
pos,
(min_z as f32, max_z as f32),
started_tick,
2019-06-06 09:04:37 +00:00
volume,
aabb,
));
2019-01-23 20:01:58 +00:00
});
todo.active_worker = Some(todo.started_tick);
}
2019-01-23 20:01:58 +00:00
// Receive a chunk mesh from a worker thread and upload it to the GPU, then store it.
// Only pull out one chunk per frame to avoid an unacceptable amount of blocking lag due
// to the GPU upload. That still gives us a 60 chunks / second budget to play with.
if let Ok(response) = self.mesh_recv.recv_timeout(Duration::new(0, 0)) {
match self.mesh_todo.get(&response.pos) {
2019-01-23 20:01:58 +00:00
// It's the mesh we want, insert the newly finished model into the terrain model
// data structure (convert the mesh to a model first of course).
Some(todo) if response.started_tick <= todo.started_tick => {
2019-09-24 15:43:51 +00:00
let load_time = self
.chunks
.get(&response.pos)
.map(|chunk| chunk.load_time)
.unwrap_or(current_time as f32);
self.chunks.insert(
response.pos,
TerrainChunkData {
2019-09-24 15:43:51 +00:00
load_time,
opaque_model: renderer
.create_model(&response.opaque_mesh)
.expect("Failed to upload chunk mesh to the GPU!"),
fluid_model: if response.fluid_mesh.vertices().len() > 0 {
Some(
renderer
.create_model(&response.fluid_mesh)
.expect("Failed to upload chunk mesh to the GPU!"),
)
} else {
None
},
sprite_instances: response
.sprite_instances
.into_iter()
.map(|(kind, instances)| {
(
kind,
renderer.create_instances(&instances).expect(
"Failed to upload chunk sprite instances to the GPU!",
),
)
})
.collect(),
locals: renderer
.create_consts(&[TerrainLocals {
model_offs: Vec3::from(
response.pos.map2(VolGrid2d::<V>::chunk_size(), |e, sz| {
e as f32 * sz as f32
}),
)
.into_array(),
2019-09-24 15:43:51 +00:00
load_time,
}])
.expect("Failed to upload chunk locals to the GPU!"),
2019-06-06 09:04:37 +00:00
visible: false,
z_bounds: response.z_bounds,
},
);
if response.started_tick == todo.started_tick {
self.mesh_todo.remove(&response.pos);
}
}
2019-01-23 20:01:58 +00:00
// Chunk must have been removed, or it was spawned on an old tick. Drop the mesh
// since it's either out of date or no longer needed.
_ => {}
2019-01-23 20:01:58 +00:00
}
}
2019-01-15 15:13:11 +00:00
2019-06-06 09:04:37 +00:00
// Construct view frustum
let frustum = Frustum::from_modelview_projection((proj_mat * view_mat).into_col_arrays());
2019-06-06 09:04:37 +00:00
// Update chunk visibility
let chunk_sz = V::RECT_SIZE.x as f32;
2019-06-06 09:04:37 +00:00
for (pos, chunk) in &mut self.chunks {
let chunk_pos = pos.map(|e| e as f32 * chunk_sz);
// Limit focus_pos to chunk bounds and ensure the chunk is within the fog boundary
let nearest_in_chunk = Vec2::from(focus_pos).clamped(chunk_pos, chunk_pos + chunk_sz);
let in_range = Vec2::<f32>::from(focus_pos).distance_squared(nearest_in_chunk)
< loaded_distance.powf(2.0);
if !in_range {
chunk.visible = in_range;
continue;
}
// Ensure the chunk is within the view frustum
let chunk_min = [chunk_pos.x, chunk_pos.y, chunk.z_bounds.0];
let chunk_max = [
chunk_pos.x + chunk_sz,
chunk_pos.y + chunk_sz,
chunk.z_bounds.1,
];
let in_frustum = frustum.aabb_intersecting(chunk_min, chunk_max);
chunk.visible = in_frustum;
2019-06-06 09:04:37 +00:00
}
}
pub fn chunk_count(&self) -> usize {
self.chunks.len()
}
pub fn visible_chunk_count(&self) -> usize {
self.chunks.iter().filter(|(_, c)| c.visible).count()
}
2019-07-21 15:04:36 +00:00
pub fn render(
&self,
renderer: &mut Renderer,
globals: &Consts<Globals>,
lights: &Consts<Light>,
2019-09-25 12:00:00 +00:00
shadows: &Consts<Shadow>,
2019-08-19 21:54:16 +00:00
focus_pos: Vec3<f32>,
2019-07-21 15:04:36 +00:00
) {
let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
(e as i32).div_euclid(sz as i32)
});
let chunks = &self.chunks;
let chunk_iter = Spiral2d::new()
.scan(0, |n, rpos| {
if *n >= chunks.len() {
None
} else {
*n += 1;
let pos = focus_chunk + rpos;
Some(chunks.get(&pos).map(|c| (pos, c)))
}
})
.filter_map(|x| x);
// Opaque
for (_, chunk) in chunk_iter.clone() {
if chunk.visible {
2019-09-25 12:00:00 +00:00
renderer.render_terrain_chunk(
&chunk.opaque_model,
globals,
&chunk.locals,
lights,
shadows,
);
}
}
}
pub fn render_translucent(
&self,
renderer: &mut Renderer,
globals: &Consts<Globals>,
lights: &Consts<Light>,
shadows: &Consts<Shadow>,
focus_pos: Vec3<f32>,
) {
let focus_chunk = Vec2::from(focus_pos).map2(TerrainChunk::RECT_SIZE, |e: f32, sz| {
(e as i32).div_euclid(sz as i32)
});
let chunks = &self.chunks;
let chunk_iter = Spiral2d::new()
.scan(0, |n, rpos| {
if *n >= chunks.len() {
None
} else {
*n += 1;
let pos = focus_chunk + rpos;
Some(chunks.get(&pos).map(|c| (pos, c)))
}
})
.filter_map(|x| x);
2019-08-19 21:54:16 +00:00
// Terrain sprites
for (pos, chunk) in chunk_iter.clone() {
if chunk.visible {
2019-08-19 21:54:16 +00:00
const SPRITE_RENDER_DISTANCE: f32 = 128.0;
let chunk_center =
pos.map2(V::RECT_SIZE, |e, sz: u32| (e as f32 + 0.5) * sz as f32);
if Vec2::from(focus_pos).distance_squared(chunk_center)
< SPRITE_RENDER_DISTANCE * SPRITE_RENDER_DISTANCE
{
for (kind, instances) in &chunk.sprite_instances {
renderer.render_sprites(
&self.sprite_models[&kind],
globals,
&instances,
lights,
2019-09-25 12:00:00 +00:00
shadows,
);
}
2019-08-19 21:54:16 +00:00
}
}
}
// Translucent
chunk_iter
.clone()
.filter(|(_, chunk)| chunk.visible)
.filter_map(|(_, chunk)| {
chunk
.fluid_model
.as_ref()
.map(|model| (model, &chunk.locals))
})
.for_each(|(model, locals)| {
2019-10-17 16:11:55 +00:00
renderer.render_fluid_chunk(model, globals, locals, lights, shadows, &self.waves)
});
}
}
#[derive(Clone)]
struct Spiral2d {
layer: i32,
i: i32,
}
impl Spiral2d {
pub fn new() -> Self {
Self { layer: 0, i: 0 }
}
}
impl Iterator for Spiral2d {
type Item = Vec2<i32>;
fn next(&mut self) -> Option<Self::Item> {
let layer_size = (self.layer * 8 + 4 * self.layer.min(1) - 4).max(1);
if self.i >= layer_size {
self.layer += 1;
self.i = 0;
}
let layer_size = (self.layer * 8 + 4 * self.layer.min(1) - 4).max(1);
let pos = Vec2::new(
-self.layer + (self.i - (layer_size / 4) * 0).max(0).min(self.layer * 2)
- (self.i - (layer_size / 4) * 2).max(0).min(self.layer * 2),
-self.layer + (self.i - (layer_size / 4) * 1).max(0).min(self.layer * 2)
- (self.i - (layer_size / 4) * 3).max(0).min(self.layer * 2),
);
self.i += 1;
Some(pos)
2019-01-15 15:13:11 +00:00
}
}