2020-07-17 01:26:21 +00:00
|
|
|
use bevy::{
|
|
|
|
prelude::*,
|
|
|
|
render::{
|
|
|
|
camera::{Camera, VisibleEntities},
|
|
|
|
mesh::shape,
|
|
|
|
},
|
|
|
|
};
|
2020-06-23 00:55:48 +00:00
|
|
|
|
2020-07-28 20:43:07 +00:00
|
|
|
/// This example visualizes camera z-ordering by setting the material of rotating cubes to their distance from the camera
|
2020-06-23 00:55:48 +00:00
|
|
|
fn main() {
|
|
|
|
App::build()
|
2020-11-03 02:38:37 +00:00
|
|
|
.add_plugin_group(DefaultPlugins)
|
2020-06-23 00:55:48 +00:00
|
|
|
.add_startup_system(setup.system())
|
|
|
|
.add_system(rotator_system.system())
|
|
|
|
.add_system(camera_order_color_system.system())
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2020-07-28 20:43:07 +00:00
|
|
|
struct Rotator;
|
|
|
|
|
2020-06-23 00:55:48 +00:00
|
|
|
/// rotates the parent, which will result in the child also rotating
|
2020-09-14 21:00:32 +00:00
|
|
|
fn rotator_system(time: Res<Time>, mut query: Query<(&Rotator, &mut Transform)>) {
|
2020-10-30 06:39:55 +00:00
|
|
|
for (_rotator, mut transform) in query.iter_mut() {
|
2020-10-18 20:03:16 +00:00
|
|
|
transform.rotation *= Quat::from_rotation_x(3.0 * time.delta_seconds);
|
2020-06-27 19:06:12 +00:00
|
|
|
}
|
2020-06-23 00:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn camera_order_color_system(
|
2020-06-23 20:42:37 +00:00
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
2020-10-30 06:39:55 +00:00
|
|
|
camera_query: Query<(&Camera, &VisibleEntities)>,
|
2020-07-10 04:18:35 +00:00
|
|
|
material_query: Query<&Handle<StandardMaterial>>,
|
2020-06-23 00:55:48 +00:00
|
|
|
) {
|
2020-10-30 06:39:55 +00:00
|
|
|
for (_camera, visible_entities) in camera_query.iter() {
|
2020-06-23 23:52:50 +00:00
|
|
|
for visible_entity in visible_entities.iter() {
|
2020-10-31 01:04:33 +00:00
|
|
|
if let Ok(material_handle) = material_query.get(visible_entity.entity) {
|
2020-10-18 20:48:15 +00:00
|
|
|
let material = materials.get_mut(&*material_handle).unwrap();
|
2020-06-24 02:15:05 +00:00
|
|
|
let value = 1.0 - (visible_entity.order.0 - 10.0) / 7.0;
|
2020-06-23 20:42:37 +00:00
|
|
|
material.albedo = Color::rgb(value, value, value);
|
|
|
|
}
|
2020-06-23 00:55:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(
|
2020-07-10 04:18:35 +00:00
|
|
|
mut commands: Commands,
|
2020-06-23 00:55:48 +00:00
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
) {
|
|
|
|
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
|
2020-07-10 04:18:35 +00:00
|
|
|
commands
|
2020-06-23 00:55:48 +00:00
|
|
|
// parent cube
|
2020-07-10 04:18:35 +00:00
|
|
|
.spawn(PbrComponents {
|
2020-10-18 20:48:15 +00:00
|
|
|
mesh: cube_handle.clone(),
|
2020-06-23 00:55:48 +00:00
|
|
|
material: materials.add(StandardMaterial {
|
|
|
|
shaded: false,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2020-09-14 21:00:32 +00:00
|
|
|
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
|
2020-06-23 00:55:48 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
2020-06-25 18:21:56 +00:00
|
|
|
.with(Rotator)
|
2020-07-10 04:18:35 +00:00
|
|
|
.with_children(|parent| {
|
2020-06-23 00:55:48 +00:00
|
|
|
// child cubes
|
2020-07-10 04:18:35 +00:00
|
|
|
parent
|
|
|
|
.spawn(PbrComponents {
|
2020-10-18 20:48:15 +00:00
|
|
|
mesh: cube_handle.clone(),
|
2020-06-23 00:55:48 +00:00
|
|
|
material: materials.add(StandardMaterial {
|
|
|
|
shaded: false,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2020-09-14 21:00:32 +00:00
|
|
|
transform: Transform::from_translation(Vec3::new(0.0, 3.0, 0.0)),
|
2020-06-23 00:55:48 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
2020-07-10 04:18:35 +00:00
|
|
|
.spawn(PbrComponents {
|
2020-06-23 00:55:48 +00:00
|
|
|
mesh: cube_handle,
|
|
|
|
material: materials.add(StandardMaterial {
|
|
|
|
shaded: false,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2020-09-14 21:00:32 +00:00
|
|
|
transform: Transform::from_translation(Vec3::new(0.0, -3.0, 0.0)),
|
2020-06-23 00:55:48 +00:00
|
|
|
..Default::default()
|
2020-07-10 04:18:35 +00:00
|
|
|
});
|
2020-06-23 00:55:48 +00:00
|
|
|
})
|
|
|
|
// camera
|
2020-07-20 00:00:08 +00:00
|
|
|
.spawn(Camera3dComponents {
|
2020-10-18 20:48:15 +00:00
|
|
|
transform: Transform::from_translation(Vec3::new(5.0, 10.0, 10.0))
|
|
|
|
.looking_at(Vec3::default(), Vec3::unit_y()),
|
2020-06-23 00:55:48 +00:00
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|