mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
cbcd826612
# Objective Closes #10319 ## Changelog * Added a new `Color::rgba_from_array([f32; 4]) -> Color` method. * Added a new `Color::rgb_from_array([f32; 3]) -> Color` method. * Added a new `Color::rgba_linear_from_array([f32; 4]) -> Color` method. * Added a new `Color::rgb_linear_from_array([f32; 3]) -> Color` method. * Added a new `Color::hsla_from_array([f32; 4]) -> Color` method. * Added a new `Color::hsl_from_array([f32; 3]) -> Color` method. * Added a new `Color::lcha_from_array([f32; 4]) -> Color` method. * Added a new `Color::lch_from_array([f32; 3]) -> Color` method. * Added a new `Color::rgba_to_vec4(&self) -> Vec4` method. * Added a new `Color::rgba_to_array(&self) -> [f32; 4]` method. * Added a new `Color::rgb_to_vec3(&self) -> Vec3` method. * Added a new `Color::rgb_to_array(&self) -> [f32; 3]` method. * Added a new `Color::rgba_linear_to_vec4(&self) -> Vec4` method. * Added a new `Color::rgba_linear_to_array(&self) -> [f32; 4]` method. * Added a new `Color::rgb_linear_to_vec3(&self) -> Vec3` method. * Added a new `Color::rgb_linear_to_array(&self) -> [f32; 3]` method. * Added a new `Color::hsla_to_vec4(&self) -> Vec4` method. * Added a new `Color::hsla_to_array(&self) -> [f32; 4]` method. * Added a new `Color::hsl_to_vec3(&self) -> Vec3` method. * Added a new `Color::hsl_to_array(&self) -> [f32; 3]` method. * Added a new `Color::lcha_to_vec4(&self) -> Vec4` method. * Added a new `Color::lcha_to_array(&self) -> [f32; 4]` method. * Added a new `Color::lch_to_vec3(&self) -> Vec3` method. * Added a new `Color::lch_to_array(&self) -> [f32; 3]` method. ## Migration Guide `Color::from(Vec4)` is now `Color::rgba_from_array(impl Into<[f32; 4]>)` `Vec4::from(Color)` is now `Color::rgba_to_vec4(&self)` Before: ```rust let color_vec4 = Vec4::new(0.5, 0.5, 0.5); let color_from_vec4 = Color::from(color_vec4); let color_array = [0.5, 0.5, 0.5]; let color_from_array = Color::from(color_array); ``` After: ```rust let color_vec4 = Vec4::new(0.5, 0.5, 0.5); let color_from_vec4 = Color::rgba_from_array(color_vec4); let color_array = [0.5, 0.5, 0.5]; let color_from_array = Color::rgba_from_array(color_array); ```
65 lines
1.9 KiB
Rust
65 lines
1.9 KiB
Rust
//! Demonstrates the use of [`UiMaterials`](UiMaterial) and how to change material values
|
|
|
|
use bevy::prelude::*;
|
|
use bevy::reflect::TypePath;
|
|
use bevy::render::render_resource::*;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_plugins(UiMaterialPlugin::<CustomUiMaterial>::default())
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, update)
|
|
.run();
|
|
}
|
|
|
|
fn update(time: Res<Time>, mut ui_materials: ResMut<Assets<CustomUiMaterial>>) {
|
|
for (_, material) in ui_materials.iter_mut() {
|
|
// rainbow color effect
|
|
let new_color = Color::hsl((time.elapsed_seconds() * 60.0) % 360.0, 1., 0.5);
|
|
material.color = new_color.rgba_to_vec4();
|
|
}
|
|
}
|
|
|
|
fn setup(mut commands: Commands, mut ui_materials: ResMut<Assets<CustomUiMaterial>>) {
|
|
// Camera so we can see UI
|
|
commands.spawn(Camera2dBundle::default());
|
|
|
|
commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
},
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent.spawn(MaterialNodeBundle {
|
|
style: Style {
|
|
position_type: PositionType::Absolute,
|
|
width: Val::Px(250.0),
|
|
height: Val::Px(250.0),
|
|
..default()
|
|
},
|
|
material: ui_materials.add(CustomUiMaterial {
|
|
color: Color::WHITE.rgba_to_vec4(),
|
|
}),
|
|
..default()
|
|
});
|
|
});
|
|
}
|
|
|
|
#[derive(AsBindGroup, Asset, TypePath, Debug, Clone)]
|
|
struct CustomUiMaterial {
|
|
#[uniform(0)]
|
|
color: Vec4,
|
|
}
|
|
|
|
impl UiMaterial for CustomUiMaterial {
|
|
fn fragment_shader() -> ShaderRef {
|
|
"shaders/circle_shader.wgsl".into()
|
|
}
|
|
}
|