Skip to content

Commit fb367da

Browse files
authored
Add Animated Material example (#11524)
# Objective - Fixes #11516 ## Solution - Add Animated Material example (colors are hue-cycling smoothly per-mesh) ![image](https://github.com/bevyengine/bevy/assets/11307157/c75b9e66-0019-41b8-85ec-647559c6ba01) Note: this example reproduces the perf issue found in #10610 pretty consistently, with and without the changes from that PR included. Frame time is sometimes around 4.3ms, other times around 12-14ms. Its pretty random per run. I think this clears #10610 for merge.
1 parent 86e91f4 commit fb367da

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,17 @@ description = "Demonstrates how to use the `Camera::viewport_to_world` method"
552552
category = "3D Rendering"
553553
wasm = true
554554

555+
[[example]]
556+
name = "animated_material"
557+
path = "examples/3d/animated_material.rs"
558+
doc-scrape-examples = true
559+
560+
[package.metadata.example.animated_material]
561+
name = "Animated Material"
562+
description = "Shows how to animate material properties"
563+
category = "3D Rendering"
564+
wasm = true
565+
555566
[[example]]
556567
name = "generate_custom_mesh"
557568
path = "examples/3d/generate_custom_mesh.rs"

examples/3d/animated_material.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//! Shows how to animate material properties
2+
3+
use bevy::prelude::*;
4+
use bevy::utils::HashSet;
5+
6+
fn main() {
7+
App::new()
8+
.add_plugins(DefaultPlugins)
9+
.add_systems(Startup, setup)
10+
.add_systems(Update, (animate_materials, make_materials_unique))
11+
.run();
12+
}
13+
14+
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
15+
commands.spawn((
16+
Camera3dBundle {
17+
transform: Transform::from_xyz(3.0, 1.0, 3.0)
18+
.looking_at(Vec3::new(0.0, -0.5, 0.0), Vec3::Y),
19+
..default()
20+
},
21+
EnvironmentMapLight {
22+
diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
23+
specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
24+
intensity: 1500.0,
25+
},
26+
));
27+
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(),
33+
transform: Transform::from_translation(Vec3::new(x as f32, 0.0, z as f32)),
34+
..default()
35+
});
36+
}
37+
}
38+
}
39+
40+
fn animate_materials(
41+
material_handles: Query<&Handle<StandardMaterial>>,
42+
time: Res<Time>,
43+
mut materials: ResMut<Assets<StandardMaterial>>,
44+
) {
45+
for (i, material_handle) in material_handles.iter().enumerate() {
46+
if let Some(material) = materials.get_mut(material_handle) {
47+
let color = Color::hsl(
48+
((i as f32 * 2.345 + time.elapsed_seconds_wrapped()) * 100.0) % 360.0,
49+
1.0,
50+
0.5,
51+
);
52+
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());
75+
}
76+
*ran = true;
77+
}
78+
}

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Example | Description
121121
[3D Scene](../examples/3d/3d_scene.rs) | Simple 3D scene with basic shapes and lighting
122122
[3D Shapes](../examples/3d/3d_shapes.rs) | A scene showcasing the built-in 3D shapes
123123
[3D Viewport To World](../examples/3d/3d_viewport_to_world.rs) | Demonstrates how to use the `Camera::viewport_to_world` method
124+
[Animated Material](../examples/3d/animated_material.rs) | Shows how to animate material properties
124125
[Anti-aliasing](../examples/3d/anti_aliasing.rs) | Compares different anti-aliasing methods
125126
[Atmospheric Fog](../examples/3d/atmospheric_fog.rs) | A scene showcasing the atmospheric fog effect
126127
[Blend Modes](../examples/3d/blend_modes.rs) | Showcases different blend modes

0 commit comments

Comments
 (0)