Skip to content

Commit e54057c

Browse files
Avoid spawning gizmo meshes when no gizmos are being drawn (#8180)
# Objective Avoid queuing empty meshes for rendering. Should prevent #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.
1 parent ce252f8 commit e54057c

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

crates/bevy_gizmos/src/gizmos.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub(crate) struct GizmoStorage {
2020
pub list_colors: Vec<ColorItem>,
2121
pub strip_positions: Vec<PositionItem>,
2222
pub strip_colors: Vec<ColorItem>,
23+
pub in_use: bool,
2324
}
2425

2526
/// A [`SystemParam`](bevy_ecs::system::SystemParam) for drawing gizmos.

crates/bevy_gizmos/src/lib.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,31 +151,42 @@ fn update_gizmo_meshes(
151151
) {
152152
let list_mesh = meshes.get_mut(&handles.list).unwrap();
153153

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

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

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

162-
let positions = mem::take(&mut storage.strip_positions);
163-
strip_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
162+
let colors = mem::take(&mut storage.list_colors);
163+
list_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
164+
}
165+
166+
if !storage.strip_positions.is_empty() {
167+
storage.in_use = true;
168+
169+
let strip_mesh = meshes.get_mut(&handles.strip).unwrap();
164170

165-
let colors = mem::take(&mut storage.strip_colors);
166-
strip_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
171+
let positions = mem::take(&mut storage.strip_positions);
172+
strip_mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
173+
174+
let colors = mem::take(&mut storage.strip_colors);
175+
strip_mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors);
176+
}
167177
}
168178

169179
fn extract_gizmo_data(
170180
mut commands: Commands,
171181
handles: Extract<Res<MeshHandles>>,
172182
config: Extract<Res<GizmoConfig>>,
183+
storage: Extract<Res<GizmoStorage>>,
173184
) {
174185
if config.is_changed() {
175186
commands.insert_resource(**config);
176187
}
177188

178-
if !config.enabled {
189+
if !config.enabled || !storage.in_use {
179190
return;
180191
}
181192

@@ -186,7 +197,7 @@ fn extract_gizmo_data(
186197
GizmoMesh,
187198
#[cfg(feature = "bevy_pbr")]
188199
(
189-
handle.clone(),
200+
handle.clone_weak(),
190201
MeshUniform {
191202
flags: 0,
192203
transform,
@@ -196,7 +207,7 @@ fn extract_gizmo_data(
196207
),
197208
#[cfg(feature = "bevy_sprite")]
198209
(
199-
Mesh2dHandle(handle.clone()),
210+
Mesh2dHandle(handle.clone_weak()),
200211
Mesh2dUniform {
201212
flags: 0,
202213
transform,

0 commit comments

Comments
 (0)