Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce AspectRatio struct #10368

Merged
merged 4 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/bloom/settings.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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::new(size.x as f32, size.y as f32).into(),
};

Some((settings.clone(), uniform))
Expand Down
26 changes: 26 additions & 0 deletions crates/bevy_math/src/aspect_ratio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! 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)
}
}

impl From<(f32, f32)> for AspectRatio {
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
fn from((width, height): (f32, f32)) -> Self {
Self::new(width, height)
}
}

impl From<AspectRatio> for f32 {
#[inline]
fn from(aspect_ratio: AspectRatio) -> Self {
aspect_ratio.0
}
}
2 changes: 2 additions & 0 deletions crates/bevy_math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#![warn(missing_docs)]

mod affine3;
mod aspect_ratio;
pub mod cubic_splines;
mod ray;
mod rects;

pub use affine3::*;
pub use aspect_ratio::AspectRatio;
pub use ray::Ray;
pub use rects::*;

Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -719,7 +721,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::new(screen_size.x as f32, screen_size.y as f32).into();
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
let mut z_slices = *z_slices;
if *total < z_slices {
warn!("ClusterConfig has more z-slices than total clusters!");
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_render/src/camera/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
self.height() as f32 / self.width() as f32
AspectRatio::new(self.width() as f32, self.height() as f32).into()
}

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