Skip to content

Commit

Permalink
Merge branch 'main' into fix/upgrade-winit-0.30
Browse files Browse the repository at this point in the history
  • Loading branch information
pietrosophya authored May 26, 2024
2 parents 8f20b76 + 7d843e0 commit b5f98e5
Show file tree
Hide file tree
Showing 41 changed files with 729 additions and 74 deletions.
36 changes: 36 additions & 0 deletions crates/bevy_color/src/color_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ pub trait Mix: Sized {
}
}

/// Trait for returning a grayscale color of a provided lightness.
pub trait Gray: Mix + Sized {
/// A pure black color.
const BLACK: Self;
/// A pure white color.
const WHITE: Self;

/// Returns a grey color with the provided lightness from (0.0 - 1.0). 0 is black, 1 is white.
fn gray(lightness: f32) -> Self {
Self::BLACK.mix(&Self::WHITE, lightness)
}
}

/// Methods for manipulating alpha values.
pub trait Alpha: Sized {
/// Return a new version of this color with the given alpha value.
Expand Down Expand Up @@ -112,6 +125,8 @@ pub(crate) fn lerp_hue(a: f32, b: f32, t: f32) -> f32 {

#[cfg(test)]
mod tests {
use std::fmt::Debug;

use super::*;
use crate::{testing::assert_approx_eq, Hsla};

Expand Down Expand Up @@ -145,4 +160,25 @@ mod tests {
assert_approx_eq!(lerp_hue(350., 10., 0.5), 0., 0.001);
assert_approx_eq!(lerp_hue(350., 10., 0.75), 5., 0.001);
}

fn verify_gray<Col>()
where
Col: Gray + Debug + PartialEq,
{
assert_eq!(Col::gray(0.), Col::BLACK);
assert_eq!(Col::gray(1.), Col::WHITE);
}

#[test]
fn test_gray() {
verify_gray::<crate::Hsla>();
verify_gray::<crate::Hsva>();
verify_gray::<crate::Hwba>();
verify_gray::<crate::Laba>();
verify_gray::<crate::Lcha>();
verify_gray::<crate::LinearRgba>();
verify_gray::<crate::Oklaba>();
verify_gray::<crate::Oklcha>();
verify_gray::<crate::Xyza>();
}
}
7 changes: 6 additions & 1 deletion crates/bevy_color/src/hsla.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
Alpha, ColorToComponents, Hsva, Hue, Hwba, Lcha, LinearRgba, Luminance, Mix, Srgba,
Alpha, ColorToComponents, Gray, Hsva, Hue, Hwba, Lcha, LinearRgba, Luminance, Mix, Srgba,
StandardColor, Xyza,
};
use bevy_math::{Vec3, Vec4};
Expand Down Expand Up @@ -119,6 +119,11 @@ impl Mix for Hsla {
}
}

impl Gray for Hsla {
const BLACK: Self = Self::new(0., 0., 0., 1.);
const WHITE: Self = Self::new(0., 0., 1., 1.);
}

impl Alpha for Hsla {
#[inline]
fn with_alpha(&self, alpha: f32) -> Self {
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_color/src/hsva.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
Alpha, ColorToComponents, Hue, Hwba, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza,
Alpha, ColorToComponents, Gray, Hue, Hwba, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza,
};
use bevy_math::{Vec3, Vec4};
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -89,6 +89,11 @@ impl Mix for Hsva {
}
}

impl Gray for Hsva {
const BLACK: Self = Self::new(0., 0., 0., 1.);
const WHITE: Self = Self::new(0., 0., 1., 1.);
}

impl Alpha for Hsva {
#[inline]
fn with_alpha(&self, alpha: f32) -> Self {
Expand Down
9 changes: 8 additions & 1 deletion crates/bevy_color/src/hwba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
//! in [_HWB - A More Intuitive Hue-Based Color Model_] by _Smith et al_.
//!
//! [_HWB - A More Intuitive Hue-Based Color Model_]: https://web.archive.org/web/20240226005220/http://alvyray.com/Papers/CG/HWB_JGTv208.pdf
use crate::{Alpha, ColorToComponents, Hue, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza};
use crate::{
Alpha, ColorToComponents, Gray, Hue, Lcha, LinearRgba, Mix, Srgba, StandardColor, Xyza,
};
use bevy_math::{Vec3, Vec4};
use bevy_reflect::prelude::*;

Expand Down Expand Up @@ -91,6 +93,11 @@ impl Mix for Hwba {
}
}

impl Gray for Hwba {
const BLACK: Self = Self::new(0., 0., 1., 1.);
const WHITE: Self = Self::new(0., 1., 0., 1.);
}

impl Alpha for Hwba {
#[inline]
fn with_alpha(&self, alpha: f32) -> Self {
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_color/src/laba.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
impl_componentwise_vector_space, Alpha, ColorToComponents, Hsla, Hsva, Hwba, LinearRgba,
impl_componentwise_vector_space, Alpha, ColorToComponents, Gray, Hsla, Hsva, Hwba, LinearRgba,
Luminance, Mix, Oklaba, Srgba, StandardColor, Xyza,
};
use bevy_math::{Vec3, Vec4};
Expand Down Expand Up @@ -101,6 +101,11 @@ impl Mix for Laba {
}
}

impl Gray for Laba {
const BLACK: Self = Self::new(0., 0., 0., 1.);
const WHITE: Self = Self::new(1., 0., 0., 1.);
}

impl Alpha for Laba {
#[inline]
fn with_alpha(&self, alpha: f32) -> Self {
Expand Down
8 changes: 7 additions & 1 deletion crates/bevy_color/src/lcha.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
Alpha, ColorToComponents, Hue, Laba, LinearRgba, Luminance, Mix, Srgba, StandardColor, Xyza,
Alpha, ColorToComponents, Gray, Hue, Laba, LinearRgba, Luminance, Mix, Srgba, StandardColor,
Xyza,
};
use bevy_math::{Vec3, Vec4};
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -122,6 +123,11 @@ impl Mix for Lcha {
}
}

impl Gray for Lcha {
const BLACK: Self = Self::new(0.0, 0.0, 0.0000136603785, 1.0);
const WHITE: Self = Self::new(1.0, 0.0, 0.0000136603785, 1.0);
}

impl Alpha for Lcha {
#[inline]
fn with_alpha(&self, alpha: f32) -> Self {
Expand Down
19 changes: 6 additions & 13 deletions crates/bevy_color/src/linear_rgba.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
color_difference::EuclideanDistance, impl_componentwise_vector_space, Alpha, ColorToComponents,
Luminance, Mix, StandardColor,
Gray, Luminance, Mix, StandardColor,
};
use bevy_math::{Vec3, Vec4};
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -121,18 +121,6 @@ impl LinearRgba {
}
}

/// Construct a new [`LinearRgba`] color with the same value for all channels and an alpha of 1.0.
///
/// A value of 0.0 is black, and a value of 1.0 is white.
pub const fn gray(value: f32) -> Self {
Self {
red: value,
green: value,
blue: value,
alpha: 1.0,
}
}

/// Return a copy of this color with the red channel set to the given value.
pub const fn with_red(self, red: f32) -> Self {
Self { red, ..self }
Expand Down Expand Up @@ -236,6 +224,11 @@ impl Mix for LinearRgba {
}
}

impl Gray for LinearRgba {
const BLACK: Self = Self::BLACK;
const WHITE: Self = Self::WHITE;
}

impl Alpha for LinearRgba {
#[inline]
fn with_alpha(&self, alpha: f32) -> Self {
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_color/src/oklaba.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
color_difference::EuclideanDistance, impl_componentwise_vector_space, Alpha, ColorToComponents,
Hsla, Hsva, Hwba, Lcha, LinearRgba, Luminance, Mix, Srgba, StandardColor, Xyza,
Gray, Hsla, Hsva, Hwba, Lcha, LinearRgba, Luminance, Mix, Srgba, StandardColor, Xyza,
};
use bevy_math::{Vec3, Vec4};
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -101,6 +101,11 @@ impl Mix for Oklaba {
}
}

impl Gray for Oklaba {
const BLACK: Self = Self::new(0., 0., 0., 1.);
const WHITE: Self = Self::new(1.0, 0.0, 0.000000059604645, 1.0);
}

impl Alpha for Oklaba {
#[inline]
fn with_alpha(&self, alpha: f32) -> Self {
Expand Down
9 changes: 7 additions & 2 deletions crates/bevy_color/src/oklcha.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
color_difference::EuclideanDistance, Alpha, ColorToComponents, Hsla, Hsva, Hue, Hwba, Laba,
Lcha, LinearRgba, Luminance, Mix, Oklaba, Srgba, StandardColor, Xyza,
color_difference::EuclideanDistance, Alpha, ColorToComponents, Gray, Hsla, Hsva, Hue, Hwba,
Laba, Lcha, LinearRgba, Luminance, Mix, Oklaba, Srgba, StandardColor, Xyza,
};
use bevy_math::{Vec3, Vec4};
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -119,6 +119,11 @@ impl Mix for Oklcha {
}
}

impl Gray for Oklcha {
const BLACK: Self = Self::new(0., 0., 0., 1.);
const WHITE: Self = Self::new(1.0, 0.000000059604645, 90.0, 1.0);
}

impl Alpha for Oklcha {
#[inline]
fn with_alpha(&self, alpha: f32) -> Self {
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_color/src/srgba.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::color_difference::EuclideanDistance;
use crate::{
impl_componentwise_vector_space, Alpha, ColorToComponents, LinearRgba, Luminance, Mix,
impl_componentwise_vector_space, Alpha, ColorToComponents, Gray, LinearRgba, Luminance, Mix,
StandardColor, Xyza,
};
use bevy_math::{Vec3, Vec4};
Expand Down Expand Up @@ -314,6 +314,11 @@ impl EuclideanDistance for Srgba {
}
}

impl Gray for Srgba {
const BLACK: Self = Self::BLACK;
const WHITE: Self = Self::WHITE;
}

impl ColorToComponents for Srgba {
fn to_f32_array(self) -> [f32; 4] {
[self.red, self.green, self.blue, self.alpha]
Expand Down
7 changes: 6 additions & 1 deletion crates/bevy_color/src/xyza.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
impl_componentwise_vector_space, Alpha, ColorToComponents, LinearRgba, Luminance, Mix,
impl_componentwise_vector_space, Alpha, ColorToComponents, Gray, LinearRgba, Luminance, Mix,
StandardColor,
};
use bevy_math::{Vec3, Vec4};
Expand Down Expand Up @@ -144,6 +144,11 @@ impl Mix for Xyza {
}
}

impl Gray for Xyza {
const BLACK: Self = Self::new(0., 0., 0., 1.);
const WHITE: Self = Self::new(0.95047, 1.0, 1.08883, 1.0);
}

impl ColorToComponents for Xyza {
fn to_f32_array(self) -> [f32; 4] {
[self.x, self.y, self.z, self.alpha]
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/bloom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod downsampling_pipeline;
mod settings;
mod upsampling_pipeline;

use bevy_color::LinearRgba;
use bevy_color::{Gray, LinearRgba};
pub use settings::{BloomCompositeMode, BloomPrefilterSettings, BloomSettings};

use crate::{
Expand Down
34 changes: 33 additions & 1 deletion crates/bevy_gizmos/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::helpers::*;
use bevy_color::Color;
use bevy_math::primitives::{
Annulus, BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, Ellipse, Line2d, Plane2d, Polygon,
Polyline2d, Primitive2d, Rectangle, RegularPolygon, Segment2d, Triangle2d,
Polyline2d, Primitive2d, Rectangle, RegularPolygon, Rhombus, Segment2d, Triangle2d,
};
use bevy_math::{Dir2, Mat2, Vec2};

Expand Down Expand Up @@ -138,6 +138,38 @@ where
}
}

// rhombus 2d

impl<'w, 's, Config, Clear> GizmoPrimitive2d<Rhombus> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

fn primitive_2d(
&mut self,
primitive: Rhombus,
position: Vec2,
angle: f32,
color: impl Into<Color>,
) -> Self::Output<'_> {
if !self.enabled {
return;
}

let [a, b, c, d] =
[(1.0, 0.0), (0.0, 1.0), (-1.0, 0.0), (0.0, -1.0)].map(|(sign_x, sign_y)| {
Vec2::new(
primitive.half_diagonals.x * sign_x,
primitive.half_diagonals.y * sign_y,
)
});
let positions = [a, b, c, d, a].map(rotate_then_translate_2d(angle, position));
self.linestrip_2d(positions, color);
}
}

// capsule 2d

impl<'w, 's, Config, Clear> GizmoPrimitive2d<Capsule2d> for Gizmos<'w, 's, Config, Clear>
Expand Down
30 changes: 29 additions & 1 deletion crates/bevy_gizmos/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::f32::consts::TAU;
use bevy_color::Color;
use bevy_math::primitives::{
BoxedPolyline3d, Capsule3d, Cone, ConicalFrustum, Cuboid, Cylinder, Line3d, Plane3d,
Polyline3d, Primitive3d, Segment3d, Sphere, Tetrahedron, Torus,
Polyline3d, Primitive3d, Segment3d, Sphere, Tetrahedron, Torus, Triangle3d,
};
use bevy_math::{Dir3, Quat, Vec3};

Expand Down Expand Up @@ -405,6 +405,34 @@ where
}
}

// triangle 3d

impl<'w, 's, Config, Clear> GizmoPrimitive3d<Triangle3d> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

fn primitive_3d(
&mut self,
primitive: Triangle3d,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
) -> Self::Output<'_> {
if !self.enabled {
return;
}

let [a, b, c] = primitive.vertices;
self.linestrip(
[a, b, c, a].map(rotate_then_translate_3d(rotation, position)),
color,
);
}
}

// cuboid

impl<'w, 's, Config, Clear> GizmoPrimitive3d<Cuboid> for Gizmos<'w, 's, Config, Clear>
Expand Down
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
5 changes: 4 additions & 1 deletion crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ symphonia-vorbis = ["bevy_audio/symphonia-vorbis"]
symphonia-wav = ["bevy_audio/symphonia-wav"]

# Shader formats
shader_format_glsl = ["bevy_render/shader_format_glsl"]
shader_format_glsl = [
"bevy_render/shader_format_glsl",
"bevy_pbr?/shader_format_glsl",
]
shader_format_spirv = ["bevy_render/shader_format_spirv"]

serialize = [
Expand Down
Loading

0 comments on commit b5f98e5

Please sign in to comment.