mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 06:30:19 +00:00
de004da8d5
# Objective The migration process for `bevy_color` (#12013) will be fairly involved: there will be hundreds of affected files, and a large number of APIs. ## Solution To allow us to proceed granularly, we're going to keep both `bevy_color::Color` (new) and `bevy_render::Color` (old) around until the migration is complete. However, simply doing this directly is confusing! They're both called `Color`, making it very hard to tell when a portion of the code has been ported. As discussed in #12056, by renaming the old `Color` type, we can make it easier to gradually migrate over, one API at a time. ## Migration Guide THIS MIGRATION GUIDE INTENTIONALLY LEFT BLANK. This change should not be shipped to end users: delete this section in the final migration guide! --------- Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com>
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 = LegacyColor::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: LegacyColor::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()
|
|
}
|
|
}
|