mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 15:14:50 +00:00
Introduce AspectRatio struct (#10368)
# Objective - Fix an inconsistency in the calculation of aspect ratio's. - Fixes #10288 ## Solution - Created an intermediate `AspectRatio` struct, as suggested in the issue. This is currently just used in any places where aspect ratio calculations happen, to prevent doing it wrong. In my and @mamekoro 's opinion, it would be better if this was used instead of a normal `f32` in various places, but I didn't want to make too many changes to begin with. ## Migration Guide - Anywhere where you are currently expecting a f32 when getting aspect ratios, you will now receive a `AspectRatio` struct. this still holds the same value. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
This commit is contained in:
parent
f12e90670a
commit
6b9cd57956
6 changed files with 40 additions and 10 deletions
|
@ -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))
|
||||
|
|
25
crates/bevy_math/src/aspect_ratio.rs
Normal file
25
crates/bevy_math/src/aspect_ratio.rs
Normal file
|
@ -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<AspectRatio> for f32 {
|
||||
#[inline]
|
||||
fn from(aspect_ratio: AspectRatio) -> Self {
|
||||
aspect_ratio.0
|
||||
}
|
||||
}
|
|
@ -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::*;
|
||||
|
||||
|
|
|
@ -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!");
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue