Emissive is now LinearRgba on StandardMaterial (#13352)

StandardMaterial stores a LinearRgba instead of a Color for emissive

Fixes #13212
This commit is contained in:
andristarr 2024-05-24 19:23:35 +02:00 committed by GitHub
parent ec01c2dc45
commit 44c0325ecd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 36 additions and 26 deletions

View File

@ -995,8 +995,7 @@ fn load_material(
// We need to operate in the Linear color space and be willing to exceed 1.0 in our channels // We need to operate in the Linear color space and be willing to exceed 1.0 in our channels
let base_emissive = LinearRgba::rgb(emissive[0], emissive[1], emissive[2]); let base_emissive = LinearRgba::rgb(emissive[0], emissive[1], emissive[2]);
let scaled_emissive = base_emissive * material.emissive_strength().unwrap_or(1.0); let emissive = base_emissive * material.emissive_strength().unwrap_or(1.0);
let emissive = Color::from(scaled_emissive);
StandardMaterial { StandardMaterial {
base_color: Color::linear_rgba(color[0], color[1], color[2], color[3]), base_color: Color::linear_rgba(color[0], color[1], color[2], color[3]),

View File

@ -1,5 +1,5 @@
use bevy_asset::Asset; use bevy_asset::Asset;
use bevy_color::Alpha; use bevy_color::{Alpha, ColorToComponents};
use bevy_math::{Affine2, Affine3, Mat2, Mat3, Vec2, Vec3, Vec4}; use bevy_math::{Affine2, Affine3, Mat2, Mat3, Vec2, Vec3, Vec4};
use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{ use bevy_render::{
@ -75,20 +75,20 @@ pub struct StandardMaterial {
/// This means that for a light emissive value, in darkness, /// This means that for a light emissive value, in darkness,
/// you will mostly see the emissive component. /// you will mostly see the emissive component.
/// ///
/// The default emissive color is [`Color::BLACK`], which doesn't add anything to the material color. /// The default emissive color is [`LinearRgba::BLACK`], which doesn't add anything to the material color.
/// ///
/// To increase emissive strength, channel values for `emissive` /// To increase emissive strength, channel values for `emissive`
/// colors can exceed `1.0`. For instance, a `base_color` of /// colors can exceed `1.0`. For instance, a `base_color` of
/// `Color::linear_rgb(1.0, 0.0, 0.0)` represents the brightest /// `LinearRgba::rgb(1.0, 0.0, 0.0)` represents the brightest
/// red for objects that reflect light, but an emissive color /// red for objects that reflect light, but an emissive color
/// like `Color::linear_rgb(1000.0, 0.0, 0.0)` can be used to create /// like `LinearRgba::rgb(1000.0, 0.0, 0.0)` can be used to create
/// intensely bright red emissive effects. /// intensely bright red emissive effects.
/// ///
/// Increasing the emissive strength of the color will impact visual effects /// Increasing the emissive strength of the color will impact visual effects
/// like bloom, but it's important to note that **an emissive material won't /// like bloom, but it's important to note that **an emissive material won't
/// light up surrounding areas like a light source**, /// light up surrounding areas like a light source**,
/// it just adds a value to the color seen on screen. /// it just adds a value to the color seen on screen.
pub emissive: Color, pub emissive: LinearRgba,
/// The weight in which the camera exposure influences the 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. /// A value of `0.0` means the emissive color is not affected by the camera exposure.
@ -689,7 +689,7 @@ impl Default for StandardMaterial {
base_color: Color::WHITE, base_color: Color::WHITE,
base_color_channel: UvChannel::Uv0, base_color_channel: UvChannel::Uv0,
base_color_texture: None, base_color_texture: None,
emissive: Color::BLACK, emissive: LinearRgba::BLACK,
emissive_exposure_weight: 0.0, emissive_exposure_weight: 0.0,
emissive_channel: UvChannel::Uv0, emissive_channel: UvChannel::Uv0,
emissive_texture: None, emissive_texture: None,
@ -972,12 +972,12 @@ impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
flags |= StandardMaterialFlags::ATTENUATION_ENABLED; flags |= StandardMaterialFlags::ATTENUATION_ENABLED;
} }
let mut emissive = LinearRgba::from(self.emissive).to_f32_array(); let mut emissive = self.emissive.to_vec4();
emissive[3] = self.emissive_exposure_weight; emissive[3] = self.emissive_exposure_weight;
StandardMaterialUniform { StandardMaterialUniform {
base_color: LinearRgba::from(self.base_color).to_f32_array().into(), base_color: LinearRgba::from(self.base_color).to_vec4(),
emissive: emissive.into(), emissive: self.emissive.to_vec4(),
roughness: self.perceptual_roughness, roughness: self.perceptual_roughness,
metallic: self.metallic, metallic: self.metallic,
reflectance: self.reflectance, reflectance: self.reflectance,

View File

@ -18,14 +18,20 @@ fn setup(
// circular base // circular base
commands.spawn(PbrBundle { commands.spawn(PbrBundle {
mesh: meshes.add(Circle::new(4.0)), mesh: meshes.add(Circle::new(4.0)),
material: materials.add(Color::WHITE), material: materials.add(StandardMaterial {
base_color: Color::WHITE,
..Default::default()
}),
transform: Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)), transform: Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
..default() ..default()
}); });
// cube // cube
commands.spawn(PbrBundle { commands.spawn(PbrBundle {
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)), mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0)),
material: materials.add(Color::srgb_u8(124, 144, 255)), material: materials.add(StandardMaterial {
base_color: Srgba::rgb_u8(124, 144, 255).into(),
..Default::default()
}),
transform: Transform::from_xyz(0.0, 0.5, 0.0), transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default() ..default()
}); });

View File

@ -52,7 +52,10 @@ fn setup(
commands.spawn(( commands.spawn((
PbrBundle { PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(20., 20.)), mesh: meshes.add(Plane3d::default().mesh().size(20., 20.)),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)), material: materials.add(StandardMaterial {
base_color: LinearRgba::rgb(0.3, 0.5, 0.3).into(),
..default()
}),
..default() ..default()
}, },
Ground, Ground,

View File

@ -261,7 +261,10 @@ fn setup(
// Plane // Plane
commands.spawn(PbrBundle { commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)), mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)),
material: materials.add(Color::srgb(0.1, 0.2, 0.1)), material: materials.add(StandardMaterial {
base_color: LinearRgba::rgb(0.1, 0.2, 0.1).into(),
..Default::default()
}),
..default() ..default()
}); });

View File

@ -41,15 +41,15 @@ fn setup_scene(
)); ));
let material_emissive1 = materials.add(StandardMaterial { let material_emissive1 = materials.add(StandardMaterial {
emissive: Color::linear_rgb(13.99, 5.32, 2.0), // 4. Put something bright in a dark environment to see the effect emissive: LinearRgba::rgb(13.99, 5.32, 2.0), // 4. Put something bright in a dark environment to see the effect
..default() ..default()
}); });
let material_emissive2 = materials.add(StandardMaterial { let material_emissive2 = materials.add(StandardMaterial {
emissive: Color::linear_rgb(2.0, 13.99, 5.32), emissive: LinearRgba::rgb(2.0, 13.99, 5.32),
..default() ..default()
}); });
let material_emissive3 = materials.add(StandardMaterial { let material_emissive3 = materials.add(StandardMaterial {
emissive: Color::linear_rgb(5.32, 2.0, 13.99), emissive: LinearRgba::rgb(5.32, 2.0, 13.99),
..default() ..default()
}); });
let material_non_emissive = materials.add(StandardMaterial { let material_non_emissive = materials.add(StandardMaterial {

View File

@ -146,7 +146,7 @@ fn setup(
mesh: meshes.add(Sphere::new(0.1).mesh().uv(32, 18)), mesh: meshes.add(Sphere::new(0.1).mesh().uv(32, 18)),
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: RED.into(), base_color: RED.into(),
emissive: Color::linear_rgba(4.0, 0.0, 0.0, 0.0), emissive: LinearRgba::new(4.0, 0.0, 0.0, 0.0),
..default() ..default()
}), }),
..default() ..default()
@ -174,7 +174,7 @@ fn setup(
mesh: meshes.add(Capsule3d::new(0.1, 0.125)), mesh: meshes.add(Capsule3d::new(0.1, 0.125)),
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: LIME.into(), base_color: LIME.into(),
emissive: Color::linear_rgba(0.0, 4.0, 0.0, 0.0), emissive: LinearRgba::new(0.0, 4.0, 0.0, 0.0),
..default() ..default()
}), }),
..default() ..default()
@ -199,7 +199,7 @@ fn setup(
mesh: meshes.add(Sphere::new(0.1).mesh().uv(32, 18)), mesh: meshes.add(Sphere::new(0.1).mesh().uv(32, 18)),
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: BLUE.into(), base_color: BLUE.into(),
emissive: Color::linear_rgba(0.0, 0.0, 713.0, 0.0), emissive: LinearRgba::new(0.0, 0.0, 713.0, 0.0),
..default() ..default()
}), }),
..default() ..default()

View File

@ -79,12 +79,12 @@ fn setup(
let sphere_mesh_direction = meshes.add(Sphere::new(0.1).mesh().uv(32, 18)); let sphere_mesh_direction = meshes.add(Sphere::new(0.1).mesh().uv(32, 18));
let red_emissive = materials.add(StandardMaterial { let red_emissive = materials.add(StandardMaterial {
base_color: RED.into(), base_color: RED.into(),
emissive: Color::linear_rgba(1.0, 0.0, 0.0, 0.0), emissive: LinearRgba::new(1.0, 0.0, 0.0, 0.0),
..default() ..default()
}); });
let maroon_emissive = materials.add(StandardMaterial { let maroon_emissive = materials.add(StandardMaterial {
base_color: MAROON.into(), base_color: MAROON.into(),
emissive: Color::linear_rgba(0.369, 0.0, 0.0, 0.0), emissive: LinearRgba::new(0.369, 0.0, 0.0, 0.0),
..default() ..default()
}); });

View File

@ -144,8 +144,7 @@ fn setup(
green: scaled_white.green + scaled_orange.green, green: scaled_white.green + scaled_orange.green,
blue: scaled_white.blue + scaled_orange.blue, blue: scaled_white.blue + scaled_orange.blue,
alpha: 1.0, alpha: 1.0,
} };
.into();
commands.spawn(( commands.spawn((
PbrBundle { PbrBundle {

View File

@ -99,7 +99,7 @@ fn generate_bodies(
mesh: meshes.add(Sphere::new(1.0).mesh().ico(5).unwrap()), mesh: meshes.add(Sphere::new(1.0).mesh().ico(5).unwrap()),
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: ORANGE_RED.into(), base_color: ORANGE_RED.into(),
emissive: (LinearRgba::from(ORANGE_RED) * 2.).into(), emissive: LinearRgba::from(ORANGE_RED) * 2.,
..default() ..default()
}), }),
..default() ..default()