From 8aa843a6f94e7fa1de18da797b21fa27776dffed Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Fri, 2 Jul 2021 07:13:34 +0200 Subject: [PATCH] bevy_pbr2: Add orthographic_pipelined example and tweak bias for shadows --- Cargo.toml | 4 ++ examples/3d/orthographic_pipelined.rs | 71 +++++++++++++++++++++++++ pipelined/bevy_pbr2/src/render/pbr.wgsl | 2 +- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 examples/3d/orthographic_pipelined.rs diff --git a/Cargo.toml b/Cargo.toml index 129a443f1b205..7bba358e3b6aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -155,6 +155,10 @@ path = "examples/3d/msaa.rs" name = "orthographic" path = "examples/3d/orthographic.rs" +[[example]] +name = "orthographic_pipelined" +path = "examples/3d/orthographic_pipelined.rs" + [[example]] name = "parenting" path = "examples/3d/parenting.rs" diff --git a/examples/3d/orthographic_pipelined.rs b/examples/3d/orthographic_pipelined.rs new file mode 100644 index 0000000000000..31f80170bb4b0 --- /dev/null +++ b/examples/3d/orthographic_pipelined.rs @@ -0,0 +1,71 @@ +use bevy::{ + ecs::prelude::*, + math::Vec3, + pbr2::{PbrBundle, PointLightBundle, StandardMaterial}, + prelude::{App, Assets, Transform}, + render2::{ + camera::OrthographicCameraBundle, + color::Color, + mesh::{shape, Mesh}, + }, + PipelinedDefaultPlugins, +}; + +fn main() { + App::new() + .add_plugins(PipelinedDefaultPlugins) + .add_startup_system(setup.system()) + .run(); +} + +/// set up a simple 3D scene +fn setup( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + // set up the camera + let mut camera = OrthographicCameraBundle::new_3d(); + camera.orthographic_projection.scale = 3.0; + camera.transform = Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y); + + // camera + commands.spawn_bundle(camera); + + // plane + commands.spawn_bundle(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })), + material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), + ..Default::default() + }); + // cubes + commands.spawn_bundle(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), + material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), + transform: Transform::from_xyz(1.5, 0.5, 1.5), + ..Default::default() + }); + commands.spawn_bundle(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), + material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), + transform: Transform::from_xyz(1.5, 0.5, -1.5), + ..Default::default() + }); + commands.spawn_bundle(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), + material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), + transform: Transform::from_xyz(-1.5, 0.5, 1.5), + ..Default::default() + }); + commands.spawn_bundle(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), + material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), + transform: Transform::from_xyz(-1.5, 0.5, -1.5), + ..Default::default() + }); + // light + commands.spawn_bundle(PointLightBundle { + transform: Transform::from_xyz(3.0, 8.0, 5.0), + ..Default::default() + }); +} diff --git a/pipelined/bevy_pbr2/src/render/pbr.wgsl b/pipelined/bevy_pbr2/src/render/pbr.wgsl index ca8ea256cb7a2..0648a4440cae5 100644 --- a/pipelined/bevy_pbr2/src/render/pbr.wgsl +++ b/pipelined/bevy_pbr2/src/render/pbr.wgsl @@ -386,7 +386,7 @@ fn fetch_shadow(light_id: i32, frag_position: vec4) -> f32 { // from LOD 0. // NOTE: with reverse projections, the shadow bias must be added to the fragment depth, not subtracted // FIXME: make the shadow bias configurable - let bias = 0.02; + let bias = 0.001; return textureSampleCompareLevel(shadow_textures, shadow_textures_sampler, frag_ls, i32(light_id), depth + bias); }