Skip to content

Commit 76e9b59

Browse files
committed
Rework extract_meshes
* Cleanup redundant code * Use a type alias to make sure the `caster_query` and `not_caster_query` really do the same thing and access the same things **Objective** Cleanup code that would otherwise be difficult to understand **Solution** * `extract_meshes` had two for loops which are functionally identical, just copy-pasted code. I extracted the common code between the two and put them into an anonymous function. * I flattened the tuple literal for the bundle batch, it looks much less nested and the code is much more readable as a result. * The parameters of `extract_meshes` were also very daunting, but they turned out to be the same query repeated twice. I extracted the query into a type alias.
1 parent b7d784d commit 76e9b59

File tree

1 file changed

+43
-71
lines changed

1 file changed

+43
-71
lines changed

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 43 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use bevy_app::Plugin;
77
use bevy_asset::{load_internal_asset, Assets, Handle, HandleUntyped};
88
use bevy_ecs::{
99
prelude::*,
10+
query::QueryItem,
1011
system::{lifetimeless::*, SystemParamItem, SystemState},
1112
};
1213
use bevy_math::{Mat4, Vec2};
@@ -106,81 +107,52 @@ bitflags::bitflags! {
106107
}
107108
}
108109

110+
pub type ExtractMeshQuery = (
111+
Entity,
112+
&'static ComputedVisibility,
113+
&'static GlobalTransform,
114+
&'static Handle<Mesh>,
115+
Option<&'static NotShadowReceiver>,
116+
);
117+
type ExtractMeshItem<'w> = QueryItem<'w, ExtractMeshQuery>;
118+
109119
pub fn extract_meshes(
110120
mut commands: Commands,
111-
mut previous_caster_len: Local<usize>,
112-
mut previous_not_caster_len: Local<usize>,
113-
caster_query: Query<
114-
(
115-
Entity,
116-
&ComputedVisibility,
117-
&GlobalTransform,
118-
&Handle<Mesh>,
119-
Option<&NotShadowReceiver>,
120-
),
121-
Without<NotShadowCaster>,
122-
>,
123-
not_caster_query: Query<
124-
(
125-
Entity,
126-
&ComputedVisibility,
127-
&GlobalTransform,
128-
&Handle<Mesh>,
129-
Option<&NotShadowReceiver>,
130-
),
131-
With<NotShadowCaster>,
132-
>,
121+
mut prev_len_caster: Local<usize>,
122+
mut prev_len_not: Local<usize>,
123+
caster_query: Query<ExtractMeshQuery, Without<NotShadowCaster>>,
124+
not_caster_query: Query<ExtractMeshQuery, With<NotShadowCaster>>,
133125
) {
134-
let mut caster_values = Vec::with_capacity(*previous_caster_len);
135-
for (entity, computed_visibility, transform, handle, not_receiver) in caster_query.iter() {
136-
if !computed_visibility.is_visible {
137-
continue;
138-
}
126+
let mesh_bundle = |(entity, _, transform, handle, not_receiver): ExtractMeshItem| {
139127
let transform = transform.compute_matrix();
140-
caster_values.push((
141-
entity,
142-
(
143-
handle.clone_weak(),
144-
MeshUniform {
145-
flags: if not_receiver.is_some() {
146-
MeshFlags::empty().bits
147-
} else {
148-
MeshFlags::SHADOW_RECEIVER.bits
149-
},
150-
transform,
151-
inverse_transpose_model: transform.inverse().transpose(),
152-
},
153-
),
154-
));
155-
}
156-
*previous_caster_len = caster_values.len();
157-
commands.insert_or_spawn_batch(caster_values);
158-
159-
let mut not_caster_values = Vec::with_capacity(*previous_not_caster_len);
160-
for (entity, computed_visibility, transform, mesh, not_receiver) in not_caster_query.iter() {
161-
if !computed_visibility.is_visible {
162-
continue;
163-
}
164-
let transform = transform.compute_matrix();
165-
not_caster_values.push((
166-
entity,
167-
(
168-
mesh.clone_weak(),
169-
MeshUniform {
170-
flags: if not_receiver.is_some() {
171-
MeshFlags::empty().bits
172-
} else {
173-
MeshFlags::SHADOW_RECEIVER.bits
174-
},
175-
transform,
176-
inverse_transpose_model: transform.inverse().transpose(),
177-
},
178-
NotShadowCaster,
179-
),
180-
));
181-
}
182-
*previous_not_caster_len = not_caster_values.len();
183-
commands.insert_or_spawn_batch(not_caster_values);
128+
let flags = if not_receiver.is_some() {
129+
MeshFlags::empty().bits
130+
} else {
131+
MeshFlags::SHADOW_RECEIVER.bits
132+
};
133+
let uniform = MeshUniform {
134+
flags,
135+
transform,
136+
inverse_transpose_model: transform.inverse().transpose(),
137+
};
138+
(entity, (handle.clone_weak(), uniform))
139+
};
140+
let with_marker = |(entity, (handle, uniform))| (entity, (handle, uniform, NotShadowCaster));
141+
let is_visible = |(_, vis, ..): &ExtractMeshItem| vis.is_visible;
142+
let mut caster_cmds = Vec::with_capacity(*prev_len_caster);
143+
let mut not_caster_cmds = Vec::with_capacity(*prev_len_not);
144+
caster_cmds.extend(caster_query.iter().filter(is_visible).map(mesh_bundle));
145+
not_caster_cmds.extend(
146+
not_caster_query
147+
.iter()
148+
.filter(is_visible)
149+
.map(mesh_bundle)
150+
.map(with_marker),
151+
);
152+
*prev_len_caster = caster_cmds.len();
153+
*prev_len_not = not_caster_cmds.len();
154+
commands.insert_or_spawn_batch(caster_cmds);
155+
commands.insert_or_spawn_batch(not_caster_cmds);
184156
}
185157

186158
#[derive(Debug, Default)]

0 commit comments

Comments
 (0)