diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e9557ce5b..cdd898490a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Linearize light colors on the CPU rather than in shaders on the GPU - You can no longer stack self buffs - Renamed "Burning Potion" to "Potion of Combustion" +- Render LoD terrain on the character selection screen ### Removed - Medium and large potions from all loot tables diff --git a/assets/voxygen/shaders/include/shadows.glsl b/assets/voxygen/shaders/include/shadows.glsl index 25018a00e1..4460c781ce 100644 --- a/assets/voxygen/shaders/include/shadows.glsl +++ b/assets/voxygen/shaders/include/shadows.glsl @@ -151,6 +151,10 @@ float ShadowCalculationPoint(uint lightIndex, vec3 fragToLight, vec3 fragNorm, / float ShadowCalculationDirected(in vec3 fragPos)//in vec4 /*light_pos[2]*/sun_pos, vec3 fragPos) { + // Don't try to calculate directed shadows if there are no directed light sources + // Applies, for example, in the char select menu + if (light_shadow_count.z < 1) { return 1.0; } + float bias = 0.000;//0.0005;//-0.0001;// 0.05 / (2.0 * view_distance.x); float diskRadius = 0.01; const vec3 sampleOffsetDirections[20] = vec3[] diff --git a/assets/voxygen/voxel/fixture/selection_bg.vox b/assets/voxygen/voxel/fixture/selection_bg.vox deleted file mode 100644 index dfe8546cfd..0000000000 --- a/assets/voxygen/voxel/fixture/selection_bg.vox +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:88283bacea75c38947c0816d7308df198e5771d1853767398af3466d0fb1b106 -size 595784 diff --git a/client/src/lib.rs b/client/src/lib.rs index a9a241c4bd..383a3f537e 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -245,6 +245,7 @@ pub struct Client { available_recipes: HashMap>, lod_zones: HashMap, lod::Zone>, lod_last_requested: Option, + lod_pos_fallback: Option>, force_update_counter: u64, max_group_size: u32, @@ -760,6 +761,7 @@ impl Client { lod_zones: HashMap::new(), lod_last_requested: None, + lod_pos_fallback: None, force_update_counter: 0, @@ -901,7 +903,7 @@ impl Client { | ClientGeneral::DeleteCharacter(_) | ClientGeneral::Character(_, _) | ClientGeneral::Spectate(_) => &mut self.character_screen_stream, - //Only in game + // Only in game ClientGeneral::ControllerInputs(_) | ClientGeneral::ControlEvent(_) | ClientGeneral::ControlAction(_) @@ -922,7 +924,7 @@ impl Client { } &mut self.in_game_stream }, - //Only in game, terrain + // Terrain ClientGeneral::TerrainChunkRequest { .. } | ClientGeneral::LodZoneRequest { .. } => { #[cfg(feature = "tracy")] @@ -931,7 +933,7 @@ impl Client { } &mut self.terrain_stream }, - //Always possible + // Always possible ClientGeneral::ChatMsg(_) | ClientGeneral::Command(_, _) | ClientGeneral::Terminate => &mut self.general_stream, @@ -1209,6 +1211,10 @@ impl Client { pub fn lod_zones(&self) -> &HashMap, lod::Zone> { &self.lod_zones } + /// Set the fallback position used for loading LoD zones when the client + /// entity does not have a position. + pub fn set_lod_pos_fallback(&mut self, pos: Vec2) { self.lod_pos_fallback = Some(pos); } + /// Returns whether the specified recipe can be crafted and the sprite, if /// any, that is required to do so. pub fn can_craft_recipe(&self, recipe: &str, amount: u32) -> (bool, Option) { @@ -2101,9 +2107,11 @@ impl Client { let now = Instant::now(); self.pending_chunks .retain(|_, created| now.duration_since(*created) < Duration::from_secs(3)); + } + if let Some(lod_pos) = pos.map(|p| p.0.xy()).or(self.lod_pos_fallback) { // Manage LoD zones - let lod_zone = pos.0.xy().map(|e| lod::from_wpos(e as i32)); + let lod_zone = lod_pos.map(|e| lod::from_wpos(e as i32)); // Request LoD zones that are in range if self diff --git a/common/net/src/msg/client.rs b/common/net/src/msg/client.rs index 8b70fbde15..fc02f6b2a2 100644 --- a/common/net/src/msg/client.rs +++ b/common/net/src/msg/client.rs @@ -129,7 +129,6 @@ impl ClientMsg { | ClientGeneral::ExitInGame | ClientGeneral::PlayerPhysics { .. } | ClientGeneral::TerrainChunkRequest { .. } - | ClientGeneral::LodZoneRequest { .. } | ClientGeneral::UnlockSkill(_) | ClientGeneral::RequestSiteInfo(_) | ClientGeneral::RequestPlayerPhysics { .. } @@ -141,7 +140,9 @@ impl ClientMsg { //Always possible ClientGeneral::ChatMsg(_) | ClientGeneral::Command(_, _) - | ClientGeneral::Terminate => true, + | ClientGeneral::Terminate + // LodZoneRequest is required by the char select screen + | ClientGeneral::LodZoneRequest { .. } => true, } }, ClientMsg::Ping(_) => true, diff --git a/common/net/src/msg/server.rs b/common/net/src/msg/server.rs index b901d284e7..89c79d76fa 100644 --- a/common/net/src/msg/server.rs +++ b/common/net/src/msg/server.rs @@ -330,7 +330,6 @@ impl ServerMsg { | ServerGeneral::InventoryUpdate(_, _) | ServerGeneral::GroupInventoryUpdate(_, _, _) | ServerGeneral::TerrainChunkUpdate { .. } - | ServerGeneral::LodZoneUpdate { .. } | ServerGeneral::TerrainBlockUpdates(_) | ServerGeneral::SetViewDistance(_) | ServerGeneral::Outcomes(_) @@ -354,7 +353,8 @@ impl ServerMsg { | ServerGeneral::CreateEntity(_) | ServerGeneral::DeleteEntity(_) | ServerGeneral::Disconnect(_) - | ServerGeneral::Notification(_) => true, + | ServerGeneral::Notification(_) + | ServerGeneral::LodZoneUpdate { .. } => true, } }, ServerMsg::Ping(_) => true, diff --git a/server/src/client.rs b/server/src/client.rs index c56e2a5eac..8b785ff663 100644 --- a/server/src/client.rs +++ b/server/src/client.rs @@ -195,7 +195,7 @@ impl Client { | ServerGeneral::SpectatePosition(_) => { PreparedMsg::new(2, &g, &self.in_game_stream_params) }, - //In-game related, terrain + // Terrain ServerGeneral::TerrainChunkUpdate { .. } | ServerGeneral::LodZoneUpdate { .. } | ServerGeneral::TerrainBlockUpdates(_) => { diff --git a/server/src/sys/msg/terrain.rs b/server/src/sys/msg/terrain.rs index 6c81c837a5..46ecff6570 100644 --- a/server/src/sys/msg/terrain.rs +++ b/server/src/sys/msg/terrain.rs @@ -61,61 +61,63 @@ impl<'a> System<'a> for Sys { |(chunk_send_emitter, server_emitter), (entity, client, maybe_presence)| { let mut chunk_requests = Vec::new(); let _ = super::try_recv_all(client, 5, |client, msg| { - let presence = match maybe_presence { - Some(g) => g, - None => { - debug!(?entity, "client is not in_game, ignoring msg"); - trace!(?msg, "ignored msg content"); - if matches!(msg, ClientGeneral::TerrainChunkRequest { .. }) { - network_metrics.chunks_request_dropped.inc(); - } - return Ok(()); - }, - }; - match msg { - ClientGeneral::TerrainChunkRequest { key } => { - let in_vd = if let Some(pos) = positions.get(entity) { - pos.0.xy().map(|e| e as f64).distance_squared( - key.map(|e| e as f64 + 0.5) - * TerrainChunkSize::RECT_SIZE.map(|e| e as f64), - ) < ((presence.terrain_view_distance.current() as f64 - 1.0 - + 2.5 * 2.0_f64.sqrt()) - * TerrainChunkSize::RECT_SIZE.x as f64) - .powi(2) - } else { - true - }; - if in_vd { - if terrain.get_key_arc(key).is_some() { - network_metrics.chunks_served_from_memory.inc(); - chunk_send_emitter.emit(ChunkSendEntry { - chunk_key: key, - entity, - }); - } else { - network_metrics.chunks_generation_triggered.inc(); - chunk_requests.push(ChunkRequest { entity, key }); + // SPECIAL CASE: LOD zone requests can be sent by non-present players + if let ClientGeneral::LodZoneRequest { key } = &msg { + client.send(ServerGeneral::LodZoneUpdate { + key: *key, + zone: lod.zone(*key).clone(), + })?; + } else { + let presence = match maybe_presence { + Some(g) => g, + None => { + debug!(?entity, "client is not in_game, ignoring msg"); + trace!(?msg, "ignored msg content"); + if matches!(msg, ClientGeneral::TerrainChunkRequest { .. }) { + network_metrics.chunks_request_dropped.inc(); } - } else { - network_metrics.chunks_request_dropped.inc(); - } - }, - ClientGeneral::LodZoneRequest { key } => { - client.send(ServerGeneral::LodZoneUpdate { - key, - zone: lod.zone(key).clone(), - })?; - }, - _ => { - debug!( - "Kicking possibly misbehaving client due to invalud terrain \ - request" - ); - server_emitter.emit(ServerEvent::ClientDisconnect( - entity, - common::comp::DisconnectReason::NetworkError, - )); - }, + return Ok(()); + }, + }; + match msg { + ClientGeneral::TerrainChunkRequest { key } => { + let in_vd = if let Some(pos) = positions.get(entity) { + pos.0.xy().map(|e| e as f64).distance_squared( + key.map(|e| e as f64 + 0.5) + * TerrainChunkSize::RECT_SIZE.map(|e| e as f64), + ) < ((presence.terrain_view_distance.current() as f64 - 1.0 + + 2.5 * 2.0_f64.sqrt()) + * TerrainChunkSize::RECT_SIZE.x as f64) + .powi(2) + } else { + true + }; + if in_vd { + if terrain.get_key_arc(key).is_some() { + network_metrics.chunks_served_from_memory.inc(); + chunk_send_emitter.emit(ChunkSendEntry { + chunk_key: key, + entity, + }); + } else { + network_metrics.chunks_generation_triggered.inc(); + chunk_requests.push(ChunkRequest { entity, key }); + } + } else { + network_metrics.chunks_request_dropped.inc(); + } + }, + _ => { + debug!( + "Kicking possibly misbehaving client due to invalud terrain \ + request" + ); + server_emitter.emit(ServerEvent::ClientDisconnect( + entity, + common::comp::DisconnectReason::NetworkError, + )); + }, + } } Ok(()) }); diff --git a/voxygen/src/lib.rs b/voxygen/src/lib.rs index 17ec046081..50873fc443 100644 --- a/voxygen/src/lib.rs +++ b/voxygen/src/lib.rs @@ -16,7 +16,8 @@ let_chains, generic_const_exprs, maybe_uninit_uninit_array, - maybe_uninit_array_assume_init + maybe_uninit_array_assume_init, + closure_lifetime_binder )] #![recursion_limit = "2048"] diff --git a/voxygen/src/menu/char_selection/mod.rs b/voxygen/src/menu/char_selection/mod.rs index 02b86ee6d3..407eaa9524 100644 --- a/voxygen/src/menu/char_selection/mod.rs +++ b/voxygen/src/menu/char_selection/mod.rs @@ -26,10 +26,12 @@ pub struct CharSelectionState { impl CharSelectionState { /// Create a new `CharSelectionState`. pub fn new(global_state: &mut GlobalState, client: Rc>) -> Self { + let sprite_render_context = (global_state.lazy_init)(global_state.window.renderer_mut()); let scene = Scene::new( global_state.window.renderer_mut(), - Some("fixture.selection_bg"), - &client.borrow(), + &mut client.borrow_mut(), + &global_state.settings, + sprite_render_context, ); let char_selection_ui = CharSelectionUi::new(global_state, &client.borrow()); @@ -224,8 +226,12 @@ impl PlayState for CharSelectionState { as f32, }; - self.scene - .maintain(global_state.window.renderer_mut(), scene_data, loadout); + self.scene.maintain( + global_state.window.renderer_mut(), + scene_data, + loadout, + &client, + ); } // Tick the client (currently only to keep the connection alive). diff --git a/voxygen/src/render/renderer.rs b/voxygen/src/render/renderer.rs index c91ab2b3a7..7cec6c479f 100644 --- a/voxygen/src/render/renderer.rs +++ b/voxygen/src/render/renderer.rs @@ -1570,9 +1570,10 @@ pub struct AltIndices { /// The mode with which culling based on the camera position relative to the /// terrain is performed. -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Default)] pub enum CullingMode { /// We need to render all elements of the given structure + #[default] None, /// We only need to render surface and shallow (i.e: in the overlapping /// region) elements of the structure diff --git a/voxygen/src/scene/camera.rs b/voxygen/src/scene/camera.rs index 5b8b1850e6..f90bc04a54 100644 --- a/voxygen/src/scene/camera.rs +++ b/voxygen/src/scene/camera.rs @@ -677,6 +677,9 @@ impl Camera { /// Get the orientation of the camera. pub fn get_orientation(&self) -> Vec3 { self.ori } + /// Get the orientation that the camera is moving toward. + pub fn get_tgt_orientation(&self) -> Vec3 { self.tgt_ori } + /// Get the field of view of the camera in radians, taking into account /// fixation. pub fn get_effective_fov(&self) -> f32 { self.fov * self.fixate } diff --git a/voxygen/src/scene/figure/cache.rs b/voxygen/src/scene/figure/cache.rs index fea728fe2b..549e0c06af 100644 --- a/voxygen/src/scene/figure/cache.rs +++ b/voxygen/src/scene/figure/cache.rs @@ -13,8 +13,7 @@ use crate::{ }, scene::{ camera::CameraMode, - terrain::{get_sprite_instances, BlocksOfInterest, SPRITE_LOD_LEVELS}, - Terrain, + terrain::{get_sprite_instances, BlocksOfInterest, SpriteRenderState, SPRITE_LOD_LEVELS}, }, }; use anim::Skeleton; @@ -632,7 +631,7 @@ where extra: ::Extra, tick: u64, slow_jobs: &SlowJobPool, - terrain: &Terrain, + sprite_render_state: &SpriteRenderState, ) -> (TerrainModelEntryLod<'c>, &'c Skel::Attr) where for<'a> &'a Skel::Body: Into, @@ -695,8 +694,8 @@ where let key = v.key().clone(); let slot = Arc::new(atomic::AtomicCell::new(None)); let manifests = self.manifests.clone(); - let sprite_data = Arc::clone(&terrain.sprite_data); - let sprite_config = Arc::clone(&terrain.sprite_config); + let sprite_data = Arc::clone(&sprite_render_state.sprite_data); + let sprite_config = Arc::clone(&sprite_render_state.sprite_config); let slot_ = Arc::clone(&slot); slow_jobs.spawn("FIGURE_MESHING", move || { diff --git a/voxygen/src/scene/figure/mod.rs b/voxygen/src/scene/figure/mod.rs index 3683843c6b..954370c1ee 100644 --- a/voxygen/src/scene/figure/mod.rs +++ b/voxygen/src/scene/figure/mod.rs @@ -6288,7 +6288,7 @@ impl FigureMgr { Arc::clone(vol), tick, &slow_jobs, - terrain, + &terrain.sprite_render_state, ); let state = self @@ -6315,7 +6315,7 @@ impl FigureMgr { (), tick, &slow_jobs, - terrain, + &terrain.sprite_render_state, ) } else { // No way to determine model (this is okay, we might just not have received @@ -7535,7 +7535,7 @@ impl FigureStateMeta { pub struct FigureState { meta: FigureStateMeta, skeleton: S, - extra: D, + pub extra: D, } impl Deref for FigureState { diff --git a/voxygen/src/scene/mod.rs b/voxygen/src/scene/mod.rs index a6c1bd245d..167913f647 100644 --- a/voxygen/src/scene/mod.rs +++ b/voxygen/src/scene/mod.rs @@ -1430,7 +1430,7 @@ impl Scene { // Draws sprites let mut sprite_drawer = first_pass.draw_sprites( &self.terrain.sprite_globals, - &self.terrain.sprite_atlas_textures, + &self.terrain.sprite_render_state.sprite_atlas_textures, ); self.figure_mgr.render_sprites( &mut sprite_drawer, diff --git a/voxygen/src/scene/simple.rs b/voxygen/src/scene/simple.rs index 8d523a3e4d..be3515ab76 100644 --- a/voxygen/src/scene/simple.rs +++ b/voxygen/src/scene/simple.rs @@ -1,35 +1,30 @@ use crate::{ - mesh::{greedy::GreedyMesh, segment::generate_mesh_base_vol_figure}, render::{ - create_skybox_mesh, pipelines::FigureSpriteAtlasData, BoneMeshes, Consts, FigureModel, - FirstPassDrawer, GlobalModel, Globals, GlobalsBindGroup, Light, LodData, Mesh, Model, + create_skybox_mesh, pipelines::terrain::BoundLocals as BoundTerrainLocals, AltIndices, + Consts, FirstPassDrawer, GlobalModel, Globals, GlobalsBindGroup, Light, Model, PointLightMatrix, RainOcclusionLocals, Renderer, Shadow, ShadowLocals, SkyboxVertex, - TerrainVertex, + SpriteGlobalsBindGroup, }, scene::{ camera::{self, Camera, CameraMode}, - figure::{ - load_mesh, FigureAtlas, FigureModelCache, FigureModelEntry, FigureState, - FigureUpdateCommonParameters, - }, + figure::{FigureAtlas, FigureModelCache, FigureState, FigureUpdateCommonParameters}, + terrain::{SpriteRenderContext, SpriteRenderState}, + CloudsLocals, CullingMode, Lod, PostProcessLocals, }, window::{Event, PressState}, + Settings, }; -use anim::{ - character::{CharacterSkeleton, IdleAnimation, SkeletonAttr}, - fixture::FixtureSkeleton, - Animation, -}; +use anim::{character::CharacterSkeleton, ship::ShipSkeleton, Animation}; use client::Client; use common::{ comp::{ humanoid, inventory::{slot::EquipSlot, Inventory}, item::ItemKind, + ship, }, - figure::Segment, slowjob::SlowJobPool, - terrain::BlockKind, + terrain::{BlockKind, CoordinateConversions}, vol::{BaseVol, ReadVol}, }; use vek::*; @@ -46,18 +41,6 @@ impl ReadVol for VoidVol { fn get(&self, _pos: Vec3) -> Result<&'_ Self::Vox, Self::Error> { Ok(&()) } } -fn generate_mesh( - greedy: &mut GreedyMesh<'_, FigureSpriteAtlasData>, - mesh: &mut Mesh, - segment: Segment, - offset: Vec3, - bone_idx: u8, -) -> BoneMeshes { - let (opaque, _, /* shadow */ _, bounds) = - generate_mesh_base_vol_figure(segment, (greedy, mesh, offset, Vec3::one(), bone_idx)); - (opaque /* , shadow */, bounds) -} - struct Skybox { model: Model, } @@ -68,17 +51,22 @@ pub struct Scene { camera: Camera, skybox: Skybox, - lod: LodData, + lod: Lod, map_bounds: Vec2, figure_atlas: FigureAtlas, - backdrop: Option<(FigureModelEntry<1>, FigureState)>, - figure_model_cache: FigureModelCache, - figure_state: Option>, + sprite_render_state: SpriteRenderState, + sprite_globals: SpriteGlobalsBindGroup, - //turning_camera: bool, - turning_character: bool, - char_ori: f32, + turning_camera: bool, + + char_pos: Vec3, + char_state: Option>, + char_model_cache: FigureModelCache, + + airship_pos: Vec3, + airship_state: Option>, + airship_model_cache: FigureModelCache, } pub struct SceneData<'a> { @@ -95,8 +83,13 @@ pub struct SceneData<'a> { } impl Scene { - pub fn new(renderer: &mut Renderer, backdrop: Option<&str>, client: &Client) -> Self { - let start_angle = 90.0f32.to_radians(); + pub fn new( + renderer: &mut Renderer, + client: &mut Client, + settings: &Settings, + sprite_render_context: SpriteRenderContext, + ) -> Self { + let start_angle = -90.0f32.to_radians(); let resolution = renderer.resolution().map(|e| e as f32); let map_bounds = Vec2::new( @@ -105,11 +98,10 @@ impl Scene { ); let mut camera = Camera::new(resolution.x / resolution.y, CameraMode::ThirdPerson); - camera.set_focus_pos(Vec3::unit_z() * 1.5); camera.set_distance(3.4); - camera.set_orientation(Vec3::new(start_angle, 0.0, 0.0)); + camera.set_orientation(Vec3::new(start_angle, 0.1, 0.0)); - let mut figure_atlas = FigureAtlas::new(renderer); + let figure_atlas = FigureAtlas::new(renderer); let data = GlobalModel { globals: renderer.create_consts(&[Globals::default()]), @@ -120,81 +112,48 @@ impl Scene { .create_rain_occlusion_bound_locals(&[RainOcclusionLocals::default()]), point_light_matrices: Box::new([PointLightMatrix::default(); 126]), }; - let lod = LodData::dummy(renderer); + let lod = Lod::new(renderer, client, settings); - let globals_bind_group = renderer.bind_globals(&data, &lod); + let globals_bind_group = renderer.bind_globals(&data, lod.get_data()); + + let world = client.world_data(); + let char_chunk = world.chunk_size().map(|e| e as i32 / 2); + let char_pos = char_chunk.cpos_to_wpos().map(|e| e as f32).with_z( + world + .lod_alt + .get(char_chunk) + .map_or(0.0, |z| *z as f32 + 48.0), + ); + client.set_lod_pos_fallback(char_pos.xy()); + client.set_lod_distance(settings.graphics.lod_distance); Self { - data, globals_bind_group, skybox: Skybox { model: renderer.create_model(&create_skybox_mesh()).unwrap(), }, - lod, map_bounds, - figure_model_cache: FigureModelCache::new(), - figure_state: None, - - backdrop: backdrop.map(|specifier| { - let mut state = FigureState::new(renderer, FixtureSkeleton, ()); - let mut greedy = FigureModel::make_greedy(); - let mut opaque_mesh = Mesh::new(); - let (segment, offset) = load_mesh(specifier, Vec3::new(-55.0, -49.5, -2.0)); - let (_opaque_mesh, bounds) = - generate_mesh(&mut greedy, &mut opaque_mesh, segment, offset, 0); - // NOTE: Since MagicaVoxel sizes are limited to 256 × 256 × 256, and there are - // at most 3 meshed vertices per unique vertex, we know the - // total size is bounded by 2^24 * 3 * 1.5 which is bounded by - // 2^27, which fits in a u32. - let range = 0..opaque_mesh.vertices().len() as u32; - let (atlas_texture_data, atlas_size) = greedy.finalize(); - let model = figure_atlas.create_figure( - renderer, - atlas_texture_data, - atlas_size, - (opaque_mesh, bounds), - [range], - ); - let mut buf = [Default::default(); anim::MAX_BONE_COUNT]; - let common_params = FigureUpdateCommonParameters { - entity: None, - pos: anim::vek::Vec3::zero(), - ori: anim::vek::Quaternion::rotation_from_to_3d( - anim::vek::Vec3::unit_y(), - anim::vek::Vec3::new(start_angle.sin(), -start_angle.cos(), 0.0), - ), - scale: 1.0, - mount_transform_pos: None, - body: None, - tools: (None, None), - col: Rgba::broadcast(1.0), - dt: 15.0, // Want to get there immediately. - _lpindex: 0, - _visible: true, - is_player: false, - _camera: &camera, - terrain: None, - ground_vel: Vec3::zero(), - }; - state.update( - renderer, - None, - &mut buf, - &common_params, - 1.0, - Some(&model), - (), - ); - (model, state) - }), figure_atlas, + sprite_render_state: sprite_render_context.state, + sprite_globals: renderer.bind_sprite_globals( + &data, + lod.get_data(), + &sprite_render_context.sprite_verts_buffer, + ), + lod, + data, camera, - //turning_camera: false, - turning_character: false, - char_ori: -start_angle, + turning_camera: false, + char_pos, + char_state: None, + char_model_cache: FigureModelCache::new(), + + airship_pos: char_pos - Vec3::unit_z() * 10.0, + airship_state: None, + airship_model_cache: FigureModelCache::new(), } } @@ -215,20 +174,15 @@ impl Scene { }, Event::MouseButton(button, state) => { if state == PressState::Pressed { - //self.turning_camera = button == MouseButton::Right; - self.turning_character = button == MouseButton::Left; + self.turning_camera = button == MouseButton::Left; } else { - //self.turning_camera = false; - self.turning_character = false; + self.turning_camera = false; } true }, Event::CursorMove(delta) => { - /*if self.turning_camera { - self.camera.rotate_by(Vec3::new(delta.x * 0.01, 0.0, 0.0)) - }*/ - if self.turning_character { - self.char_ori += delta.x * 0.01; + if self.turning_camera { + self.camera.rotate_by(delta.with_z(0.0) * 0.01); } true }, @@ -242,7 +196,13 @@ impl Scene { renderer: &mut Renderer, scene_data: SceneData, inventory: Option<&Inventory>, + client: &Client, ) { + self.camera + .force_focus_pos(self.char_pos + Vec3::unit_z() * 1.5); + let ori = self.camera.get_tgt_orientation(); + self.camera + .set_orientation(Vec3::new(ori.x, ori.y.max(-0.25), ori.z)); self.camera.update( scene_data.time, /* 1.0 / 60.0 */ scene_data.delta_time, @@ -254,13 +214,18 @@ impl Scene { view_mat, proj_mat, cam_pos, + proj_mat_inv, + view_mat_inv, .. } = self.camera.dependents(); - const VD: f32 = 115.0; // View Distance + const VD: f32 = 0.0; // View Distance const TIME: f64 = 8.6 * 60.0 * 60.0; - const SHADOW_NEAR: f32 = 1.0; - const SHADOW_FAR: f32 = 25.0; + const SHADOW_NEAR: f32 = 0.25; + const SHADOW_FAR: f32 = 1.0; + + self.lod + .maintain(renderer, client, self.camera.get_focus_pos(), &self.camera); renderer.update_consts(&mut self.data.globals, &[Globals::new( view_mat, @@ -268,7 +233,7 @@ impl Scene { cam_pos, self.camera.get_focus_pos(), VD, - self.lod.tgt_detail as f32, + self.lod.get_data().tgt_detail as f32, self.map_bounds, TIME, scene_data.time, @@ -288,8 +253,12 @@ impl Scene { self.camera.get_mode(), 250.0, )]); + renderer.update_clouds_locals(CloudsLocals::new(proj_mat_inv, view_mat_inv)); + renderer.update_postprocess_locals(PostProcessLocals::new(proj_mat_inv, view_mat_inv)); - self.figure_model_cache + self.char_model_cache + .clean(&mut self.figure_atlas, scene_data.tick); + self.airship_model_cache .clean(&mut self.figure_atlas, scene_data.tick); let item_info = |equip_slot| { @@ -310,12 +279,37 @@ impl Scene { let hands = (active_tool_hand, second_tool_hand); + fn figure_params( + camera: &Camera, + dt: f32, + pos: Vec3, + ) -> FigureUpdateCommonParameters<'_> { + FigureUpdateCommonParameters { + entity: None, + pos: pos.into(), + ori: anim::vek::Quaternion::identity().rotated_z(std::f32::consts::PI * -0.5), + scale: 1.0, + mount_transform_pos: None, + body: None, + tools: (None, None), + col: Rgba::broadcast(1.0), + dt, + _lpindex: 0, + _visible: true, + is_player: false, + _camera: camera, + terrain: None, + ground_vel: Vec3::zero(), + } + } + if let Some(body) = scene_data.body { - let figure_state = self.figure_state.get_or_insert_with(|| { + let char_state = self.char_state.get_or_insert_with(|| { FigureState::new(renderer, CharacterSkeleton::default(), body) }); - let tgt_skeleton = IdleAnimation::update_skeleton( - figure_state.skeleton_mut(), + let params = figure_params(&self.camera, scene_data.delta_time, self.char_pos); + let tgt_skeleton = anim::character::IdleAnimation::update_skeleton( + char_state.skeleton_mut(), ( active_tool_kind, second_tool_kind, @@ -324,51 +318,74 @@ impl Scene { ), scene_data.time as f32, &mut 0.0, - &SkeletonAttr::from(&body), + &anim::character::SkeletonAttr::from(&body), ); let dt_lerp = (scene_data.delta_time * 15.0).min(1.0); - *figure_state.skeleton_mut() = - Lerp::lerp(&*figure_state.skeleton_mut(), &tgt_skeleton, dt_lerp); - - let model = self - .figure_model_cache - .get_or_create_model( - renderer, - &mut self.figure_atlas, - body, - inventory, - (), - scene_data.tick, - CameraMode::default(), - None, - scene_data.slow_job_pool, - None, - ) - .0; - let mut buf = [Default::default(); anim::MAX_BONE_COUNT]; - let common_params = FigureUpdateCommonParameters { - entity: None, - pos: anim::vek::Vec3::zero(), - ori: anim::vek::Quaternion::rotation_from_to_3d( - anim::vek::Vec3::unit_y(), - anim::vek::Vec3::new(self.char_ori.sin(), -self.char_ori.cos(), 0.0), - ), - scale: 1.0, - mount_transform_pos: None, - body: None, - tools: (None, None), - col: Rgba::broadcast(1.0), - dt: scene_data.delta_time, - _lpindex: 0, - _visible: true, - is_player: false, - _camera: &self.camera, - terrain: None, - ground_vel: Vec3::zero(), - }; - - figure_state.update(renderer, None, &mut buf, &common_params, 1.0, model, body); + *char_state.skeleton_mut() = + Lerp::lerp(&*char_state.skeleton_mut(), &tgt_skeleton, dt_lerp); + let (model, _) = self.char_model_cache.get_or_create_model( + renderer, + &mut self.figure_atlas, + body, + inventory, + (), + scene_data.tick, + CameraMode::default(), + None, + scene_data.slow_job_pool, + None, + ); + char_state.update( + renderer, + None, + &mut [Default::default(); anim::MAX_BONE_COUNT], + ¶ms, + 1.0, + model, + body, + ); } + + let airship_body = ship::Body::DefaultAirship; + let airship_state = self.airship_state.get_or_insert_with(|| { + FigureState::new(renderer, ShipSkeleton::default(), airship_body) + }); + let params = figure_params(&self.camera, scene_data.delta_time, self.airship_pos); + let tgt_skeleton = anim::ship::IdleAnimation::update_skeleton( + airship_state.skeleton_mut(), + ( + None, + None, + scene_data.time as f32, + scene_data.time as f32, + (params.ori * Vec3::unit_y()).into(), + (params.ori * Vec3::unit_y()).into(), + ), + scene_data.time as f32, + &mut 0.0, + &anim::ship::SkeletonAttr::from(&airship_body), + ); + let dt_lerp = (scene_data.delta_time * 15.0).min(1.0); + *airship_state.skeleton_mut() = + Lerp::lerp(&*airship_state.skeleton_mut(), &tgt_skeleton, dt_lerp); + let (model, _) = self.airship_model_cache.get_or_create_terrain_model( + renderer, + &mut self.figure_atlas, + airship_body, + (), + scene_data.tick, + scene_data.slow_job_pool, + &self.sprite_render_state, + ); + airship_state.update( + renderer, + None, + &mut [Default::default(); anim::MAX_BONE_COUNT], + ¶ms, + 1.0, + model, + airship_body, + ); } pub fn global_bind_group(&self) -> &GlobalsBindGroup { &self.globals_bind_group } @@ -382,7 +399,7 @@ impl Scene { ) { let mut figure_drawer = drawer.draw_figures(); if let Some(body) = body { - let model = &self.figure_model_cache.get_model( + let model = &self.char_model_cache.get_model( &self.figure_atlas, body, inventory, @@ -392,28 +409,61 @@ impl Scene { None, ); - if let Some((model, figure_state)) = model.zip(self.figure_state.as_ref()) { + if let Some((model, char_state)) = model.zip(self.char_state.as_ref()) { if let Some(lod) = model.lod_model(0) { figure_drawer.draw( lod, - figure_state.bound(), + char_state.bound(), self.figure_atlas.texture(ModelEntryRef::Figure(model)), ); } } } - if let Some((model, state)) = &self.backdrop { + let model = &self.airship_model_cache.get_model( + &self.figure_atlas, + ship::Body::DefaultAirship, + Default::default(), + tick, + CameraMode::default(), + None, + None, + ); + if let Some((model, airship_state)) = model.zip(self.airship_state.as_ref()) { if let Some(lod) = model.lod_model(0) { figure_drawer.draw( lod, - state.bound(), - self.figure_atlas.texture(ModelEntryRef::Figure(model)), + airship_state.bound(), + self.figure_atlas.texture(ModelEntryRef::Terrain(model)), ); } } + drop(figure_drawer); + let mut sprite_drawer = drawer.draw_sprites( + &self.sprite_globals, + &self.sprite_render_state.sprite_atlas_textures, + ); + if let (Some(sprite_instances), Some(data)) = ( + self.airship_model_cache + .get_sprites(ship::Body::DefaultAirship), + self.airship_state.as_ref().map(|s| &s.extra), + ) { + sprite_drawer.draw( + data, + &sprite_instances[0], + &AltIndices { + deep_end: 0, + underground_end: 0, + }, + CullingMode::None, + ); + } + drop(sprite_drawer); + + self.lod.render(drawer, Default::default()); + drawer.draw_skybox(&self.skybox.model); } } diff --git a/voxygen/src/scene/terrain.rs b/voxygen/src/scene/terrain.rs index 91534ea01c..ed3a8708d4 100644 --- a/voxygen/src/scene/terrain.rs +++ b/voxygen/src/scene/terrain.rs @@ -483,10 +483,6 @@ pub struct Terrain { /// might significantly reduce the number of textures we need for /// particularly difficult locations. atlas: AtlasAllocator, - /// FIXME: This could possibly become an `AssetHandle`, to get - /// hot-reloading for free, but I am not sure if sudden changes of this - /// value would break something - pub sprite_config: Arc, chunks: HashMap, TerrainChunkData>, /// Temporary storage for dead chunks that might still be shadowing chunks /// in view. We wait until either the chunk definitely cannot be @@ -516,9 +512,8 @@ pub struct Terrain { // GPU data // Maps sprite kind + variant to data detailing how to render it - pub sprite_data: Arc>, + pub sprite_render_state: SpriteRenderState, pub sprite_globals: SpriteGlobalsBindGroup, - pub sprite_atlas_textures: Arc>, /// As stated previously, this is always the very latest texture into which /// we allocate. Code cannot assume that this is the assigned texture /// for any particular chunk; look at the `texture` field in @@ -533,12 +528,20 @@ impl TerrainChunkData { } #[derive(Clone)] -pub struct SpriteRenderContext { - sprite_config: Arc, +pub struct SpriteRenderState { + /// FIXME: This could possibly become an `AssetHandle`, to get + /// hot-reloading for free, but I am not sure if sudden changes of this + /// value would break something + pub sprite_config: Arc, // Maps sprite kind + variant to data detailing how to render it - sprite_data: Arc>, - sprite_atlas_textures: Arc>, - sprite_verts_buffer: Arc, + pub sprite_data: Arc>, + pub sprite_atlas_textures: Arc>, +} + +#[derive(Clone)] +pub struct SpriteRenderContext { + pub state: SpriteRenderState, + pub sprite_verts_buffer: Arc, } pub type SpriteRenderContextLazy = Box SpriteRenderContext>; @@ -705,10 +708,12 @@ impl SpriteRenderContext { let sprite_verts_buffer = renderer.create_sprite_verts(sprite_mesh); Self { - // TODO: these are all Arcs, would it makes sense to factor out the Arc? - sprite_config: Arc::clone(&sprite_config), - sprite_data: Arc::new(sprite_data), - sprite_atlas_textures: Arc::new(sprite_atlas_textures), + state: SpriteRenderState { + // TODO: these are all Arcs, would it makes sense to factor out the Arc? + sprite_config: Arc::clone(&sprite_config), + sprite_data: Arc::new(sprite_data), + sprite_atlas_textures: Arc::new(sprite_atlas_textures), + }, sprite_verts_buffer: Arc::new(sprite_verts_buffer), } }; @@ -732,7 +737,6 @@ impl Terrain { Self { atlas, - sprite_config: sprite_render_context.sprite_config, chunks: HashMap::default(), shadow_chunks: Vec::default(), mesh_send_tmp: send, @@ -740,8 +744,7 @@ impl Terrain { mesh_todo: HashMap::default(), mesh_todos_active: Arc::new(AtomicU64::new(0)), mesh_recv_overflow: 0.0, - sprite_data: sprite_render_context.sprite_data, - sprite_atlas_textures: sprite_render_context.sprite_atlas_textures, + sprite_render_state: sprite_render_context.state, sprite_globals: renderer.bind_sprite_globals( global_model, lod_data, @@ -1227,8 +1230,8 @@ impl Terrain { // Queue the worker thread. let started_tick = todo.started_tick; - let sprite_data = Arc::clone(&self.sprite_data); - let sprite_config = Arc::clone(&self.sprite_config); + let sprite_data = Arc::clone(&self.sprite_render_state.sprite_data); + let sprite_config = Arc::clone(&self.sprite_render_state.sprite_config); let cnt = Arc::clone(&self.mesh_todos_active); cnt.fetch_add(1, Ordering::Relaxed); scene_data