forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Directional light and shadow
- Loading branch information
Showing
11 changed files
with
638 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
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.system()) | ||
.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, | ||
shadow_bias_min: 0.00001, | ||
shadow_bias_max: 0.0001, | ||
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; | ||
commands | ||
.spawn_bundle(DirectionalLightBundle { | ||
directional_light: DirectionalLight { | ||
illuminance: 10000.0, | ||
shadow_bias_min: 0.00001, | ||
shadow_bias_max: 0.0001, | ||
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() | ||
}, | ||
transform: Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::PI / 2.0)), | ||
..Default::default() | ||
}); | ||
|
||
// camera | ||
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() | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.