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](479f43bbf3/crates/bevy_pbr/src/material.rs (L307)) for [bevy_pbr/src/wireframe.rs#L106](2b6e67f4cb/crates/bevy_pbr/src/wireframe.rs (L106))
- Credits to beep for finding this out!
This commit is contained in:
David Taralla 2022-05-04 22:28:16 +00:00
parent 3f4ac65682
commit f02bea5bfc

View file

@ -14,7 +14,7 @@ use bevy_render::{
PipelineCache, PolygonMode, RenderPipelineDescriptor, Shader, SpecializedMeshPipeline, PipelineCache, PolygonMode, RenderPipelineDescriptor, Shader, SpecializedMeshPipeline,
SpecializedMeshPipelineError, SpecializedMeshPipelines, SpecializedMeshPipelineError, SpecializedMeshPipelines,
}, },
view::{ExtractedView, Msaa}, view::{ExtractedView, Msaa, VisibleEntities},
RenderApp, RenderStage, RenderApp, RenderStage,
}; };
use bevy_utils::tracing::error; use bevy_utils::tracing::error;
@ -115,14 +115,14 @@ fn queue_wireframes(
Query<(Entity, &Handle<Mesh>, &MeshUniform)>, Query<(Entity, &Handle<Mesh>, &MeshUniform)>,
Query<(Entity, &Handle<Mesh>, &MeshUniform), With<Wireframe>>, Query<(Entity, &Handle<Mesh>, &MeshUniform), With<Wireframe>>,
)>, )>,
mut views: Query<(&ExtractedView, &mut RenderPhase<Opaque3d>)>, mut views: Query<(&ExtractedView, &VisibleEntities, &mut RenderPhase<Opaque3d>)>,
) { ) {
let draw_custom = opaque_3d_draw_functions let draw_custom = opaque_3d_draw_functions
.read() .read()
.get_id::<DrawWireframes>() .get_id::<DrawWireframes>()
.unwrap(); .unwrap();
let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples); 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_matrix = view.transform.compute_matrix();
let view_row_2 = view_matrix.row(2); let view_row_2 = view_matrix.row(2);
@ -154,9 +154,19 @@ fn queue_wireframes(
}; };
if wireframe_config.global { 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 { } 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);
} }
} }
} }