Skip to content

Commit dca740e

Browse files
authored
Fix clear color in CompositingSpace::Srgb (#23963)
# Objective Split off from #23803, Fix `ClearColor` with `CompositingSpace::Srgb`. ## Solution Convert clear color to Srgb ## Testing Code from #9213 (comment): ```rs //! use bevy::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins) .insert_resource(ClearColor(Color::srgba(0., 0.5, 0., 1.))) .add_systems(Startup, |mut commands: Commands| { commands.spawn((Camera2d, CompositingSpace::Srgb)); commands.spawn(( Sprite::from_color(Color::WHITE.with_alpha(0.5), Vec2::new(512., 512.)), Transform::from_xyz(-256., 0., 0.), )); commands.spawn(( Sprite::from_color(Color::BLACK.with_alpha(0.5), Vec2::new(512., 512.)), Transform::from_xyz(256., 0., 0.), )); }) .run(); } ``` Before: <img width="1410" height="878" alt="屏幕截图_20260414_120259" src="https://github.com/user-attachments/assets/199e2832-d0ba-46fe-835a-8775c2a4adc6" /> After: <img width="1410" height="878" alt="屏幕截图_20260414_115624" src="https://github.com/user-attachments/assets/f4b26170-3a78-44f1-917d-8f061da83ad5" />
1 parent dd27fe7 commit dca740e

3 files changed

Lines changed: 22 additions & 16 deletions

File tree

crates/bevy_camera/src/components.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ pub enum CompositingSpace {
9595
/// Gamma-encoded blending. Matches most image editors. Uses default sRGB target.
9696
#[default]
9797
Srgb,
98-
/// Linear light blending. Physically correct. Requires [`Hdr`].
98+
/// Linear light blending. Physically correct.
9999
Linear,
100-
/// Perceptually uniform blending. Often smoother gradients. Requires [`Hdr`].
100+
/// Perceptually uniform blending. Often smoother gradients. Requires [`Hdr`] because its value can be outside [0, 1].
101101
Oklab,
102102
}

crates/bevy_color/src/srgba.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,18 @@ impl From<Srgba> for Xyza {
415415
}
416416
}
417417

418+
#[cfg(feature = "wgpu-types")]
419+
impl From<Srgba> for wgpu_types::Color {
420+
fn from(color: Srgba) -> Self {
421+
wgpu_types::Color {
422+
r: color.red as f64,
423+
g: color.green as f64,
424+
b: color.blue as f64,
425+
a: color.alpha as f64,
426+
}
427+
}
428+
}
429+
418430
/// Error returned if a hex string could not be parsed as a color.
419431
#[derive(Debug, Error, PartialEq, Eq)]
420432
pub enum HexColorError {

crates/bevy_render/src/view/mod.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
};
2727
use alloc::sync::{Arc, Weak};
2828
use bevy_app::{App, Plugin};
29-
use bevy_color::{LinearRgba, Oklaba};
29+
use bevy_color::{LinearRgba, Oklaba, Srgba};
3030
use bevy_derive::{Deref, DerefMut};
3131
use bevy_ecs::prelude::*;
3232
use bevy_image::ToExtents;
@@ -1177,19 +1177,13 @@ pub fn prepare_view_targets(
11771177
};
11781178

11791179
// Convert clear color to the format expected by the main texture
1180-
let converted_clear_color: Option<WgpuColor> = clear_color.map(|color| {
1181-
let linear: LinearRgba = color.into();
1182-
if camera
1183-
.compositing_space
1184-
.is_some_and(|s| s == CompositingSpace::Oklab)
1185-
{
1186-
// Main texture stores Oklab; convert linear RGB to Oklab for correct clear
1187-
let oklab: Oklaba = linear.into();
1188-
oklab.into()
1189-
} else {
1190-
linear.into()
1191-
}
1192-
});
1180+
let converted_clear_color: Option<WgpuColor> =
1181+
clear_color.map(|color| match camera.compositing_space {
1182+
// If main texture stores Oklab or Srgb, convert Color to it for correct clear.
1183+
Some(CompositingSpace::Oklab) => Oklaba::from(color).into(),
1184+
Some(CompositingSpace::Srgb) => Srgba::from(color).into(),
1185+
Some(CompositingSpace::Linear) | None => LinearRgba::from(color).into(),
1186+
});
11931187

11941188
let key: MainTextureKey = (
11951189
camera.target.clone(),

0 commit comments

Comments
 (0)