From 10f2d985d3b0a975098f7ddfd8a7939ce6a9eff4 Mon Sep 17 00:00:00 2001 From: Robin KAY Date: Sat, 31 Dec 2022 04:57:13 +0000 Subject: [PATCH] Add Lerp trait impls for outline volume and stencil types. --- Cargo.toml | 1 + src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 601659e..6a4041d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ bevy = { version = "0.9", default-features = false, features = [ "bevy_core_pipeline", ] } bitfield = "0.14" +interpolation = "0.2" thiserror = "1.0" [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index af88eea..de39d07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,6 +32,7 @@ use bevy::render::render_phase::{sort_phase_system, AddRenderCommand, DrawFuncti use bevy::render::render_resource::{SpecializedMeshPipelines, VertexFormat}; use bevy::render::{RenderApp, RenderStage}; use bevy::transform::TransformSystem; +use interpolation::Lerp; use crate::draw::{ queue_outline_stencil_mesh, queue_outline_volume_mesh, DrawOutline, DrawStencil, @@ -86,6 +87,16 @@ impl ExtractComponent for OutlineStencil { } } +impl Lerp for OutlineStencil { + type Scalar = f32; + + fn lerp(&self, other: &Self, scalar: &Self::Scalar) -> Self { + OutlineStencil { + offset: self.offset.lerp(&other.offset, scalar), + } + } +} + /// A component for rendering outlines around meshes. #[derive(Clone, Component, Default)] pub struct OutlineVolume { @@ -97,6 +108,28 @@ pub struct OutlineVolume { pub colour: Color, } +impl Lerp for OutlineVolume { + type Scalar = f32; + + fn lerp(&self, other: &Self, scalar: &Self::Scalar) -> Self { + OutlineVolume { + visible: if *scalar >= 0.5 { + other.visible + } else { + self.visible + }, + width: self.width.lerp(&other.width, scalar), + colour: { + let [r, g, b, a] = self + .colour + .as_linear_rgba_f32() + .lerp(&other.colour.as_linear_rgba_f32(), scalar); + Color::rgba_linear(r, g, b, a) + }, + } + } +} + /// A bundle for rendering stenciled outlines around meshes. #[derive(Bundle, Clone, Default)] pub struct OutlineBundle {