From f02bea5bfc8a6c5ef2a4f418c4cc30ad44410d63 Mon Sep 17 00:00:00 2001 From: David Taralla Date: Wed, 4 May 2022 22:28:16 +0000 Subject: [PATCH] Make Wireframe respect visible entities (#4660) # Objective - Make meshes with a Wireframe component not render if they are not in the VisibleEntities list of a given camera - See [discussion](https://discord.com/channels/691052431525675048/742884593551802431/971392761972527144) on the Bevy Engine Discord - Fixes this kind of issues: ![image](https://user-images.githubusercontent.com/1733200/166746303-39003d57-8b07-4ae2-9ddf-bacdb04e7d84.png) Camera for the RenderTexture in the bottom left is set to only see layer 1 entities. The three colored lines are on the render layer 1, but not the sphere (which has a Wireframe component). ## Solution - Mimick what is done in [bevy_pbr/src/material.rs#L307](https://github.com/bevyengine/bevy/blob/479f43bbf34834ad2d4667de43351b6fa51f22d1/crates/bevy_pbr/src/material.rs#L307) for [bevy_pbr/src/wireframe.rs#L106](https://github.com/bevyengine/bevy/blob/2b6e67f4cb441f658cad17486eea9e3485e56709/crates/bevy_pbr/src/wireframe.rs#L106) - Credits to beep for finding this out! --- crates/bevy_pbr/src/wireframe.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/crates/bevy_pbr/src/wireframe.rs b/crates/bevy_pbr/src/wireframe.rs index 53f39fe0a15ac..b8116c6f1fe6d 100644 --- a/crates/bevy_pbr/src/wireframe.rs +++ b/crates/bevy_pbr/src/wireframe.rs @@ -14,7 +14,7 @@ use bevy_render::{ PipelineCache, PolygonMode, RenderPipelineDescriptor, Shader, SpecializedMeshPipeline, SpecializedMeshPipelineError, SpecializedMeshPipelines, }, - view::{ExtractedView, Msaa}, + view::{ExtractedView, Msaa, VisibleEntities}, RenderApp, RenderStage, }; use bevy_utils::tracing::error; @@ -115,14 +115,14 @@ fn queue_wireframes( Query<(Entity, &Handle, &MeshUniform)>, Query<(Entity, &Handle, &MeshUniform), With>, )>, - mut views: Query<(&ExtractedView, &mut RenderPhase)>, + mut views: Query<(&ExtractedView, &VisibleEntities, &mut RenderPhase)>, ) { let draw_custom = opaque_3d_draw_functions .read() .get_id::() .unwrap(); let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples); - for (view, mut opaque_phase) in views.iter_mut() { + for (view, visible_entities, mut opaque_phase) in views.iter_mut() { let view_matrix = view.transform.compute_matrix(); let view_row_2 = view_matrix.row(2); @@ -154,9 +154,19 @@ fn queue_wireframes( }; if wireframe_config.global { - material_meshes.p0().iter().for_each(add_render_phase); + let query = material_meshes.p0(); + visible_entities + .entities + .iter() + .filter_map(|visible_entity| query.get(*visible_entity).ok()) + .for_each(add_render_phase); } else { - material_meshes.p1().iter().for_each(add_render_phase); + let query = material_meshes.p1(); + visible_entities + .entities + .iter() + .filter_map(|visible_entity| query.get(*visible_entity).ok()) + .for_each(add_render_phase); } } }