Add SetOutlineDepth component.

This commit is contained in:
Robin KAY 2022-11-21 23:46:36 +00:00
parent a792cca681
commit 8afc8f9d14
1 changed files with 31 additions and 10 deletions

View File

@ -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<GlobalTransform>,
Option<(&SetOutlineDepth, Changed<SetOutlineDepth>)>,
Option<(&Children, Changed<Children>)>,
),
Without<InheritOutlineDepth>,
@ -24,17 +33,28 @@ pub(crate) fn compute_outline_depth(
mut computed_query: Query<(&mut ComputedOutlineDepth, Changed<InheritOutlineDepth>)>,
child_query: Query<(&Children, Changed<Children>)>,
) {
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<InheritOutlineDepth>)>,
child_query: &Query<(&Children, Changed<Children>)>,
) {
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,