Skip to content
Closed
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
16 changes: 16 additions & 0 deletions crates/bevy_pbr/src/deferred/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,22 @@ impl SpecializedRenderPipeline for DeferredLightingLayout {
shader_defs.push("MULTIPLE_LIGHTMAPS_IN_ARRAY".into());
}

if cfg!(feature = "pbr_multi_layer_material_textures") {
shader_defs.push("PBR_MULTI_LAYER_MATERIAL_TEXTURES_SUPPORTED".into());
}

if cfg!(feature = "pbr_transmission_textures") {
shader_defs.push("PBR_TRANSMISSION_TEXTURES_SUPPORTED".into());
}

if cfg!(feature = "pbr_anisotropy_texture") {
shader_defs.push("PBR_ANISOTROPY_TEXTURE_SUPPORTED".into());
}

if cfg!(feature = "pbr_specular_textures") {
shader_defs.push("PBR_SPECULAR_TEXTURES_SUPPORTED".into());
}

#[cfg(all(feature = "webgl", target_arch = "wasm32", not(feature = "webgpu")))]
shader_defs.push("SIXTEEN_BYTE_ALIGNMENT".into());

Expand Down
11 changes: 11 additions & 0 deletions crates/bevy_pbr/src/pbr_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,17 @@ impl Material for StandardMaterial {
StandardMaterialKey::ANISOTROPY,
"STANDARD_MATERIAL_ANISOTROPY",
),
] {
if key.bind_group_data.contains(flags) {
shader_defs.push(shader_def.into());
}
}

if cfg!(feature = "pbr_multi_layer_material_textures") {
shader_defs.push("PBR_MULTI_LAYER_MATERIAL_TEXTURES_SUPPORTED".into());
}

for (flags, shader_def) in [
(
StandardMaterialKey::BASE_COLOR_UV,
"STANDARD_MATERIAL_BASE_COLOR_UV_B",
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_pbr/src/render/mesh_view_types.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ struct ScreenSpaceReflectionsSettings {
linear_march_exponent: f32,
bisection_steps: u32,
use_secant: u32,
samples: u32,
};

struct EnvironmentMapUniform {
Expand Down
17 changes: 11 additions & 6 deletions crates/bevy_pbr/src/render/pbr_ambient.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ fn ambient_light(
diffuse_color: vec3<f32>,
specular_color: vec3<f32>,
perceptual_roughness: f32,
occlusion: vec3<f32>,
diffuse_occlusion: vec3<f32>,
specular_occlusion: f32,
) -> vec3<f32> {
let diffuse_ambient = EnvBRDFApprox(diffuse_color, F_AB(1.0, NdotV));
let specular_ambient = EnvBRDFApprox(specular_color, F_AB(perceptual_roughness, NdotV));

let F_ab_diffuse = F_AB(1.0, NdotV);
let diffuse_ambient = diffuse_color * (F_ab_diffuse.x + F_ab_diffuse.y) * diffuse_occlusion;
let Fr = max(vec3(1.0 - perceptual_roughness), specular_color) - specular_color;
let kS = specular_color + Fr * pow(1.0 - NdotV, 5.0);
let F_ab_vals = F_AB(perceptual_roughness, NdotV);
let Ess = F_ab_vals.x + F_ab_vals.y;
// No real world material has specular values under 0.02, so we use this range as a
// "pre-baked specular occlusion" that extinguishes the fresnel term, for artistic control.
// See: https://google.github.io/filament/Filament.html#specularocclusion
let specular_occlusion = saturate(dot(specular_color, vec3(50.0 * 0.33)));
let pre_baked_specular_occlusion = saturate(dot(specular_color, vec3(50.0 * 0.33)));
let specular_ambient = kS * Ess * pre_baked_specular_occlusion * specular_occlusion;

return (diffuse_ambient + specular_ambient * specular_occlusion) * lights.ambient_color.rgb * occlusion;
return (diffuse_ambient + specular_ambient) * lights.ambient_color.rgb;
}
28 changes: 14 additions & 14 deletions crates/bevy_pbr/src/render/pbr_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ fn apply_pbr_lighting(
// NdotV = 1.0;
// F0 = vec3<f32>(0.0)
// diffuse_occlusion = vec3<f32>(1.0)
transmitted_light += ambient::ambient_light(diffuse_transmissive_lobe_world_position, -in.N, -in.V, 1.0, diffuse_transmissive_color, vec3<f32>(0.0), 1.0, vec3<f32>(1.0));
transmitted_light += ambient::ambient_light(diffuse_transmissive_lobe_world_position, -in.N, -in.V, 1.0, diffuse_transmissive_color, vec3<f32>(0.0), 1.0, vec3<f32>(1.0), 1.0);
#endif

// Diffuse indirect lighting can come from a variety of sources. The
Expand Down Expand Up @@ -613,23 +613,23 @@ fn apply_pbr_lighting(
let use_ssr = false;
#endif // SCREEN_SPACE_REFLECTIONS

if (!use_ssr) {
#ifdef STANDARD_MATERIAL_ANISOTROPY
var bent_normal_lighting_input = lighting_input;
bend_normal_for_anisotropy(&bent_normal_lighting_input);
let environment_map_lighting_input = &bent_normal_lighting_input;
var bent_normal_lighting_input = lighting_input;
bend_normal_for_anisotropy(&bent_normal_lighting_input);
let environment_map_lighting_input = &bent_normal_lighting_input;
#else // STANDARD_MATERIAL_ANISOTROPY
let environment_map_lighting_input = &lighting_input;
let environment_map_lighting_input = &lighting_input;
#endif // STANDARD_MATERIAL_ANISOTROPY

let environment_light = environment_map::environment_map_light(
environment_map_lighting_input,
&clusterable_object_index_ranges,
found_diffuse_indirect,
);
let environment_light = environment_map::environment_map_light(
environment_map_lighting_input,
&clusterable_object_index_ranges,
found_diffuse_indirect,
);

indirect_light += environment_light.diffuse * diffuse_occlusion +
environment_light.specular * specular_occlusion;
indirect_light += environment_light.diffuse * diffuse_occlusion;
if (!use_ssr) {
indirect_light += environment_light.specular * specular_occlusion;
}
#endif // ENVIRONMENT_MAP

Expand All @@ -642,7 +642,7 @@ fn apply_pbr_lighting(
let enable_ambient = true;
#endif // LIGHTMAP
if (enable_ambient) {
indirect_light += ambient::ambient_light(in.world_position, in.N, in.V, NdotV, diffuse_color, F0, perceptual_roughness, diffuse_occlusion);
indirect_light += ambient::ambient_light(in.world_position, in.N, in.V, NdotV, diffuse_color, F0, perceptual_roughness, diffuse_occlusion, specular_occlusion);
}

// we'll use the specular component of the transmitted environment
Expand Down
37 changes: 36 additions & 1 deletion crates/bevy_pbr/src/ssr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ pub struct ScreenSpaceReflections {
/// line-line intersection between the ray approach rate and the surface
/// gradient.
pub use_secant: bool,

/// The number of samples to take per pixel.
///
/// Higher values result in higher-quality reflections, especially for
/// rough materials, but take more GPU time.
pub samples: u32,
}

/// A version of [`ScreenSpaceReflections`] for upload to the GPU.
Expand All @@ -140,6 +146,7 @@ pub struct ScreenSpaceReflectionsUniform {
bisection_steps: u32,
/// A boolean converted to a `u32`.
use_secant: u32,
samples: u32,
}

/// The node in the render graph that traces screen space reflections.
Expand Down Expand Up @@ -180,6 +187,7 @@ pub struct ScreenSpaceReflectionsPipelineKey {
is_hdr: bool,
has_environment_maps: bool,
has_atmosphere: bool,
has_ssao: bool,
}

impl Plugin for ScreenSpaceReflectionsPlugin {
Expand Down Expand Up @@ -240,12 +248,13 @@ impl Default for ScreenSpaceReflections {
// <https://gist.github.com/h3r2tic/9c8356bdaefbe80b1a22ae0aaee192db?permalink_comment_id=4552149#gistcomment-4552149>.
fn default() -> Self {
Self {
perceptual_roughness_threshold: 0.1,
perceptual_roughness_threshold: 1.0,
linear_steps: 16,
bisection_steps: 4,
use_secant: true,
thickness: 0.25,
linear_march_exponent: 1.0,
samples: 1,
}
}
}
Expand Down Expand Up @@ -424,6 +433,7 @@ pub fn prepare_ssr_pipelines(
Has<NormalPrepass>,
Has<MotionVectorPrepass>,
Has<ExtractedAtmosphere>,
Has<crate::ssao::ScreenSpaceAmbientOcclusion>,
),
(
With<ScreenSpaceReflectionsUniform>,
Expand All @@ -439,6 +449,7 @@ pub fn prepare_ssr_pipelines(
has_normal_prepass,
has_motion_vector_prepass,
has_atmosphere,
has_ssao,
) in &views
{
// SSR is only supported in the deferred pipeline, which has no MSAA
Expand All @@ -465,6 +476,7 @@ pub fn prepare_ssr_pipelines(
is_hdr: extracted_view.hdr,
has_environment_maps,
has_atmosphere,
has_ssao,
},
);

Expand Down Expand Up @@ -555,6 +567,28 @@ impl SpecializedRenderPipeline for ScreenSpaceReflectionsPipeline {
#[cfg(not(target_arch = "wasm32"))]
shader_defs.push("USE_DEPTH_SAMPLERS".into());

if key.has_ssao {
shader_defs.push("SCREEN_SPACE_AMBIENT_OCCLUSION".into());
}

if cfg!(feature = "pbr_multi_layer_material_textures") {
shader_defs.push("PBR_MULTI_LAYER_MATERIAL_TEXTURES_SUPPORTED".into());
}

if cfg!(feature = "pbr_transmission_textures") {
shader_defs.push("PBR_TRANSMISSION_TEXTURES_SUPPORTED".into());
}

if cfg!(feature = "pbr_anisotropy_texture") {
shader_defs.push("PBR_ANISOTROPY_TEXTURE_SUPPORTED".into());
}

if cfg!(feature = "pbr_specular_textures") {
shader_defs.push("PBR_SPECULAR_TEXTURES_SUPPORTED".into());
}

shader_defs.push("STANDARD_MATERIAL_CLEARCOAT".into());

RenderPipelineDescriptor {
label: Some("SSR pipeline".into()),
layout,
Expand Down Expand Up @@ -587,6 +621,7 @@ impl From<ScreenSpaceReflections> for ScreenSpaceReflectionsUniform {
linear_march_exponent: settings.linear_march_exponent,
bisection_steps: settings.bisection_steps,
use_secant: settings.use_secant as u32,
samples: settings.samples,
}
}
}
Loading
Loading