Color

Enum Color 

Source
pub enum Color {
    Srgba(Srgba),
    LinearRgba(LinearRgba),
    Hsla(Hsla),
    Hsva(Hsva),
    Hwba(Hwba),
    Laba(Laba),
    Lcha(Lcha),
    Oklaba(Oklaba),
    Oklcha(Oklcha),
    Xyza(Xyza),
}
Expand description

An enumerated type that can represent any of the color types in this crate.

This is useful when you need to store a color in a data structure that can’t be generic over the color type.

§Conversion

Conversion between the various color spaces is achieved using Rust’s native From trait. Because certain color spaces are defined by their transformation to and from another space, these From implementations reflect that set of definitions.

let color = Srgba::rgb(0.5, 0.5, 0.5);

// Using From explicitly
let linear_color = LinearRgba::from(color);

// Using Into
let linear_color: LinearRgba = color.into();

For example, the sRGB space is defined by its relationship with Linear RGB, and HWB by its with sRGB. As such, it is the responsibility of sRGB to provide From implementations for Linear RGB, and HWB for sRGB. To then provide conversion between Linear RGB and HWB directly, HWB is responsible for implementing these conversions, delegating to sRGB as an intermediatory. This ensures that all conversions take the shortest path between any two spaces, and limit the proliferation of domain specific knowledge for each color space to their respective definitions.

GPU

§Operations

Color supports all the standard color operations, such as mixing, luminance and hue adjustment, and diffing. These operations delegate to the concrete color space contained by Color, but will convert to Oklch for operations which aren’t supported in the current space. After performing the operation, if a conversion was required, the result will be converted back into the original color space.

let red_hsv = Color::hsv(0., 1., 1.);
let red_srgb = Color::srgb(1., 0., 0.);

// HSV has a definition of hue, so it will be returned.
red_hsv.hue();

// SRGB doesn't have a native definition for hue.
// Converts to Oklch and returns that result.
red_srgb.hue();

Oklch has been chosen as the intermediary space in cases where conversion is required due to its perceptual uniformity and broad support for Bevy’s color operations. To avoid the cost of repeated conversion, and ensure consistent results where that is desired, first convert this Color into your desired color space.

Variants§

§

Srgba(Srgba)

A color in the sRGB color space with alpha.

§

LinearRgba(LinearRgba)

A color in the linear sRGB color space with alpha.

§

Hsla(Hsla)

A color in the HSL color space with alpha.

§

Hsva(Hsva)

A color in the HSV color space with alpha.

§

Hwba(Hwba)

A color in the HWB color space with alpha.

§

Laba(Laba)

A color in the LAB color space with alpha.

§

Lcha(Lcha)

A color in the LCH color space with alpha.

§

Oklaba(Oklaba)

A color in the Oklab color space with alpha.

§

Oklcha(Oklcha)

A color in the Oklch color space with alpha.

§

Xyza(Xyza)

A color in the XYZ color space with alpha.

Implementations§

Source§

impl Color

Source

pub const WHITE: Color

A fully white Color::LinearRgba color with an alpha of 1.0.

Source

pub const BLACK: Color

A fully black Color::LinearRgba color with an alpha of 1.0.

Source

pub const NONE: Color

A fully transparent Color::LinearRgba color with 0 red, green and blue.

Source

pub fn to_linear(&self) -> LinearRgba

Return the color as a linear RGBA color.

Examples found in repository?
examples/ui/ui_material.rs (line 101)
90fn animate(
91    mut materials: ResMut<Assets<CustomUiMaterial>>,
92    q: Query<&MaterialNode<CustomUiMaterial>>,
93    time: Res<Time>,
94) {
95    let duration = 2.0;
96    for handle in &q {
97        if let Some(material) = materials.get_mut(handle) {
98            // rainbow color effect
99            let new_color = Color::hsl((time.elapsed_secs() * 60.0) % 360.0, 1., 0.5);
100            let border_color = Color::hsl((time.elapsed_secs() * 60.0) % 360.0, 0.75, 0.75);
101            material.color = new_color.to_linear().to_vec4();
102            material.slider.x =
103                ((time.elapsed_secs() % (duration * 2.0)) - duration).abs() / duration;
104            material.border_color = border_color.to_linear().to_vec4();
105        }
106    }
107}
More examples
Hide additional examples
examples/3d/light_textures.rs (line 262)
228fn spawn_light_textures(
229    commands: &mut Commands,
230    asset_server: &AssetServer,
231    meshes: &mut Assets<Mesh>,
232    materials: &mut Assets<StandardMaterial>,
233) {
234    commands.spawn((
235        SpotLight {
236            color: Color::srgb(1.0, 1.0, 0.8),
237            intensity: 10e6,
238            outer_angle: 0.25,
239            inner_angle: 0.25,
240            shadows_enabled: true,
241            ..default()
242        },
243        Transform::from_translation(Vec3::new(6.0, 1.0, 2.0)).looking_at(Vec3::ZERO, Vec3::Y),
244        SpotLightTexture {
245            image: asset_server.load("lightmaps/torch_spotlight_texture.png"),
246        },
247        Visibility::Inherited,
248        Selection::SpotLight,
249    ));
250
251    commands.spawn((
252        Visibility::Hidden,
253        Transform::from_translation(Vec3::new(0.0, 1.8, 0.01)).with_scale(Vec3::splat(0.1)),
254        Selection::PointLight,
255        children![
256            SceneRoot(
257                asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/Faces/faces.glb")),
258            ),
259            (
260                Mesh3d(meshes.add(Sphere::new(1.0))),
261                MeshMaterial3d(materials.add(StandardMaterial {
262                    emissive: Color::srgb(0.0, 0.0, 300.0).to_linear(),
263                    ..default()
264                })),
265            ),
266            (
267                PointLight {
268                    color: Color::srgb(0.0, 0.0, 1.0),
269                    intensity: 1e6,
270                    shadows_enabled: true,
271                    ..default()
272                },
273                PointLightTexture {
274                    image: asset_server.load("lightmaps/faces_pointlight_texture_blurred.png"),
275                    cubemap_layout: CubemapLayout::CrossVertical,
276                },
277            )
278        ],
279    ));
280}
Source

pub fn to_srgba(&self) -> Srgba

Return the color as an SRGBA color.

Examples found in repository?
examples/2d/wireframe_2d.rs (line 122)
104fn update_colors(
105    keyboard_input: Res<ButtonInput<KeyCode>>,
106    mut config: ResMut<Wireframe2dConfig>,
107    mut wireframe_colors: Query<&mut Wireframe2dColor>,
108    mut text: Single<&mut Text>,
109) {
110    text.0 = format!(
111        "Controls
112---------------
113Z - Toggle global
114X - Change global color
115C - Change color of the circle wireframe
116
117Wireframe2dConfig
118-------------
119Global: {}
120Color: {:?}",
121        config.global,
122        config.default_color.to_srgba(),
123    );
124
125    // Toggle showing a wireframe on all meshes
126    if keyboard_input.just_pressed(KeyCode::KeyZ) {
127        config.global = !config.global;
128    }
129
130    // Toggle the global wireframe color
131    if keyboard_input.just_pressed(KeyCode::KeyX) {
132        config.default_color = if config.default_color == WHITE.into() {
133            RED.into()
134        } else {
135            WHITE.into()
136        };
137    }
138
139    // Toggle the color of a wireframe using `Wireframe2dColor` and not the global color
140    if keyboard_input.just_pressed(KeyCode::KeyC) {
141        for mut color in &mut wireframe_colors {
142            color.color = if color.color == GREEN.into() {
143                RED.into()
144            } else {
145                GREEN.into()
146            };
147        }
148    }
149}
Source

pub const fn srgba(red: f32, green: f32, blue: f32, alpha: f32) -> Color

Creates a new Color object storing a Srgba color.

§Arguments
  • red - Red channel. [0.0, 1.0]
  • green - Green channel. [0.0, 1.0]
  • blue - Blue channel. [0.0, 1.0]
  • alpha - Alpha channel. [0.0, 1.0]
Examples found in repository?
examples/3d/clearcoat.rs (line 133)
120fn spawn_coated_glass_bubble_sphere(
121    commands: &mut Commands,
122    materials: &mut Assets<StandardMaterial>,
123    sphere: &Handle<Mesh>,
124) {
125    commands
126        .spawn((
127            Mesh3d(sphere.clone()),
128            MeshMaterial3d(materials.add(StandardMaterial {
129                clearcoat: 1.0,
130                clearcoat_perceptual_roughness: 0.1,
131                metallic: 0.5,
132                perceptual_roughness: 0.1,
133                base_color: Color::srgba(0.9, 0.9, 0.9, 0.3),
134                alpha_mode: AlphaMode::Blend,
135                ..default()
136            })),
137            Transform::from_xyz(-1.0, -1.0, 0.0).with_scale(Vec3::splat(SPHERE_SCALE)),
138        ))
139        .insert(ExampleSphere);
140}
More examples
Hide additional examples
examples/2d/transparency_2d.rs (line 26)
13fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
14    commands.spawn(Camera2d);
15
16    let sprite_handle = asset_server.load("branding/icon.png");
17
18    commands.spawn((
19        Sprite::from_image(sprite_handle.clone()),
20        Transform::from_xyz(-100.0, 0.0, 0.0),
21    ));
22    commands.spawn((
23        Sprite {
24            image: sprite_handle.clone(),
25            // Alpha channel of the color controls transparency.
26            color: Color::srgba(0.0, 0.0, 1.0, 0.7),
27            ..default()
28        },
29        Transform::from_xyz(0.0, 0.0, 0.1),
30    ));
31    commands.spawn((
32        Sprite {
33            image: sprite_handle,
34            color: Color::srgba(0.0, 1.0, 0.0, 0.3),
35            ..default()
36        },
37        Transform::from_xyz(100.0, 0.0, 0.2),
38    ));
39}
examples/3d/atmospheric_fog.rs (line 31)
26fn setup_camera_fog(mut commands: Commands) {
27    commands.spawn((
28        Camera3d::default(),
29        Transform::from_xyz(-1.0, 0.1, 1.0).looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
30        DistanceFog {
31            color: Color::srgba(0.35, 0.48, 0.66, 1.0),
32            directional_light_color: Color::srgba(1.0, 0.95, 0.85, 0.5),
33            directional_light_exponent: 30.0,
34            falloff: FogFalloff::from_visibility_colors(
35                15.0, // distance in world units up to which objects retain visibility (>= 5% contrast)
36                Color::srgb(0.35, 0.5, 0.66), // atmospheric extinction color (after light is lost due to absorption by atmospheric particles)
37                Color::srgb(0.8, 0.844, 1.0), // atmospheric inscattering color (light gained due to scattering from the sun)
38            ),
39        },
40    ));
41}
examples/3d/texture.rs (line 39)
15fn setup(
16    mut commands: Commands,
17    asset_server: Res<AssetServer>,
18    mut meshes: ResMut<Assets<Mesh>>,
19    mut materials: ResMut<Assets<StandardMaterial>>,
20) {
21    // load a texture and retrieve its aspect ratio
22    let texture_handle = asset_server.load("branding/bevy_logo_dark_big.png");
23    let aspect = 0.25;
24
25    // create a new quad mesh. this is what we will apply the texture to
26    let quad_width = 8.0;
27    let quad_handle = meshes.add(Rectangle::new(quad_width, quad_width * aspect));
28
29    // this material renders the texture normally
30    let material_handle = materials.add(StandardMaterial {
31        base_color_texture: Some(texture_handle.clone()),
32        alpha_mode: AlphaMode::Blend,
33        unlit: true,
34        ..default()
35    });
36
37    // this material modulates the texture to make it red (and slightly transparent)
38    let red_material_handle = materials.add(StandardMaterial {
39        base_color: Color::srgba(1.0, 0.0, 0.0, 0.5),
40        base_color_texture: Some(texture_handle.clone()),
41        alpha_mode: AlphaMode::Blend,
42        unlit: true,
43        ..default()
44    });
45
46    // and lets make this one blue! (and also slightly transparent)
47    let blue_material_handle = materials.add(StandardMaterial {
48        base_color: Color::srgba(0.0, 0.0, 1.0, 0.5),
49        base_color_texture: Some(texture_handle),
50        alpha_mode: AlphaMode::Blend,
51        unlit: true,
52        ..default()
53    });
54
55    // textured quad - normal
56    commands.spawn((
57        Mesh3d(quad_handle.clone()),
58        MeshMaterial3d(material_handle),
59        Transform::from_xyz(0.0, 0.0, 1.5).with_rotation(Quat::from_rotation_x(-PI / 5.0)),
60    ));
61    // textured quad - modulated
62    commands.spawn((
63        Mesh3d(quad_handle.clone()),
64        MeshMaterial3d(red_material_handle),
65        Transform::from_rotation(Quat::from_rotation_x(-PI / 5.0)),
66    ));
67    // textured quad - modulated
68    commands.spawn((
69        Mesh3d(quad_handle),
70        MeshMaterial3d(blue_material_handle),
71        Transform::from_xyz(0.0, 0.0, -1.5).with_rotation(Quat::from_rotation_x(-PI / 5.0)),
72    ));
73    // camera
74    commands.spawn((
75        Camera3d::default(),
76        Transform::from_xyz(3.0, 5.0, 8.0).looking_at(Vec3::ZERO, Vec3::Y),
77    ));
78}
examples/ui/transparency_ui.rs (line 49)
14fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
15    commands.spawn(Camera2d);
16
17    let font_handle = asset_server.load("fonts/FiraSans-Bold.ttf");
18
19    commands
20        .spawn(Node {
21            width: percent(100),
22            height: percent(100),
23            align_items: AlignItems::Center,
24            justify_content: JustifyContent::SpaceAround,
25            ..default()
26        })
27        .with_children(|parent| {
28            parent
29                .spawn((
30                    Button,
31                    Node {
32                        width: px(150),
33                        height: px(65),
34                        justify_content: JustifyContent::Center,
35                        align_items: AlignItems::Center,
36                        ..default()
37                    },
38                    BackgroundColor(Color::srgb(0.1, 0.5, 0.1)),
39                ))
40                .with_children(|parent| {
41                    parent.spawn((
42                        Text::new("Button 1"),
43                        TextFont {
44                            font: font_handle.clone(),
45                            font_size: 33.0,
46                            ..default()
47                        },
48                        // Alpha channel of the color controls transparency.
49                        TextColor(Color::srgba(1.0, 1.0, 1.0, 0.2)),
50                    ));
51                });
52
53            // Button with a different color,
54            // to demonstrate the text looks different due to its transparency.
55            parent
56                .spawn((
57                    Button,
58                    Node {
59                        width: px(150),
60                        height: px(65),
61                        justify_content: JustifyContent::Center,
62                        align_items: AlignItems::Center,
63                        ..default()
64                    },
65                    BackgroundColor(Color::srgb(0.5, 0.1, 0.5)),
66                ))
67                .with_children(|parent| {
68                    parent.spawn((
69                        Text::new("Button 2"),
70                        TextFont {
71                            font: font_handle.clone(),
72                            font_size: 33.0,
73                            ..default()
74                        },
75                        // Alpha channel of the color controls transparency.
76                        TextColor(Color::srgba(1.0, 1.0, 1.0, 0.2)),
77                    ));
78                });
79        });
80}
examples/math/random_sampling.rs (line 75)
51fn setup(
52    mut commands: Commands,
53    mut meshes: ResMut<Assets<Mesh>>,
54    mut materials: ResMut<Assets<StandardMaterial>>,
55) {
56    // Use seeded rng and store it in a resource; this makes the random output reproducible.
57    let seeded_rng = ChaCha8Rng::seed_from_u64(19878367467712);
58    commands.insert_resource(RandomSource(seeded_rng));
59
60    // Make a plane for establishing space.
61    commands.spawn((
62        Mesh3d(meshes.add(Plane3d::default().mesh().size(12.0, 12.0))),
63        MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
64        Transform::from_xyz(0.0, -2.5, 0.0),
65    ));
66
67    // Store the shape we sample from in a resource:
68    let shape = Cuboid::from_length(2.9);
69    commands.insert_resource(SampledShape(shape));
70
71    // The sampled shape shown transparently:
72    commands.spawn((
73        Mesh3d(meshes.add(shape)),
74        MeshMaterial3d(materials.add(StandardMaterial {
75            base_color: Color::srgba(0.2, 0.1, 0.6, 0.3),
76            alpha_mode: AlphaMode::Blend,
77            cull_mode: None,
78            ..default()
79        })),
80    ));
81
82    // A light:
83    commands.spawn((
84        PointLight {
85            shadows_enabled: true,
86            ..default()
87        },
88        Transform::from_xyz(4.0, 8.0, 4.0),
89    ));
90
91    // A camera:
92    commands.spawn((
93        Camera3d::default(),
94        Transform::from_xyz(-2.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
95    ));
96
97    // Store the mesh and material for sample points in resources:
98    commands.insert_resource(PointMesh(
99        meshes.add(
100            Sphere::new(0.03)
101                .mesh()
102                .kind(SphereKind::Ico { subdivisions: 3 }),
103        ),
104    ));
105    commands.insert_resource(PointMaterial(materials.add(StandardMaterial {
106        base_color: Color::srgb(1.0, 0.8, 0.8),
107        metallic: 0.8,
108        ..default()
109    })));
110
111    // Instructions for the example:
112    commands.spawn((
113        Text::new(
114            "Controls:\n\
115            M: Toggle between sampling boundary and interior.\n\
116            R: Restart (erase all samples).\n\
117            S: Add one random sample.\n\
118            D: Add 100 random samples.\n\
119            Rotate camera by holding left mouse and panning left/right.",
120        ),
121        Node {
122            position_type: PositionType::Absolute,
123            top: px(12),
124            left: px(12),
125            ..default()
126        },
127    ));
128
129    // The mode starts with interior points.
130    commands.insert_resource(Mode::Interior);
131
132    // Starting mouse-pressed state is false.
133    commands.insert_resource(MousePressed(false));
134}
Source

pub const fn srgb(red: f32, green: f32, blue: f32) -> Color

Creates a new Color object storing a Srgba color with an alpha of 1.0.

§Arguments
  • red - Red channel. [0.0, 1.0]
  • green - Green channel. [0.0, 1.0]
  • blue - Blue channel. [0.0, 1.0]
Examples found in repository?
examples/dev_tools/fps_overlay.rs (line 12)
12    const RED: Color = Color::srgb(1.0, 0.0, 0.0);
13    const GREEN: Color = Color::srgb(0.0, 1.0, 0.0);
More examples
Hide additional examples
examples/games/game_menu.rs (line 6)
6const TEXT_COLOR: Color = Color::srgb(0.9, 0.9, 0.9);
7
8// Enum that will be used as a global state for the game
9#[derive(Clone, Copy, Default, Eq, PartialEq, Debug, Hash, States)]
10enum GameState {
11    #[default]
12    Splash,
13    Menu,
14    Game,
15}
16
17// One of the two settings that can be set through the menu. It will be a resource in the app
18#[derive(Resource, Debug, Component, PartialEq, Eq, Clone, Copy)]
19enum DisplayQuality {
20    Low,
21    Medium,
22    High,
23}
24
25// One of the two settings that can be set through the menu. It will be a resource in the app
26#[derive(Resource, Debug, Component, PartialEq, Eq, Clone, Copy)]
27struct Volume(u32);
28
29fn main() {
30    App::new()
31        .add_plugins(DefaultPlugins)
32        // Insert as resource the initial value for the settings resources
33        .insert_resource(DisplayQuality::Medium)
34        .insert_resource(Volume(7))
35        // Declare the game state, whose starting value is determined by the `Default` trait
36        .init_state::<GameState>()
37        .add_systems(Startup, setup)
38        // Adds the plugins for each state
39        .add_plugins((splash::splash_plugin, menu::menu_plugin, game::game_plugin))
40        .run();
41}
42
43fn setup(mut commands: Commands) {
44    commands.spawn(Camera2d);
45}
46
47mod splash {
48    use bevy::prelude::*;
49
50    use super::GameState;
51
52    // This plugin will display a splash screen with Bevy logo for 1 second before switching to the menu
53    pub fn splash_plugin(app: &mut App) {
54        // As this plugin is managing the splash screen, it will focus on the state `GameState::Splash`
55        app
56            // When entering the state, spawn everything needed for this screen
57            .add_systems(OnEnter(GameState::Splash), splash_setup)
58            // While in this state, run the `countdown` system
59            .add_systems(Update, countdown.run_if(in_state(GameState::Splash)));
60    }
61
62    // Tag component used to tag entities added on the splash screen
63    #[derive(Component)]
64    struct OnSplashScreen;
65
66    // Newtype to use a `Timer` for this screen as a resource
67    #[derive(Resource, Deref, DerefMut)]
68    struct SplashTimer(Timer);
69
70    fn splash_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
71        let icon = asset_server.load("branding/icon.png");
72        // Display the logo
73        commands.spawn((
74            // This entity will be despawned when exiting the state
75            DespawnOnExit(GameState::Splash),
76            Node {
77                align_items: AlignItems::Center,
78                justify_content: JustifyContent::Center,
79                width: percent(100),
80                height: percent(100),
81                ..default()
82            },
83            OnSplashScreen,
84            children![(
85                ImageNode::new(icon),
86                Node {
87                    // This will set the logo to be 200px wide, and auto adjust its height
88                    width: px(200),
89                    ..default()
90                },
91            )],
92        ));
93        // Insert the timer as a resource
94        commands.insert_resource(SplashTimer(Timer::from_seconds(1.0, TimerMode::Once)));
95    }
96
97    // Tick the timer, and change state when finished
98    fn countdown(
99        mut game_state: ResMut<NextState<GameState>>,
100        time: Res<Time>,
101        mut timer: ResMut<SplashTimer>,
102    ) {
103        if timer.tick(time.delta()).is_finished() {
104            game_state.set(GameState::Menu);
105        }
106    }
107}
108
109mod game {
110    use bevy::{
111        color::palettes::basic::{BLUE, LIME},
112        prelude::*,
113    };
114
115    use super::{DisplayQuality, GameState, Volume, TEXT_COLOR};
116
117    // This plugin will contain the game. In this case, it's just be a screen that will
118    // display the current settings for 5 seconds before returning to the menu
119    pub fn game_plugin(app: &mut App) {
120        app.add_systems(OnEnter(GameState::Game), game_setup)
121            .add_systems(Update, game.run_if(in_state(GameState::Game)));
122    }
123
124    // Tag component used to tag entities added on the game screen
125    #[derive(Component)]
126    struct OnGameScreen;
127
128    #[derive(Resource, Deref, DerefMut)]
129    struct GameTimer(Timer);
130
131    fn game_setup(
132        mut commands: Commands,
133        display_quality: Res<DisplayQuality>,
134        volume: Res<Volume>,
135    ) {
136        commands.spawn((
137            DespawnOnExit(GameState::Game),
138            Node {
139                width: percent(100),
140                height: percent(100),
141                // center children
142                align_items: AlignItems::Center,
143                justify_content: JustifyContent::Center,
144                ..default()
145            },
146            OnGameScreen,
147            children![(
148                Node {
149                    // This will display its children in a column, from top to bottom
150                    flex_direction: FlexDirection::Column,
151                    // `align_items` will align children on the cross axis. Here the main axis is
152                    // vertical (column), so the cross axis is horizontal. This will center the
153                    // children
154                    align_items: AlignItems::Center,
155                    ..default()
156                },
157                BackgroundColor(Color::BLACK),
158                children![
159                    (
160                        Text::new("Will be back to the menu shortly..."),
161                        TextFont {
162                            font_size: 67.0,
163                            ..default()
164                        },
165                        TextColor(TEXT_COLOR),
166                        Node {
167                            margin: UiRect::all(px(50)),
168                            ..default()
169                        },
170                    ),
171                    (
172                        Text::default(),
173                        Node {
174                            margin: UiRect::all(px(50)),
175                            ..default()
176                        },
177                        children![
178                            (
179                                TextSpan(format!("quality: {:?}", *display_quality)),
180                                TextFont {
181                                    font_size: 50.0,
182                                    ..default()
183                                },
184                                TextColor(BLUE.into()),
185                            ),
186                            (
187                                TextSpan::new(" - "),
188                                TextFont {
189                                    font_size: 50.0,
190                                    ..default()
191                                },
192                                TextColor(TEXT_COLOR),
193                            ),
194                            (
195                                TextSpan(format!("volume: {:?}", *volume)),
196                                TextFont {
197                                    font_size: 50.0,
198                                    ..default()
199                                },
200                                TextColor(LIME.into()),
201                            ),
202                        ]
203                    ),
204                ]
205            )],
206        ));
207        // Spawn a 5 seconds timer to trigger going back to the menu
208        commands.insert_resource(GameTimer(Timer::from_seconds(5.0, TimerMode::Once)));
209    }
210
211    // Tick the timer, and change state when finished
212    fn game(
213        time: Res<Time>,
214        mut game_state: ResMut<NextState<GameState>>,
215        mut timer: ResMut<GameTimer>,
216    ) {
217        if timer.tick(time.delta()).is_finished() {
218            game_state.set(GameState::Menu);
219        }
220    }
221}
222
223mod menu {
224    use bevy::{
225        app::AppExit,
226        color::palettes::css::CRIMSON,
227        ecs::spawn::{SpawnIter, SpawnWith},
228        prelude::*,
229    };
230
231    use super::{DisplayQuality, GameState, Volume, TEXT_COLOR};
232
233    // This plugin manages the menu, with 5 different screens:
234    // - a main menu with "New Game", "Settings", "Quit"
235    // - a settings menu with two submenus and a back button
236    // - two settings screen with a setting that can be set and a back button
237    pub fn menu_plugin(app: &mut App) {
238        app
239            // At start, the menu is not enabled. This will be changed in `menu_setup` when
240            // entering the `GameState::Menu` state.
241            // Current screen in the menu is handled by an independent state from `GameState`
242            .init_state::<MenuState>()
243            .add_systems(OnEnter(GameState::Menu), menu_setup)
244            // Systems to handle the main menu screen
245            .add_systems(OnEnter(MenuState::Main), main_menu_setup)
246            // Systems to handle the settings menu screen
247            .add_systems(OnEnter(MenuState::Settings), settings_menu_setup)
248            // Systems to handle the display settings screen
249            .add_systems(
250                OnEnter(MenuState::SettingsDisplay),
251                display_settings_menu_setup,
252            )
253            .add_systems(
254                Update,
255                (setting_button::<DisplayQuality>.run_if(in_state(MenuState::SettingsDisplay)),),
256            )
257            // Systems to handle the sound settings screen
258            .add_systems(OnEnter(MenuState::SettingsSound), sound_settings_menu_setup)
259            .add_systems(
260                Update,
261                setting_button::<Volume>.run_if(in_state(MenuState::SettingsSound)),
262            )
263            // Common systems to all screens that handles buttons behavior
264            .add_systems(
265                Update,
266                (menu_action, button_system).run_if(in_state(GameState::Menu)),
267            );
268    }
269
270    // State used for the current menu screen
271    #[derive(Clone, Copy, Default, Eq, PartialEq, Debug, Hash, States)]
272    enum MenuState {
273        Main,
274        Settings,
275        SettingsDisplay,
276        SettingsSound,
277        #[default]
278        Disabled,
279    }
280
281    // Tag component used to tag entities added on the main menu screen
282    #[derive(Component)]
283    struct OnMainMenuScreen;
284
285    // Tag component used to tag entities added on the settings menu screen
286    #[derive(Component)]
287    struct OnSettingsMenuScreen;
288
289    // Tag component used to tag entities added on the display settings menu screen
290    #[derive(Component)]
291    struct OnDisplaySettingsMenuScreen;
292
293    // Tag component used to tag entities added on the sound settings menu screen
294    #[derive(Component)]
295    struct OnSoundSettingsMenuScreen;
296
297    const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15);
298    const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25);
299    const HOVERED_PRESSED_BUTTON: Color = Color::srgb(0.25, 0.65, 0.25);
300    const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35);
examples/games/stepping.rs (line 88)
88const FONT_COLOR: Color = Color::srgb(0.2, 0.2, 0.2);
examples/math/sampling_primitives.rs (line 65)
65const SKY_COLOR: Color = Color::srgb(0.02, 0.06, 0.15);
66
67const SMALL_3D: f32 = 0.5;
68const BIG_3D: f32 = 1.0;
69
70// primitives
71
72const CUBOID: Cuboid = Cuboid {
73    half_size: Vec3::new(SMALL_3D, BIG_3D, SMALL_3D),
74};
75
76const SPHERE: Sphere = Sphere {
77    radius: 1.5 * SMALL_3D,
78};
79
80const TRIANGLE_3D: Triangle3d = Triangle3d {
81    vertices: [
82        Vec3::new(BIG_3D, -BIG_3D * 0.5, 0.0),
83        Vec3::new(0.0, BIG_3D, 0.0),
84        Vec3::new(-BIG_3D, -BIG_3D * 0.5, 0.0),
85    ],
86};
87
88const CAPSULE_3D: Capsule3d = Capsule3d {
89    radius: SMALL_3D,
90    half_length: SMALL_3D,
91};
92
93const CYLINDER: Cylinder = Cylinder {
94    radius: SMALL_3D,
95    half_height: SMALL_3D,
96};
97
98const TETRAHEDRON: Tetrahedron = Tetrahedron {
99    vertices: [
100        Vec3::new(-BIG_3D, -BIG_3D * 0.67, BIG_3D * 0.5),
101        Vec3::new(BIG_3D, -BIG_3D * 0.67, BIG_3D * 0.5),
102        Vec3::new(0.0, -BIG_3D * 0.67, -BIG_3D * 1.17),
103        Vec3::new(0.0, BIG_3D, 0.0),
104    ],
105};
106
107// Components, Resources
108
109/// Resource for the random sampling mode, telling whether to sample the interior or the boundary.
110#[derive(Resource)]
111enum SamplingMode {
112    Interior,
113    Boundary,
114}
115
116/// Resource for storing whether points should spawn by themselves
117#[derive(Resource)]
118enum SpawningMode {
119    Manual,
120    Automatic,
121}
122
123/// Resource for tracking how many points should be spawned
124#[derive(Resource)]
125struct SpawnQueue(usize);
126
127#[derive(Resource)]
128struct PointCounter(usize);
129
130/// Resource storing the shapes being sampled and their translations.
131#[derive(Resource)]
132struct SampledShapes(Vec<(Shape, Vec3)>);
133
134impl SampledShapes {
135    fn new() -> Self {
136        let shapes = Shape::list_all_shapes();
137
138        let n_shapes = shapes.len();
139
140        let translations =
141            (0..n_shapes).map(|i| (i as f32 - n_shapes as f32 / 2.0) * DISTANCE_BETWEEN_SHAPES);
142
143        SampledShapes(shapes.into_iter().zip(translations).collect())
144    }
145}
146
147/// Enum listing the shapes that can be sampled
148#[derive(Clone, Copy)]
149enum Shape {
150    Cuboid,
151    Sphere,
152    Capsule,
153    Cylinder,
154    Tetrahedron,
155    Triangle,
156}
157struct ShapeMeshBuilder {
158    shape: Shape,
159}
160
161impl Shape {
162    /// Return a vector containing all implemented shapes
163    fn list_all_shapes() -> Vec<Shape> {
164        vec![
165            Shape::Cuboid,
166            Shape::Sphere,
167            Shape::Capsule,
168            Shape::Cylinder,
169            Shape::Tetrahedron,
170            Shape::Triangle,
171        ]
172    }
173}
174
175impl ShapeSample for Shape {
176    type Output = Vec3;
177    fn sample_interior<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec3 {
178        match self {
179            Shape::Cuboid => CUBOID.sample_interior(rng),
180            Shape::Sphere => SPHERE.sample_interior(rng),
181            Shape::Capsule => CAPSULE_3D.sample_interior(rng),
182            Shape::Cylinder => CYLINDER.sample_interior(rng),
183            Shape::Tetrahedron => TETRAHEDRON.sample_interior(rng),
184            Shape::Triangle => TRIANGLE_3D.sample_interior(rng),
185        }
186    }
187
188    fn sample_boundary<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::Output {
189        match self {
190            Shape::Cuboid => CUBOID.sample_boundary(rng),
191            Shape::Sphere => SPHERE.sample_boundary(rng),
192            Shape::Capsule => CAPSULE_3D.sample_boundary(rng),
193            Shape::Cylinder => CYLINDER.sample_boundary(rng),
194            Shape::Tetrahedron => TETRAHEDRON.sample_boundary(rng),
195            Shape::Triangle => TRIANGLE_3D.sample_boundary(rng),
196        }
197    }
198}
199
200impl Meshable for Shape {
201    type Output = ShapeMeshBuilder;
202
203    fn mesh(&self) -> Self::Output {
204        ShapeMeshBuilder { shape: *self }
205    }
206}
207
208impl MeshBuilder for ShapeMeshBuilder {
209    fn build(&self) -> Mesh {
210        match self.shape {
211            Shape::Cuboid => CUBOID.mesh().into(),
212            Shape::Sphere => SPHERE.mesh().into(),
213            Shape::Capsule => CAPSULE_3D.mesh().into(),
214            Shape::Cylinder => CYLINDER.mesh().into(),
215            Shape::Tetrahedron => TETRAHEDRON.mesh().into(),
216            Shape::Triangle => TRIANGLE_3D.mesh().into(),
217        }
218    }
219}
220
221/// The source of randomness used by this example.
222#[derive(Resource)]
223struct RandomSource(ChaCha8Rng);
224
225/// A container for the handle storing the mesh used to display sampled points as spheres.
226#[derive(Resource)]
227struct PointMesh(Handle<Mesh>);
228
229/// A container for the handle storing the material used to display sampled points.
230#[derive(Resource)]
231struct PointMaterial {
232    interior: Handle<StandardMaterial>,
233    boundary: Handle<StandardMaterial>,
234}
235
236/// Marker component for sampled points.
237#[derive(Component)]
238struct SamplePoint;
239
240/// Component for animating the spawn animation of lights.
241#[derive(Component)]
242struct SpawningPoint {
243    progress: f32,
244}
245
246/// Marker component for lights which should change intensity.
247#[derive(Component)]
248struct DespawningPoint {
249    progress: f32,
250}
251
252/// Marker component for lights which should change intensity.
253#[derive(Component)]
254struct FireflyLights;
255
256/// The pressed state of the mouse, used for camera motion.
257#[derive(Resource)]
258struct MousePressed(bool);
259
260/// Camera movement component.
261#[derive(Component)]
262struct CameraRig {
263    /// Rotation around the vertical axis of the camera (radians).
264    /// Positive changes makes the camera look more from the right.
265    pub yaw: f32,
266    /// Rotation around the horizontal axis of the camera (radians) (-pi/2; pi/2).
267    /// Positive looks down from above.
268    pub pitch: f32,
269    /// Distance from the center, smaller distance causes more zoom.
270    pub distance: f32,
271    /// Location in 3D space at which the camera is looking and around which it is orbiting.
272    pub target: Vec3,
273}
274
275fn setup(
276    mut commands: Commands,
277    mut meshes: ResMut<Assets<Mesh>>,
278    mut materials: ResMut<Assets<StandardMaterial>>,
279    shapes: Res<SampledShapes>,
280) {
281    // Use seeded rng and store it in a resource; this makes the random output reproducible.
282    let seeded_rng = ChaCha8Rng::seed_from_u64(4); // Chosen by a fair die roll, guaranteed to be random.
283    commands.insert_resource(RandomSource(seeded_rng));
284
285    // Make a plane for establishing space.
286    commands.spawn((
287        Mesh3d(meshes.add(Plane3d::default().mesh().size(20.0, 20.0))),
288        MeshMaterial3d(materials.add(StandardMaterial {
289            base_color: Color::srgb(0.3, 0.5, 0.3),
290            perceptual_roughness: 0.95,
291            metallic: 0.0,
292            ..default()
293        })),
294        Transform::from_xyz(0.0, -2.5, 0.0),
295    ));
296
297    let shape_material = materials.add(StandardMaterial {
298        base_color: Color::srgba(0.2, 0.1, 0.6, 0.3),
299        reflectance: 0.0,
300        alpha_mode: AlphaMode::Blend,
301        cull_mode: None,
302        ..default()
303    });
304
305    // Spawn shapes to be sampled
306    for (shape, translation) in shapes.0.iter() {
307        // The sampled shape shown transparently:
308        commands.spawn((
309            Mesh3d(meshes.add(shape.mesh())),
310            MeshMaterial3d(shape_material.clone()),
311            Transform::from_translation(*translation),
312        ));
313
314        // Lights which work as the bulk lighting of the fireflies:
315        commands.spawn((
316            PointLight {
317                range: 4.0,
318                radius: 0.6,
319                intensity: 1.0,
320                shadows_enabled: false,
321                color: Color::LinearRgba(INSIDE_POINT_COLOR),
322                ..default()
323            },
324            Transform::from_translation(*translation),
325            FireflyLights,
326        ));
327    }
328
329    // Global light:
330    commands.spawn((
331        PointLight {
332            color: SKY_COLOR,
333            intensity: 2_000.0,
334            shadows_enabled: false,
335            ..default()
336        },
337        Transform::from_xyz(4.0, 8.0, 4.0),
338    ));
339
340    // A camera:
341    commands.spawn((
342        Camera3d::default(),
343        Camera {
344            clear_color: ClearColorConfig::Custom(SKY_COLOR),
345            ..default()
346        },
347        Tonemapping::TonyMcMapface,
348        Transform::from_xyz(-2.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
349        Bloom::NATURAL,
350        CameraRig {
351            yaw: 0.56,
352            pitch: 0.45,
353            distance: 8.0,
354            target: Vec3::ZERO,
355        },
356    ));
357
358    // Store the mesh and material for sample points in resources:
359    commands.insert_resource(PointMesh(
360        meshes.add(Sphere::new(0.03).mesh().ico(1).unwrap()),
361    ));
362    commands.insert_resource(PointMaterial {
363        interior: materials.add(StandardMaterial {
364            base_color: Color::BLACK,
365            reflectance: 0.05,
366            emissive: 2.5 * INSIDE_POINT_COLOR,
367            ..default()
368        }),
369        boundary: materials.add(StandardMaterial {
370            base_color: Color::BLACK,
371            reflectance: 0.05,
372            emissive: 1.5 * BOUNDARY_POINT_COLOR,
373            ..default()
374        }),
375    });
376
377    // Instructions for the example:
378    commands.spawn((
379        Text::new(
380            "Controls:\n\
381            M: Toggle between sampling boundary and interior.\n\
382            A: Toggle automatic spawning & despawning of points.\n\
383            R: Restart (erase all samples).\n\
384            S: Add one random sample.\n\
385            D: Add 100 random samples.\n\
386            Rotate camera by holding left mouse and panning.\n\
387            Zoom camera by scrolling via mouse or +/-.\n\
388            Move camera by L/R arrow keys.\n\
389            Tab: Toggle this text",
390        ),
391        Node {
392            position_type: PositionType::Absolute,
393            top: px(12),
394            left: px(12),
395            ..default()
396        },
397    ));
398
399    // No points are scheduled to spawn initially.
400    commands.insert_resource(SpawnQueue(0));
401
402    // No points have been spawned initially.
403    commands.insert_resource(PointCounter(0));
404
405    // The mode starts with interior points.
406    commands.insert_resource(SamplingMode::Interior);
407
408    // Points spawn automatically by default.
409    commands.insert_resource(SpawningMode::Automatic);
410
411    // Starting mouse-pressed state is false.
412    commands.insert_resource(MousePressed(false));
413}
examples/ui/display_and_visibility.rs (line 10)
10const HIDDEN_COLOR: Color = Color::srgb(1.0, 0.7, 0.7);
11
12fn main() {
13    App::new()
14        .add_plugins(DefaultPlugins)
15        .add_systems(Startup, setup)
16        .add_systems(
17            Update,
18            (
19                buttons_handler::<Display>,
20                buttons_handler::<Visibility>,
21                text_hover,
22            ),
23        )
24        .run();
25}
26
27#[derive(Component)]
28struct Target<T> {
29    id: Entity,
30    phantom: std::marker::PhantomData<T>,
31}
32
33impl<T> Target<T> {
34    fn new(id: Entity) -> Self {
35        Self {
36            id,
37            phantom: std::marker::PhantomData,
38        }
39    }
40}
41
42trait TargetUpdate {
43    type TargetComponent: Component<Mutability = Mutable>;
44    const NAME: &'static str;
45    fn update_target(&self, target: &mut Self::TargetComponent) -> String;
46}
47
48impl TargetUpdate for Target<Display> {
49    type TargetComponent = Node;
50    const NAME: &'static str = "Display";
51    fn update_target(&self, node: &mut Self::TargetComponent) -> String {
52        node.display = match node.display {
53            Display::Flex => Display::None,
54            Display::None => Display::Flex,
55            Display::Block | Display::Grid => unreachable!(),
56        };
57        format!("{}::{:?} ", Self::NAME, node.display)
58    }
59}
60
61impl TargetUpdate for Target<Visibility> {
62    type TargetComponent = Visibility;
63    const NAME: &'static str = "Visibility";
64    fn update_target(&self, visibility: &mut Self::TargetComponent) -> String {
65        *visibility = match *visibility {
66            Visibility::Inherited => Visibility::Visible,
67            Visibility::Visible => Visibility::Hidden,
68            Visibility::Hidden => Visibility::Inherited,
69        };
70        format!("{}::{visibility:?}", Self::NAME)
71    }
72}
73
74fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
75    let palette: [Color; 4] = PALETTE.map(|hex| Srgba::hex(hex).unwrap().into());
76
77    let text_font = TextFont {
78        font: asset_server.load("fonts/FiraSans-Bold.ttf"),
79        ..default()
80    };
81
82    commands.spawn(Camera2d);
83    commands
84        .spawn((
85            Node {
86                width: percent(100),
87                height: percent(100),
88                flex_direction: FlexDirection::Column,
89                align_items: AlignItems::Center,
90                justify_content: JustifyContent::SpaceEvenly,
91                ..Default::default()
92            },
93            BackgroundColor(Color::BLACK),
94        ))
95        .with_children(|parent| {
96            parent.spawn((
97                Text::new("Use the panel on the right to change the Display and Visibility properties for the respective nodes of the panel on the left"),
98                text_font.clone(),
99                TextLayout::new_with_justify(Justify::Center),
100                Node {
101                    margin: UiRect::bottom(px(10)),
102                    ..Default::default()
103                },
104            ));
105
106            parent
107                .spawn(Node {
108                    width: percent(100),
109                    ..default()
110                })
111                .with_children(|parent| {
112                    let mut target_ids = vec![];
113                    parent
114                        .spawn(Node {
115                            width: percent(50),
116                            height: px(520),
117                            justify_content: JustifyContent::Center,
118                            ..default()
119                        })
120                        .with_children(|parent| {
121                            target_ids = spawn_left_panel(parent, &palette);
122                        });
123
124                    parent
125                        .spawn(Node {
126                            width: percent(50),
127                            justify_content: JustifyContent::Center,
128                            ..default()
129                        })
130                        .with_children(|parent| {
131                            spawn_right_panel(parent, text_font, &palette, target_ids);
132                        });
133                });
134
135            parent
136                .spawn(Node {
137                    flex_direction: FlexDirection::Row,
138                    align_items: AlignItems::Start,
139                    justify_content: JustifyContent::Start,
140                    column_gap: px(10),
141                    ..default()
142                })
143                .with_children(|builder| {
144                    let text_font = TextFont {
145                        font: asset_server.load("fonts/FiraSans-Bold.ttf"),
146                        ..default()
147                    };
148
149                    builder.spawn((
150                        Text::new("Display::None\nVisibility::Hidden\nVisibility::Inherited"),
151                        text_font.clone(),
152                        TextColor(HIDDEN_COLOR),
153                        TextLayout::new_with_justify(Justify::Center),
154                    ));
155                    builder.spawn((
156                        Text::new("-\n-\n-"),
157                        text_font.clone(),
158                        TextColor(DARK_GRAY.into()),
159                        TextLayout::new_with_justify(Justify::Center),
160                    ));
161                    builder.spawn((Text::new("The UI Node and its descendants will not be visible and will not be allotted any space in the UI layout.\nThe UI Node will not be visible but will still occupy space in the UI layout.\nThe UI node will inherit the visibility property of its parent. If it has no parent it will be visible."), text_font));
162                });
163        });
164}
165
166fn spawn_left_panel(builder: &mut ChildSpawnerCommands, palette: &[Color; 4]) -> Vec<Entity> {
167    let mut target_ids = vec![];
168    builder
169        .spawn((
170            Node {
171                padding: UiRect::all(px(10)),
172                ..default()
173            },
174            BackgroundColor(Color::WHITE),
175        ))
176        .with_children(|parent| {
177            parent
178                .spawn((Node::default(), BackgroundColor(Color::BLACK)))
179                .with_children(|parent| {
180                    let id = parent
181                        .spawn((
182                            Node {
183                                align_items: AlignItems::FlexEnd,
184                                justify_content: JustifyContent::FlexEnd,
185                                ..default()
186                            },
187                            BackgroundColor(palette[0]),
188                            Outline {
189                                width: px(4),
190                                color: DARK_CYAN.into(),
191                                offset: px(10),
192                            },
193                        ))
194                        .with_children(|parent| {
195                            parent.spawn(Node {
196                                width: px(100),
197                                height: px(500),
198                                ..default()
199                            });
200
201                            let id = parent
202                                .spawn((
203                                    Node {
204                                        height: px(400),
205                                        align_items: AlignItems::FlexEnd,
206                                        justify_content: JustifyContent::FlexEnd,
207                                        ..default()
208                                    },
209                                    BackgroundColor(palette[1]),
210                                ))
211                                .with_children(|parent| {
212                                    parent.spawn(Node {
213                                        width: px(100),
214                                        height: px(400),
215                                        ..default()
216                                    });
217
218                                    let id = parent
219                                        .spawn((
220                                            Node {
221                                                height: px(300),
222                                                align_items: AlignItems::FlexEnd,
223                                                justify_content: JustifyContent::FlexEnd,
224                                                ..default()
225                                            },
226                                            BackgroundColor(palette[2]),
227                                        ))
228                                        .with_children(|parent| {
229                                            parent.spawn(Node {
230                                                width: px(100),
231                                                height: px(300),
232                                                ..default()
233                                            });
234
235                                            let id = parent
236                                                .spawn((
237                                                    Node {
238                                                        width: px(200),
239                                                        height: px(200),
240                                                        ..default()
241                                                    },
242                                                    BackgroundColor(palette[3]),
243                                                ))
244                                                .id();
245                                            target_ids.push(id);
246                                        })
247                                        .id();
248                                    target_ids.push(id);
249                                })
250                                .id();
251                            target_ids.push(id);
252                        })
253                        .id();
254                    target_ids.push(id);
255                });
256        });
257    target_ids
258}
259
260fn spawn_right_panel(
261    parent: &mut ChildSpawnerCommands,
262    text_font: TextFont,
263    palette: &[Color; 4],
264    mut target_ids: Vec<Entity>,
265) {
266    let spawn_buttons = |parent: &mut ChildSpawnerCommands, target_id| {
267        spawn_button::<Display>(parent, text_font.clone(), target_id);
268        spawn_button::<Visibility>(parent, text_font.clone(), target_id);
269    };
270    parent
271        .spawn((
272            Node {
273                padding: UiRect::all(px(10)),
274                ..default()
275            },
276            BackgroundColor(Color::WHITE),
277        ))
278        .with_children(|parent| {
279            parent
280                .spawn((
281                    Node {
282                        width: px(500),
283                        height: px(500),
284                        flex_direction: FlexDirection::Column,
285                        align_items: AlignItems::FlexEnd,
286                        justify_content: JustifyContent::SpaceBetween,
287                        padding: UiRect {
288                            left: px(5),
289                            top: px(5),
290                            ..default()
291                        },
292                        ..default()
293                    },
294                    BackgroundColor(palette[0]),
295                    Outline {
296                        width: px(4),
297                        color: DARK_CYAN.into(),
298                        offset: px(10),
299                    },
300                ))
301                .with_children(|parent| {
302                    spawn_buttons(parent, target_ids.pop().unwrap());
303
304                    parent
305                        .spawn((
306                            Node {
307                                width: px(400),
308                                height: px(400),
309                                flex_direction: FlexDirection::Column,
310                                align_items: AlignItems::FlexEnd,
311                                justify_content: JustifyContent::SpaceBetween,
312                                padding: UiRect {
313                                    left: px(5),
314                                    top: px(5),
315                                    ..default()
316                                },
317                                ..default()
318                            },
319                            BackgroundColor(palette[1]),
320                        ))
321                        .with_children(|parent| {
322                            spawn_buttons(parent, target_ids.pop().unwrap());
323
324                            parent
325                                .spawn((
326                                    Node {
327                                        width: px(300),
328                                        height: px(300),
329                                        flex_direction: FlexDirection::Column,
330                                        align_items: AlignItems::FlexEnd,
331                                        justify_content: JustifyContent::SpaceBetween,
332                                        padding: UiRect {
333                                            left: px(5),
334                                            top: px(5),
335                                            ..default()
336                                        },
337                                        ..default()
338                                    },
339                                    BackgroundColor(palette[2]),
340                                ))
341                                .with_children(|parent| {
342                                    spawn_buttons(parent, target_ids.pop().unwrap());
343
344                                    parent
345                                        .spawn((
346                                            Node {
347                                                width: px(200),
348                                                height: px(200),
349                                                align_items: AlignItems::FlexStart,
350                                                justify_content: JustifyContent::SpaceBetween,
351                                                flex_direction: FlexDirection::Column,
352                                                padding: UiRect {
353                                                    left: px(5),
354                                                    top: px(5),
355                                                    ..default()
356                                                },
357                                                ..default()
358                                            },
359                                            BackgroundColor(palette[3]),
360                                        ))
361                                        .with_children(|parent| {
362                                            spawn_buttons(parent, target_ids.pop().unwrap());
363
364                                            parent.spawn(Node {
365                                                width: px(100),
366                                                height: px(100),
367                                                ..default()
368                                            });
369                                        });
370                                });
371                        });
372                });
373        });
374}
375
376fn spawn_button<T>(parent: &mut ChildSpawnerCommands, text_font: TextFont, target: Entity)
377where
378    T: Default + std::fmt::Debug + Send + Sync + 'static,
379    Target<T>: TargetUpdate,
380{
381    parent
382        .spawn((
383            Button,
384            Node {
385                align_self: AlignSelf::FlexStart,
386                padding: UiRect::axes(px(5), px(1)),
387                ..default()
388            },
389            BackgroundColor(Color::BLACK.with_alpha(0.5)),
390            Target::<T>::new(target),
391        ))
392        .with_children(|builder| {
393            builder.spawn((
394                Text(format!("{}::{:?}", Target::<T>::NAME, T::default())),
395                text_font,
396                TextLayout::new_with_justify(Justify::Center),
397            ));
398        });
399}
400
401fn buttons_handler<T>(
402    mut left_panel_query: Query<&mut <Target<T> as TargetUpdate>::TargetComponent>,
403    mut visibility_button_query: Query<(&Target<T>, &Interaction, &Children), Changed<Interaction>>,
404    mut text_query: Query<(&mut Text, &mut TextColor)>,
405) where
406    T: Send + Sync,
407    Target<T>: TargetUpdate + Component,
408{
409    for (target, interaction, children) in visibility_button_query.iter_mut() {
410        if matches!(interaction, Interaction::Pressed) {
411            let mut target_value = left_panel_query.get_mut(target.id).unwrap();
412            for &child in children {
413                if let Ok((mut text, mut text_color)) = text_query.get_mut(child) {
414                    **text = target.update_target(target_value.as_mut());
415                    text_color.0 = if text.contains("None") || text.contains("Hidden") {
416                        Color::srgb(1.0, 0.7, 0.7)
417                    } else {
418                        Color::WHITE
419                    };
420                }
421            }
422        }
423    }
424}
examples/games/breakout.rs (line 45)
45const BACKGROUND_COLOR: Color = Color::srgb(0.9, 0.9, 0.9);
46const PADDLE_COLOR: Color = Color::srgb(0.3, 0.3, 0.7);
47const BALL_COLOR: Color = Color::srgb(1.0, 0.5, 0.5);
48const BRICK_COLOR: Color = Color::srgb(0.5, 0.5, 1.0);
49const WALL_COLOR: Color = Color::srgb(0.8, 0.8, 0.8);
50const TEXT_COLOR: Color = Color::srgb(0.5, 0.5, 1.0);
51const SCORE_COLOR: Color = Color::srgb(1.0, 0.5, 0.5);
Source

pub const fn srgb_from_array(array: [f32; 3]) -> Color

Reads an array of floats to creates a new Color object storing a Srgba color with an alpha of 1.0.

§Arguments
  • array - Red, Green and Blue channels. Each channel is in the range [0.0, 1.0]
Source

pub const fn srgba_u8(red: u8, green: u8, blue: u8, alpha: u8) -> Color

Creates a new Color object storing a Srgba color from u8 values.

§Arguments
  • red - Red channel. [0, 255]
  • green - Green channel. [0, 255]
  • blue - Blue channel. [0, 255]
  • alpha - Alpha channel. [0, 255]
Examples found in repository?
examples/3d/anti_aliasing.rs (line 476)
412fn setup(
413    mut commands: Commands,
414    mut meshes: ResMut<Assets<Mesh>>,
415    mut materials: ResMut<Assets<StandardMaterial>>,
416    mut images: ResMut<Assets<Image>>,
417    asset_server: Res<AssetServer>,
418) {
419    // Plane
420    commands.spawn((
421        Mesh3d(meshes.add(Plane3d::default().mesh().size(20.0, 20.0))),
422        MeshMaterial3d(materials.add(Color::srgb(0.1, 0.2, 0.1))),
423    ));
424
425    let cube_material = materials.add(StandardMaterial {
426        base_color_texture: Some(images.add(uv_debug_texture())),
427        ..default()
428    });
429
430    // Cubes
431    for i in 0..5 {
432        commands.spawn((
433            Mesh3d(meshes.add(Cuboid::new(0.25, 0.25, 0.25))),
434            MeshMaterial3d(cube_material.clone()),
435            Transform::from_xyz(i as f32 * 0.25 - 1.0, 0.125, -i as f32 * 0.5),
436        ));
437    }
438
439    // Flight Helmet
440    commands.spawn(SceneRoot(asset_server.load(
441        GltfAssetLabel::Scene(0).from_asset("models/FlightHelmet/FlightHelmet.gltf"),
442    )));
443
444    // Light
445    commands.spawn((
446        DirectionalLight {
447            illuminance: light_consts::lux::FULL_DAYLIGHT,
448            shadows_enabled: true,
449            ..default()
450        },
451        Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, PI * -0.15, PI * -0.15)),
452        CascadeShadowConfigBuilder {
453            maximum_distance: 3.0,
454            first_cascade_far_bound: 0.9,
455            ..default()
456        }
457        .build(),
458    ));
459
460    // Camera
461    commands.spawn((
462        Camera3d::default(),
463        Hdr,
464        Transform::from_xyz(0.7, 0.7, 1.0).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
465        ContrastAdaptiveSharpening {
466            enabled: false,
467            ..default()
468        },
469        EnvironmentMapLight {
470            diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
471            specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
472            intensity: 150.0,
473            ..default()
474        },
475        DistanceFog {
476            color: Color::srgba_u8(43, 44, 47, 255),
477            falloff: FogFalloff::Linear {
478                start: 1.0,
479                end: 4.0,
480            },
481            ..default()
482        },
483    ));
484
485    // example instructions
486    commands.spawn((
487        Text::default(),
488        Node {
489            position_type: PositionType::Absolute,
490            top: px(12),
491            left: px(12),
492            ..default()
493        },
494    ));
495}
Source

pub const fn srgb_u8(red: u8, green: u8, blue: u8) -> Color

Creates a new Color object storing a Srgba color from u8 values with an alpha of 1.0.

§Arguments
  • red - Red channel. [0, 255]
  • green - Green channel. [0, 255]
  • blue - Blue channel. [0, 255]
Examples found in repository?
examples/picking/debug_picking.rs (line 96)
86fn on_click_spawn_cube(
87    _click: On<Pointer<Click>>,
88    mut commands: Commands,
89    mut meshes: ResMut<Assets<Mesh>>,
90    mut materials: ResMut<Assets<StandardMaterial>>,
91    mut num: Local<usize>,
92) {
93    commands
94        .spawn((
95            Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
96            MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
97            Transform::from_xyz(0.0, 0.25 + 0.55 * *num as f32, 0.0),
98        ))
99        // With the MeshPickingPlugin added, you can add pointer event observers to meshes:
100        .observe(on_drag_rotate);
101    *num += 1;
102}
More examples
Hide additional examples
examples/picking/simple_picking.rs (line 72)
62fn on_click_spawn_cube(
63    _click: On<Pointer<Click>>,
64    mut commands: Commands,
65    mut meshes: ResMut<Assets<Mesh>>,
66    mut materials: ResMut<Assets<StandardMaterial>>,
67    mut num: Local<usize>,
68) {
69    commands
70        .spawn((
71            Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
72            MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
73            Transform::from_xyz(0.0, 0.25 + 0.55 * *num as f32, 0.0),
74        ))
75        // With the MeshPickingPlugin added, you can add pointer event observers to meshes:
76        .observe(on_drag_rotate);
77    *num += 1;
78}
examples/3d/color_grading.rs (line 328)
321fn add_camera(commands: &mut Commands, asset_server: &AssetServer, color_grading: ColorGrading) {
322    commands.spawn((
323        Camera3d::default(),
324        Hdr,
325        Transform::from_xyz(0.7, 0.7, 1.0).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
326        color_grading,
327        DistanceFog {
328            color: Color::srgb_u8(43, 44, 47),
329            falloff: FogFalloff::Linear {
330                start: 1.0,
331                end: 8.0,
332            },
333            ..default()
334        },
335        EnvironmentMapLight {
336            diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
337            specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
338            intensity: 2000.0,
339            ..default()
340        },
341    ));
342}
examples/3d/post_processing.rs (line 67)
61fn spawn_camera(commands: &mut Commands, asset_server: &AssetServer) {
62    commands.spawn((
63        Camera3d::default(),
64        Hdr,
65        Transform::from_xyz(0.7, 0.7, 1.0).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
66        DistanceFog {
67            color: Color::srgb_u8(43, 44, 47),
68            falloff: FogFalloff::Linear {
69                start: 1.0,
70                end: 8.0,
71            },
72            ..default()
73        },
74        EnvironmentMapLight {
75            diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
76            specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
77            intensity: 2000.0,
78            ..default()
79        },
80        // Include the `ChromaticAberration` component.
81        ChromaticAberration::default(),
82    ));
83}
examples/3d/motion_blur.rs (line 182)
174fn spawn_barriers(
175    meshes: &mut Assets<Mesh>,
176    materials: &mut Assets<StandardMaterial>,
177    commands: &mut Commands,
178) {
179    const N_CONES: usize = 100;
180    let capsule = meshes.add(Capsule3d::default());
181    let matl = materials.add(StandardMaterial {
182        base_color: Color::srgb_u8(255, 87, 51),
183        reflectance: 1.0,
184        ..default()
185    });
186    let mut spawn_with_offset = |offset: f32| {
187        for i in 0..N_CONES {
188            let pos = race_track_pos(
189                offset,
190                (i as f32) / (N_CONES as f32) * std::f32::consts::PI * 2.0,
191            );
192            commands.spawn((
193                Mesh3d(capsule.clone()),
194                MeshMaterial3d(matl.clone()),
195                Transform::from_xyz(pos.x, -0.65, pos.y).with_scale(Vec3::splat(0.07)),
196            ));
197        }
198    };
199    spawn_with_offset(0.04);
200    spawn_with_offset(-0.04);
201}
examples/3d/3d_scene.rs (line 27)
13fn setup(
14    mut commands: Commands,
15    mut meshes: ResMut<Assets<Mesh>>,
16    mut materials: ResMut<Assets<StandardMaterial>>,
17) {
18    // circular base
19    commands.spawn((
20        Mesh3d(meshes.add(Circle::new(4.0))),
21        MeshMaterial3d(materials.add(Color::WHITE)),
22        Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
23    ));
24    // cube
25    commands.spawn((
26        Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
27        MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
28        Transform::from_xyz(0.0, 0.5, 0.0),
29    ));
30    // light
31    commands.spawn((
32        PointLight {
33            shadows_enabled: true,
34            ..default()
35        },
36        Transform::from_xyz(4.0, 8.0, 4.0),
37    ));
38    // camera
39    commands.spawn((
40        Camera3d::default(),
41        Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y),
42    ));
43}
Source

pub const fn linear_rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Color

Creates a new Color object storing a LinearRgba color.

§Arguments
  • red - Red channel. [0.0, 1.0]
  • green - Green channel. [0.0, 1.0]
  • blue - Blue channel. [0.0, 1.0]
  • alpha - Alpha channel. [0.0, 1.0]
Source

pub const fn linear_rgb(red: f32, green: f32, blue: f32) -> Color

Creates a new Color object storing a LinearRgba color with an alpha of 1.0.

§Arguments
  • red - Red channel. [0.0, 1.0]
  • green - Green channel. [0.0, 1.0]
  • blue - Blue channel. [0.0, 1.0]
Examples found in repository?
examples/stress_tests/many_cubes.rs (line 499)
493fn update_materials(mut materials: ResMut<Assets<StandardMaterial>>, time: Res<Time>) {
494    let elapsed = time.elapsed_secs();
495    for (i, (_, material)) in materials.iter_mut().enumerate() {
496        let hue = (elapsed + i as f32 * 0.005).rem_euclid(1.0);
497        // This is much faster than using base_color.set_hue(hue), and in a tight loop it shows.
498        let color = fast_hue_to_rgb(hue);
499        material.base_color = Color::linear_rgb(color.x, color.y, color.z);
500    }
501}
More examples
Hide additional examples
examples/stress_tests/bevymark.rs (line 342)
319fn mouse_handler(
320    mut commands: Commands,
321    args: Res<Args>,
322    time: Res<Time>,
323    mouse_button_input: Res<ButtonInput<MouseButton>>,
324    window: Query<&Window>,
325    bird_resources: ResMut<BirdResources>,
326    mut counter: ResMut<BevyCounter>,
327    mut rng: Local<Option<ChaCha8Rng>>,
328    mut wave: Local<usize>,
329) {
330    let Ok(window) = window.single() else {
331        return;
332    };
333
334    if rng.is_none() {
335        // We're seeding the PRNG here to make this example deterministic for testing purposes.
336        // This isn't strictly required in practical use unless you need your app to be deterministic.
337        *rng = Some(ChaCha8Rng::seed_from_u64(42));
338    }
339    let rng = rng.as_mut().unwrap();
340
341    if mouse_button_input.just_released(MouseButton::Left) {
342        counter.color = Color::linear_rgb(rng.random(), rng.random(), rng.random());
343    }
344
345    if mouse_button_input.pressed(MouseButton::Left) {
346        let spawn_count = (BIRDS_PER_SECOND as f64 * time.delta_secs_f64()) as usize;
347        spawn_birds(
348            &mut commands,
349            args.into_inner(),
350            &window.resolution,
351            &mut counter,
352            spawn_count,
353            bird_resources.into_inner(),
354            None,
355            *wave,
356        );
357        *wave += 1;
358    }
359}
360
361fn bird_velocity_transform(
362    half_extents: Vec2,
363    mut translation: Vec3,
364    velocity_rng: &mut ChaCha8Rng,
365    waves: Option<usize>,
366    dt: f32,
367) -> (Transform, Vec3) {
368    let mut velocity = Vec3::new(MAX_VELOCITY * (velocity_rng.random::<f32>() - 0.5), 0., 0.);
369
370    if let Some(waves) = waves {
371        // Step the movement and handle collisions as if the wave had been spawned at fixed time intervals
372        // and with dt-spaced frames of simulation
373        for _ in 0..(waves * (FIXED_TIMESTEP / dt).round() as usize) {
374            step_movement(&mut translation, &mut velocity, dt);
375            handle_collision(half_extents, &translation, &mut velocity);
376        }
377    }
378    (
379        Transform::from_translation(translation).with_scale(Vec3::splat(BIRD_SCALE)),
380        velocity,
381    )
382}
383
384const FIXED_DELTA_TIME: f32 = 1.0 / 60.0;
385
386fn spawn_birds(
387    commands: &mut Commands,
388    args: &Args,
389    primary_window_resolution: &WindowResolution,
390    counter: &mut BevyCounter,
391    spawn_count: usize,
392    bird_resources: &mut BirdResources,
393    waves_to_simulate: Option<usize>,
394    wave: usize,
395) {
396    let bird_x = (primary_window_resolution.width() / -2.) + HALF_BIRD_SIZE;
397    let bird_y = (primary_window_resolution.height() / 2.) - HALF_BIRD_SIZE;
398
399    let half_extents = 0.5 * primary_window_resolution.size();
400
401    let color = counter.color;
402    let current_count = counter.count;
403
404    match args.mode {
405        Mode::Sprite => {
406            let batch = (0..spawn_count)
407                .map(|count| {
408                    let bird_z = if args.ordered_z {
409                        (current_count + count) as f32 * 0.00001
410                    } else {
411                        bird_resources.transform_rng.random::<f32>()
412                    };
413
414                    let (transform, velocity) = bird_velocity_transform(
415                        half_extents,
416                        Vec3::new(bird_x, bird_y, bird_z),
417                        &mut bird_resources.velocity_rng,
418                        waves_to_simulate,
419                        FIXED_DELTA_TIME,
420                    );
421
422                    let color = if args.vary_per_instance {
423                        Color::linear_rgb(
424                            bird_resources.color_rng.random(),
425                            bird_resources.color_rng.random(),
426                            bird_resources.color_rng.random(),
427                        )
428                    } else {
429                        color
430                    };
431                    (
432                        Sprite {
433                            image: bird_resources
434                                .textures
435                                .choose(&mut bird_resources.material_rng)
436                                .unwrap()
437                                .clone(),
438                            color,
439                            ..default()
440                        },
441                        transform,
442                        Bird { velocity },
443                    )
444                })
445                .collect::<Vec<_>>();
446            commands.spawn_batch(batch);
447        }
448        Mode::Mesh2d => {
449            let batch = (0..spawn_count)
450                .map(|count| {
451                    let bird_z = if args.ordered_z {
452                        (current_count + count) as f32 * 0.00001
453                    } else {
454                        bird_resources.transform_rng.random::<f32>()
455                    };
456
457                    let (transform, velocity) = bird_velocity_transform(
458                        half_extents,
459                        Vec3::new(bird_x, bird_y, bird_z),
460                        &mut bird_resources.velocity_rng,
461                        waves_to_simulate,
462                        FIXED_DELTA_TIME,
463                    );
464
465                    let material =
466                        if args.vary_per_instance || args.material_texture_count > args.waves {
467                            bird_resources
468                                .materials
469                                .choose(&mut bird_resources.material_rng)
470                                .unwrap()
471                                .clone()
472                        } else {
473                            bird_resources.materials[wave % bird_resources.materials.len()].clone()
474                        };
475                    (
476                        Mesh2d(bird_resources.quad.clone()),
477                        MeshMaterial2d(material),
478                        transform,
479                        Bird { velocity },
480                    )
481                })
482                .collect::<Vec<_>>();
483            commands.spawn_batch(batch);
484        }
485    }
486
487    counter.count += spawn_count;
488    counter.color = Color::linear_rgb(
489        bird_resources.color_rng.random(),
490        bird_resources.color_rng.random(),
491        bird_resources.color_rng.random(),
492    );
493}
examples/usage/context_menu.rs (line 101)
81fn on_trigger_menu(event: On<OpenContextMenu>, mut commands: Commands) {
82    commands.trigger(CloseContextMenus);
83
84    let pos = event.pos;
85
86    debug!("open context menu at: {pos}");
87
88    commands
89        .spawn((
90            Name::new("context menu"),
91            ContextMenu,
92            Node {
93                position_type: PositionType::Absolute,
94                left: px(pos.x),
95                top: px(pos.y),
96                flex_direction: FlexDirection::Column,
97                ..default()
98            },
99            BorderColor::all(Color::BLACK),
100            BorderRadius::all(px(4)),
101            BackgroundColor(Color::linear_rgb(0.1, 0.1, 0.1)),
102            children![
103                context_item("fuchsia", basic::FUCHSIA),
104                context_item("gray", basic::GRAY),
105                context_item("maroon", basic::MAROON),
106                context_item("purple", basic::PURPLE),
107                context_item("teal", basic::TEAL),
108            ],
109        ))
110        .observe(
111            |event: On<Pointer<Press>>,
112             menu_items: Query<&ContextMenuItem>,
113             mut clear_col: ResMut<ClearColor>,
114             mut commands: Commands| {
115                let target = event.original_event_target();
116
117                if let Ok(item) = menu_items.get(target) {
118                    clear_col.0 = item.0.into();
119                    commands.trigger(CloseContextMenus);
120                }
121            },
122        );
123}
examples/2d/cpu_draw.rs (lines 102-106)
92fn draw(
93    my_handle: Res<MyProcGenImage>,
94    mut images: ResMut<Assets<Image>>,
95    // Used to keep track of where we are
96    mut i: Local<u32>,
97    mut draw_color: Local<Color>,
98    mut seeded_rng: ResMut<SeededRng>,
99) {
100    if *i == 0 {
101        // Generate a random color on first run.
102        *draw_color = Color::linear_rgb(
103            seeded_rng.0.random(),
104            seeded_rng.0.random(),
105            seeded_rng.0.random(),
106        );
107    }
108
109    // Get the image from Bevy's asset storage.
110    let image = images.get_mut(&my_handle.0).expect("Image not found");
111
112    // Compute the position of the pixel to draw.
113
114    let center = Vec2::new(IMAGE_WIDTH as f32 / 2.0, IMAGE_HEIGHT as f32 / 2.0);
115    let max_radius = IMAGE_HEIGHT.min(IMAGE_WIDTH) as f32 / 2.0;
116    let rot_speed = 0.0123;
117    let period = 0.12345;
118
119    let r = ops::sin(*i as f32 * period) * max_radius;
120    let xy = Vec2::from_angle(*i as f32 * rot_speed) * r + center;
121    let (x, y) = (xy.x as u32, xy.y as u32);
122
123    // Get the old color of that pixel.
124    let old_color = image.get_color_at(x, y).unwrap();
125
126    // If the old color is our current color, change our drawing color.
127    let tolerance = 1.0 / 255.0;
128    if old_color.distance(&draw_color) <= tolerance {
129        *draw_color = Color::linear_rgb(
130            seeded_rng.0.random(),
131            seeded_rng.0.random(),
132            seeded_rng.0.random(),
133        );
134    }
135
136    // Set the new color, but keep old alpha value from image.
137    image
138        .set_color_at(x, y, draw_color.with_alpha(old_color.alpha()))
139        .unwrap();
140
141    *i += 1;
142}
examples/2d/2d_viewport_to_world.rs (line 168)
121fn setup(
122    mut commands: Commands,
123    mut meshes: ResMut<Assets<Mesh>>,
124    mut materials: ResMut<Assets<ColorMaterial>>,
125    window: Single<&Window>,
126) {
127    let window_size = window.resolution.physical_size().as_vec2();
128
129    // Initialize centered, non-window-filling viewport
130    commands.spawn((
131        Camera2d,
132        Camera {
133            viewport: Some(Viewport {
134                physical_position: (window_size * 0.125).as_uvec2(),
135                physical_size: (window_size * 0.75).as_uvec2(),
136                ..default()
137            }),
138            ..default()
139        },
140    ));
141
142    // Create a minimal UI explaining how to interact with the example
143    commands.spawn((
144        Text::new(
145            "Move the mouse to see the circle follow your cursor.\n\
146                    Use the arrow keys to move the camera.\n\
147                    Use the comma and period keys to zoom in and out.\n\
148                    Use the WASD keys to move the viewport.\n\
149                    Use the IJKL keys to resize the viewport.",
150        ),
151        Node {
152            position_type: PositionType::Absolute,
153            top: px(12),
154            left: px(12),
155            ..default()
156        },
157    ));
158
159    // Add mesh to make camera movement visible
160    commands.spawn((
161        Mesh2d(meshes.add(Rectangle::new(40.0, 20.0))),
162        MeshMaterial2d(materials.add(Color::from(GREEN))),
163    ));
164
165    // Add background to visualize viewport bounds
166    commands.spawn((
167        Mesh2d(meshes.add(Rectangle::new(50000.0, 50000.0))),
168        MeshMaterial2d(materials.add(Color::linear_rgb(0.01, 0.01, 0.01))),
169        Transform::from_translation(Vec3::new(0.0, 0.0, -200.0)),
170    ));
171}
examples/3d/motion_blur.rs (line 79)
53fn setup_scene(
54    asset_server: Res<AssetServer>,
55    mut images: ResMut<Assets<Image>>,
56    mut commands: Commands,
57    mut meshes: ResMut<Assets<Mesh>>,
58    mut materials: ResMut<Assets<StandardMaterial>>,
59) {
60    commands.insert_resource(AmbientLight {
61        color: Color::WHITE,
62        brightness: 300.0,
63        ..default()
64    });
65    commands.insert_resource(CameraMode::Chase);
66    commands.spawn((
67        DirectionalLight {
68            illuminance: 3_000.0,
69            shadows_enabled: true,
70            ..default()
71        },
72        Transform::default().looking_to(Vec3::new(-1.0, -0.7, -1.0), Vec3::X),
73    ));
74    // Sky
75    commands.spawn((
76        Mesh3d(meshes.add(Sphere::default())),
77        MeshMaterial3d(materials.add(StandardMaterial {
78            unlit: true,
79            base_color: Color::linear_rgb(0.1, 0.6, 1.0),
80            ..default()
81        })),
82        Transform::default().with_scale(Vec3::splat(-4000.0)),
83    ));
84    // Ground
85    let mut plane: Mesh = Plane3d::default().into();
86    let uv_size = 4000.0;
87    let uvs = vec![[uv_size, 0.0], [0.0, 0.0], [0.0, uv_size], [uv_size; 2]];
88    plane.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
89    commands.spawn((
90        Mesh3d(meshes.add(plane)),
91        MeshMaterial3d(materials.add(StandardMaterial {
92            base_color: Color::WHITE,
93            perceptual_roughness: 1.0,
94            base_color_texture: Some(images.add(uv_debug_texture())),
95            ..default()
96        })),
97        Transform::from_xyz(0.0, -0.65, 0.0).with_scale(Vec3::splat(80.)),
98    ));
99
100    spawn_cars(&asset_server, &mut meshes, &mut materials, &mut commands);
101    spawn_trees(&mut meshes, &mut materials, &mut commands);
102    spawn_barriers(&mut meshes, &mut materials, &mut commands);
103}
104
105fn spawn_cars(
106    asset_server: &AssetServer,
107    meshes: &mut Assets<Mesh>,
108    materials: &mut Assets<StandardMaterial>,
109    commands: &mut Commands,
110) {
111    const N_CARS: usize = 20;
112    let box_mesh = meshes.add(Cuboid::new(0.3, 0.15, 0.55));
113    let cylinder = meshes.add(Cylinder::default());
114    let logo = asset_server.load("branding/icon.png");
115    let wheel_matl = materials.add(StandardMaterial {
116        base_color: Color::WHITE,
117        base_color_texture: Some(logo.clone()),
118        ..default()
119    });
120
121    let mut matl = |color| {
122        materials.add(StandardMaterial {
123            base_color: color,
124            ..default()
125        })
126    };
127
128    let colors = [
129        matl(Color::linear_rgb(1.0, 0.0, 0.0)),
130        matl(Color::linear_rgb(1.0, 1.0, 0.0)),
131        matl(Color::BLACK),
132        matl(Color::linear_rgb(0.0, 0.0, 1.0)),
133        matl(Color::linear_rgb(0.0, 1.0, 0.0)),
134        matl(Color::linear_rgb(1.0, 0.0, 1.0)),
135        matl(Color::linear_rgb(0.5, 0.5, 0.0)),
136        matl(Color::linear_rgb(1.0, 0.5, 0.0)),
137    ];
138
139    let make_wheel = |x: f32, z: f32| {
140        (
141            Mesh3d(cylinder.clone()),
142            MeshMaterial3d(wheel_matl.clone()),
143            Transform::from_xyz(0.14 * x, -0.045, 0.15 * z)
144                .with_scale(Vec3::new(0.15, 0.04, 0.15))
145                .with_rotation(Quat::from_rotation_z(std::f32::consts::FRAC_PI_2)),
146            Rotates,
147        )
148    };
149
150    for i in 0..N_CARS {
151        let color = colors[i % colors.len()].clone();
152        commands
153            .spawn((
154                Mesh3d(box_mesh.clone()),
155                MeshMaterial3d(color.clone()),
156                Transform::from_scale(Vec3::splat(0.5)),
157                Moves(i as f32 * 2.0),
158                children![
159                    (
160                        Mesh3d(box_mesh.clone()),
161                        MeshMaterial3d(color),
162                        Transform::from_xyz(0.0, 0.08, 0.03).with_scale(Vec3::new(1.0, 1.0, 0.5)),
163                    ),
164                    make_wheel(1.0, 1.0),
165                    make_wheel(1.0, -1.0),
166                    make_wheel(-1.0, 1.0),
167                    make_wheel(-1.0, -1.0)
168                ],
169            ))
170            .insert_if(CameraTracked, || i == 0);
171    }
172}
173
174fn spawn_barriers(
175    meshes: &mut Assets<Mesh>,
176    materials: &mut Assets<StandardMaterial>,
177    commands: &mut Commands,
178) {
179    const N_CONES: usize = 100;
180    let capsule = meshes.add(Capsule3d::default());
181    let matl = materials.add(StandardMaterial {
182        base_color: Color::srgb_u8(255, 87, 51),
183        reflectance: 1.0,
184        ..default()
185    });
186    let mut spawn_with_offset = |offset: f32| {
187        for i in 0..N_CONES {
188            let pos = race_track_pos(
189                offset,
190                (i as f32) / (N_CONES as f32) * std::f32::consts::PI * 2.0,
191            );
192            commands.spawn((
193                Mesh3d(capsule.clone()),
194                MeshMaterial3d(matl.clone()),
195                Transform::from_xyz(pos.x, -0.65, pos.y).with_scale(Vec3::splat(0.07)),
196            ));
197        }
198    };
199    spawn_with_offset(0.04);
200    spawn_with_offset(-0.04);
201}
202
203fn spawn_trees(
204    meshes: &mut Assets<Mesh>,
205    materials: &mut Assets<StandardMaterial>,
206    commands: &mut Commands,
207) {
208    const N_TREES: usize = 30;
209    let capsule = meshes.add(Capsule3d::default());
210    let sphere = meshes.add(Sphere::default());
211    let leaves = materials.add(Color::linear_rgb(0.0, 1.0, 0.0));
212    let trunk = materials.add(Color::linear_rgb(0.4, 0.2, 0.2));
213
214    let mut spawn_with_offset = |offset: f32| {
215        for i in 0..N_TREES {
216            let pos = race_track_pos(
217                offset,
218                (i as f32) / (N_TREES as f32) * std::f32::consts::PI * 2.0,
219            );
220            let [x, z] = pos.into();
221            commands.spawn((
222                Mesh3d(sphere.clone()),
223                MeshMaterial3d(leaves.clone()),
224                Transform::from_xyz(x, -0.3, z).with_scale(Vec3::splat(0.3)),
225            ));
226            commands.spawn((
227                Mesh3d(capsule.clone()),
228                MeshMaterial3d(trunk.clone()),
229                Transform::from_xyz(x, -0.5, z).with_scale(Vec3::new(0.05, 0.3, 0.05)),
230            ));
231        }
232    };
233    spawn_with_offset(0.07);
234    spawn_with_offset(-0.07);
235}
Source

pub const fn hsla( hue: f32, saturation: f32, lightness: f32, alpha: f32, ) -> Color

Creates a new Color object storing a Hsla color.

§Arguments
  • hue - Hue channel. [0.0, 360.0]
  • saturation - Saturation channel. [0.0, 1.0]
  • lightness - Lightness channel. [0.0, 1.0]
  • alpha - Alpha channel. [0.0, 1.0]
Examples found in repository?
examples/shader_advanced/custom_shader_instancing.rs (line 59)
50fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>) {
51    commands.spawn((
52        Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
53        InstanceMaterialData(
54            (1..=10)
55                .flat_map(|x| (1..=10).map(move |y| (x as f32 / 10.0, y as f32 / 10.0)))
56                .map(|(x, y)| InstanceData {
57                    position: Vec3::new(x * 10.0 - 5.0, y * 10.0 - 5.0, 0.0),
58                    scale: 1.0,
59                    color: LinearRgba::from(Color::hsla(x * 360., y, 0.5, 1.0)).to_f32_array(),
60                })
61                .collect(),
62        ),
63        // NOTE: Frustum culling is done based on the Aabb of the Mesh and the GlobalTransform.
64        // As the cube is at the origin, if its Aabb moves outside the view frustum, all the
65        // instanced cubes will be culled.
66        // The InstanceMaterialData contains the 'GlobalTransform' information for this custom
67        // instancing, and that is not taken into account with the built-in frustum culling.
68        // We must disable the built-in frustum culling by adding the `NoFrustumCulling` marker
69        // component to avoid incorrect culling.
70        NoFrustumCulling,
71    ));
72
73    // camera
74    commands.spawn((
75        Camera3d::default(),
76        Transform::from_xyz(0.0, 0.0, 15.0).looking_at(Vec3::ZERO, Vec3::Y),
77        // We need this component because we use `draw_indexed` and `draw`
78        // instead of `draw_indirect_indexed` and `draw_indirect` in
79        // `DrawMeshInstanced::render`.
80        NoIndirectDrawing,
81    ));
82}
More examples
Hide additional examples
examples/testbed/2d.rs (line 181)
147    pub fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
148        commands.spawn((Camera2d, DespawnOnExit(super::Scene::Text)));
149
150        for (i, justify) in [
151            Justify::Left,
152            Justify::Right,
153            Justify::Center,
154            Justify::Justified,
155        ]
156        .into_iter()
157        .enumerate()
158        {
159            let y = 230. - 150. * i as f32;
160            spawn_anchored_text(&mut commands, -300. * Vec3::X + y * Vec3::Y, justify, None);
161            spawn_anchored_text(
162                &mut commands,
163                300. * Vec3::X + y * Vec3::Y,
164                justify,
165                Some(TextBounds::new(150., 60.)),
166            );
167        }
168
169        let sans_serif = TextFont::from(asset_server.load("fonts/FiraSans-Bold.ttf"));
170
171        const NUM_ITERATIONS: usize = 10;
172        for i in 0..NUM_ITERATIONS {
173            let fraction = i as f32 / (NUM_ITERATIONS - 1) as f32;
174
175            commands.spawn((
176                Text2d::new("Bevy"),
177                sans_serif.clone(),
178                Transform::from_xyz(0.0, fraction * 200.0, i as f32)
179                    .with_scale(1.0 + Vec2::splat(fraction).extend(1.))
180                    .with_rotation(Quat::from_rotation_z(fraction * core::f32::consts::PI)),
181                TextColor(Color::hsla(fraction * 360.0, 0.8, 0.8, 0.8)),
182                DespawnOnExit(super::Scene::Text),
183            ));
184        }
185
186        commands.spawn((
187            Text2d::new("This text is invisible."),
188            Visibility::Hidden,
189            DespawnOnExit(super::Scene::Text),
190        ));
191    }
Source

pub const fn hsl(hue: f32, saturation: f32, lightness: f32) -> Color

Creates a new Color object storing a Hsla color with an alpha of 1.0.

§Arguments
  • hue - Hue channel. [0.0, 360.0]
  • saturation - Saturation channel. [0.0, 1.0]
  • lightness - Lightness channel. [0.0, 1.0]
Examples found in repository?
examples/ecs/observers.rs (line 161)
156fn draw_shapes(mut gizmos: Gizmos, mines: Query<&Mine>) {
157    for mine in &mines {
158        gizmos.circle_2d(
159            mine.pos,
160            mine.size,
161            Color::hsl((mine.size - 4.0) / 16.0 * 360.0, 1.0, 0.8),
162        );
163    }
164}
More examples
Hide additional examples
examples/stress_tests/many_materials.rs (lines 99-103)
92fn 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}
examples/ui/ui_material.rs (line 99)
90fn animate(
91    mut materials: ResMut<Assets<CustomUiMaterial>>,
92    q: Query<&MaterialNode<CustomUiMaterial>>,
93    time: Res<Time>,
94) {
95    let duration = 2.0;
96    for handle in &q {
97        if let Some(material) = materials.get_mut(handle) {
98            // rainbow color effect
99            let new_color = Color::hsl((time.elapsed_secs() * 60.0) % 360.0, 1., 0.5);
100            let border_color = Color::hsl((time.elapsed_secs() * 60.0) % 360.0, 0.75, 0.75);
101            material.color = new_color.to_linear().to_vec4();
102            material.slider.x =
103                ((time.elapsed_secs() % (duration * 2.0)) - duration).abs() / duration;
104            material.border_color = border_color.to_linear().to_vec4();
105        }
106    }
107}
examples/ecs/entity_disabling.rs (line 123)
104fn setup_scene(
105    mut commands: Commands,
106    mut meshes: ResMut<Assets<Mesh>>,
107    mut materials: ResMut<Assets<ColorMaterial>>,
108) {
109    commands.spawn(Camera2d);
110
111    let named_shapes = [
112        (Name::new("Annulus"), meshes.add(Annulus::new(25.0, 50.0))),
113        (
114            Name::new("Bestagon"),
115            meshes.add(RegularPolygon::new(50.0, 6)),
116        ),
117        (Name::new("Rhombus"), meshes.add(Rhombus::new(75.0, 100.0))),
118    ];
119    let num_shapes = named_shapes.len();
120
121    for (i, (name, shape)) in named_shapes.into_iter().enumerate() {
122        // Distribute colors evenly across the rainbow.
123        let color = Color::hsl(360. * i as f32 / num_shapes as f32, 0.95, 0.7);
124
125        commands.spawn((
126            name,
127            DisableOnClick,
128            Mesh2d(shape),
129            MeshMaterial2d(materials.add(color)),
130            Transform::from_xyz(
131                // Distribute shapes from -X_EXTENT/2 to +X_EXTENT/2.
132                -X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
133                0.0,
134                0.0,
135            ),
136        ));
137    }
138}
examples/stress_tests/many_gradients.rs (line 157)
141fn animate_gradients(
142    mut gradients: Query<(&mut BackgroundGradient, &GradientNode)>,
143    args: Res<Args>,
144    time: Res<Time>,
145) {
146    if !args.animate {
147        return;
148    }
149
150    let t = time.elapsed_secs();
151
152    for (mut bg_gradient, node) in &mut gradients {
153        let offset = node.index as f32 * 0.01;
154        let hue_shift = sin(t + offset) * 0.5 + 0.5;
155
156        if let Some(Gradient::Linear(gradient)) = bg_gradient.0.get_mut(0) {
157            let color1 = Color::hsl(hue_shift * 360.0, 1.0, 0.5);
158            let color2 = Color::hsl((hue_shift + 0.3) * 360.0 % 360.0, 1.0, 0.5);
159
160            gradient.stops = vec![
161                ColorStop::new(color1, percent(0)),
162                ColorStop::new(color2, percent(100)),
163                ColorStop::new(
164                    Color::hsl((hue_shift + 0.1) * 360.0 % 360.0, 1.0, 0.5),
165                    percent(20),
166                ),
167                ColorStop::new(
168                    Color::hsl((hue_shift + 0.15) * 360.0 % 360.0, 1.0, 0.5),
169                    percent(40),
170                ),
171                ColorStop::new(
172                    Color::hsl((hue_shift + 0.2) * 360.0 % 360.0, 1.0, 0.5),
173                    percent(60),
174                ),
175                ColorStop::new(
176                    Color::hsl((hue_shift + 0.25) * 360.0 % 360.0, 1.0, 0.5),
177                    percent(80),
178                ),
179                ColorStop::new(
180                    Color::hsl((hue_shift + 0.28) * 360.0 % 360.0, 1.0, 0.5),
181                    percent(90),
182                ),
183            ];
184        }
185    }
186}
examples/testbed/2d.rs (line 93)
66    pub fn setup(
67        mut commands: Commands,
68        mut meshes: ResMut<Assets<Mesh>>,
69        mut materials: ResMut<Assets<ColorMaterial>>,
70    ) {
71        commands.spawn((Camera2d, DespawnOnExit(super::Scene::Shapes)));
72
73        let shapes = [
74            meshes.add(Circle::new(50.0)),
75            meshes.add(CircularSector::new(50.0, 1.0)),
76            meshes.add(CircularSegment::new(50.0, 1.25)),
77            meshes.add(Ellipse::new(25.0, 50.0)),
78            meshes.add(Annulus::new(25.0, 50.0)),
79            meshes.add(Capsule2d::new(25.0, 50.0)),
80            meshes.add(Rhombus::new(75.0, 100.0)),
81            meshes.add(Rectangle::new(50.0, 100.0)),
82            meshes.add(RegularPolygon::new(50.0, 6)),
83            meshes.add(Triangle2d::new(
84                Vec2::Y * 50.0,
85                Vec2::new(-50.0, -50.0),
86                Vec2::new(50.0, -50.0),
87            )),
88        ];
89        let num_shapes = shapes.len();
90
91        for (i, shape) in shapes.into_iter().enumerate() {
92            // Distribute colors evenly across the rainbow.
93            let color = Color::hsl(360. * i as f32 / num_shapes as f32, 0.95, 0.7);
94
95            commands.spawn((
96                Mesh2d(shape),
97                MeshMaterial2d(materials.add(color)),
98                Transform::from_xyz(
99                    // Distribute shapes from -X_EXTENT/2 to +X_EXTENT/2.
100                    -X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
101                    0.0,
102                    0.0,
103                ),
104                DespawnOnExit(super::Scene::Shapes),
105            ));
106        }
107    }
Source

pub const fn hsva(hue: f32, saturation: f32, value: f32, alpha: f32) -> Color

Creates a new Color object storing a Hsva color.

§Arguments
  • hue - Hue channel. [0.0, 360.0]
  • saturation - Saturation channel. [0.0, 1.0]
  • value - Value channel. [0.0, 1.0]
  • alpha - Alpha channel. [0.0, 1.0]
Examples found in repository?
examples/3d/specular_tint.rs (line 111)
75fn setup(
76    mut commands: Commands,
77    asset_server: Res<AssetServer>,
78    app_status: Res<AppStatus>,
79    mut meshes: ResMut<Assets<Mesh>>,
80    mut standard_materials: ResMut<Assets<StandardMaterial>>,
81) {
82    // Spawns a camera.
83    commands.spawn((
84        Transform::from_xyz(-2.0, 0.0, 3.5).looking_at(Vec3::ZERO, Vec3::Y),
85        Hdr,
86        Camera3d::default(),
87        Skybox {
88            image: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
89            brightness: 3000.0,
90            ..default()
91        },
92        EnvironmentMapLight {
93            diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
94            specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
95            // We want relatively high intensity here in order for the specular
96            // tint to show up well.
97            intensity: 25000.0,
98            ..default()
99        },
100    ));
101
102    // Spawn the sphere.
103    commands.spawn((
104        Transform::from_rotation(Quat::from_rotation_x(PI * 0.5)),
105        Mesh3d(meshes.add(Sphere::default().mesh().uv(32, 18))),
106        MeshMaterial3d(standard_materials.add(StandardMaterial {
107            // We want only reflected specular light here, so we set the base
108            // color as black.
109            base_color: Color::BLACK,
110            reflectance: 1.0,
111            specular_tint: Color::hsva(app_status.hue, 1.0, 1.0, 1.0),
112            // The object must not be metallic, or else the reflectance is
113            // ignored per the Filament spec:
114            //
115            // <https://google.github.io/filament/Filament.html#listing_fnormal>
116            metallic: 0.0,
117            perceptual_roughness: 0.0,
118            ..default()
119        })),
120    ));
121
122    // Spawn the help text.
123    commands.spawn((
124        Node {
125            position_type: PositionType::Absolute,
126            bottom: px(12),
127            left: px(12),
128            ..default()
129        },
130        app_status.create_text(),
131    ));
132}
133
134/// Rotates the camera a bit every frame.
135fn rotate_camera(mut cameras: Query<&mut Transform, With<Camera3d>>) {
136    for mut camera_transform in cameras.iter_mut() {
137        camera_transform.translation =
138            Quat::from_rotation_y(ROTATION_SPEED) * camera_transform.translation;
139        camera_transform.look_at(Vec3::ZERO, Vec3::Y);
140    }
141}
142
143/// Alters the hue of the solid color a bit every frame.
144fn shift_hue(
145    mut app_status: ResMut<AppStatus>,
146    objects_with_materials: Query<&MeshMaterial3d<StandardMaterial>>,
147    mut standard_materials: ResMut<Assets<StandardMaterial>>,
148) {
149    if app_status.tint_type != TintType::Solid {
150        return;
151    }
152
153    app_status.hue += HUE_SHIFT_SPEED;
154
155    for material_handle in objects_with_materials.iter() {
156        let Some(material) = standard_materials.get_mut(material_handle) else {
157            continue;
158        };
159        material.specular_tint = Color::hsva(app_status.hue, 1.0, 1.0, 1.0);
160    }
161}
Source

pub const fn hsv(hue: f32, saturation: f32, value: f32) -> Color

Creates a new Color object storing a Hsva color with an alpha of 1.0.

§Arguments
  • hue - Hue channel. [0.0, 360.0]
  • saturation - Saturation channel. [0.0, 1.0]
  • value - Value channel. [0.0, 1.0]
Examples found in repository?
examples/stress_tests/many_cameras_lights.rs (line 59)
34fn setup(
35    mut commands: Commands,
36    mut meshes: ResMut<Assets<Mesh>>,
37    mut materials: ResMut<Assets<StandardMaterial>>,
38    window: Query<&Window>,
39) -> Result {
40    // circular base
41    commands.spawn((
42        Mesh3d(meshes.add(Circle::new(4.0))),
43        MeshMaterial3d(materials.add(Color::WHITE)),
44        Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
45    ));
46
47    // cube
48    commands.spawn((
49        Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
50        MeshMaterial3d(materials.add(Color::WHITE)),
51        Transform::from_xyz(0.0, 0.5, 0.0),
52    ));
53
54    // lights
55    for i in 0..NUM_LIGHTS {
56        let angle = (i as f32) / (NUM_LIGHTS as f32) * PI * 2.0;
57        commands.spawn((
58            PointLight {
59                color: Color::hsv(angle.to_degrees(), 1.0, 1.0),
60                intensity: 2_000_000.0 / NUM_LIGHTS as f32,
61                shadows_enabled: true,
62                ..default()
63            },
64            Transform::from_xyz(sin(angle) * 4.0, 2.0, cos(angle) * 4.0),
65        ));
66    }
67
68    // cameras
69    let window = window.single()?;
70    let width = window.resolution.width() / CAMERA_COLS as f32 * window.resolution.scale_factor();
71    let height = window.resolution.height() / CAMERA_ROWS as f32 * window.resolution.scale_factor();
72    let mut i = 0;
73    for y in 0..CAMERA_COLS {
74        for x in 0..CAMERA_ROWS {
75            let angle = i as f32 / (CAMERA_ROWS * CAMERA_COLS) as f32 * PI * 2.0;
76            commands.spawn((
77                Camera3d::default(),
78                Camera {
79                    viewport: Some(Viewport {
80                        physical_position: UVec2::new(
81                            (x as f32 * width) as u32,
82                            (y as f32 * height) as u32,
83                        ),
84                        physical_size: UVec2::new(width as u32, height as u32),
85                        ..default()
86                    }),
87                    order: i,
88                    ..default()
89                },
90                Transform::from_xyz(sin(angle) * 4.0, 2.5, cos(angle) * 4.0)
91                    .looking_at(Vec3::ZERO, Vec3::Y),
92            ));
93            i += 1;
94        }
95    }
96    Ok(())
97}
Source

pub const fn hwba(hue: f32, whiteness: f32, blackness: f32, alpha: f32) -> Color

Creates a new Color object storing a Hwba color.

§Arguments
  • hue - Hue channel. [0.0, 360.0]
  • whiteness - Whiteness channel. [0.0, 1.0]
  • blackness - Blackness channel. [0.0, 1.0]
  • alpha - Alpha channel. [0.0, 1.0]
Source

pub const fn hwb(hue: f32, whiteness: f32, blackness: f32) -> Color

Creates a new Color object storing a Hwba color with an alpha of 1.0.

§Arguments
  • hue - Hue channel. [0.0, 360.0]
  • whiteness - Whiteness channel. [0.0, 1.0]
  • blackness - Blackness channel. [0.0, 1.0]
Source

pub const fn laba(lightness: f32, a: f32, b: f32, alpha: f32) -> Color

Creates a new Color object storing a Laba color.

§Arguments
  • lightness - Lightness channel. [0.0, 1.5]
  • a - a axis. [-1.5, 1.5]
  • b - b axis. [-1.5, 1.5]
  • alpha - Alpha channel. [0.0, 1.0]
Source

pub const fn lab(lightness: f32, a: f32, b: f32) -> Color

Creates a new Color object storing a Laba color with an alpha of 1.0.

§Arguments
  • lightness - Lightness channel. [0.0, 1.5]
  • a - a axis. [-1.5, 1.5]
  • b - b axis. [-1.5, 1.5]
Source

pub const fn lcha(lightness: f32, chroma: f32, hue: f32, alpha: f32) -> Color

Creates a new Color object storing a Lcha color.

§Arguments
  • lightness - Lightness channel. [0.0, 1.5]
  • chroma - Chroma channel. [0.0, 1.5]
  • hue - Hue channel. [0.0, 360.0]
  • alpha - Alpha channel. [0.0, 1.0]
Source

pub const fn lch(lightness: f32, chroma: f32, hue: f32) -> Color

Creates a new Color object storing a Lcha color with an alpha of 1.0.

§Arguments
  • lightness - Lightness channel. [0.0, 1.5]
  • chroma - Chroma channel. [0.0, 1.5]
  • hue - Hue channel. [0.0, 360.0]
Source

pub const fn oklaba(lightness: f32, a: f32, b: f32, alpha: f32) -> Color

Creates a new Color object storing a Oklaba color.

§Arguments
  • lightness - Lightness channel. [0.0, 1.0]
  • a - Green-red channel. [-1.0, 1.0]
  • b - Blue-yellow channel. [-1.0, 1.0]
  • alpha - Alpha channel. [0.0, 1.0]
Source

pub const fn oklab(lightness: f32, a: f32, b: f32) -> Color

Creates a new Color object storing a Oklaba color with an alpha of 1.0.

§Arguments
  • lightness - Lightness channel. [0.0, 1.0]
  • a - Green-red channel. [-1.0, 1.0]
  • b - Blue-yellow channel. [-1.0, 1.0]
Source

pub const fn oklcha(lightness: f32, chroma: f32, hue: f32, alpha: f32) -> Color

Creates a new Color object storing a Oklcha color.

§Arguments
  • lightness - Lightness channel. [0.0, 1.0]
  • chroma - Chroma channel. [0.0, 1.0]
  • hue - Hue channel. [0.0, 360.0]
  • alpha - Alpha channel. [0.0, 1.0]
Source

pub const fn oklch(lightness: f32, chroma: f32, hue: f32) -> Color

Creates a new Color object storing a Oklcha color with an alpha of 1.0.

§Arguments
  • lightness - Lightness channel. [0.0, 1.0]
  • chroma - Chroma channel. [0.0, 1.0]
  • hue - Hue channel. [0.0, 360.0]
Source

pub const fn xyza(x: f32, y: f32, z: f32, alpha: f32) -> Color

Creates a new Color object storing a Xyza color.

§Arguments
  • x - x-axis. [0.0, 1.0]
  • y - y-axis. [0.0, 1.0]
  • z - z-axis. [0.0, 1.0]
  • alpha - Alpha channel. [0.0, 1.0]
Source

pub const fn xyz(x: f32, y: f32, z: f32) -> Color

Creates a new Color object storing a Xyza color with an alpha of 1.0.

§Arguments
  • x - x-axis. [0.0, 1.0]
  • y - y-axis. [0.0, 1.0]
  • z - z-axis. [0.0, 1.0]

Trait Implementations§

Source§

impl Alpha for Color

Source§

fn with_alpha(&self, alpha: f32) -> Color

Return a new version of this color with the given alpha value.
Source§

fn alpha(&self) -> f32

Return the alpha component of this color.
Source§

fn set_alpha(&mut self, alpha: f32)

Sets the alpha component of this color.
Source§

fn is_fully_transparent(&self) -> bool

Is the alpha component of this color less than or equal to 0.0?
Source§

fn is_fully_opaque(&self) -> bool

Is the alpha component of this color greater than or equal to 1.0?
Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Color

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Color

Source§

fn default() -> Color

A fully white Color::LinearRgba color with an alpha of 1.0.

Source§

impl<'de> Deserialize<'de> for Color

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Color, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Enum for Color

Source§

fn field(&self, __name_param: &str) -> Option<&(dyn PartialReflect + 'static)>

Returns a reference to the value of the field (in the current variant) with the given name. Read more
Source§

fn field_at( &self, __index_param: usize, ) -> Option<&(dyn PartialReflect + 'static)>

Returns a reference to the value of the field (in the current variant) at the given index.
Source§

fn field_mut( &mut self, __name_param: &str, ) -> Option<&mut (dyn PartialReflect + 'static)>

Returns a mutable reference to the value of the field (in the current variant) with the given name. Read more
Source§

fn field_at_mut( &mut self, __index_param: usize, ) -> Option<&mut (dyn PartialReflect + 'static)>

Returns a mutable reference to the value of the field (in the current variant) at the given index.
Source§

fn index_of(&self, __name_param: &str) -> Option<usize>

Returns the index of the field (in the current variant) with the given name. Read more
Source§

fn name_at(&self, __index_param: usize) -> Option<&str>

Returns the name of the field (in the current variant) with the given index. Read more
Source§

fn iter_fields(&self) -> VariantFieldIter<'_>

Returns an iterator over the values of the current variant’s fields.
Source§

fn field_len(&self) -> usize

Returns the number of fields in the current variant.
Source§

fn variant_name(&self) -> &str

The name of the current variant.
Source§

fn variant_index(&self) -> usize

The index of the current variant.
Source§

fn variant_type(&self) -> VariantType

The type of the current variant.
Source§

fn to_dynamic_enum(&self) -> DynamicEnum

Creates a new DynamicEnum from this enum.
Source§

fn is_variant(&self, variant_type: VariantType) -> bool

Returns true if the current variant’s type matches the given one.
Source§

fn variant_path(&self) -> String

Returns the full path to the current variant.
Source§

fn get_represented_enum_info(&self) -> Option<&'static EnumInfo>

Will return None if TypeInfo is not available.
Source§

impl EuclideanDistance for Color

Source§

fn distance_squared(&self, other: &Color) -> f32

Distance squared from self to other.
Source§

fn distance(&self, other: &Self) -> f32

Distance from self to other.
Source§

impl From<Color> for AngularColorStop

Source§

fn from(color: Color) -> AngularColorStop

Converts to this type from the input type.
Source§

impl From<Color> for ClearColorConfig

Source§

fn from(value: Color) -> ClearColorConfig

Converts to this type from the input type.
Source§

impl From<Color> for ColorMaterial

Source§

fn from(color: Color) -> ColorMaterial

Converts to this type from the input type.
Source§

impl From<Color> for ColorStop

Source§

fn from(color: Color) -> ColorStop

Converts to this type from the input type.
Source§

impl From<Color> for Hsla

Source§

fn from(value: Color) -> Hsla

Converts to this type from the input type.
Source§

impl From<Color> for Hsva

Source§

fn from(value: Color) -> Hsva

Converts to this type from the input type.
Source§

impl From<Color> for Hwba

Source§

fn from(value: Color) -> Hwba

Converts to this type from the input type.
Source§

impl From<Color> for Laba

Source§

fn from(value: Color) -> Laba

Converts to this type from the input type.
Source§

impl From<Color> for Lcha

Source§

fn from(value: Color) -> Lcha

Converts to this type from the input type.
Source§

impl From<Color> for LinearRgba

Source§

fn from(value: Color) -> LinearRgba

Converts to this type from the input type.
Source§

impl From<Color> for Oklaba

Source§

fn from(value: Color) -> Oklaba

Converts to this type from the input type.
Source§

impl From<Color> for Oklcha

Source§

fn from(value: Color) -> Oklcha

Converts to this type from the input type.
Source§

impl From<Color> for Srgba

Source§

fn from(value: Color) -> Srgba

Converts to this type from the input type.
Source§

impl From<Color> for StandardMaterial

Source§

fn from(color: Color) -> StandardMaterial

Converts to this type from the input type.
Source§

impl From<Color> for Xyza

Source§

fn from(value: Color) -> Xyza

Converts to this type from the input type.
Source§

impl From<Hsla> for Color

Source§

fn from(value: Hsla) -> Color

Converts to this type from the input type.
Source§

impl From<Hsva> for Color

Source§

fn from(value: Hsva) -> Color

Converts to this type from the input type.
Source§

impl From<Hwba> for Color

Source§

fn from(value: Hwba) -> Color

Converts to this type from the input type.
Source§

impl From<Laba> for Color

Source§

fn from(value: Laba) -> Color

Converts to this type from the input type.
Source§

impl From<Lcha> for Color

Source§

fn from(value: Lcha) -> Color

Converts to this type from the input type.
Source§

impl From<LinearRgba> for Color

Source§

fn from(value: LinearRgba) -> Color

Converts to this type from the input type.
Source§

impl From<Oklaba> for Color

Source§

fn from(value: Oklaba) -> Color

Converts to this type from the input type.
Source§

impl From<Oklcha> for Color

Source§

fn from(value: Oklcha) -> Color

Converts to this type from the input type.
Source§

impl From<Srgba> for Color

Source§

fn from(value: Srgba) -> Color

Converts to this type from the input type.
Source§

impl From<Xyza> for Color

Source§

fn from(value: Xyza) -> Color

Converts to this type from the input type.
Source§

impl FromArg for Color

Source§

type This<'from_arg> = Color

The type to convert into. Read more
Source§

fn from_arg(arg: Arg<'_>) -> Result<<Color as FromArg>::This<'_>, ArgError>

Creates an item from an argument. Read more
Source§

impl FromReflect for Color

Source§

fn from_reflect(__param0: &(dyn PartialReflect + 'static)) -> Option<Color>

Constructs a concrete instance of Self from a reflected value.
Source§

fn take_from_reflect( reflect: Box<dyn PartialReflect>, ) -> Result<Self, Box<dyn PartialReflect>>

Attempts to downcast the given value to Self using, constructing the value using from_reflect if that fails. Read more
Source§

impl GetOwnership for Color

Source§

fn ownership() -> Ownership

Returns the ownership of Self.
Source§

impl GetTypeRegistration for Color

Source§

fn get_type_registration() -> TypeRegistration

Returns the default TypeRegistration for this type.
Source§

fn register_type_dependencies(registry: &mut TypeRegistry)

Registers other types needed by this type. Read more
Source§

impl Hue for Color

Source§

fn with_hue(&self, hue: f32) -> Color

Return a new version of this color with the hue channel set to the given value.
Source§

fn hue(&self) -> f32

Return the hue of this color [0.0, 360.0].
Source§

fn set_hue(&mut self, hue: f32)

Sets the hue of this color.
Source§

fn rotate_hue(&self, degrees: f32) -> Self

Return a new version of this color with the hue channel rotated by the given degrees.
Source§

impl IntoReturn for Color

Source§

fn into_return<'into_return>(self) -> Return<'into_return>
where Color: 'into_return,

Converts Self into a Return value.
Source§

impl Luminance for Color

Source§

fn luminance(&self) -> f32

Return the luminance of this color (0.0 - 1.0).
Source§

fn with_luminance(&self, value: f32) -> Color

Return a new version of this color with the given luminance. The resulting color will be clamped to the valid range for the color space; for some color spaces, clamping may cause the hue or chroma to change.
Source§

fn darker(&self, amount: f32) -> Color

Return a darker version of this color. The amount should be between 0.0 and 1.0. The amount represents an absolute decrease in luminance, and is distributive: color.darker(a).darker(b) == color.darker(a + b). Colors are clamped to black if the amount would cause them to go below black. Read more
Source§

fn lighter(&self, amount: f32) -> Color

Return a lighter version of this color. The amount should be between 0.0 and 1.0. The amount represents an absolute increase in luminance, and is distributive: color.lighter(a).lighter(b) == color.lighter(a + b). Colors are clamped to white if the amount would cause them to go above white. Read more
Source§

impl Mix for Color

Source§

fn mix(&self, other: &Color, factor: f32) -> Color

Linearly interpolate between this and another color, by factor. Factor should be between 0.0 and 1.0.
Source§

fn mix_assign(&mut self, other: Self, factor: f32)

Linearly interpolate between this and another color, by factor, storing the result in this color. Factor should be between 0.0 and 1.0.
Source§

impl PartialEq for Color

Source§

fn eq(&self, other: &Color) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialReflect for Color

Source§

fn get_represented_type_info(&self) -> Option<&'static TypeInfo>

Returns the TypeInfo of the type represented by this value. Read more
Source§

fn try_apply( &mut self, __value_param: &(dyn PartialReflect + 'static), ) -> Result<(), ApplyError>

Tries to apply a reflected value to this value. Read more
Source§

fn reflect_kind(&self) -> ReflectKind

Returns a zero-sized enumeration of “kinds” of type. Read more
Source§

fn reflect_ref(&self) -> ReflectRef<'_>

Returns an immutable enumeration of “kinds” of type. Read more
Source§

fn reflect_mut(&mut self) -> ReflectMut<'_>

Returns a mutable enumeration of “kinds” of type. Read more
Source§

fn reflect_owned(self: Box<Color>) -> ReflectOwned

Returns an owned enumeration of “kinds” of type. Read more
Source§

fn try_into_reflect( self: Box<Color>, ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>

Attempts to cast this type to a boxed, fully-reflected value.
Source§

fn try_as_reflect(&self) -> Option<&(dyn Reflect + 'static)>

Attempts to cast this type to a fully-reflected value.
Source§

fn try_as_reflect_mut(&mut self) -> Option<&mut (dyn Reflect + 'static)>

Attempts to cast this type to a mutable, fully-reflected value.
Source§

fn into_partial_reflect(self: Box<Color>) -> Box<dyn PartialReflect>

Casts this type to a boxed, reflected value. Read more
Source§

fn as_partial_reflect(&self) -> &(dyn PartialReflect + 'static)

Casts this type to a reflected value. Read more
Source§

fn as_partial_reflect_mut(&mut self) -> &mut (dyn PartialReflect + 'static)

Casts this type to a mutable, reflected value. Read more
Source§

fn reflect_hash(&self) -> Option<u64>

Returns a hash of the value (which includes the type). Read more
Source§

fn reflect_partial_eq( &self, value: &(dyn PartialReflect + 'static), ) -> Option<bool>

Returns a “partial equality” comparison result. Read more
Source§

fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError>

Attempts to clone Self using reflection. Read more
Source§

fn apply(&mut self, value: &(dyn PartialReflect + 'static))

Applies a reflected value to this value. Read more
Source§

fn to_dynamic(&self) -> Box<dyn PartialReflect>

Converts this reflected value into its dynamic representation based on its kind. Read more
Source§

fn reflect_clone_and_take<T>(&self) -> Result<T, ReflectCloneError>
where T: 'static, Self: Sized + TypePath,

For a type implementing PartialReflect, combines reflect_clone and take in a useful fashion, automatically constructing an appropriate ReflectCloneError if the downcast fails. Read more
Source§

fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Debug formatter for the value. Read more
Source§

fn is_dynamic(&self) -> bool

Indicates whether or not this type is a dynamic type. Read more
Source§

impl Reflect for Color

Source§

fn into_any(self: Box<Color>) -> Box<dyn Any>

Returns the value as a Box<dyn Any>. Read more
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Returns the value as a &dyn Any. Read more
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Returns the value as a &mut dyn Any. Read more
Source§

fn into_reflect(self: Box<Color>) -> Box<dyn Reflect>

Casts this type to a boxed, fully-reflected value.
Source§

fn as_reflect(&self) -> &(dyn Reflect + 'static)

Casts this type to a fully-reflected value.
Source§

fn as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)

Casts this type to a mutable, fully-reflected value.
Source§

fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>

Performs a type-checked assignment of a reflected value to this value. Read more
Source§

impl Saturation for Color

Source§

fn with_saturation(&self, saturation: f32) -> Color

Return a new version of this color with the saturation channel set to the given value.
Source§

fn saturation(&self) -> f32

Return the saturation of this color [0.0, 1.0].
Source§

fn set_saturation(&mut self, saturation: f32)

Sets the saturation of this color.
Source§

impl Serialize for Color

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TypePath for Color

Source§

fn type_path() -> &'static str

Returns the fully qualified path of the underlying type. Read more
Source§

fn short_type_path() -> &'static str

Returns a short, pretty-print enabled path to the type. Read more
Source§

fn type_ident() -> Option<&'static str>

Returns the name of the type, or None if it is anonymous. Read more
Source§

fn crate_name() -> Option<&'static str>

Returns the name of the crate the type is in, or None if it is anonymous. Read more
Source§

fn module_path() -> Option<&'static str>

Returns the path to the module the type is in, or None if it is anonymous. Read more
Source§

impl Typed for Color

Source§

fn type_info() -> &'static TypeInfo

Returns the compile-time info for the underlying type.
Source§

impl Copy for Color

Source§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnwindSafe for Color

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

Source§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U

Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Conv for T

Source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DynamicTypePath for T
where T: TypePath,

Source§

impl<T> DynamicTyped for T
where T: Typed,

Source§

impl<T> FmtForward for T

Source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
Source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
Source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
Source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
Source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
Source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
Source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
Source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
Source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using default().

Source§

impl<T> GetPath for T
where T: Reflect + ?Sized,

Source§

fn reflect_path<'p>( &self, path: impl ReflectPath<'p>, ) -> Result<&(dyn PartialReflect + 'static), ReflectPathError<'p>>

Returns a reference to the value specified by path. Read more
Source§

fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p>, ) -> Result<&mut (dyn PartialReflect + 'static), ReflectPathError<'p>>

Returns a mutable reference to the value specified by path. Read more
Source§

fn path<'p, T>( &self, path: impl ReflectPath<'p>, ) -> Result<&T, ReflectPathError<'p>>
where T: Reflect,

Returns a statically typed reference to the value specified by path. Read more
Source§

fn path_mut<'p, T>( &mut self, path: impl ReflectPath<'p>, ) -> Result<&mut T, ReflectPathError<'p>>
where T: Reflect,

Returns a statically typed mutable reference to the value specified by path. Read more
Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

impl<T> Identity for T
where T: ?Sized,

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
Source§

impl<T> InitializeFromFunction<T> for T

Source§

fn initialize_from_function(f: fn() -> T) -> T

Create an instance of this type from an initialization function
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoResult<T> for T

Source§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<A> Is for A
where A: Any,

Source§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. Read more
Source§

impl<T> NoneValue for T
where T: Default,

Source§

type NoneType = T

Source§

fn null_value() -> T

The none-equivalent value.
Source§

impl<T> Pipe for T
where T: ?Sized,

Source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
Source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
Source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
Source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
Source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
Source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

Source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>

Source§

impl<Ret> SpawnIfAsync<(), Ret> for Ret

Source§

fn spawn(self) -> Ret

Spawn the value into the dioxus runtime if it is an async block
Source§

impl<T, O> SuperFrom<T> for O
where O: From<T>,

Source§

fn super_from(input: T) -> O

Convert from a type to another type.
Source§

impl<T, O, M> SuperInto<O, M> for T
where O: SuperFrom<T, M>,

Source§

fn super_into(self) -> O

Convert from a type to another type.
Source§

impl<T> Tap for T

Source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
Source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
Source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
Source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
Source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
Source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
Source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
Source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
Source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
Source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T> TryConv for T

Source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

fn clone_type_data(&self) -> Box<dyn TypeData>

Creates a type-erased clone of this value.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> Reflectable for T

Source§

impl<T> Settings for T
where T: 'static + Send + Sync,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,