From bd13dcedddb8203a575d3423273c9e58d565540d Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 8 Jul 2021 04:49:33 +0200 Subject: [PATCH] Directional light and shadow (#6) Directional light and shadow --- Cargo.toml | 4 + .../src/components/global_transform.rs | 2 +- .../src/components/transform.rs | 2 +- crates/crevice/src/glam.rs | 1 + examples/3d/3d_scene_pipelined.rs | 57 +++- examples/3d/cornell_box_pipelined.rs | 178 +++++++++++++ pipelined/bevy_pbr2/src/bundle.rs | 12 +- pipelined/bevy_pbr2/src/light.rs | 77 +++++- pipelined/bevy_pbr2/src/render/light.rs | 247 +++++++++++++++--- pipelined/bevy_pbr2/src/render/mod.rs | 46 +++- pipelined/bevy_pbr2/src/render/pbr.wgsl | 77 +++++- 11 files changed, 638 insertions(+), 65 deletions(-) create mode 100644 examples/3d/cornell_box_pipelined.rs diff --git a/Cargo.toml b/Cargo.toml index 129a443f1b205..4d9cf45a81f20 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index ceca829bc74f6..51ec76fbee68a 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -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 diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index f4a538d30ecb8..43515ed39a908 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -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 diff --git a/crates/crevice/src/glam.rs b/crates/crevice/src/glam.rs index 18fe4cbf677d6..aa57061ffad06 100644 --- a/crates/crevice/src/glam.rs +++ b/crates/crevice/src/glam.rs @@ -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), diff --git a/examples/3d/3d_scene_pipelined.rs b/examples/3d/3d_scene_pipelined.rs index 97801d9796fbd..b3b37efba0271 100644 --- a/examples/3d/3d_scene_pipelined.rs +++ b/examples/3d/3d_scene_pipelined.rs @@ -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}, }, @@ -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(); } @@ -32,11 +36,15 @@ fn setup( mut meshes: ResMut>, mut materials: ResMut>, ) { + 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() }), @@ -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, @@ -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, @@ -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); @@ -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