Skip to content

Commit 09cb590

Browse files
committed
Improve OrthographicCamera consistency and usability (#6201)
# Objective - Terminology used in field names and docs aren't accurate - `window_origin` doesn't have any effect when `scaling_mode` is `ScalingMode::None` - `left`, `right`, `bottom`, and `top` are set automatically unless `scaling_mode` is `None`. Fields that only sometimes give feedback are confusing. - `ScalingMode::WindowSize` has no arguments, which is inconsistent with other `ScalingMode`s. 1 pixel = 1 world unit is also typically way too wide. - `OrthographicProjection` feels generally less streamlined than its `PerspectiveProjection` counterpart - Fixes #5818 - Fixes #6190 ## Solution - Improve consistency in `OrthographicProjection`'s public fields (they should either always give feedback or never give feedback). - Improve consistency in `ScalingMode`'s arguments - General usability improvements - Improve accuracy of terminology: - "Window" should refer to the physical window on the desktop - "Viewport" should refer to the component in the window that images are drawn on (typically all of it) - "View frustum" should refer to the volume captured by the projection --- ## Changelog ### Added - Added argument to `ScalingMode::WindowSize` that specifies the number of pixels that equals one world unit. - Added documentation for fields and enums ### Changed - Renamed `window_origin` to `viewport_origin`, which now: - Affects all `ScalingMode`s - Takes a fraction of the viewport's width and height instead of an enum - Removed `WindowOrigin` enum as it's obsolete - Renamed `ScalingMode::None` to `ScalingMode::Fixed`, which now: - Takes arguments to specify the projection size - Replaced `left`, `right`, `bottom`, and `top` fields with a single `area: Rect` - `scale` is now applied before updating `area`. Reading from it will take `scale` into account. - Documentation changes to make terminology more accurate and consistent ## Migration Guide - Change `window_origin` to `viewport_origin`; replace `WindowOrigin::Center` with `Vec2::new(0.5, 0.5)` and `WindowOrigin::BottomLeft` with `Vec2::new(0.0, 0.0)` - For shadow projections and such, replace `left`, `right`, `bottom`, and `top` with `area: Rect::new(left, bottom, right, top)` - For camera projections, remove l/r/b/t values from `OrthographicProjection` instantiations, as they no longer have any effect in any `ScalingMode` - Change `ScalingMode::None` to `ScalingMode::Fixed` - Replace manual changes of l/r/b/t with: - Arguments in `ScalingMode::Fixed` to specify size - `viewport_origin` to specify offset - Change `ScalingMode::WindowSize` to `ScalingMode::WindowSize(1.0)`
1 parent 26e00f9 commit 09cb590

File tree

3 files changed

+79
-64
lines changed

3 files changed

+79
-64
lines changed

crates/bevy_gltf/src/loader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,9 @@ fn load_node(
719719
let projection = match camera.projection() {
720720
gltf::camera::Projection::Orthographic(orthographic) => {
721721
let xmag = orthographic.xmag();
722-
let orthographic_projection: OrthographicProjection = OrthographicProjection {
723-
far: orthographic.zfar(),
722+
let orthographic_projection = OrthographicProjection {
724723
near: orthographic.znear(),
724+
far: orthographic.zfar(),
725725
scaling_mode: ScalingMode::FixedHorizontal(1.0),
726726
scale: xmag,
727727
..Default::default()

crates/bevy_render/src/camera/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ impl Plugin for CameraPlugin {
1818
app.register_type::<Camera>()
1919
.register_type::<Viewport>()
2020
.register_type::<Option<Viewport>>()
21-
.register_type::<WindowOrigin>()
2221
.register_type::<ScalingMode>()
2322
.register_type::<CameraRenderGraph>()
2423
.register_type::<RenderTarget>()

crates/bevy_render/src/camera/projection.rs

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::marker::PhantomData;
22

33
use bevy_app::{App, CoreSchedule, CoreSet, Plugin, StartupSet};
44
use bevy_ecs::{prelude::*, reflect::ReflectComponent};
5-
use bevy_math::Mat4;
5+
use bevy_math::{Mat4, Rect, Vec2};
66
use bevy_reflect::{
77
std_traits::ReflectDefault, FromReflect, GetTypeRegistration, Reflect, ReflectDeserialize,
88
ReflectSerialize,
@@ -169,57 +169,92 @@ impl Default for PerspectiveProjection {
169169
}
170170
}
171171

172-
// TODO: make this a component instead of a property
173-
#[derive(Debug, Clone, Reflect, FromReflect, Serialize, Deserialize)]
174-
#[reflect(Serialize, Deserialize)]
175-
pub enum WindowOrigin {
176-
Center,
177-
BottomLeft,
178-
}
179-
180172
#[derive(Debug, Clone, Reflect, FromReflect, Serialize, Deserialize)]
181173
#[reflect(Serialize, Deserialize)]
182174
pub enum ScalingMode {
183-
/// Manually specify left/right/top/bottom values.
184-
/// Ignore window resizing; the image will stretch.
185-
None,
186-
/// Match the window size. 1 world unit = 1 pixel.
187-
WindowSize,
175+
/// Manually specify the projection's size, ignoring window resizing. The image will stretch.
176+
/// Arguments are in world units.
177+
Fixed { width: f32, height: f32 },
178+
/// Match the viewport size.
179+
/// The argument is the number of pixels that equals one world unit.
180+
WindowSize(f32),
188181
/// Keeping the aspect ratio while the axes can't be smaller than given minimum.
189182
/// Arguments are in world units.
190183
AutoMin { min_width: f32, min_height: f32 },
191184
/// Keeping the aspect ratio while the axes can't be bigger than given maximum.
192185
/// Arguments are in world units.
193186
AutoMax { max_width: f32, max_height: f32 },
194-
/// Keep vertical axis constant; resize horizontal with aspect ratio.
195-
/// The argument is the desired height of the viewport in world units.
187+
/// Keep the projection's height constant; width will be adjusted to match aspect ratio.
188+
/// The argument is the desired height of the projection in world units.
196189
FixedVertical(f32),
197-
/// Keep horizontal axis constant; resize vertical with aspect ratio.
198-
/// The argument is the desired width of the viewport in world units.
190+
/// Keep the projection's width constant; height will be adjusted to match aspect ratio.
191+
/// The argument is the desired width of the projection in world units.
199192
FixedHorizontal(f32),
200193
}
201194

195+
/// Project a 3D space onto a 2D surface using parallel lines, i.e., unlike [`PerspectiveProjection`],
196+
/// the size of objects remains the same regardless of their distance to the camera.
197+
///
198+
/// The volume contained in the projection is called the *view frustum*. Since the viewport is rectangular
199+
/// and projection lines are parallel, the view frustum takes the shape of a cuboid.
200+
///
201+
/// Note that the scale of the projection and the apparent size of objects are inversely proportional.
202+
/// As the size of the projection increases, the size of objects decreases.
202203
#[derive(Component, Debug, Clone, Reflect, FromReflect)]
203204
#[reflect(Component, Default)]
204205
pub struct OrthographicProjection {
205-
pub left: f32,
206-
pub right: f32,
207-
pub bottom: f32,
208-
pub top: f32,
206+
/// The distance of the near clipping plane in world units.
207+
///
208+
/// Objects closer than this will not be rendered.
209+
///
210+
/// Defaults to `0.0`
209211
pub near: f32,
212+
/// The distance of the far clipping plane in world units.
213+
///
214+
/// Objects further than this will not be rendered.
215+
///
216+
/// Defaults to `1000.0`
210217
pub far: f32,
211-
pub window_origin: WindowOrigin,
218+
/// Specifies the origin of the viewport as a normalized position from 0 to 1, where (0, 0) is the bottom left
219+
/// and (1, 1) is the top right. This determines where the camera's position sits inside the viewport.
220+
///
221+
/// When the projection scales due to viewport resizing, the position of the camera, and thereby `viewport_origin`,
222+
/// remains at the same relative point.
223+
///
224+
/// Consequently, this is pivot point when scaling. With a bottom left pivot, the projection will expand
225+
/// upwards and to the right. With a top right pivot, the projection will expand downwards and to the left.
226+
/// Values in between will caused the projection to scale proportionally on each axis.
227+
///
228+
/// Defaults to `(0.5, 0.5)`, which makes scaling affect opposite sides equally, keeping the center
229+
/// point of the viewport centered.
230+
pub viewport_origin: Vec2,
231+
/// How the projection will scale when the viewport is resized.
232+
///
233+
/// Defaults to `ScalingMode::WindowScale(1.0)`
212234
pub scaling_mode: ScalingMode,
235+
/// Scales the projection in world units.
236+
///
237+
/// As scale increases, the apparent size of objects decreases, and vice versa.
238+
///
239+
/// Defaults to `1.0`
213240
pub scale: f32,
241+
/// The area that the projection covers relative to `viewport_origin`.
242+
///
243+
/// Bevy's [`camera_system`](crate::camera::camera_system) automatically
244+
/// updates this value when the viewport is resized depending on `OrthographicProjection`'s other fields.
245+
/// In this case, `area` should not be manually modified.
246+
///
247+
/// It may be necessary to set this manually for shadow projections and such.
248+
pub area: Rect,
214249
}
215250

216251
impl CameraProjection for OrthographicProjection {
217252
fn get_projection_matrix(&self) -> Mat4 {
218253
Mat4::orthographic_rh(
219-
self.left * self.scale,
220-
self.right * self.scale,
221-
self.bottom * self.scale,
222-
self.top * self.scale,
254+
self.area.min.x,
255+
self.area.max.x,
256+
self.area.min.y,
257+
self.area.max.y,
223258
// NOTE: near and far are swapped to invert the depth range from [0,1] to [1,0]
224259
// This is for interoperability with pipelines using infinite reverse perspective projections.
225260
self.far,
@@ -228,8 +263,8 @@ impl CameraProjection for OrthographicProjection {
228263
}
229264

230265
fn update(&mut self, width: f32, height: f32) {
231-
let (viewport_width, viewport_height) = match self.scaling_mode {
232-
ScalingMode::WindowSize => (width, height),
266+
let (projection_width, projection_height) = match self.scaling_mode {
267+
ScalingMode::WindowSize(pixel_scale) => (width / pixel_scale, height / pixel_scale),
233268
ScalingMode::AutoMin {
234269
min_width,
235270
min_height,
@@ -260,34 +295,18 @@ impl CameraProjection for OrthographicProjection {
260295
ScalingMode::FixedHorizontal(viewport_width) => {
261296
(viewport_width, height * viewport_width / width)
262297
}
263-
ScalingMode::None => return,
298+
ScalingMode::Fixed { width, height } => (width, height),
264299
};
265300

266-
match self.window_origin {
267-
WindowOrigin::Center => {
268-
let half_width = viewport_width / 2.0;
269-
let half_height = viewport_height / 2.0;
270-
self.left = -half_width;
271-
self.bottom = -half_height;
272-
self.right = half_width;
273-
self.top = half_height;
301+
let origin_x = projection_width * self.viewport_origin.x;
302+
let origin_y = projection_height * self.viewport_origin.y;
274303

275-
if let ScalingMode::WindowSize = self.scaling_mode {
276-
if self.scale == 1.0 {
277-
self.left = self.left.floor();
278-
self.bottom = self.bottom.floor();
279-
self.right = self.right.floor();
280-
self.top = self.top.floor();
281-
}
282-
}
283-
}
284-
WindowOrigin::BottomLeft => {
285-
self.left = 0.0;
286-
self.bottom = 0.0;
287-
self.right = viewport_width;
288-
self.top = viewport_height;
289-
}
290-
}
304+
self.area = Rect::new(
305+
self.scale * -origin_x,
306+
self.scale * -origin_y,
307+
self.scale * (projection_width - origin_x),
308+
self.scale * (projection_height - origin_y),
309+
);
291310
}
292311

293312
fn far(&self) -> f32 {
@@ -298,15 +317,12 @@ impl CameraProjection for OrthographicProjection {
298317
impl Default for OrthographicProjection {
299318
fn default() -> Self {
300319
OrthographicProjection {
301-
left: -1.0,
302-
right: 1.0,
303-
bottom: -1.0,
304-
top: 1.0,
320+
scale: 1.0,
305321
near: 0.0,
306322
far: 1000.0,
307-
window_origin: WindowOrigin::Center,
308-
scaling_mode: ScalingMode::WindowSize,
309-
scale: 1.0,
323+
viewport_origin: Vec2::new(0.5, 0.5),
324+
scaling_mode: ScalingMode::WindowSize(1.0),
325+
area: Rect::new(-1.0, -1.0, 1.0, 1.0),
310326
}
311327
}
312328
}

0 commit comments

Comments
 (0)