From d9028ec9788beaea51c1ca3fc375c9b9b3172a68 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 25 Jan 2022 14:17:21 -0500 Subject: [PATCH] Dynamic model now used for trail. --- voxygen/src/render/mod.rs | 6 +-- voxygen/src/render/pipelines/trail.rs | 37 +++------------ voxygen/src/render/renderer/drawer.rs | 4 +- .../src/render/renderer/pipeline_creation.rs | 2 +- voxygen/src/scene/mod.rs | 7 ++- voxygen/src/scene/trail.rs | 47 ++++++++++++------- 6 files changed, 45 insertions(+), 58 deletions(-) diff --git a/voxygen/src/render/mod.rs b/voxygen/src/render/mod.rs index 30d6be2f7d..d6c63c51fa 100644 --- a/voxygen/src/render/mod.rs +++ b/voxygen/src/render/mod.rs @@ -28,7 +28,6 @@ pub use self::{ fluid::Vertex as FluidVertex, lod_terrain::{LodData, Vertex as LodTerrainVertex}, particle::{Instance as ParticleInstance, Vertex as ParticleVertex}, - trail::{Instance as TrailInstance, Vertex as TrailVertex}, postprocess::Locals as PostProcessLocals, shadow::{Locals as ShadowLocals, PointLightMatrix}, skybox::{create_mesh as create_skybox_mesh, Vertex as SkyboxVertex}, @@ -37,6 +36,7 @@ pub use self::{ Vertex as SpriteVertex, VERT_PAGE_SIZE as SPRITE_VERT_PAGE_SIZE, }, terrain::{Locals as TerrainLocals, TerrainLayout, Vertex as TerrainVertex}, + trail::{Instance as TrailInstance, Vertex as TrailVertex}, ui::{ create_quad as create_ui_quad, create_quad_vert_gradient as create_ui_quad_vert_gradient, create_tri as create_ui_tri, @@ -47,9 +47,9 @@ pub use self::{ }, renderer::{ drawer::{ - DebugDrawer, Drawer, FigureDrawer, FigureShadowDrawer, FirstPassDrawer, ParticleDrawer, TrailDrawer, + DebugDrawer, Drawer, FigureDrawer, FigureShadowDrawer, FirstPassDrawer, ParticleDrawer, PreparedUiDrawer, SecondPassDrawer, ShadowPassDrawer, SpriteDrawer, TerrainDrawer, - TerrainShadowDrawer, ThirdPassDrawer, UiDrawer, + TerrainShadowDrawer, ThirdPassDrawer, TrailDrawer, UiDrawer, }, ColLightInfo, Renderer, }, diff --git a/voxygen/src/render/pipelines/trail.rs b/voxygen/src/render/pipelines/trail.rs index 3eaeaeb39a..955732005a 100644 --- a/voxygen/src/render/pipelines/trail.rs +++ b/voxygen/src/render/pipelines/trail.rs @@ -7,31 +7,9 @@ use vek::*; #[derive(Copy, Clone, Debug, Zeroable, Pod)] pub struct Vertex { pub pos: [f32; 3], - // ____BBBBBBBBGGGGGGGGRRRRRRRR - // col: u32 = "v_col", - // ...AANNN - // A = AO - // N = Normal - norm_ao: u32, } impl Vertex { - #[allow(clippy::collapsible_else_if)] - pub fn new(pos: Vec3, norm: Vec3) -> Self { - let norm_bits = if norm.x != 0.0 { - if norm.x < 0.0 { 0 } else { 1 } - } else if norm.y != 0.0 { - if norm.y < 0.0 { 2 } else { 3 } - } else { - if norm.z < 0.0 { 4 } else { 5 } - }; - - Self { - pos: pos.into_array(), - norm_ao: norm_bits, - } - } - fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { const ATTRIBUTES: [wgpu::VertexAttribute; 2] = wgpu::vertex_attr_array![0 => Float32x3, 1 => Uint32]; @@ -58,18 +36,14 @@ pub struct Instance { // The lifespan in seconds of the trail inst_lifespan: f32, - // The two positions that define where the line of the object making the trail, e.g. sword tip and sword hilt + // The two positions that define where the line of the object making the trail, e.g. sword tip + // and sword hilt inner_pos: [f32; 3], outer_pos: [f32; 3], } impl Instance { - pub fn new( - inst_time: f64, - lifespan: f32, - inner_pos: Vec3, - outer_pos: Vec3, - ) -> Self { + pub fn new(inst_time: f64, lifespan: f32, inner_pos: Vec3, outer_pos: Vec3) -> Self { Self { inst_time: inst_time as f32, inst_lifespan: lifespan, @@ -79,13 +53,16 @@ impl Instance { } fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { - const ATTRIBUTES: [wgpu::VertexAttribute; 4] = wgpu::vertex_attr_array![2 => Float32, 3 => Float32, 4 => Float32x3, 5 => Float32x3]; + const ATTRIBUTES: [wgpu::VertexAttribute; 4] = + wgpu::vertex_attr_array![2 => Float32, 3 => Float32, 4 => Float32x3, 5 => Float32x3]; wgpu::VertexBufferLayout { array_stride: mem::size_of::() as wgpu::BufferAddress, step_mode: wgpu::InputStepMode::Instance, attributes: &ATTRIBUTES, } } + + pub fn points(&self) -> ([f32; 3], [f32; 3]) { (self.inner_pos, self.outer_pos) } } impl Default for Instance { diff --git a/voxygen/src/render/renderer/drawer.rs b/voxygen/src/render/renderer/drawer.rs index 0c4c7b4fe2..8ca65c7b17 100644 --- a/voxygen/src/render/renderer/drawer.rs +++ b/voxygen/src/render/renderer/drawer.rs @@ -5,7 +5,7 @@ use super::{ model::{DynamicModel, Model, SubModel}, pipelines::{ blit, bloom, clouds, debug, figure, fluid, lod_terrain, particle, shadow, skybox, - sprite, terrain, ui, ColLights, GlobalsBindGroup, ShadowTexturesBindGroup, trail, + sprite, terrain, trail, ui, ColLights, GlobalsBindGroup, ShadowTexturesBindGroup, }, }, Renderer, ShadowMap, ShadowMapRenderer, @@ -879,7 +879,7 @@ pub struct TrailDrawer<'pass_ref, 'pass: 'pass_ref> { impl<'pass_ref, 'pass: 'pass_ref> TrailDrawer<'pass_ref, 'pass> { pub fn draw<'data: 'pass>( &mut self, - model: &'data Model, + model: &'data DynamicModel, instances: &'data Instances, ) { self.render_pass.set_vertex_buffer(0, model.buf().slice(..)); diff --git a/voxygen/src/render/renderer/pipeline_creation.rs b/voxygen/src/render/renderer/pipeline_creation.rs index a8e79317d8..c04346aacc 100644 --- a/voxygen/src/render/renderer/pipeline_creation.rs +++ b/voxygen/src/render/renderer/pipeline_creation.rs @@ -2,7 +2,7 @@ use super::{ super::{ pipelines::{ blit, bloom, clouds, debug, figure, fluid, lod_terrain, particle, postprocess, shadow, - skybox, sprite, terrain, ui, trail, + skybox, sprite, terrain, trail, ui, }, AaMode, BloomMode, CloudMode, FluidMode, LightingMode, PipelineModes, RenderError, ShadowMode, diff --git a/voxygen/src/scene/mod.rs b/voxygen/src/scene/mod.rs index 993c720ddd..fdc49c2343 100644 --- a/voxygen/src/scene/mod.rs +++ b/voxygen/src/scene/mod.rs @@ -14,8 +14,8 @@ pub use self::{ figure::FigureMgr, lod::Lod, particle::ParticleMgr, - trail::TrailMgr, terrain::{SpriteRenderContextLazy, Terrain}, + trail::TrailMgr, }; use crate::{ audio::{ambient::AmbientMgr, music::MusicMgr, sfx::SfxMgr, AudioFrontend}, @@ -555,9 +555,8 @@ impl Scene { self.particle_mgr .maintain(renderer, scene_data, &self.terrain, lights); - // Maintain the trails. - self.trail_mgr - .maintain(renderer, scene_data, &self.terrain); + // Maintain the trails. + self.trail_mgr.maintain(renderer, scene_data); // Update light constants lights.extend( diff --git a/voxygen/src/scene/trail.rs b/voxygen/src/scene/trail.rs index ddb6634da3..5f618ee546 100644 --- a/voxygen/src/scene/trail.rs +++ b/voxygen/src/scene/trail.rs @@ -1,12 +1,6 @@ -use super::{SceneData, Terrain}; -use crate::{ - render::{ - Instances, TrailDrawer, - TrailInstance, Renderer, - }, -}; -use common::{ - terrain::TerrainChunk, +use super::SceneData; +use crate::render::{ + DynamicModel, Instances, Mesh, Quad, Renderer, TrailDrawer, TrailInstance, TrailVertex, }; use common_base::span; use std::time::Duration; @@ -18,6 +12,9 @@ pub struct TrailMgr { /// GPU Instance Buffer instances: Instances, + + /// GPU vertex buffers + dynamic_model: DynamicModel, } impl TrailMgr { @@ -25,15 +22,11 @@ impl TrailMgr { Self { trails: Vec::new(), instances: default_instances(renderer), + dynamic_model: renderer.create_dynamic_model(120), } } - pub fn maintain( - &mut self, - renderer: &mut Renderer, - scene_data: &SceneData, - terrain: &Terrain, - ) { + pub fn maintain(&mut self, renderer: &mut Renderer, scene_data: &SceneData) { span!(_guard, "maintain", "TrailMgr::maintain"); if scene_data.trails_enabled { // remove dead Trails @@ -57,7 +50,7 @@ impl TrailMgr { let all_cpu_instances = self .trails .iter() - .map(|p| p.instance) + .map(|t| t.instance) .collect::>(); // TODO: optimise buffer writes @@ -66,12 +59,30 @@ impl TrailMgr { .expect("Failed to upload trail instances to the GPU!"); self.instances = gpu_instances; + + for (i, trail) in self.trails.iter().enumerate() { + if i > 0 { + if let Some((inner1, outer1)) = self.trails.get(i - 1).map(|t| t.instance.points()) + { + let (inner2, outer2) = trail.instance.points(); + let point = |pos| TrailVertex { pos }; + let mut mesh = Mesh::new(); + mesh.push_quad(Quad::new( + point(inner1), + point(outer1), + point(inner2), + point(outer2), + )); + renderer.update_model(&self.dynamic_model, &mesh, 4 * i) + } + } + } } pub fn render<'a>(&'a self, drawer: &mut TrailDrawer<'_, 'a>, scene_data: &SceneData) { span!(_guard, "render", "TrailMgr::render"); if scene_data.trails_enabled { - drawer.draw(&self.instances); + drawer.draw(&self.dynamic_model, &self.instances); } } @@ -95,7 +106,7 @@ struct Trail { } impl Trail { - fn new(lifespan: Duration, time: f64, inner_pos: Vec3, outer_pos: Vec3)-> Self { + fn new(lifespan: Duration, time: f64, inner_pos: Vec3, outer_pos: Vec3) -> Self { Trail { alive_until: time + lifespan.as_secs_f64(), instance: TrailInstance::new(time, lifespan.as_secs_f32(), inner_pos, outer_pos),