Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/stress_tests/many_gradients.rs
6592 views
1
//! Stress test demonstrating gradient performance improvements.
2
//!
3
//! This example creates many UI nodes with gradients to measure the performance
4
//! impact of pre-converting colors to the target color space on the CPU.
5
6
use argh::FromArgs;
7
use bevy::{
8
color::palettes::css::*,
9
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
10
math::ops::sin,
11
prelude::*,
12
ui::{
13
BackgroundGradient, ColorStop, Display, Gradient, InterpolationColorSpace, LinearGradient,
14
RepeatedGridTrack,
15
},
16
window::{PresentMode, WindowResolution},
17
winit::{UpdateMode, WinitSettings},
18
};
19
20
const COLS: usize = 30;
21
22
#[derive(FromArgs, Resource, Debug)]
23
/// Gradient stress test
24
struct Args {
25
/// how many gradients per group (default: 900)
26
#[argh(option, default = "900")]
27
gradient_count: usize,
28
29
/// whether to animate gradients by changing colors
30
#[argh(switch)]
31
animate: bool,
32
33
/// use sRGB interpolation
34
#[argh(switch)]
35
srgb: bool,
36
37
/// use HSL interpolation
38
#[argh(switch)]
39
hsl: bool,
40
}
41
42
fn main() {
43
let args: Args = argh::from_env();
44
let total_gradients = args.gradient_count;
45
46
println!("Gradient stress test with {total_gradients} gradients");
47
println!(
48
"Color space: {}",
49
if args.srgb {
50
"sRGB"
51
} else if args.hsl {
52
"HSL"
53
} else {
54
"OkLab (default)"
55
}
56
);
57
58
App::new()
59
.add_plugins((
60
LogDiagnosticsPlugin::default(),
61
FrameTimeDiagnosticsPlugin::default(),
62
DefaultPlugins.set(WindowPlugin {
63
primary_window: Some(Window {
64
title: "Gradient Stress Test".to_string(),
65
resolution: WindowResolution::new(1920, 1080).with_scale_factor_override(1.0),
66
present_mode: PresentMode::AutoNoVsync,
67
..default()
68
}),
69
..default()
70
}),
71
))
72
.insert_resource(WinitSettings {
73
focused_mode: UpdateMode::Continuous,
74
unfocused_mode: UpdateMode::Continuous,
75
})
76
.insert_resource(args)
77
.add_systems(Startup, setup)
78
.add_systems(Update, animate_gradients)
79
.run();
80
}
81
82
fn setup(mut commands: Commands, args: Res<Args>) {
83
commands.spawn(Camera2d);
84
85
let rows_to_spawn = args.gradient_count.div_ceil(COLS);
86
87
// Create a grid of gradients
88
commands
89
.spawn(Node {
90
width: percent(100),
91
height: percent(100),
92
display: Display::Grid,
93
grid_template_columns: RepeatedGridTrack::flex(COLS as u16, 1.0),
94
grid_template_rows: RepeatedGridTrack::flex(rows_to_spawn as u16, 1.0),
95
..default()
96
})
97
.with_children(|parent| {
98
for i in 0..args.gradient_count {
99
let angle = (i as f32 * 10.0) % 360.0;
100
101
let mut gradient = LinearGradient::new(
102
angle,
103
vec![
104
ColorStop::new(RED, percent(0)),
105
ColorStop::new(BLUE, percent(100)),
106
ColorStop::new(GREEN, percent(20)),
107
ColorStop::new(YELLOW, percent(40)),
108
ColorStop::new(ORANGE, percent(60)),
109
ColorStop::new(LIME, percent(80)),
110
ColorStop::new(DARK_CYAN, percent(90)),
111
],
112
);
113
114
gradient.color_space = if args.srgb {
115
InterpolationColorSpace::Srgba
116
} else if args.hsl {
117
InterpolationColorSpace::Hsla
118
} else {
119
InterpolationColorSpace::Oklaba
120
};
121
122
parent.spawn((
123
Node {
124
width: percent(100),
125
height: percent(100),
126
..default()
127
},
128
BackgroundGradient(vec![Gradient::Linear(gradient)]),
129
GradientNode { index: i },
130
));
131
}
132
});
133
}
134
135
#[derive(Component)]
136
struct GradientNode {
137
index: usize,
138
}
139
140
fn animate_gradients(
141
mut gradients: Query<(&mut BackgroundGradient, &GradientNode)>,
142
args: Res<Args>,
143
time: Res<Time>,
144
) {
145
if !args.animate {
146
return;
147
}
148
149
let t = time.elapsed_secs();
150
151
for (mut bg_gradient, node) in &mut gradients {
152
let offset = node.index as f32 * 0.01;
153
let hue_shift = sin(t + offset) * 0.5 + 0.5;
154
155
if let Some(Gradient::Linear(gradient)) = bg_gradient.0.get_mut(0) {
156
let color1 = Color::hsl(hue_shift * 360.0, 1.0, 0.5);
157
let color2 = Color::hsl((hue_shift + 0.3) * 360.0 % 360.0, 1.0, 0.5);
158
159
gradient.stops = vec![
160
ColorStop::new(color1, percent(0)),
161
ColorStop::new(color2, percent(100)),
162
ColorStop::new(
163
Color::hsl((hue_shift + 0.1) * 360.0 % 360.0, 1.0, 0.5),
164
percent(20),
165
),
166
ColorStop::new(
167
Color::hsl((hue_shift + 0.15) * 360.0 % 360.0, 1.0, 0.5),
168
percent(40),
169
),
170
ColorStop::new(
171
Color::hsl((hue_shift + 0.2) * 360.0 % 360.0, 1.0, 0.5),
172
percent(60),
173
),
174
ColorStop::new(
175
Color::hsl((hue_shift + 0.25) * 360.0 % 360.0, 1.0, 0.5),
176
percent(80),
177
),
178
ColorStop::new(
179
Color::hsl((hue_shift + 0.28) * 360.0 % 360.0, 1.0, 0.5),
180
percent(90),
181
),
182
];
183
}
184
}
185
}
186
187