Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 18 additions & 18 deletions crates/bevy_pbr/src/atmosphere/bindings.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
atmosphere::types::{Atmosphere, AtmosphereSettings, AtmosphereTransforms}
}

@group(0) @binding(0) var<uniform> atmosphere: Atmosphere;
@group(0) @binding(1) var<uniform> settings: AtmosphereSettings;
@group(0) @binding(2) var<uniform> atmosphere_transforms: AtmosphereTransforms;
@group(0) @binding(3) var<uniform> view: View;
@group(0) @binding(4) var<uniform> lights: Lights;
@group(0) @binding(5) var transmittance_lut: texture_2d<f32>;
@group(0) @binding(6) var transmittance_lut_sampler: sampler;
@group(0) @binding(7) var multiscattering_lut: texture_2d<f32>;
@group(0) @binding(8) var multiscattering_lut_sampler: sampler;
@group(0) @binding(9) var sky_view_lut: texture_2d<f32>;
@group(0) @binding(10) var sky_view_lut_sampler: sampler;
@group(0) @binding(11) var aerial_view_lut: texture_3d<f32>;
@group(0) @binding(12) var aerial_view_lut_sampler: sampler;
@group(0) @binding(14) var directional_shadow_texture: texture_depth_2d_array;
@group(0) @binding(15) var directional_shadow_sampler: sampler_comparison;
@group(0) @binding(16) var blue_noise_texture: texture_2d_array<f32>;
@group(0) @binding(17) var blue_noise_sampler: sampler;
@group(0) @binding(18) var<uniform> probe_transform_buffer: mat4x4<f32>;
@group(1) @binding(0) var<uniform> atmosphere: Atmosphere;
@group(1) @binding(1) var<uniform> settings: AtmosphereSettings;
@group(1) @binding(2) var<uniform> atmosphere_transforms: AtmosphereTransforms;
@group(1) @binding(3) var<uniform> view: View;
@group(1) @binding(4) var<uniform> lights: Lights;
@group(1) @binding(5) var transmittance_lut: texture_2d<f32>;
@group(1) @binding(6) var transmittance_lut_sampler: sampler;
@group(1) @binding(7) var multiscattering_lut: texture_2d<f32>;
@group(1) @binding(8) var multiscattering_lut_sampler: sampler;
@group(1) @binding(9) var sky_view_lut: texture_2d<f32>;
@group(1) @binding(10) var sky_view_lut_sampler: sampler;
@group(1) @binding(11) var aerial_view_lut: texture_3d<f32>;
@group(1) @binding(12) var aerial_view_lut_sampler: sampler;
@group(1) @binding(14) var directional_shadow_texture: texture_depth_2d_array;
@group(1) @binding(15) var directional_shadow_sampler: sampler_comparison;
@group(1) @binding(16) var blue_noise_texture: texture_2d_array<f32>;
@group(1) @binding(17) var blue_noise_sampler: sampler;
@group(1) @binding(18) var<uniform> probe_transform_buffer: mat4x4<f32>;
20 changes: 16 additions & 4 deletions crates/bevy_pbr/src/atmosphere/functions.wgsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#define_import_path bevy_pbr::atmosphere::functions

#import bevy_render::maths::{PI, HALF_PI, PI_2, fast_acos, fast_acos_4, fast_atan2}

#import bevy_pbr::shadows
#import bevy_pbr::atmosphere::{
types::Atmosphere,
bindings::{
Expand All @@ -14,9 +14,6 @@
transmittance_lut_r_mu_to_uv, transmittance_lut_uv_to_r_mu,
ray_intersects_ground, distance_to_top_atmosphere_boundary,
distance_to_bottom_atmosphere_boundary
},
shadows::{
get_shadow
}
}

Expand Down Expand Up @@ -246,6 +243,21 @@ fn sample_atmosphere(r: f32) -> AtmosphereSample {
return sample;
}

fn get_shadow(light_index: u32, P: vec3<f32>, ray_dir: vec3<f32>) -> f32 {
// For raymarched volumes, we can use the ray direction as the normal
// since we don't have surface normals
let world_normal = -ray_dir; // Point against ray direction
let world_pos = vec4<f32>((P + vec3<f32>(0.0, -atmosphere.bottom_radius, 0.0)) / settings.scene_units_to_m, 1.0);

// Get view space Z coordinate for cascade selection
let view_pos = view.view_from_world * world_pos;
let view_z = view_pos.z;

// Assuming we're using the first directional light (index 0)
let light = &lights.directional_lights[light_index];
return shadows::fetch_directional_shadow(light_index, world_pos, world_normal, view_z);
}

/// evaluates L_scat, equation 3 in the paper, which gives the total single-order scattering towards the view at a single point
fn sample_local_inscattering(local_atmosphere: AtmosphereSample, ray_dir: vec3<f32>, world_pos: vec3<f32>, shadow: bool) -> vec3<f32> {
let local_r = length(world_pos);
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_pbr/src/atmosphere/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use bevy_ecs::{
schedule::IntoScheduleConfigs,
system::Query,
};
use bevy_light::{DirectionalLight};
use bevy_light::DirectionalLight;
use bevy_math::{UVec2, UVec3, Vec3};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{
Expand All @@ -66,7 +66,7 @@ use bevy_core_pipeline::core_3d::{graph::Core3d, Camera3d};
use bevy_transform::components::Transform;
use resources::{
prepare_atmosphere_transforms, queue_render_sky_pipelines, AtmosphereTransforms,
RenderSkyBindGroupLayouts,
RenderSkyPipeline,
};
use tracing::warn;

Expand Down Expand Up @@ -138,11 +138,11 @@ impl Plugin for AtmospherePlugin {

render_app
.init_resource::<AtmosphereBindGroupLayouts>()
.init_resource::<RenderSkyBindGroupLayouts>()
.init_resource::<RenderSkyPipeline>()
.init_resource::<AtmosphereSamplers>()
.init_resource::<AtmospherePipelines>()
.init_resource::<AtmosphereTransforms>()
.init_resource::<SpecializedRenderPipelines<RenderSkyBindGroupLayouts>>()
.init_resource::<SpecializedRenderPipelines<RenderSkyPipeline>>()
.init_resource::<AtmosphereBuffer>()
.add_systems(
Render,
Expand Down
42 changes: 36 additions & 6 deletions crates/bevy_pbr/src/atmosphere/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ use bevy_render::{
view::{ViewTarget, ViewUniformOffset},
};

use crate::{resources::{AtmosphereEnvironmentMap, AtmosphereProbeBindGroups}, ViewLightsUniformOffset};
use crate::{
resources::{AtmosphereEnvironmentMap, AtmosphereProbeBindGroups},
MeshViewBindGroup, ViewEnvironmentMapUniformOffset, ViewFogUniformOffset,
ViewLightProbesUniformOffset, ViewLightsUniformOffset, ViewScreenSpaceReflectionsUniformOffset,
};

use super::{
resources::{
Expand Down Expand Up @@ -92,7 +96,7 @@ impl ViewNode for AtmosphereLutsNode {

luts_pass.set_pipeline(transmittance_lut_pipeline);
luts_pass.set_bind_group(
0,
1,
&bind_groups.transmittance_lut,
&[
atmosphere_uniforms_offset.index(),
Expand All @@ -106,7 +110,7 @@ impl ViewNode for AtmosphereLutsNode {

luts_pass.set_pipeline(multiscattering_lut_pipeline);
luts_pass.set_bind_group(
0,
1,
&bind_groups.multiscattering_lut,
&[
atmosphere_uniforms_offset.index(),
Expand All @@ -124,7 +128,7 @@ impl ViewNode for AtmosphereLutsNode {

luts_pass.set_pipeline(sky_view_lut_pipeline);
luts_pass.set_bind_group(
0,
1,
&bind_groups.sky_view_lut,
&[
atmosphere_uniforms_offset.index(),
Expand All @@ -141,7 +145,7 @@ impl ViewNode for AtmosphereLutsNode {

luts_pass.set_pipeline(aerial_view_lut_pipeline);
luts_pass.set_bind_group(
0,
1,
&bind_groups.aerial_view_lut,
&[
atmosphere_uniforms_offset.index(),
Expand Down Expand Up @@ -171,6 +175,13 @@ impl ViewNode for RenderSkyNode {
Read<ViewUniformOffset>,
Read<ViewLightsUniformOffset>,
Read<RenderSkyPipelineId>,
Read<MeshViewBindGroup>,
Read<ViewUniformOffset>,
Read<ViewLightsUniformOffset>,
Read<ViewFogUniformOffset>,
Read<ViewLightProbesUniformOffset>,
Read<ViewScreenSpaceReflectionsUniformOffset>,
Read<ViewEnvironmentMapUniformOffset>,
);

fn run<'w>(
Expand All @@ -186,6 +197,13 @@ impl ViewNode for RenderSkyNode {
view_uniforms_offset,
lights_uniforms_offset,
render_sky_pipeline_id,
view_bind_group,
view_uniform_offset,
view_lights_offset,
view_fog_offset,
view_light_probes_offset,
view_ssr_offset,
view_environment_map_offset,
): QueryItem<'w, '_, Self::ViewQuery>,
world: &'w World,
) -> Result<(), NodeRunError> {
Expand All @@ -210,6 +228,18 @@ impl ViewNode for RenderSkyNode {
render_sky_pass.set_pipeline(render_sky_pipeline);
render_sky_pass.set_bind_group(
0,
&view_bind_group.main,
&[
view_uniform_offset.offset,
view_lights_offset.offset,
view_fog_offset.offset,
**view_light_probes_offset,
**view_ssr_offset,
**view_environment_map_offset,
],
);
render_sky_pass.set_bind_group(
1,
&atmosphere_bind_groups.render_sky,
&[
atmosphere_uniforms_offset.index(),
Expand Down Expand Up @@ -291,7 +321,7 @@ impl Node for EnvironmentNode {

pass.set_pipeline(environment_pipeline);
pass.set_bind_group(
0,
1,
&bind_groups.environment,
&[
atmosphere_uniforms_offset.index(),
Expand Down
Loading
Loading