diff --git a/crates/bevy_core_pipeline/src/bloom/settings.rs b/crates/bevy_core_pipeline/src/bloom/settings.rs index b2bb1c6396801..8d2d3bdd4d275 100644 --- a/crates/bevy_core_pipeline/src/bloom/settings.rs +++ b/crates/bevy_core_pipeline/src/bloom/settings.rs @@ -1,6 +1,6 @@ use super::downsampling_pipeline::BloomUniforms; use bevy_ecs::{prelude::Component, query::QueryItem, reflect::ReflectComponent}; -use bevy_math::{URect, UVec4, Vec4}; +use bevy_math::{AspectRatio, URect, UVec4, Vec4}; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_render::{extract_component::ExtractComponent, prelude::Camera}; @@ -210,7 +210,7 @@ impl ExtractComponent for BloomSettings { viewport: UVec4::new(origin.x, origin.y, size.x, size.y).as_vec4() / UVec4::new(target_size.x, target_size.y, target_size.x, target_size.y) .as_vec4(), - aspect: size.x as f32 / size.y as f32, + aspect: AspectRatio::from_pixels(size.x, size.y).into(), }; Some((settings.clone(), uniform)) diff --git a/crates/bevy_math/src/aspect_ratio.rs b/crates/bevy_math/src/aspect_ratio.rs new file mode 100644 index 0000000000000..1d18c690b8f09 --- /dev/null +++ b/crates/bevy_math/src/aspect_ratio.rs @@ -0,0 +1,25 @@ +//! Provides a simple aspect ratio struct to help with calculations. + +/// An `AspectRatio` is the ratio of width to height. +pub struct AspectRatio(f32); + +impl AspectRatio { + /// Create a new `AspectRatio` from a given `width` and `height`. + #[inline] + pub fn new(width: f32, height: f32) -> Self { + Self(width / height) + } + + /// Create a new `AspectRatio` from a given amount of `x` pixels and `y` pixels. + #[inline] + pub fn from_pixels(x: u32, y: u32) -> Self { + Self::new(x as f32, y as f32) + } +} + +impl From for f32 { + #[inline] + fn from(aspect_ratio: AspectRatio) -> Self { + aspect_ratio.0 + } +} diff --git a/crates/bevy_math/src/lib.rs b/crates/bevy_math/src/lib.rs index 4ad9dcc3731ba..c6fb416d7eacf 100644 --- a/crates/bevy_math/src/lib.rs +++ b/crates/bevy_math/src/lib.rs @@ -7,12 +7,14 @@ #![warn(missing_docs)] mod affine3; +mod aspect_ratio; pub mod cubic_splines; pub mod primitives; mod ray; mod rects; pub use affine3::*; +pub use aspect_ratio::AspectRatio; pub use ray::{Ray2d, Ray3d}; pub use rects::*; diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index bcded3f475fc1..c67d0cfe578cd 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -1,7 +1,9 @@ use std::collections::HashSet; use bevy_ecs::prelude::*; -use bevy_math::{Mat4, UVec2, UVec3, Vec2, Vec3, Vec3A, Vec3Swizzles, Vec4, Vec4Swizzles}; +use bevy_math::{ + AspectRatio, Mat4, UVec2, UVec3, Vec2, Vec3, Vec3A, Vec3Swizzles, Vec4, Vec4Swizzles, +}; use bevy_reflect::prelude::*; use bevy_render::{ camera::{Camera, CameraProjection}, @@ -732,7 +734,8 @@ impl ClusterConfig { ClusterConfig::FixedZ { total, z_slices, .. } => { - let aspect_ratio = screen_size.x as f32 / screen_size.y as f32; + let aspect_ratio: f32 = + AspectRatio::from_pixels(screen_size.x, screen_size.y).into(); let mut z_slices = *z_slices; if *total < z_slices { warn!("ClusterConfig has more z-slices than total clusters!"); diff --git a/crates/bevy_render/src/camera/projection.rs b/crates/bevy_render/src/camera/projection.rs index 912dc8009f37c..25b495b4f1dec 100644 --- a/crates/bevy_render/src/camera/projection.rs +++ b/crates/bevy_render/src/camera/projection.rs @@ -2,7 +2,7 @@ use std::marker::PhantomData; use bevy_app::{App, Plugin, PostStartup, PostUpdate}; use bevy_ecs::{prelude::*, reflect::ReflectComponent}; -use bevy_math::{Mat4, Rect, Vec2, Vec3A}; +use bevy_math::{AspectRatio, Mat4, Rect, Vec2, Vec3A}; use bevy_reflect::{ std_traits::ReflectDefault, GetTypeRegistration, Reflect, ReflectDeserialize, ReflectSerialize, }; @@ -155,7 +155,7 @@ impl CameraProjection for PerspectiveProjection { } fn update(&mut self, width: f32, height: f32) { - self.aspect_ratio = width / height; + self.aspect_ratio = AspectRatio::new(width, height).into(); } fn far(&self) -> f32 { diff --git a/crates/bevy_render/src/texture/image.rs b/crates/bevy_render/src/texture/image.rs index 327ede2be3ece..6e31b5892c760 100644 --- a/crates/bevy_render/src/texture/image.rs +++ b/crates/bevy_render/src/texture/image.rs @@ -14,7 +14,7 @@ use crate::{ use bevy_asset::Asset; use bevy_derive::{Deref, DerefMut}; use bevy_ecs::system::{lifetimeless::SRes, Resource, SystemParamItem}; -use bevy_math::{UVec2, Vec2}; +use bevy_math::{AspectRatio, UVec2, Vec2}; use bevy_reflect::Reflect; use serde::{Deserialize, Serialize}; use std::hash::Hash; @@ -539,10 +539,10 @@ impl Image { self.texture_descriptor.size.height } - /// Returns the aspect ratio (height/width) of a 2D image. + /// Returns the aspect ratio (width / height) of a 2D image. #[inline] - pub fn aspect_ratio(&self) -> f32 { - self.height() as f32 / self.width() as f32 + pub fn aspect_ratio(&self) -> AspectRatio { + AspectRatio::from_pixels(self.width(), self.height()) } /// Returns the size of a 2D image as f32.