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()
|
|
|
|
.add_default_plugins()
|
|
|
|
.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)>) {
|
|
|
|
for (_rotator, mut transform) in &mut query.iter() {
|
2020-09-19 20:35:48 +00:00
|
|
|
let rotation = transform.rotation() * Quat::from_rotation_x(3.0 * time.delta_seconds);
|
|
|
|
transform.set_rotation(rotation);
|
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-07-10 04:18:35 +00:00
|
|
|
mut camera_query: Query<(&Camera, &VisibleEntities)>,
|
|
|
|
material_query: Query<&Handle<StandardMaterial>>,
|
2020-06-23 00:55:48 +00:00
|
|
|
) {
|
2020-07-22 03:12:15 +00:00
|
|
|
for (_camera, visible_entities) in &mut camera_query.iter() {
|
2020-06-23 23:52:50 +00:00
|
|
|
for visible_entity in visible_entities.iter() {
|
2020-07-10 04:18:35 +00:00
|
|
|
if let Ok(material_handle) =
|
|
|
|
material_query.get::<Handle<StandardMaterial>>(visible_entity.entity)
|
2020-06-24 02:15:05 +00:00
|
|
|
{
|
2020-06-23 20:42:37 +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-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, 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-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
|
|
|
.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-09-14 21:00:32 +00:00
|
|
|
transform: Transform::new(Mat4::face_toward(
|
2020-06-23 00:55:48 +00:00
|
|
|
Vec3::new(5.0, 10.0, 10.0),
|
|
|
|
Vec3::new(0.0, 0.0, 0.0),
|
2020-06-24 22:29:10 +00:00
|
|
|
Vec3::new(0.0, 1.0, 0.0),
|
2020-06-23 00:55:48 +00:00
|
|
|
)),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|