Dynamic model now used for trail.

This commit is contained in:
Sam 2022-01-25 14:17:21 -05:00
parent 0794f980fe
commit d9028ec978
6 changed files with 45 additions and 58 deletions

View File

@ -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,
},

View File

@ -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<f32>, norm: Vec3<f32>) -> 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<f32>,
outer_pos: Vec3<f32>,
) -> Self {
pub fn new(inst_time: f64, lifespan: f32, inner_pos: Vec3<f32>, outer_pos: Vec3<f32>) -> 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::<Self>() 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 {

View File

@ -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<trail::Vertex>,
model: &'data DynamicModel<trail::Vertex>,
instances: &'data Instances<trail::Instance>,
) {
self.render_pass.set_vertex_buffer(0, model.buf().slice(..));

View File

@ -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,

View File

@ -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(

View File

@ -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<TrailInstance>,
/// GPU vertex buffers
dynamic_model: DynamicModel<TrailVertex>,
}
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<TerrainChunk>,
) {
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::<Vec<TrailInstance>>();
// 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<f32>, outer_pos: Vec3<f32>)-> Self {
fn new(lifespan: Duration, time: f64, inner_pos: Vec3<f32>, outer_pos: Vec3<f32>) -> Self {
Trail {
alive_until: time + lifespan.as_secs_f64(),
instance: TrailInstance::new(time, lifespan.as_secs_f32(), inner_pos, outer_pos),