From 8afc8f9d1473119d33334b54f565d87869d24a95 Mon Sep 17 00:00:00 2001 From: Robin KAY Date: Mon, 21 Nov 2022 23:46:36 +0000 Subject: [PATCH] Add SetOutlineDepth component. --- src/computed.rs | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/computed.rs b/src/computed.rs index 076080b..4a8c3b3 100644 --- a/src/computed.rs +++ b/src/computed.rs @@ -6,7 +6,15 @@ pub struct ComputedOutlineDepth { pub(crate) origin: Vec3, } -/// A component which specifies that this entity lies at the same depth as its parent. +/// A component which specifies how the outline depth should be computed. +#[derive(Clone, Component)] +#[non_exhaustive] +pub enum SetOutlineDepth { + /// A flat plane facing the camera and intersecting the specified point in model-space. + Flat { model_origin: Vec3 }, +} + +/// A component which specifies that this outline lies at the same depth as its parent. #[derive(Clone, Component, Default)] pub struct InheritOutlineDepth; @@ -17,6 +25,7 @@ pub(crate) fn compute_outline_depth( &mut ComputedOutlineDepth, &GlobalTransform, Changed, + Option<(&SetOutlineDepth, Changed)>, Option<(&Children, Changed)>, ), Without, @@ -24,17 +33,28 @@ pub(crate) fn compute_outline_depth( mut computed_query: Query<(&mut ComputedOutlineDepth, Changed)>, child_query: Query<(&Children, Changed)>, ) { - for (mut computed, transform, changed_transform, children) in root_query.iter_mut() { - if changed_transform { + for (mut computed, transform, changed_transform, set_depth, children) in root_query.iter_mut() { + let mut changed = changed_transform; + if changed { + let origin = if let Some((sd, sd_changed)) = set_depth { + changed |= sd_changed; + match sd { + SetOutlineDepth::Flat { + model_origin: origin, + } => *origin, + } + } else { + Vec3::ZERO + }; let matrix = transform.compute_matrix(); - computed.origin = matrix.project_point3(Vec3::ZERO); + computed.origin = matrix.project_point3(origin); } if let Some((cs, changed_children)) = children { - let changed2 = changed_children || changed_transform; + changed |= changed_children; for child in cs.iter() { propagate_outline_depth( &computed, - changed2, + changed, *child, &mut computed_query, &child_query, @@ -46,21 +66,22 @@ pub(crate) fn compute_outline_depth( fn propagate_outline_depth( root_computed: &ComputedOutlineDepth, - changed: bool, + mut changed: bool, entity: Entity, computed_query: &mut Query<(&mut ComputedOutlineDepth, Changed)>, child_query: &Query<(&Children, Changed)>, ) { if let Ok((mut computed, changed_inherit)) = computed_query.get_mut(entity) { - if changed_inherit || changed { + changed |= changed_inherit; + if changed { *computed = root_computed.clone(); } if let Ok((cs, changed_children)) = child_query.get(entity) { - let changed2 = changed_children || changed_inherit || changed; + changed |= changed_children; for child in cs.iter() { propagate_outline_depth( root_computed, - changed2, + changed, *child, computed_query, child_query,