Skip to content

Commit 18a7c30

Browse files
authored
Shortcircuit SceneInstanceReady on models that do not have ColorOverride (#21751)
# Objective The `edit_material_on_gltf` example was iterating over all materials of a Gltf even when it did not have `ColorOverride`. ## Solution Shortcircuit observer if scene does not have `ColorOverride` ## Testing Ran the example
1 parent 7d84e5b commit 18a7c30

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

examples/gltf/edit_material_on_gltf.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414
}
1515

1616
/// This is added to a [`SceneRoot`] and will cause the [`StandardMaterial::base_color`]
17-
/// of all materials to be overwritten
17+
/// of materials with [`GltfMaterialName`] equal to `LeatherPartsMat`.
1818
#[derive(Component)]
1919
struct ColorOverride(Color);
2020

@@ -48,6 +48,10 @@ fn setup_scene(mut commands: Commands, asset_server: Res<AssetServer>) {
4848
));
4949
}
5050

51+
/// On [`SceneInstanceReady`], iterates over all descendants of the scene
52+
/// and modifies the tint of the material for the materials named `LeatherPartsMat`.
53+
///
54+
/// If the [`SceneRoot`] does not have a [`ColorOverride`], it is skipped.
5155
fn change_material(
5256
scene_ready: On<SceneInstanceReady>,
5357
mut commands: Commands,
@@ -57,6 +61,13 @@ fn change_material(
5761
mut asset_materials: ResMut<Assets<StandardMaterial>>,
5862
) {
5963
info!("processing Scene Entity: {}", scene_ready.entity);
64+
65+
// Get the `ColorOverride` of the entity, if it does not have a color override, return
66+
let Ok(color_override) = color_override.get(scene_ready.entity) else {
67+
info!("{} does not have a color override", scene_ready.entity);
68+
return;
69+
};
70+
6071
// Iterate over all children recursively
6172
for descendant in children.iter_descendants(scene_ready.entity) {
6273
// Get the material id and name which were created from the glTF file information
@@ -72,10 +83,6 @@ fn change_material(
7283
match material_name.0.as_str() {
7384
"LeatherPartsMat" => {
7485
info!("editing LeatherPartsMat to use ColorOverride tint");
75-
// Get the `ColorOverride` of the entity, if it does not have a color override, skip
76-
let Ok(color_override) = color_override.get(scene_ready.entity) else {
77-
continue;
78-
};
7986
// Create a copy of the material and override base color
8087
// If you intend on creating multiple models with the same tint, it
8188
// is best to cache the handle somewhere, as having multiple materials

0 commit comments

Comments
 (0)