Skip to content

Commit

Permalink
Avoid spawning gizmo meshes when no gizmos are being drawn (bevyengin…
Browse files Browse the repository at this point in the history
…e#8180)

# Objective

Avoid queuing empty meshes for rendering.
Should prevent bevyengine#8144 from triggering when no gizmos are in use. Not a
real fix, unfortunately.

## Solution

Add an `in_use` field to `GizmoStorage` and only set it to true when
there are gizmos to draw.
  • Loading branch information
tim-blackbird authored Apr 17, 2023
1 parent ce252f8 commit e54057c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions crates/bevy_gizmos/src/gizmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub(crate) struct GizmoStorage {
pub list_colors: Vec<ColorItem>,
pub strip_positions: Vec<PositionItem>,
pub strip_colors: Vec<ColorItem>,
pub in_use: bool,
}

/// A [`SystemParam`](bevy_ecs::system::SystemParam) for drawing gizmos.
Expand Down
35 changes: 23 additions & 12 deletions crates/bevy_gizmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,42 @@ fn update_gizmo_meshes(
) {
let list_mesh = meshes.get_mut(&handles.list).unwrap();

let positions = mem::take(&mut storage.list_positions);
list_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
storage.in_use = false;

let colors = mem::take(&mut storage.list_colors);
list_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
if !storage.list_positions.is_empty() {
storage.in_use = true;

let strip_mesh = meshes.get_mut(&handles.strip).unwrap();
let positions = mem::take(&mut storage.list_positions);
list_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);

let positions = mem::take(&mut storage.strip_positions);
strip_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
let colors = mem::take(&mut storage.list_colors);
list_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
}

if !storage.strip_positions.is_empty() {
storage.in_use = true;

let strip_mesh = meshes.get_mut(&handles.strip).unwrap();

let colors = mem::take(&mut storage.strip_colors);
strip_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
let positions = mem::take(&mut storage.strip_positions);
strip_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);

let colors = mem::take(&mut storage.strip_colors);
strip_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
}
}

fn extract_gizmo_data(
mut commands: Commands,
handles: Extract<Res<MeshHandles>>,
config: Extract<Res<GizmoConfig>>,
storage: Extract<Res<GizmoStorage>>,
) {
if config.is_changed() {
commands.insert_resource(**config);
}

if !config.enabled {
if !config.enabled || !storage.in_use {
return;
}

Expand All @@ -186,7 +197,7 @@ fn extract_gizmo_data(
GizmoMesh,
#[cfg(feature = "bevy_pbr")]
(
handle.clone(),
handle.clone_weak(),
MeshUniform {
flags: 0,
transform,
Expand All @@ -196,7 +207,7 @@ fn extract_gizmo_data(
),
#[cfg(feature = "bevy_sprite")]
(
Mesh2dHandle(handle.clone()),
Mesh2dHandle(handle.clone_weak()),
Mesh2dUniform {
flags: 0,
transform,
Expand Down

0 comments on commit e54057c

Please sign in to comment.