Change flat flag to enum.

This commit is contained in:
Robin KAY 2023-02-21 21:12:44 +00:00
parent 6896b5ae73
commit 78081558bb
4 changed files with 44 additions and 23 deletions

View File

@ -1,10 +1,12 @@
use bevy::prelude::*;
use crate::uniforms::DepthMode;
/// A component for storing the computed depth at which the outline lies.
#[derive(Clone, Component, Default)]
pub struct ComputedOutlineDepth {
pub(crate) origin: Vec3,
pub(crate) flat: bool,
pub(crate) world_origin: Vec3,
pub(crate) depth_mode: DepthMode,
}
/// A component which specifies how the outline depth should be computed.
@ -37,21 +39,22 @@ pub(crate) fn compute_outline_depth(
child_query: Query<(&Children, Changed<Children>)>,
) {
for (mut computed, transform, changed_transform, set_depth, children) in root_query.iter_mut() {
let mut changed = computed.is_added() || changed_transform || set_depth.filter(|(_, c)| *c).is_some();
let mut changed =
computed.is_added() || changed_transform || set_depth.filter(|(_, c)| *c).is_some();
if changed {
let (origin, flat) = if let Some((sd, _)) = set_depth {
let (origin, depth_mode) = if let Some((sd, _)) = set_depth {
match sd {
SetOutlineDepth::Flat {
model_origin: origin,
} => (*origin, true),
SetOutlineDepth::Real => (Vec3::NAN, false),
} => (*origin, DepthMode::Flat),
SetOutlineDepth::Real => (Vec3::NAN, DepthMode::Real),
}
} else {
(Vec3::ZERO, true)
(Vec3::ZERO, DepthMode::Flat)
};
let matrix = transform.compute_matrix();
computed.origin = matrix.project_point3(origin);
computed.flat = flat;
computed.world_origin = matrix.project_point3(origin);
computed.depth_mode = depth_mode;
}
if let Some((cs, changed_children)) = children {
changed |= changed_children;

View File

@ -65,7 +65,7 @@ pub(crate) fn queue_outline_stencil_mesh(
if let Some(mesh) = render_meshes.get(mesh_handle) {
let key = base_key
.with_primitive_topology(mesh.primitive_topology)
.with_flat_depth(stencil_flags.flat_depth)
.with_depth_mode(stencil_flags.depth_mode)
.with_offset_zero(stencil_uniform.offset == 0.0);
let pipeline = pipelines
.specialize(&mut pipeline_cache, &stencil_pipeline, key, &mesh.layout)
@ -145,7 +145,7 @@ pub(crate) fn queue_outline_volume_mesh(
} else {
PassType::Opaque
})
.with_flat_depth(volume_flags.flat_depth)
.with_depth_mode(volume_flags.depth_mode)
.with_offset_zero(volume_uniform.offset == 0.0);
let pipeline = pipelines
.specialize(&mut pipeline_cache, &outline_pipeline, key, &mesh.layout)

View File

@ -22,7 +22,9 @@ use bevy::{
};
use bitfield::{bitfield_bitrange, bitfield_fields};
use crate::uniforms::{OutlineFragmentUniform, OutlineStencilUniform, OutlineVolumeUniform};
use crate::uniforms::{
DepthMode, OutlineFragmentUniform, OutlineStencilUniform, OutlineVolumeUniform,
};
use crate::view_uniforms::OutlineViewUniform;
use crate::ATTRIBUTE_OUTLINE_NORMAL;
@ -49,8 +51,8 @@ impl PipelineKey {
msaa_samples_minus_one, set_msaa_samples_minus_one: 5, 0;
primitive_topology_int, set_primitive_topology_int: 8, 6;
pass_type_int, set_pass_type_int: 10, 9;
pub flat_depth, set_flat_depth: 11;
pub offset_zero, set_offset_zero: 12;
depth_mode_int, set_depth_mode_int: 12, 11;
pub offset_zero, set_offset_zero: 13;
}
pub(crate) fn new() -> Self {
@ -101,10 +103,18 @@ impl PipelineKey {
self
}
pub(crate) fn with_flat_depth(mut self, flat_depth: bool) -> Self {
self.set_flat_depth(flat_depth);
pub(crate) fn with_depth_mode(mut self, depth_mode: DepthMode) -> Self {
self.set_depth_mode_int(depth_mode as u32);
self
}
pub(crate) fn depth_mode(&self) -> DepthMode {
match self.depth_mode_int() {
x if x == DepthMode::Flat as u32 => DepthMode::Flat,
x if x == DepthMode::Real as u32 => DepthMode::Real,
x => panic!("Invalid value for DepthMode: {}", x),
}
}
}
#[derive(Resource)]
@ -214,7 +224,7 @@ impl SpecializedMeshPipeline for OutlinePipeline {
);
bind_layouts.push(self.outline_view_bind_group_layout.clone());
let cull_mode;
if key.flat_depth() {
if key.depth_mode() == DepthMode::Flat {
vertex_defs.push("FLAT_DEPTH".to_string());
cull_mode = Some(Face::Back);
} else if key.pass_type() == PassType::Stencil {

View File

@ -35,14 +35,22 @@ pub(crate) struct OutlineFragmentUniform {
pub colour: Vec4,
}
#[derive(Clone, Copy, Default, PartialEq)]
pub(crate) enum DepthMode {
#[default]
Invalid = 0,
Flat = 1,
Real = 2,
}
#[derive(Component)]
pub(crate) struct OutlineStencilFlags {
pub flat_depth: bool,
pub depth_mode: DepthMode,
}
#[derive(Component)]
pub(crate) struct OutlineVolumeFlags {
pub flat_depth: bool,
pub depth_mode: DepthMode,
}
#[derive(Resource)]
@ -63,11 +71,11 @@ pub(crate) fn extract_outline_stencil_uniforms(
commands
.get_or_spawn(entity)
.insert(OutlineStencilUniform {
origin: computed.origin,
origin: computed.world_origin,
offset: stencil.offset,
})
.insert(OutlineStencilFlags {
flat_depth: computed.flat,
depth_mode: computed.depth_mode,
});
}
}
@ -83,14 +91,14 @@ pub(crate) fn extract_outline_volume_uniforms(
commands
.get_or_spawn(entity)
.insert(OutlineVolumeUniform {
origin: computed.origin,
origin: computed.world_origin,
offset: outline.width,
})
.insert(OutlineFragmentUniform {
colour: outline.colour.as_linear_rgba_f32().into(),
})
.insert(OutlineVolumeFlags {
flat_depth: computed.flat,
depth_mode: computed.depth_mode,
});
}
}