Add enabled flag to OutlineStencil.

This commit is contained in:
Robin KAY 2023-03-16 22:03:38 +00:00
parent 2a4184ab32
commit 79285311f2
3 changed files with 31 additions and 9 deletions

View File

@ -37,7 +37,10 @@ fn setup(
colour: Color::WHITE,
width: 10.0,
},
stencil: OutlineStencil { offset: 5.0 },
stencil: OutlineStencil {
offset: 5.0,
..default()
},
..default()
};

View File

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

View File

@ -71,6 +71,9 @@ pub(crate) fn extract_outline_stencil_uniforms(
query: Extract<Query<(Entity, &OutlineStencil, &ComputedOutlineDepth)>>,
) {
for (entity, stencil, computed) in query.iter() {
if !stencil.enabled {
continue;
}
commands
.get_or_spawn(entity)
.insert(OutlineStencilUniform {