Skip to content

Commit

Permalink
Emissive is now LinearRgba on StandardMaterial (#13352)
Browse files Browse the repository at this point in the history
StandardMaterial stores a LinearRgba instead of a Color for emissive

Fixes #13212
  • Loading branch information
andristarr authored May 24, 2024
1 parent ec01c2d commit 44c0325
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 26 deletions.
3 changes: 1 addition & 2 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 = Color::from(scaled_emissive);
let emissive = base_emissive * material.emissive_strength().unwrap_or(1.0);

StandardMaterial {
base_color: Color::linear_rgba(color[0], color[1], color[2], color[3]),
Expand Down
18 changes: 9 additions & 9 deletions crates/bevy_pbr/src/pbr_material.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{
Expand Down Expand Up @@ -75,20 +75,20 @@ pub struct StandardMaterial {
/// This means that for a light emissive value, in darkness,
/// 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`
/// 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
/// 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.
///
/// 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
/// light up surrounding areas like a light source**,
/// 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.
/// A value of `0.0` means the emissive color is not affected by the camera exposure.
Expand Down Expand Up @@ -689,7 +689,7 @@ impl Default for StandardMaterial {
base_color: Color::WHITE,
base_color_channel: UvChannel::Uv0,
base_color_texture: None,
emissive: Color::BLACK,
emissive: LinearRgba::BLACK,
emissive_exposure_weight: 0.0,
emissive_channel: UvChannel::Uv0,
emissive_texture: None,
Expand Down Expand Up @@ -972,12 +972,12 @@ impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
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;

StandardMaterialUniform {
base_color: LinearRgba::from(self.base_color).to_f32_array().into(),
emissive: emissive.into(),
base_color: LinearRgba::from(self.base_color).to_vec4(),
emissive: self.emissive.to_vec4(),
roughness: self.perceptual_roughness,
metallic: self.metallic,
reflectance: self.reflectance,
Expand Down
10 changes: 8 additions & 2 deletions examples/3d/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ fn setup(
// circular base
commands.spawn(PbrBundle {
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)),
..default()
});
// cube
commands.spawn(PbrBundle {
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),
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/3d_viewport_to_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ fn setup(
commands.spawn((
PbrBundle {
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()
},
Ground,
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/anti_aliasing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ fn setup(
// Plane
commands.spawn(PbrBundle {
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()
});

Expand Down
6 changes: 3 additions & 3 deletions examples/3d/bloom_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ fn setup_scene(
));

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()
});
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()
});
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()
});
let material_non_emissive = materials.add(StandardMaterial {
Expand Down
6 changes: 3 additions & 3 deletions examples/3d/lighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn setup(
mesh: meshes.add(Sphere::new(0.1).mesh().uv(32, 18)),
material: materials.add(StandardMaterial {
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()
Expand Down Expand Up @@ -174,7 +174,7 @@ fn setup(
mesh: meshes.add(Capsule3d::new(0.1, 0.125)),
material: materials.add(StandardMaterial {
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()
Expand All @@ -199,7 +199,7 @@ fn setup(
mesh: meshes.add(Sphere::new(0.1).mesh().uv(32, 18)),
material: materials.add(StandardMaterial {
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()
Expand Down
4 changes: 2 additions & 2 deletions examples/3d/spotlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ fn setup(
let sphere_mesh_direction = meshes.add(Sphere::new(0.1).mesh().uv(32, 18));
let red_emissive = materials.add(StandardMaterial {
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()
});
let maroon_emissive = materials.add(StandardMaterial {
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()
});

Expand Down
3 changes: 1 addition & 2 deletions examples/3d/transmission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ fn setup(
green: scaled_white.green + scaled_orange.green,
blue: scaled_white.blue + scaled_orange.blue,
alpha: 1.0,
}
.into();
};

commands.spawn((
PbrBundle {
Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/iter_combinations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn generate_bodies(
mesh: meshes.add(Sphere::new(1.0).mesh().ico(5).unwrap()),
material: materials.add(StandardMaterial {
base_color: ORANGE_RED.into(),
emissive: (LinearRgba::from(ORANGE_RED) * 2.).into(),
emissive: LinearRgba::from(ORANGE_RED) * 2.,
..default()
}),
..default()
Expand Down

0 comments on commit 44c0325

Please sign in to comment.