Skip to content

Commit

Permalink
Directional light and shadow (#6)
Browse files Browse the repository at this point in the history
Directional light and shadow
  • Loading branch information
superdump authored Jul 8, 2021
1 parent 811ed3e commit bd13dce
Show file tree
Hide file tree
Showing 11 changed files with 638 additions and 65 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ path = "examples/3d/3d_scene.rs"
name = "3d_scene_pipelined"
path = "examples/3d/3d_scene_pipelined.rs"

[[example]]
name = "cornell_box_pipelined"
path = "examples/3d/cornell_box_pipelined.rs"

[[example]]
name = "load_gltf"
path = "examples/3d/load_gltf.rs"
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_transform/src/components/global_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl GlobalTransform {
#[doc(hidden)]
#[inline]
pub fn rotate(&mut self, rotation: Quat) {
self.rotation *= rotation;
self.rotation = rotation * self.rotation;
}

/// Multiplies `self` with `transform` component by component, returning the
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Transform {
/// Rotates the transform by the given rotation.
#[inline]
pub fn rotate(&mut self, rotation: Quat) {
self.rotation *= rotation;
self.rotation = rotation * self.rotation;
}

/// Multiplies `self` with `transform` component by component, returning the
Expand Down
1 change: 1 addition & 0 deletions crates/crevice/src/glam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ macro_rules! glam_vectors {
}

glam_vectors! {
glam::UVec4, UVec4, (x, y, z, w),
glam::Vec2, Vec2, (x, y),
glam::Vec3, Vec3, (x, y, z),
glam::Vec4, Vec4, (x, y, z, w),
Expand Down
57 changes: 48 additions & 9 deletions examples/3d/3d_scene_pipelined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use bevy::{
ecs::prelude::*,
input::Input,
math::{Quat, Vec3},
pbr2::{PbrBundle, PointLight, PointLightBundle, StandardMaterial},
pbr2::{
AmbientLight, DirectionalLight, DirectionalLightBundle, PbrBundle, PointLight,
PointLightBundle, StandardMaterial,
},
prelude::{App, Assets, BuildChildren, KeyCode, Transform},
render2::{
camera::PerspectiveCameraBundle,
camera::{OrthographicProjection, PerspectiveCameraBundle},
color::Color,
mesh::{shape, Mesh},
},
Expand All @@ -21,6 +24,7 @@ fn main() {
.add_plugin(LogDiagnosticsPlugin::default())
.add_startup_system(setup.system())
.add_system(movement.system())
.add_system(animate_light_direction.system())
.run();
}

Expand All @@ -32,11 +36,15 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.insert_resource(AmbientLight {
color: Color::ORANGE_RED,
brightness: 0.02,
});
// plane
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Plane { size: 10.0 })),
material: materials.add(StandardMaterial {
base_color: Color::INDIGO,
base_color: Color::WHITE,
perceptual_roughness: 1.0,
..Default::default()
}),
Expand All @@ -46,7 +54,7 @@ fn setup(
let mut transform = Transform::from_xyz(2.5, 2.5, 0.0);
transform.rotate(Quat::from_rotation_z(std::f32::consts::FRAC_PI_2));
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Box::new(5.0, 0.15, 5.0))),
transform,
material: materials.add(StandardMaterial {
base_color: Color::INDIGO,
Expand All @@ -59,7 +67,7 @@ fn setup(
let mut transform = Transform::from_xyz(0.0, 2.5, -2.5);
transform.rotate(Quat::from_rotation_x(std::f32::consts::FRAC_PI_2));
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Box::new(5.0, 0.15, 5.0))),
transform,
material: materials.add(StandardMaterial {
base_color: Color::INDIGO,
Expand All @@ -76,7 +84,7 @@ fn setup(
base_color: Color::PINK,
..Default::default()
}),
transform: Transform::from_xyz(0.0, 1.0, 0.0),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
})
.insert(Movable);
Expand Down Expand Up @@ -174,14 +182,45 @@ fn setup(
});
});

const HALF_SIZE: f32 = 10.0;
commands.spawn_bundle(DirectionalLightBundle {
directional_light: DirectionalLight {
// Configure the projection to better fit the scene
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 {
translation: Vec3::new(0.0, 2.0, 0.0),
rotation: Quat::from_rotation_x(-1.2),
..Default::default()
},
..Default::default()
});

// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 5.0, 7.5)
.looking_at(Vec3::new(0.0, 2.0, 0.0), Vec3::Y),
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});
}

fn animate_light_direction(
time: Res<Time>,
mut query: Query<&mut Transform, With<DirectionalLight>>,
) {
for mut transform in query.iter_mut() {
transform.rotate(Quat::from_rotation_y(time.delta_seconds() * 0.5));
}
}

fn movement(
input: Res<Input<KeyCode>>,
time: Res<Time>,
Expand Down
178 changes: 178 additions & 0 deletions examples/3d/cornell_box_pipelined.rs
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()
});
}
12 changes: 10 additions & 2 deletions pipelined/bevy_pbr2/src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{PointLight, StandardMaterial};
use crate::{DirectionalLight, PointLight, StandardMaterial};
use bevy_asset::Handle;
use bevy_ecs::bundle::Bundle;
use bevy_render2::mesh::Mesh;
Expand All @@ -23,10 +23,18 @@ impl Default for PbrBundle {
}
}

/// A component bundle for "light" entities
/// A component bundle for "point light" entities
#[derive(Debug, Bundle, Default)]
pub struct PointLightBundle {
pub point_light: PointLight,
pub transform: Transform,
pub global_transform: GlobalTransform,
}

/// A component bundle for "directional light" entities
#[derive(Debug, Bundle, Default)]
pub struct DirectionalLightBundle {
pub directional_light: DirectionalLight,
pub transform: Transform,
pub global_transform: GlobalTransform,
}
Loading

0 comments on commit bd13dce

Please sign in to comment.