Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/stress_tests/many_materials.rs
6592 views
1
//! Benchmark to test rendering many animated materials
2
use argh::FromArgs;
3
use bevy::{
4
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
5
prelude::*,
6
window::{PresentMode, WindowResolution},
7
winit::{UpdateMode, WinitSettings},
8
};
9
use std::f32::consts::PI;
10
11
#[derive(FromArgs, Resource)]
12
/// Command-line arguments for the `many_materials` stress test.
13
struct Args {
14
/// the size of the grid of materials to render (n x n)
15
#[argh(option, short = 'n', default = "10")]
16
grid_size: usize,
17
}
18
19
fn main() {
20
// `from_env` panics on the web
21
#[cfg(not(target_arch = "wasm32"))]
22
let args: Args = argh::from_env();
23
#[cfg(target_arch = "wasm32")]
24
let args = Args::from_args(&[], &[]).unwrap();
25
26
App::new()
27
.add_plugins((
28
DefaultPlugins.set(WindowPlugin {
29
primary_window: Some(Window {
30
resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),
31
title: "many_materials".into(),
32
present_mode: PresentMode::AutoNoVsync,
33
..default()
34
}),
35
..default()
36
}),
37
FrameTimeDiagnosticsPlugin::default(),
38
LogDiagnosticsPlugin::default(),
39
))
40
.insert_resource(WinitSettings {
41
focused_mode: UpdateMode::Continuous,
42
unfocused_mode: UpdateMode::Continuous,
43
})
44
.insert_resource(args)
45
.add_systems(Startup, setup)
46
.add_systems(Update, animate_materials)
47
.run();
48
}
49
50
fn setup(
51
mut commands: Commands,
52
args: Res<Args>,
53
mesh_assets: ResMut<Assets<Mesh>>,
54
material_assets: ResMut<Assets<StandardMaterial>>,
55
) {
56
let args = args.into_inner();
57
let material_assets = material_assets.into_inner();
58
let mesh_assets = mesh_assets.into_inner();
59
let n = args.grid_size;
60
61
// Camera
62
let w = n as f32;
63
commands.spawn((
64
Camera3d::default(),
65
Transform::from_xyz(w * 1.25, w + 1.0, w * 1.25)
66
.looking_at(Vec3::new(0.0, (w * -1.1) + 1.0, 0.0), Vec3::Y),
67
));
68
69
// Light
70
commands.spawn((
71
Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 1.0, -PI / 4.)),
72
DirectionalLight {
73
illuminance: 3000.0,
74
shadows_enabled: true,
75
..default()
76
},
77
));
78
79
// Cubes
80
let mesh_handle = mesh_assets.add(Cuboid::from_size(Vec3::ONE));
81
for x in 0..n {
82
for z in 0..n {
83
commands.spawn((
84
Mesh3d(mesh_handle.clone()),
85
MeshMaterial3d(material_assets.add(Color::WHITE)),
86
Transform::from_translation(Vec3::new(x as f32, 0.0, z as f32)),
87
));
88
}
89
}
90
}
91
92
fn animate_materials(
93
material_handles: Query<&MeshMaterial3d<StandardMaterial>>,
94
time: Res<Time>,
95
mut materials: ResMut<Assets<StandardMaterial>>,
96
) {
97
for (i, material_handle) in material_handles.iter().enumerate() {
98
if let Some(material) = materials.get_mut(material_handle) {
99
let color = Color::hsl(
100
((i as f32 * 2.345 + time.elapsed_secs()) * 100.0) % 360.0,
101
1.0,
102
0.5,
103
);
104
material.base_color = color;
105
}
106
}
107
}
108
109