Skip to content

Commit 79a2e5e

Browse files
authored
simplify animated_material example (#11576)
# Objective - example `animated_material` is more complex that needed to show how to animate materials - it makes CI crash because it uses too much memory ## Solution - Simplify the example
1 parent eb8de36 commit 79a2e5e

File tree

1 file changed

+13
-31
lines changed

1 file changed

+13
-31
lines changed

examples/3d/animated_material.rs

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
//! Shows how to animate material properties
22
33
use bevy::prelude::*;
4-
use bevy::utils::HashSet;
54

65
fn main() {
76
App::new()
87
.add_plugins(DefaultPlugins)
98
.add_systems(Startup, setup)
10-
.add_systems(Update, (animate_materials, make_materials_unique))
9+
.add_systems(Update, animate_materials)
1110
.run();
1211
}
1312

14-
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
13+
fn setup(
14+
mut commands: Commands,
15+
asset_server: Res<AssetServer>,
16+
mut meshes: ResMut<Assets<Mesh>>,
17+
mut materials: ResMut<Assets<StandardMaterial>>,
18+
) {
1519
commands.spawn((
1620
Camera3dBundle {
1721
transform: Transform::from_xyz(3.0, 1.0, 3.0)
@@ -25,11 +29,12 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
2529
},
2630
));
2731

28-
let helmet = asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0");
29-
for x in -2..3 {
30-
for z in -2..3 {
31-
commands.spawn(SceneBundle {
32-
scene: helmet.clone(),
32+
let cube = meshes.add(shape::Cube { size: 0.5 });
33+
for x in -1..2 {
34+
for z in -1..2 {
35+
commands.spawn(PbrBundle {
36+
mesh: cube.clone(),
37+
material: materials.add(Color::WHITE),
3338
transform: Transform::from_translation(Vec3::new(x as f32, 0.0, z as f32)),
3439
..default()
3540
});
@@ -50,29 +55,6 @@ fn animate_materials(
5055
0.5,
5156
);
5257
material.base_color = color;
53-
material.emissive = color;
54-
}
55-
}
56-
}
57-
58-
/// This is needed because by default assets are loaded with shared materials
59-
/// But we want to animate every helmet independently of the others, so we must duplicate the materials
60-
fn make_materials_unique(
61-
mut material_handles: Query<&mut Handle<StandardMaterial>>,
62-
mut materials: ResMut<Assets<StandardMaterial>>,
63-
mut ran: Local<bool>,
64-
) {
65-
if *ran {
66-
return;
67-
}
68-
let mut set = HashSet::new();
69-
for mut material_handle in material_handles.iter_mut() {
70-
if set.contains(&material_handle.id()) {
71-
let material = materials.get(&*material_handle).unwrap().clone();
72-
*material_handle = materials.add(material);
73-
} else {
74-
set.insert(material_handle.id());
7558
}
76-
*ran = true;
7759
}
7860
}

0 commit comments

Comments
 (0)