Skip to content

Commit

Permalink
add emissive exposure weight to standard material
Browse files Browse the repository at this point in the history
  • Loading branch information
geckoxx committed May 15, 2024
1 parent f91fd32 commit 6641f59
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
13 changes: 12 additions & 1 deletion crates/bevy_pbr/src/pbr_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ pub struct StandardMaterial {
/// it just adds a value to the color seen on screen.
pub emissive: Color,

/// The weight in which the camera exposure influences the emissive color.
/// A value of `0.0` means the emissive color is not affected by the camera exposure.
/// In opposition, a value of `1.0` means the emissive color is multiplied by the camera exposure.
///
/// Defaults to `0.0`
pub emissive_exposure_weight: f32,

/// The UV channel to use for the [`StandardMaterial::emissive_texture`].
///
/// Defaults to [`UvChannel::Uv0`].
Expand Down Expand Up @@ -683,6 +690,7 @@ impl Default for StandardMaterial {
base_color_channel: UvChannel::Uv0,
base_color_texture: None,
emissive: Color::BLACK,
emissive_exposure_weight: 0.0,
emissive_channel: UvChannel::Uv0,
emissive_texture: None,
// Matches Blender's default roughness.
Expand Down Expand Up @@ -964,9 +972,12 @@ impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
flags |= StandardMaterialFlags::ATTENUATION_ENABLED;
}

let mut emissive = LinearRgba::from(self.emissive).to_f32_array();
emissive[3] = self.emissive_exposure_weight;

StandardMaterialUniform {
base_color: LinearRgba::from(self.base_color).to_f32_array().into(),
emissive: LinearRgba::from(self.emissive).to_f32_array().into(),
emissive: emissive.into(),
roughness: self.perceptual_roughness,
metallic: self.metallic,
reflectance: self.reflectance,
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_pbr/src/render/pbr_fragment.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ fn pbr_input_from_standard_material(
pbr_input.material.alpha_cutoff = pbr_bindings::material.alpha_cutoff;

// emissive
// TODO use .a for exposure compensation in HDR
var emissive: vec4<f32> = pbr_bindings::material.emissive;
#ifdef VERTEX_UVS
if ((pbr_bindings::material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_EMISSIVE_TEXTURE_BIT) != 0u) {
Expand All @@ -191,7 +190,7 @@ fn pbr_input_from_standard_material(
uv,
#endif
bias,
).rgb, 1.0);
).rgb, emissive.a);
}
#endif
pbr_input.material.emissive = emissive;
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_pbr/src/render/pbr_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ fn apply_pbr_lighting(
emissive_light = emissive_light * (0.04 + (1.0 - 0.04) * pow(1.0 - clearcoat_NdotV, 5.0));
#endif

emissive_light = emissive_light * mix(1.0, view_bindings::view.exposure, emissive.a);

#ifdef STANDARD_MATERIAL_SPECULAR_TRANSMISSION
transmitted_light += transmission::specular_transmissive_light(in.world_position, in.frag_coord.xyz, view_z, in.N, in.V, F0, ior, thickness, perceptual_roughness, specular_transmissive_color, specular_transmitted_environment_light).rgb;

Expand All @@ -585,7 +587,7 @@ fn apply_pbr_lighting(

// Total light
output_color = vec4<f32>(
view_bindings::view.exposure * (transmitted_light + direct_light + indirect_light + emissive_light),
(view_bindings::view.exposure * (transmitted_light + direct_light + indirect_light)) + emissive_light,
output_color.a
);

Expand Down

0 comments on commit 6641f59

Please sign in to comment.