Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify animated_material example #11576

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 13 additions & 31 deletions examples/3d/animated_material.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
//! Shows how to animate material properties

use bevy::prelude::*;
use bevy::utils::HashSet;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (animate_materials, make_materials_unique))
.add_systems(Update, animate_materials)
.run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(3.0, 1.0, 3.0)
Expand All @@ -25,11 +29,12 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
));

let helmet = asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0");
for x in -2..3 {
for z in -2..3 {
commands.spawn(SceneBundle {
scene: helmet.clone(),
let cube = meshes.add(shape::Cube { size: 0.5 });
for x in -1..2 {
for z in -1..2 {
Comment on lines +32 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the materials aren't unique there's no point in spawning more than one helmet

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just realized this isnt helmets anymore

commands.spawn(PbrBundle {
mesh: cube.clone(),
material: materials.add(Color::WHITE),
transform: Transform::from_translation(Vec3::new(x as f32, 0.0, z as f32)),
..default()
});
Expand All @@ -50,29 +55,6 @@ fn animate_materials(
0.5,
);
material.base_color = color;
material.emissive = color;
}
}
}

/// This is needed because by default assets are loaded with shared materials
/// But we want to animate every helmet independently of the others, so we must duplicate the materials
fn make_materials_unique(
mut material_handles: Query<&mut Handle<StandardMaterial>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut ran: Local<bool>,
) {
if *ran {
return;
}
let mut set = HashSet::new();
for mut material_handle in material_handles.iter_mut() {
if set.contains(&material_handle.id()) {
let material = materials.get(&*material_handle).unwrap().clone();
*material_handle = materials.add(material);
} else {
set.insert(material_handle.id());
}
*ran = true;
}
}