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 03:01:17 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2020-12-16 05:57:16 +00:00
|
|
|
.add_startup_system(setup.system())
|
|
|
|
.add_system(rotator_system.system())
|
|
|
|
.add_system(camera_order_color_system.system())
|
2020-06-23 00:55:48 +00:00
|
|
|
.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-11-13 02:22:46 +00:00
|
|
|
fn rotator_system(time: Res<Time>, mut query: Query<&mut Transform, With<Rotator>>) {
|
|
|
|
for mut transform in query.iter_mut() {
|
2020-11-28 21:08:31 +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-11-13 02:22:46 +00:00
|
|
|
camera_query: Query<&VisibleEntities, With<Camera>>,
|
2020-07-10 04:18:35 +00:00
|
|
|
material_query: Query<&Handle<StandardMaterial>>,
|
2020-06-23 00:55:48 +00:00
|
|
|
) {
|
2020-11-13 02:22:46 +00:00
|
|
|
for 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();
|
2021-02-09 20:37:49 +00:00
|
|
|
let value = 1.0 - (visible_entity.order.0.sqrt() - 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-11-08 20:34:05 +00:00
|
|
|
commands: &mut Commands,
|
2020-06-23 00:55:48 +00:00
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
) {
|
2020-11-21 22:51:24 +00:00
|
|
|
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 2.0 }));
|
2020-07-10 04:18:35 +00:00
|
|
|
commands
|
2020-06-23 00:55:48 +00:00
|
|
|
// parent cube
|
2020-11-16 04:32:23 +00:00
|
|
|
.spawn(PbrBundle {
|
2020-10-18 20:48:15 +00:00
|
|
|
mesh: cube_handle.clone(),
|
2020-06-23 00:55:48 +00:00
|
|
|
material: materials.add(StandardMaterial {
|
2021-02-01 01:13:16 +00:00
|
|
|
unlit: true,
|
2020-06-23 00:55:48 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-01-07 01:17:06 +00:00
|
|
|
transform: Transform::from_xyz(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
|
2020-11-16 04:32:23 +00:00
|
|
|
.spawn(PbrBundle {
|
2020-10-18 20:48:15 +00:00
|
|
|
mesh: cube_handle.clone(),
|
2020-06-23 00:55:48 +00:00
|
|
|
material: materials.add(StandardMaterial {
|
2021-02-01 01:13:16 +00:00
|
|
|
unlit: true,
|
2020-06-23 00:55:48 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-01-07 01:17:06 +00:00
|
|
|
transform: Transform::from_xyz(0.0, 3.0, 0.0),
|
2020-06-23 00:55:48 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
2020-11-16 04:32:23 +00:00
|
|
|
.spawn(PbrBundle {
|
2020-06-23 00:55:48 +00:00
|
|
|
mesh: cube_handle,
|
|
|
|
material: materials.add(StandardMaterial {
|
2021-02-01 01:13:16 +00:00
|
|
|
unlit: true,
|
2020-06-23 00:55:48 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-01-07 01:17:06 +00:00
|
|
|
transform: Transform::from_xyz(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
|
2021-01-30 10:31:03 +00:00
|
|
|
.spawn(PerspectiveCameraBundle {
|
2021-01-07 01:17:06 +00:00
|
|
|
transform: Transform::from_xyz(5.0, 10.0, 10.0)
|
2020-10-18 20:48:15 +00:00
|
|
|
.looking_at(Vec3::default(), Vec3::unit_y()),
|
2020-06-23 00:55:48 +00:00
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|