Trails shrinks over time

This commit is contained in:
Sam 2022-02-15 16:54:41 -05:00
parent ab4c5dac87
commit 87434c472c
2 changed files with 36 additions and 2 deletions

View File

@ -1,6 +1,9 @@
use super::super::{AaMode, GlobalsLayouts, Vertex as VertexTrait};
use bytemuck::{Pod, Zeroable};
use std::mem;
use std::{
mem,
ops::{Add, Mul},
};
#[repr(C)]
#[derive(Copy, Clone, Debug, Zeroable, Pod, PartialEq)]
@ -22,6 +25,26 @@ impl Vertex {
pub fn zero() -> Self { Self { pos: [0.0; 3] } }
}
impl Mul<f32> for Vertex {
type Output = Self;
fn mul(self, val: f32) -> Self::Output {
Self {
pos: self.pos.map(|a| a * val),
}
}
}
impl Add<Vertex> for Vertex {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
Self {
pos: self.pos.zip(other.pos).map(|(a, b)| a + b),
}
}
}
impl VertexTrait for Vertex {
const QUADS_INDEX: Option<wgpu::IndexFormat> = Some(wgpu::IndexFormat::Uint16);
const STRIDE: wgpu::BufferAddress = mem::size_of::<Self>() as wgpu::BufferAddress;

View File

@ -21,6 +21,7 @@ pub struct TrailMgr {
}
const TRAIL_DYNAMIC_MODEL_SIZE: usize = 15;
const TRAIL_SHRINKAGE: f32 = 0.8;
impl TrailMgr {
pub fn new(renderer: &mut Renderer) -> Self {
@ -39,8 +40,18 @@ impl TrailMgr {
// Update offset
self.offset = (self.offset + 1) % TRAIL_DYNAMIC_MODEL_SIZE;
// Reset quad for each entity mesh at new offset
self.entity_meshes.values_mut().for_each(|mesh| {
// Shrink size of each quad over time
let vertices = mesh.vertices_mut_vec();
for i in 0..TRAIL_DYNAMIC_MODEL_SIZE {
// Verts per quad are in b, c, a, d order
vertices[i * 4 + 2] = vertices[i * 4 + 2] * TRAIL_SHRINKAGE
+ vertices[i * 4] * (1.0 - TRAIL_SHRINKAGE);
vertices[i * 4 + 3] = vertices[i * 4 + 3] * TRAIL_SHRINKAGE
+ vertices[i * 4 + 1] * (1.0 - TRAIL_SHRINKAGE);
}
// Reset quad for each entity mesh at new offset
let zero = TrailVertex::zero();
mesh.replace_quad(self.offset * 4, Quad::new(zero, zero, zero, zero));
});