Skip to content

Commit

Permalink
Make Wireframe respect visible entities (#4660)
Browse files Browse the repository at this point in the history
# 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!
  • Loading branch information
dtaralla committed May 4, 2022
1 parent 3f4ac65 commit f02bea5
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions crates/bevy_pbr/src/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,14 +115,14 @@ fn queue_wireframes(
Query<(Entity, &Handle<Mesh>, &MeshUniform)>,
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
.read()
.get_id::<DrawWireframes>()
.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);

Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit f02bea5

Please sign in to comment.