Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directional light and shadow #6

Merged
merged 52 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
f7b61a3
bevy_pbr2: Add support for most of the StandardMaterial textures
superdump Jun 19, 2021
a7d743f
bevy_pbr2: Ensure RenderCommandQueue is ready for PbrShaders init
superdump Jun 19, 2021
78e7603
texture_pipelined: Add a light to the scene so we can see stuff
superdump Jun 19, 2021
984b9ad
WIP bevy_pbr2: back to front sorting hack
superdump Jun 19, 2021
d2f11bd
bevy_pbr2: Uniform control flow for texture sampling in pbr.frag
superdump Jun 20, 2021
af71877
bevy_pbr2: Simplify texture and sampler names
superdump Jun 20, 2021
4c32a3a
bevy_pbr2: Match default 'AmbientColor' of current bevy_pbr for now
superdump Jun 20, 2021
b5aae65
bevy_pbr2: Convert from non-linear to linear sRGB for the color uniform
superdump Jun 20, 2021
57617b2
bevy_pbr2: Add pbr_pipelined example
superdump Jun 20, 2021
64385dc
Fix view vector in pbr frag to work in ortho
aevyrie Jun 21, 2021
f40c3b9
bevy_pbr2: Use a 90 degree y fov and light range projection for lights
superdump Jun 21, 2021
d41f69a
bevy_pbr2: Add AmbientLight resource
superdump Jun 21, 2021
3965713
bevy_pbr2: Convert PointLight color to linear sRGB for use in fragmen…
superdump Jun 21, 2021
5d1dfb1
bevy_pbr2: pbr.frag: Rename PointLight.projection to view_projection
superdump Jun 21, 2021
dd758b9
bevy_pbr2: PointLight is an OmniLight as it has a radius
superdump Jun 21, 2021
78dcc39
bevy_pbr2: Factoring out duplicated code
superdump Jun 21, 2021
530a938
bevy_pbr2: Add support for DirectionalLight
superdump Jun 21, 2021
6fe9950
bevy_pbr2: Add support for directional light shadows
superdump Jun 22, 2021
57e7ef8
bevy_pbr2: Add a configurable shadow bias min/max to avoid self-shado…
superdump Jun 22, 2021
d7af224
bevy_pbr2: Fix use of both omni and directional lights
superdump Jun 23, 2021
f76ea05
bevy_pbr2: Move directional light's view back to the origin
superdump Jun 23, 2021
fd5b0f3
3d_scene_pipelined: Move the cube to sit on the plane
superdump Jun 23, 2021
8fbdf64
3d_scene_pipelined: Remove material properties that prevent receiving…
superdump Jun 23, 2021
3a9a23e
3d_scene_pipelined: Add more lights and animate the directional light
superdump Jun 23, 2021
b20a500
Merge branch 'pipelined-rendering' into standard-material-textures
superdump Jun 25, 2021
50f01a3
bevy_pbr2: Implement RenderAsset for StandardMaterial
superdump Jun 25, 2021
14829c6
Merge branch 'standard-material-textures' into directional-light-and-…
superdump Jun 25, 2021
a0eb342
Merge branch 'pipelined-rendering' into standard-material-textures
superdump Jun 27, 2021
7cbb00d
Merge branch 'standard-material-textures' into directional-light-and-…
superdump Jun 27, 2021
9f6ef59
Merge branch 'pipelined-rendering' into directional-light-and-shadow
superdump Jun 28, 2021
0633e65
Force main thread for prepare_windows system
TheRawMeatball Jun 29, 2021
f8d96d1
bevy_pbr2: Fix light uniforms
superdump Jun 29, 2021
fba4011
bevy_pbr2: Fix the position outputs from the vertex stage
superdump Jun 29, 2021
0fecd3d
Merge branch 'pipelined-rendering' into directional-light-and-shadow
superdump Jun 29, 2021
3a19a63
Merge branch 'thread-fix' into directional-light-and-shadow
superdump Jun 29, 2021
10819b0
Merge branch 'wgsl-pbr-fixes' into directional-light-and-shadow
superdump Jun 29, 2021
df6074d
Merge branch 'pipelined-rendering' into directional-light-and-shadow
superdump Jun 30, 2021
ff33307
crevice: Add std{140,430}_padded_size_static
superdump Jul 1, 2021
0530368
bevy_render2/bevy_pbr2: Use std140_padded_size_static
superdump Jul 1, 2021
6226865
Merge branch 'crevice-padded-size' into directional-light-and-shadow
superdump Jul 1, 2021
7ed3832
bevy_pbr2: Port directional lights and shadows to wgsl
superdump Jul 1, 2021
bbb9a81
Merge branch 'pipelined-rendering' into directional-light-and-shadow
superdump Jul 2, 2021
baf966c
3d_scene_pipelined: tweak shadow biases for this scene
superdump Jul 2, 2021
b59fa7f
3d_scene_pipelined: Use boxes for walls to avoid light leakage
superdump Jul 2, 2021
14965e7
3d_scene_pipelined: Remove duplicate camera
superdump Jul 2, 2021
4e01e37
Add something resembling the cornell box
superdump Jul 2, 2021
37efe64
cornell_box_pipelined: Improve the ceiling light a little
superdump Jul 2, 2021
98428bb
Revert "Merge branch 'crevice-padded-size' into directional-light-and…
superdump Jul 7, 2021
348adb9
bevy_pbr2: Fix GpuLights min_binding_size
superdump Jul 7, 2021
c6d1d6c
fix rotation
cart Jul 8, 2021
0f9cc99
Use light transform to determine direction and move bias defaults int…
cart Jul 8, 2021
8efae35
simplify examples
cart Jul 8, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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