Skip to content

Commit

Permalink
Change OrthographicProjection update to use `Rect::from_center_half…
Browse files Browse the repository at this point in the history
…_size` in the calculation of `area`
  • Loading branch information
hukasu committed Feb 2, 2025
1 parent 55283bb commit 1b6ef8d
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions crates/bevy_render/src/camera/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,18 +602,18 @@ impl CameraProjection for OrthographicProjection {
}

fn update(&mut self, width: f32, height: f32) {
let (projection_width, projection_height) = match self.scaling_mode {
ScalingMode::WindowSize => (width, height),
let projection_dimensions = match self.scaling_mode {
ScalingMode::WindowSize => Vec2::new(width, height),
ScalingMode::AutoMin {
min_width,
min_height,
} => {
// Compare Pixels of current width and minimal height and Pixels of minimal width with current height.
// Then use bigger (min_height when true) as what it refers to (height when true) and calculate rest so it can't get under minimum.
if width * min_height > min_width * height {
(width * min_height / height, min_height)
Vec2::new(width * min_height / height, min_height)
} else {
(min_width, height * min_width / width)
Vec2::new(min_width, height * min_width / width)
}
}
ScalingMode::AutoMax {
Expand All @@ -623,29 +623,23 @@ impl CameraProjection for OrthographicProjection {
// Compare Pixels of current width and maximal height and Pixels of maximal width with current height.
// Then use smaller (max_height when true) as what it refers to (height when true) and calculate rest so it can't get over maximum.
if width * max_height < max_width * height {
(width * max_height / height, max_height)
Vec2::new(width * max_height / height, max_height)
} else {
(max_width, height * max_width / width)
Vec2::new(max_width, height * max_width / width)
}
}
ScalingMode::FixedVertical { viewport_height } => {
(width * viewport_height / height, viewport_height)
Vec2::new(width * viewport_height / height, viewport_height)
}
ScalingMode::FixedHorizontal { viewport_width } => {
(viewport_width, height * viewport_width / width)
Vec2::new(viewport_width, height * viewport_width / width)
}
ScalingMode::Fixed { width, height } => (width, height),
ScalingMode::Fixed { width, height } => Vec2::new(width, height),
};

let origin_x = projection_width * self.viewport_origin.x;
let origin_y = projection_height * self.viewport_origin.y;
let origin = projection_dimensions * self.viewport_origin;

self.area = Rect::new(
self.scale * -origin_x,
self.scale * -origin_y,
self.scale * (projection_width - origin_x),
self.scale * (projection_height - origin_y),
);
self.area = Rect::from_center_half_size(Vec2::default(), origin * self.scale);
}

fn far(&self) -> f32 {
Expand Down

0 comments on commit 1b6ef8d

Please sign in to comment.