diff --git a/examples/shapes.rs b/examples/shapes.rs index 6bdfdfe..42ddf9f 100644 --- a/examples/shapes.rs +++ b/examples/shapes.rs @@ -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, }) diff --git a/src/lib.rs b/src/lib.rs index 70abd8b..c128def 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::::extract_visible()) - .add_plugin(ExtractComponentPlugin::::default()) - .add_plugin(ExtractComponentPlugin::::default()) .add_plugin(UniformComponentPlugin::::default()) .add_plugin(UniformComponentPlugin::::default()) .add_plugin(UniformComponentPlugin::::default()) @@ -160,6 +163,7 @@ impl Plugin for OutlinePlugin { .add_render_command::() .add_render_command::() .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::) .add_system_to_stage(RenderStage::PhaseSort, sort_phase_system::) .add_system_to_stage( diff --git a/src/uniforms.rs b/src/uniforms.rs index 13ae787..612144b 100644 --- a/src/uniforms.rs +++ b/src/uniforms.rs @@ -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; - type Filter = (); - - fn extract_component(item: QueryItem) -> Self { - OutlineVertexUniform { width: item.width } - } -} - #[derive(Clone, Component, ShaderType)] pub struct OutlineFragmentUniform { pub colour: Vec4, } -impl ExtractComponent for OutlineFragmentUniform { - type Query = Read; - type Filter = (); - - fn extract_component(item: QueryItem) -> 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>) { + 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,