bevy/examples/3d/cornell_box_pipelined.rs

173 lines
5.6 KiB
Rust
Raw Normal View History

use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
ecs::prelude::*,
math::{Mat4, Quat, Vec3},
pbr2::{
AmbientLight, DirectionalLight, DirectionalLightBundle, PbrBundle, PointLight,
PointLightBundle, StandardMaterial,
},
prelude::{App, Assets, BuildChildren, Transform},
render2::{
camera::{OrthographicProjection, PerspectiveCameraBundle},
color::Color,
mesh::{shape, Mesh},
},
PipelinedDefaultPlugins,
};
fn main() {
App::new()
.add_plugins(PipelinedDefaultPlugins)
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(LogDiagnosticsPlugin::default())
.add_startup_system(setup)
.run();
}
/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let box_size = 2.0;
let box_thickness = 0.15;
let box_offset = (box_size + box_thickness) / 2.0;
// left - red
let mut transform = Transform::from_xyz(-box_offset, box_offset, 0.0);
transform.rotate(Quat::from_rotation_z(std::f32::consts::FRAC_PI_2));
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Box::new(
box_size,
box_thickness,
box_size,
))),
transform,
material: materials.add(StandardMaterial {
base_color: Color::rgb(0.63, 0.065, 0.05),
..Default::default()
}),
..Default::default()
});
// right - green
let mut transform = Transform::from_xyz(box_offset, box_offset, 0.0);
transform.rotate(Quat::from_rotation_z(std::f32::consts::FRAC_PI_2));
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Box::new(
box_size,
box_thickness,
box_size,
))),
transform,
material: materials.add(StandardMaterial {
base_color: Color::rgb(0.14, 0.45, 0.091),
..Default::default()
}),
..Default::default()
});
// bottom - white
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Box::new(
box_size + 2.0 * box_thickness,
box_thickness,
box_size,
))),
material: materials.add(StandardMaterial {
base_color: Color::rgb(0.725, 0.71, 0.68),
..Default::default()
}),
..Default::default()
});
// top - white
let transform = Transform::from_xyz(0.0, 2.0 * box_offset, 0.0);
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Box::new(
box_size + 2.0 * box_thickness,
box_thickness,
box_size,
))),
transform,
material: materials.add(StandardMaterial {
base_color: Color::rgb(0.725, 0.71, 0.68),
..Default::default()
}),
..Default::default()
});
// back - white
let mut transform = Transform::from_xyz(0.0, box_offset, -box_offset);
transform.rotate(Quat::from_rotation_x(std::f32::consts::FRAC_PI_2));
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Box::new(
box_size + 2.0 * box_thickness,
box_thickness,
box_size + 2.0 * box_thickness,
))),
transform,
material: materials.add(StandardMaterial {
base_color: Color::rgb(0.725, 0.71, 0.68),
..Default::default()
}),
..Default::default()
});
// ambient light
commands.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 0.02,
});
// top light
commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 0.4 })),
transform: Transform::from_matrix(Mat4::from_scale_rotation_translation(
Vec3::ONE,
Quat::from_rotation_x(std::f32::consts::PI),
Vec3::new(0.0, box_size + 0.5 * box_thickness, 0.0),
)),
material: materials.add(StandardMaterial {
base_color: Color::WHITE,
emissive: Color::WHITE * 100.0,
..Default::default()
}),
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PointLightBundle {
point_light: PointLight {
color: Color::WHITE,
intensity: 25.0,
..Default::default()
},
transform: Transform::from_translation((box_thickness + 0.05) * Vec3::Y),
..Default::default()
});
});
// directional light
const HALF_SIZE: f32 = 10.0;
2021-07-08 03:01:41 +00:00
commands.spawn_bundle(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: 10000.0,
shadow_projection: OrthographicProjection {
left: -HALF_SIZE,
right: HALF_SIZE,
bottom: -HALF_SIZE,
top: HALF_SIZE,
near: -10.0 * HALF_SIZE,
far: 10.0 * HALF_SIZE,
..Default::default()
},
..Default::default()
2021-07-08 03:01:41 +00:00
},
transform: Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::PI / 2.0)),
..Default::default()
});
// camera
2021-07-08 03:01:41 +00:00
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.0, box_offset, 4.0)
.looking_at(Vec3::new(0.0, box_offset, 0.0), Vec3::Y),
..Default::default()
});
}