From 3de391be21666c90661066ca395c63813352b4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sun, 28 Nov 2021 10:40:42 +0000 Subject: [PATCH] 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 --- examples/shader/custom_shader_pipelined.rs | 2 +- pipelined/bevy_pbr2/src/material.rs | 2 +- pipelined/bevy_pbr2/src/render/light.rs | 8 ++++---- pipelined/bevy_render2/src/color/mod.rs | 21 +++++++++++++++------ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/examples/shader/custom_shader_pipelined.rs b/examples/shader/custom_shader_pipelined.rs index dab60f7e6d..f046b3184c 100644 --- a/examples/shader/custom_shader_pipelined.rs +++ b/examples/shader/custom_shader_pipelined.rs @@ -90,7 +90,7 @@ impl RenderAsset for CustomMaterial { extracted_asset: Self::ExtractedAsset, (render_device, custom_pipeline): &mut SystemParamItem, ) -> Result> { - 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, diff --git a/pipelined/bevy_pbr2/src/material.rs b/pipelined/bevy_pbr2/src/material.rs index c2b4c4f767..a94483f774 100644 --- a/pipelined/bevy_pbr2/src/material.rs +++ b/pipelined/bevy_pbr2/src/material.rs @@ -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, diff --git a/pipelined/bevy_pbr2/src/render/light.rs b/pipelined/bevy_pbr2/src/render/light.rs index a1a5f2673b..dff2c21b59 100644 --- a/pipelined/bevy_pbr2/src/render/light.rs +++ b/pipelined/bevy_pbr2/src/render/light.rs @@ -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, diff --git a/pipelined/bevy_render2/src/color/mod.rs b/pipelined/bevy_render2/src/color/mod.rs index c229da03a5..5a3816e205 100644 --- a/pipelined/bevy_render2/src/color/mod.rs +++ b/pipelined/bevy_render2/src/color/mod.rs @@ -633,12 +633,21 @@ impl From for Color { impl From for wgpu::Color { fn from(color: Color) -> Self { - let color = 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, + if let Color::RgbaLinear { + red, + green, + blue, + alpha, + } = color.as_rgba_linear() + { + wgpu::Color { + r: red as f64, + g: green as f64, + b: blue as f64, + a: alpha as f64, + } + } else { + unreachable!() } } }