From 79285311f254f1db14db5538df94fb4cc4eeb4f4 Mon Sep 17 00:00:00 2001 From: Robin KAY Date: Thu, 16 Mar 2023 22:03:38 +0000 Subject: [PATCH] Add enabled flag to OutlineStencil. --- examples/pieces.rs | 5 ++++- src/lib.rs | 32 ++++++++++++++++++++++++-------- src/uniforms.rs | 3 +++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/examples/pieces.rs b/examples/pieces.rs index b9a100a..48e296f 100644 --- a/examples/pieces.rs +++ b/examples/pieces.rs @@ -37,7 +37,10 @@ fn setup( colour: Color::WHITE, width: 10.0, }, - stencil: OutlineStencil { offset: 5.0 }, + stencil: OutlineStencil { + offset: 5.0, + ..default() + }, ..default() }; diff --git a/src/lib.rs b/src/lib.rs index d5e1038..fbc1e68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,12 +73,23 @@ pub const ATTRIBUTE_OUTLINE_NORMAL: MeshVertexAttribute = pub const OUTLINE_PASS_NODE_NAME: &str = "bevy_mod_outline_node"; /// A component for stenciling meshes during outline rendering. -#[derive(Clone, Component, Default)] +#[derive(Clone, Component)] pub struct OutlineStencil { + /// Enable rendering of the stencil + pub enabled: bool, /// Offset of the stencil in logical pixels pub offset: f32, } +impl Default for OutlineStencil { + fn default() -> Self { + OutlineStencil { + enabled: true, + offset: 0.0, + } + } +} + impl ExtractComponent for OutlineStencil { type Query = &'static OutlineStencil; type Filter = (); @@ -89,11 +100,22 @@ impl ExtractComponent for OutlineStencil { } } +fn lerp_bool(this: bool, other: bool, scalar: f32) -> bool { + if scalar <= 0.0 { + this + } else if scalar >= 1.0 { + other + } else { + this | other + } +} + impl Lerp for OutlineStencil { type Scalar = f32; fn lerp(&self, other: &Self, scalar: &Self::Scalar) -> Self { OutlineStencil { + enabled: lerp_bool(self.enabled, other.enabled, *scalar), offset: self.offset.lerp(&other.offset, scalar), } } @@ -115,13 +137,7 @@ impl Lerp for OutlineVolume { fn lerp(&self, other: &Self, scalar: &Self::Scalar) -> Self { OutlineVolume { - visible: if *scalar <= 0.0 { - self.visible - } else if *scalar >= 1.0 { - other.visible - } else { - self.visible | other.visible - }, + visible: lerp_bool(self.visible, other.visible, *scalar), width: self.width.lerp(&other.width, scalar), colour: { let [r, g, b, a] = self diff --git a/src/uniforms.rs b/src/uniforms.rs index 5efa110..b72aa4c 100644 --- a/src/uniforms.rs +++ b/src/uniforms.rs @@ -71,6 +71,9 @@ pub(crate) fn extract_outline_stencil_uniforms( query: Extract>, ) { for (entity, stencil, computed) in query.iter() { + if !stencil.enabled { + continue; + } commands .get_or_spawn(entity) .insert(OutlineStencilUniform {