Add visible flag to Outline component.

This commit is contained in:
Robin KAY 2022-08-08 22:40:38 +01:00
parent 3dd9c4c367
commit 75dcc9077c
3 changed files with 32 additions and 30 deletions

View File

@ -36,6 +36,7 @@ fn setup(
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
let mut cube_mesh = Mesh::from(Cube { size: 1.0 });
cube_mesh.generate_outline_normals().unwrap();
commands
@ -46,11 +47,13 @@ fn setup(
..default()
})
.insert(Outline {
visible: true,
colour: Color::rgba(0.0, 1.0, 0.0, 1.0),
width: 25.0,
})
.insert(OutlineStencil)
.insert(Wobbles);
commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(Torus {
@ -65,6 +68,7 @@ fn setup(
..default()
})
.insert(Outline {
visible: true,
colour: Color::rgba(1.0, 0.0, 1.0, 0.3),
width: 15.0,
})

View File

@ -16,7 +16,10 @@ use crate::node::{OpaqueOutline, OutlineNode, StencilOutline, TransparentOutline
use crate::pipeline::{
OutlinePipeline, COMMON_SHADER_HANDLE, OUTLINE_SHADER_HANDLE, STENCIL_SHADER_HANDLE,
};
use crate::uniforms::{queue_outline_bind_group, OutlineFragmentUniform, OutlineVertexUniform};
use crate::uniforms::{
extract_outline_uniforms, queue_outline_bind_group, OutlineFragmentUniform,
OutlineVertexUniform,
};
use crate::view_uniforms::{
extract_outline_view_uniforms, queue_outline_view_bind_group, OutlineViewUniform,
};
@ -52,6 +55,8 @@ impl ExtractComponent for OutlineStencil {
/// A component for rendering outlines around meshes.
#[derive(Clone, Component)]
pub struct Outline {
/// Enable rendering of the outline
pub visible: bool,
/// Width of the outline in logical pixels
pub width: f32,
/// Colour of the outline
@ -145,8 +150,6 @@ impl Plugin for OutlinePlugin {
);
app.add_plugin(ExtractComponentPlugin::<OutlineStencil>::extract_visible())
.add_plugin(ExtractComponentPlugin::<OutlineVertexUniform>::default())
.add_plugin(ExtractComponentPlugin::<OutlineFragmentUniform>::default())
.add_plugin(UniformComponentPlugin::<OutlineVertexUniform>::default())
.add_plugin(UniformComponentPlugin::<OutlineFragmentUniform>::default())
.add_plugin(UniformComponentPlugin::<OutlineViewUniform>::default())
@ -160,6 +163,7 @@ impl Plugin for OutlinePlugin {
.add_render_command::<OpaqueOutline, DrawOutline>()
.add_render_command::<TransparentOutline, DrawOutline>()
.add_system_to_stage(RenderStage::Extract, extract_outline_view_uniforms)
.add_system_to_stage(RenderStage::Extract, extract_outline_uniforms)
.add_system_to_stage(RenderStage::PhaseSort, sort_phase_system::<StencilOutline>)
.add_system_to_stage(RenderStage::PhaseSort, sort_phase_system::<OpaqueOutline>)
.add_system_to_stage(

View File

@ -1,17 +1,15 @@
use bevy::{
ecs::{
query::QueryItem,
system::{
lifetimeless::{Read, SQuery, SRes},
SystemParamItem,
},
ecs::system::{
lifetimeless::{Read, SQuery, SRes},
SystemParamItem,
},
prelude::*,
render::{
extract_component::{ComponentUniforms, DynamicUniformIndex, ExtractComponent},
extract_component::{ComponentUniforms, DynamicUniformIndex},
render_phase::{EntityRenderCommand, RenderCommandResult, TrackedRenderPass},
render_resource::{BindGroup, BindGroupDescriptor, BindGroupEntry, ShaderType},
renderer::RenderDevice,
Extract,
},
};
@ -22,35 +20,31 @@ pub struct OutlineVertexUniform {
pub width: f32,
}
impl ExtractComponent for OutlineVertexUniform {
type Query = Read<Outline>;
type Filter = ();
fn extract_component(item: QueryItem<Self::Query>) -> Self {
OutlineVertexUniform { width: item.width }
}
}
#[derive(Clone, Component, ShaderType)]
pub struct OutlineFragmentUniform {
pub colour: Vec4,
}
impl ExtractComponent for OutlineFragmentUniform {
type Query = Read<Outline>;
type Filter = ();
fn extract_component(item: QueryItem<Self::Query>) -> Self {
OutlineFragmentUniform {
colour: item.colour.as_linear_rgba_f32().into(),
}
}
}
pub struct OutlineBindGroup {
pub bind_group: BindGroup,
}
pub fn extract_outline_uniforms(mut commands: Commands, query: Extract<Query<(Entity, &Outline)>>) {
for (entity, outline) in query.iter() {
if !outline.visible || outline.colour.a() == 0.0 {
continue;
}
commands
.get_or_spawn(entity)
.insert(OutlineVertexUniform {
width: outline.width,
})
.insert(OutlineFragmentUniform {
colour: outline.colour.as_linear_rgba_f32().into(),
});
}
}
pub fn queue_outline_bind_group(
mut commands: Commands,
render_device: Res<RenderDevice>,