fix calls to as_rgba_linear (#3200)

# Objective

- After #3192, some places where `as_rgba_linear` was called were doing too many conversions

## Solution

- Fix the conversions
This commit is contained in:
François 2021-11-28 10:40:42 +00:00
parent e8412df021
commit 3de391be21
4 changed files with 21 additions and 12 deletions

View file

@ -90,7 +90,7 @@ impl RenderAsset for CustomMaterial {
extracted_asset: Self::ExtractedAsset,
(render_device, custom_pipeline): &mut SystemParamItem<Self::Param>,
) -> Result<Self::PreparedAsset, PrepareAssetError<Self::ExtractedAsset>> {
let color: Vec4 = extracted_asset.color.as_rgba_linear().into();
let color = Vec4::from_slice(&extracted_asset.color.as_linear_rgba_f32());
let buffer = render_device.create_buffer_with_data(&BufferInitDescriptor {
contents: color.as_std140().as_bytes(),
label: None,

View file

@ -238,7 +238,7 @@ impl RenderAsset for StandardMaterial {
};
let value = StandardMaterialUniformData {
base_color: material.base_color.as_rgba_linear().into(),
base_color: material.base_color.as_linear_rgba_f32().into(),
emissive: material.emissive.into(),
roughness: material.perceptual_roughness,
metallic: material.metallic,

View file

@ -509,7 +509,6 @@ pub fn prepare_lights(
) {
light_meta.view_gpu_lights.clear();
let ambient_color = ambient_light.color.as_rgba_linear() * ambient_light.brightness;
// Pre-calculate for PointLights
let cube_face_projection =
Mat4::perspective_infinite_reverse_rh(std::f32::consts::FRAC_PI_2, 1.0, 0.1);
@ -555,7 +554,8 @@ pub fn prepare_lights(
let mut view_lights = Vec::new();
let mut gpu_lights = GpuLights {
ambient_color: ambient_color.into(),
ambient_color: Vec4::from_slice(&ambient_light.color.as_linear_rgba_f32())
* ambient_light.brightness,
n_point_lights: point_lights.iter().len() as u32,
n_directional_lights: directional_lights.iter().len() as u32,
point_lights: [GpuPointLight::default(); MAX_POINT_LIGHTS],
@ -622,7 +622,7 @@ pub fn prepare_lights(
projection: cube_face_projection,
// premultiply color by intensity
// we don't use the alpha at all, so no reason to multiply only [0..3]
color: (light.color.as_rgba_linear() * light.intensity).into(),
color: Vec4::from_slice(&light.color.as_linear_rgba_f32()) * light.intensity,
radius: light.radius,
position: light.transform.translation,
inverse_square_range: 1.0 / (light.range * light.range),
@ -669,7 +669,7 @@ pub fn prepare_lights(
gpu_lights.directional_lights[i] = GpuDirectionalLight {
// premultiply color by intensity
// we don't use the alpha at all, so no reason to multiply only [0..3]
color: (light.color.as_rgba_linear() * intensity).into(),
color: Vec4::from_slice(&light.color.as_linear_rgba_f32()) * intensity,
dir_to_light,
// NOTE: * view is correct, it should not be view.inverse() here
view_projection: projection * view,

View file

@ -633,12 +633,21 @@ impl From<Vec4> for Color {
impl From<Color> for wgpu::Color {
fn from(color: Color) -> Self {
let color = color.as_rgba_linear();
if let Color::RgbaLinear {
red,
green,
blue,
alpha,
} = color.as_rgba_linear()
{
wgpu::Color {
r: color.r() as f64,
g: color.g() as f64,
b: color.b() as f64,
a: color.a() as f64,
r: red as f64,
g: green as f64,
b: blue as f64,
a: alpha as f64,
}
} else {
unreachable!()
}
}
}