Skip to content

Commit 29c632b

Browse files
miniexBluefingerDasLixou
authored
Add common aspect ratio constants and improve documentation (#15091)
Hello, I'd like to contribute to this project by adding some useful constants and improving the documentation for the AspectRatio struct. Here's a summary of the changes I've made: 1. Added new constants for common aspect ratios: - SIXTEEN_NINE (16:9) - FOUR_THREE (4:3) - ULTRAWIDE (21:9) 2. Enhanced the overall documentation: - Improved module-level documentation with an overview and use cases - Expanded explanation of the AspectRatio struct with examples - Added detailed descriptions and examples for all methods (both existing and new) - Included explanations for the newly introduced constant values - Added clarifications for From trait implementations These changes aim to make the AspectRatio API more user-friendly and easier to understand. The new constants provide convenient access to commonly used aspect ratios, which I believe will be helpful in many scenarios. --------- Co-authored-by: Gonçalo Rica Pais da Silva <bluefinger@gmail.com> Co-authored-by: Lixou <82600264+DasLixou@users.noreply.github.com>
1 parent dac4a5b commit 29c632b

File tree

5 files changed

+87
-14
lines changed

5 files changed

+87
-14
lines changed

crates/bevy_core_pipeline/src/bloom/settings.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ impl ExtractComponent for BloomSettings {
229229
viewport: UVec4::new(origin.x, origin.y, size.x, size.y).as_vec4()
230230
/ UVec4::new(target_size.x, target_size.y, target_size.x, target_size.y)
231231
.as_vec4(),
232-
aspect: AspectRatio::from_pixels(size.x, size.y).into(),
232+
aspect: AspectRatio::try_from_pixels(size.x, size.y)
233+
.expect("Valid screen size values for Bloom settings")
234+
.ratio(),
233235
uv_offset: settings.uv_offset,
234236
};
235237

crates/bevy_math/src/aspect_ratio.rs

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Provides a simple aspect ratio struct to help with calculations.
22
33
use crate::Vec2;
4+
use thiserror::Error;
45

56
#[cfg(feature = "bevy_reflect")]
67
use bevy_reflect::Reflect;
@@ -11,23 +12,74 @@ use bevy_reflect::Reflect;
1112
pub struct AspectRatio(f32);
1213

1314
impl AspectRatio {
14-
/// Create a new `AspectRatio` from a given `width` and `height`.
15+
/// Standard 16:9 aspect ratio
16+
pub const SIXTEEN_NINE: Self = Self(16.0 / 9.0);
17+
/// Standard 4:3 aspect ratio
18+
pub const FOUR_THREE: Self = Self(4.0 / 3.0);
19+
/// Standard 21:9 ultrawide aspect ratio
20+
pub const ULTRAWIDE: Self = Self(21.0 / 9.0);
21+
22+
/// Attempts to create a new [`AspectRatio`] from a given width and height.
23+
///
24+
/// # Errors
25+
///
26+
/// Returns an `Err` with `AspectRatioError` if:
27+
/// - Either width or height is zero (`AspectRatioError::Zero`)
28+
/// - Either width or height is infinite (`AspectRatioError::Infinite`)
29+
/// - Either width or height is NaN (`AspectRatioError::NaN`)
30+
#[inline]
31+
pub fn try_new(width: f32, height: f32) -> Result<Self, AspectRatioError> {
32+
match (width, height) {
33+
(w, h) if w == 0.0 || h == 0.0 => Err(AspectRatioError::Zero),
34+
(w, h) if w.is_infinite() || h.is_infinite() => Err(AspectRatioError::Infinite),
35+
(w, h) if w.is_nan() || h.is_nan() => Err(AspectRatioError::NaN),
36+
_ => Ok(Self(width / height)),
37+
}
38+
}
39+
40+
/// Attempts to create a new [`AspectRatio`] from a given amount of x pixels and y pixels.
41+
#[inline]
42+
pub fn try_from_pixels(x: u32, y: u32) -> Result<Self, AspectRatioError> {
43+
Self::try_new(x as f32, y as f32)
44+
}
45+
46+
/// Returns the aspect ratio as a f32 value.
47+
#[inline]
48+
pub fn ratio(&self) -> f32 {
49+
self.0
50+
}
51+
52+
/// Returns the inverse of this aspect ratio (height/width).
1553
#[inline]
16-
pub fn new(width: f32, height: f32) -> Self {
17-
Self(width / height)
54+
pub fn inverse(&self) -> Self {
55+
Self(1.0 / self.0)
1856
}
1957

20-
/// Create a new `AspectRatio` from a given amount of `x` pixels and `y` pixels.
58+
/// Returns true if the aspect ratio represents a landscape orientation.
2159
#[inline]
22-
pub fn from_pixels(x: u32, y: u32) -> Self {
23-
Self::new(x as f32, y as f32)
60+
pub fn is_landscape(&self) -> bool {
61+
self.0 > 1.0
62+
}
63+
64+
/// Returns true if the aspect ratio represents a portrait orientation.
65+
#[inline]
66+
pub fn is_portrait(&self) -> bool {
67+
self.0 < 1.0
68+
}
69+
70+
/// Returns true if the aspect ratio is exactly square.
71+
#[inline]
72+
pub fn is_square(&self) -> bool {
73+
self.0 == 1.0
2474
}
2575
}
2676

27-
impl From<Vec2> for AspectRatio {
77+
impl TryFrom<Vec2> for AspectRatio {
78+
type Error = AspectRatioError;
79+
2880
#[inline]
29-
fn from(value: Vec2) -> Self {
30-
Self::new(value.x, value.y)
81+
fn try_from(value: Vec2) -> Result<Self, Self::Error> {
82+
Self::try_new(value.x, value.y)
3183
}
3284
}
3385

@@ -37,3 +89,17 @@ impl From<AspectRatio> for f32 {
3789
aspect_ratio.0
3890
}
3991
}
92+
93+
/// An Error type for when [`super::AspectRatio`] is provided invalid width or height values
94+
#[derive(Error, Debug, PartialEq, Eq, Clone, Copy)]
95+
pub enum AspectRatioError {
96+
/// Error due to width or height having zero as a value.
97+
#[error("AspectRatio error: width or height is zero")]
98+
Zero,
99+
/// Error due towidth or height being infinite.
100+
#[error("AspectRatio error: width or height is infinite")]
101+
Infinite,
102+
/// Error due to width or height being Not a Number (NaN).
103+
#[error("AspectRatio error: width or height is NaN")]
104+
NaN,
105+
}

crates/bevy_pbr/src/cluster/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ impl ClusterConfig {
257257
ClusterConfig::FixedZ {
258258
total, z_slices, ..
259259
} => {
260-
let aspect_ratio: f32 =
261-
AspectRatio::from_pixels(screen_size.x, screen_size.y).into();
260+
let aspect_ratio: f32 = AspectRatio::try_from_pixels(screen_size.x, screen_size.y)
261+
.expect("Failed to calculate aspect ratio for Cluster: screen dimensions must be positive, non-zero values")
262+
.ratio();
262263
let mut z_slices = *z_slices;
263264
if *total < z_slices {
264265
warn!("ClusterConfig has more z-slices than total clusters!");

crates/bevy_render/src/camera/projection.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ impl CameraProjection for PerspectiveProjection {
190190
}
191191

192192
fn update(&mut self, width: f32, height: f32) {
193-
self.aspect_ratio = AspectRatio::new(width, height).into();
193+
self.aspect_ratio = AspectRatio::try_new(width, height)
194+
.expect("Failed to update PerspectiveProjection: width and height must be positive, non-zero values")
195+
.ratio();
194196
}
195197

196198
fn far(&self) -> f32 {

crates/bevy_render/src/texture/image.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,9 @@ impl Image {
666666
/// Returns the aspect ratio (width / height) of a 2D image.
667667
#[inline]
668668
pub fn aspect_ratio(&self) -> AspectRatio {
669-
AspectRatio::from_pixels(self.width(), self.height())
669+
AspectRatio::try_from_pixels(self.width(), self.height()).expect(
670+
"Failed to calculate aspect ratio: Image dimensions must be positive, non-zero values",
671+
)
670672
}
671673

672674
/// Returns the size of a 2D image as f32.

0 commit comments

Comments
 (0)