-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Contact Shadows #22382
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
Open
aevyrie
wants to merge
18
commits into
bevyengine:main
Choose a base branch
from
aevyrie:contact-shadows
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+906
−223
Open
Contact Shadows #22382
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
331ba66
Implement contact shadows
aevyrie 375b2b1
Formatting
aevyrie 835ecd6
Bump contact shadow steps to match ssr and improve appearance
aevyrie d96c7d6
Review feedback
aevyrie bae7d05
Factor out contact shadow logic
aevyrie 00b02e5
Add STBN support
aevyrie 3fa5a25
Update example defaults
aevyrie 20151bd
Example fixes
aevyrie 07f2b89
Add light setting `contact_shadows_enabled` and rename `shadows_enabl…
aevyrie 8f45d15
Require the stbn feature for the contact shadows example
aevyrie f2fe279
CI lints
aevyrie 8297194
Merge branch 'main' into contact-shadows
aevyrie 3c0f881
Update example appearance
aevyrie 06b4f1e
Merge remote-tracking branch 'origin/main' into contact-shadows
aevyrie ce20572
Equalize light intensity
aevyrie 3e5530c
Fix lint
aevyrie e04c02a
Merge branch 'main' into contact-shadows
aevyrie 246c803
Add TAA and SSAO to example
aevyrie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,140 @@ | ||
| //! Contact shadows implemented via screenspace raymarching. | ||
|
|
||
| use bevy_app::{App, Plugin}; | ||
| use bevy_derive::{Deref, DerefMut}; | ||
| use bevy_ecs::{ | ||
| component::Component, | ||
| entity::Entity, | ||
| query::{QueryItem, With}, | ||
| reflect::ReflectComponent, | ||
| resource::Resource, | ||
| schedule::IntoScheduleConfigs, | ||
| system::{Commands, Query, Res, ResMut}, | ||
| }; | ||
| use bevy_reflect::{std_traits::ReflectDefault, Reflect}; | ||
| use bevy_render::{ | ||
| extract_component::{ExtractComponent, ExtractComponentPlugin}, | ||
| render_resource::{DynamicUniformBuffer, ShaderType}, | ||
| renderer::{RenderDevice, RenderQueue}, | ||
| view::ExtractedView, | ||
| Render, RenderApp, RenderSystems, | ||
| }; | ||
| use bevy_utils::default; | ||
|
|
||
| /// Enables contact shadows for a camera. | ||
| pub struct ContactShadowsPlugin; | ||
|
|
||
| /// Add this component to a camera to enable contact shadows. | ||
| /// | ||
| /// Contact shadows are a screen-space technique that adds small-scale shadows | ||
| /// in areas where traditional shadow maps may lack detail, such as where | ||
| /// objects touch the ground. | ||
| /// | ||
| /// This can be used in forward or deferred rendering, but the depth prepass is required. | ||
| #[derive(Clone, Copy, Component, Reflect)] | ||
| #[reflect(Component, Default, Clone)] | ||
| #[require(bevy_core_pipeline::prepass::DepthPrepass)] | ||
| pub struct ContactShadows { | ||
| /// The number of steps to be taken at regular intervals to find an initial | ||
| /// intersection. | ||
| pub linear_steps: u32, | ||
| /// When marching the depth buffer, we only have 2.5D information and don't | ||
| /// know how thick surfaces are. We shall assume that the depth buffer | ||
| /// fragments are cuboids with a constant thickness defined by this | ||
| /// parameter. | ||
| pub thickness: f32, | ||
| /// The length of the contact shadow ray in world space. | ||
| pub length: f32, | ||
| } | ||
|
|
||
| impl Default for ContactShadows { | ||
| fn default() -> Self { | ||
| Self { | ||
| linear_steps: 16, | ||
| thickness: 0.1, | ||
| length: 0.3, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A version of [`ContactShadows`] for upload to the GPU. | ||
| #[derive(Clone, Copy, Component, ShaderType, Default)] | ||
| pub struct ContactShadowsUniform { | ||
| pub linear_steps: u32, | ||
| pub thickness: f32, | ||
| pub length: f32, | ||
| } | ||
|
|
||
| impl From<ContactShadows> for ContactShadowsUniform { | ||
| fn from(settings: ContactShadows) -> Self { | ||
| Self { | ||
| linear_steps: settings.linear_steps, | ||
| thickness: settings.thickness, | ||
| length: settings.length, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ExtractComponent for ContactShadows { | ||
| type QueryData = &'static ContactShadows; | ||
| type QueryFilter = (); | ||
| type Out = ContactShadows; | ||
|
|
||
| fn extract_component(settings: QueryItem<'_, '_, Self::QueryData>) -> Option<Self::Out> { | ||
| Some(*settings) | ||
| } | ||
| } | ||
|
|
||
| /// A GPU buffer that stores the contact shadow settings for each view. | ||
| #[derive(Resource, Default)] | ||
| pub struct ContactShadowsBuffer(pub DynamicUniformBuffer<ContactShadowsUniform>); | ||
|
|
||
| impl Plugin for ContactShadowsPlugin { | ||
| fn build(&self, app: &mut App) { | ||
| app.register_type::<ContactShadows>() | ||
| .add_plugins(ExtractComponentPlugin::<ContactShadows>::default()); | ||
|
|
||
| let Some(render_app) = app.get_sub_app_mut(RenderApp) else { | ||
| return; | ||
| }; | ||
|
|
||
| render_app | ||
| .init_resource::<ContactShadowsBuffer>() | ||
| .add_systems( | ||
| Render, | ||
| prepare_contact_shadows_settings.in_set(RenderSystems::PrepareResources), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| fn prepare_contact_shadows_settings( | ||
| mut commands: Commands, | ||
| views: Query<(Entity, Option<&ContactShadows>), With<ExtractedView>>, | ||
| mut contact_shadows_buffer: ResMut<ContactShadowsBuffer>, | ||
| render_device: Res<RenderDevice>, | ||
| render_queue: Res<RenderQueue>, | ||
| ) { | ||
| contact_shadows_buffer.0.clear(); | ||
| for (entity, settings) in &views { | ||
| let uniform = if let Some(settings) = settings { | ||
| ContactShadowsUniform::from(*settings) | ||
| } else { | ||
| ContactShadowsUniform { | ||
| linear_steps: 0, | ||
| ..default() | ||
| } | ||
| }; | ||
| let offset = contact_shadows_buffer.0.push(&uniform); | ||
| commands | ||
| .entity(entity) | ||
| .insert(ViewContactShadowsUniformOffset(offset)); | ||
| } | ||
| contact_shadows_buffer | ||
| .0 | ||
| .write_buffer(&render_device, &render_queue); | ||
| } | ||
|
|
||
| /// A component that stores the offset within the [`ContactShadowsBuffer`] for | ||
| /// each view. | ||
| #[derive(Component, Default, Deref, DerefMut)] | ||
| pub struct ViewContactShadowsUniformOffset(pub u32); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will need a migration guide