Return URect instead of (UVec2, UVec2) in Camera::physical_viewport_rect (#9085)

# Objective

Continue #7867 now that we have URect #7984
- Return `URect` instead of `(UVec2, UVec2)` in
`Camera::physical_viewport_rect`
 - Add `URect` and `IRect` to prelude

## Changelog

- Changed `Camera::physical_viewport_rect` return type from `(UVec2,
UVec2)` to `URect`
- `URect` and `IRect` were added to prelude

## Migration Guide

Before:

```rust
fn view_physical_camera_rect(camera_query: Query<&Camera>) {
    let camera = camera_query.single();
    let Some((min, max)) = camera.physical_viewport_rect() else { return };
    dbg!(min, max);
}
```

After:

```rust
fn view_physical_camera_rect(camera_query: Query<&Camera>) {
    let camera = camera_query.single();
    let Some(URect { min, max }) = camera.physical_viewport_rect() else { return };
    dbg!(min, max);
}
```
This commit is contained in:
Ame 2023-07-15 15:25:22 -06:00 committed by GitHub
parent 73457665ba
commit 7154b59438
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 12 deletions

View file

@ -1,6 +1,6 @@
use super::downsampling_pipeline::BloomUniforms;
use bevy_ecs::{prelude::Component, query::QueryItem, reflect::ReflectComponent};
use bevy_math::{UVec4, Vec4};
use bevy_math::{URect, UVec4, Vec4};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{extract_component::ExtractComponent, prelude::Camera};
@ -196,7 +196,7 @@ impl ExtractComponent for BloomSettings {
camera.is_active,
camera.hdr,
) {
(Some((origin, _)), Some(size), Some(target_size), true, true) => {
(Some(URect { min: origin, .. }), Some(size), Some(target_size), true, true) => {
let threshold = settings.prefilter_settings.threshold;
let threshold_softness = settings.prefilter_settings.threshold_softness;
let knee = threshold * threshold_softness.clamp(0.0, 1.0);

View file

@ -19,8 +19,8 @@ pub mod prelude {
#[doc(hidden)]
pub use crate::{
cubic_splines::{BSpline, Bezier, CardinalSpline, CubicGenerator, CubicSegment, Hermite},
BVec2, BVec3, BVec4, EulerRot, IVec2, IVec3, IVec4, Mat2, Mat3, Mat4, Quat, Ray, Rect,
UVec2, UVec3, UVec4, Vec2, Vec3, Vec4,
BVec2, BVec3, BVec4, EulerRot, IRect, IVec2, IVec3, IVec4, Mat2, Mat3, Mat4, Quat, Ray,
Rect, URect, UVec2, UVec3, UVec4, Vec2, Vec3, Vec4,
};
}

View file

@ -19,7 +19,7 @@ use bevy_ecs::{
system::{Commands, Query, Res, ResMut, Resource},
};
use bevy_log::warn;
use bevy_math::{Mat4, Ray, Rect, UVec2, UVec4, Vec2, Vec3};
use bevy_math::{Mat4, Ray, Rect, URect, UVec2, UVec4, Vec2, Vec3};
use bevy_reflect::prelude::*;
use bevy_transform::components::GlobalTransform;
use bevy_utils::{HashMap, HashSet};
@ -138,18 +138,18 @@ impl Camera {
Some((physical_size.as_dvec2() / scale).as_vec2())
}
/// The rendered physical bounds (minimum, maximum) of the camera. If the `viewport` field is
/// The rendered physical bounds [`URect`] of the camera. If the `viewport` field is
/// set to [`Some`], this will be the rect of that custom viewport. Otherwise it will default to
/// the full physical rect of the current [`RenderTarget`].
#[inline]
pub fn physical_viewport_rect(&self) -> Option<(UVec2, UVec2)> {
pub fn physical_viewport_rect(&self) -> Option<URect> {
let min = self
.viewport
.as_ref()
.map(|v| v.physical_position)
.unwrap_or(UVec2::ZERO);
let max = min + self.physical_viewport_size()?;
Some((min, max))
Some(URect { min, max })
}
/// The rendered logical bounds [`Rect`] of the camera. If the `viewport` field is set to
@ -157,7 +157,7 @@ impl Camera {
/// full logical rect of the current [`RenderTarget`].
#[inline]
pub fn logical_viewport_rect(&self) -> Option<Rect> {
let (min, max) = self.physical_viewport_rect()?;
let URect { min, max } = self.physical_viewport_rect()?;
Some(Rect {
min: self.to_logical(min)?,
max: self.to_logical(max)?,
@ -636,7 +636,14 @@ pub fn extract_cameras(
continue;
}
if let (Some((viewport_origin, _)), Some(viewport_size), Some(target_size)) = (
if let (
Some(URect {
min: viewport_origin,
..
}),
Some(viewport_size),
Some(target_size),
) = (
camera.physical_viewport_rect(),
camera.physical_viewport_size(),
camera.physical_target_size(),

View file

@ -16,7 +16,7 @@ use crate::{
use bevy_app::prelude::*;
use bevy_asset::{load_internal_asset, AssetEvent, Assets, Handle, HandleUntyped};
use bevy_ecs::prelude::*;
use bevy_math::{Mat4, Rect, UVec4, Vec2, Vec3, Vec4Swizzles};
use bevy_math::{Mat4, Rect, URect, UVec4, Vec2, Vec3, Vec4Swizzles};
use bevy_reflect::TypeUuid;
use bevy_render::texture::DEFAULT_IMAGE_HANDLE;
use bevy_render::{
@ -453,7 +453,14 @@ pub fn extract_default_ui_camera_view<T: Component>(
if matches!(camera_ui, Some(&UiCameraConfig { show_ui: false, .. })) {
continue;
}
if let (Some(logical_size), Some((physical_origin, _)), Some(physical_size)) = (
if let (
Some(logical_size),
Some(URect {
min: physical_origin,
..
}),
Some(physical_size),
) = (
camera.logical_viewport_size(),
camera.physical_viewport_rect(),
camera.physical_viewport_size(),