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

[Merged by Bors] - OrthographicProjection: place origin at integer pixel with WindowSize scaling mode #4085

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
OrthographicProjection: place origin at integer pixel with WindowSize…
… scaling mode

One way to avoid texture atlas bleeding is to ensure that every vertex is
placed at an integer pixel coordinate. This is a particularly appealing
solution for regular structures like tile maps.

Doing so is currently harder than necessary when the WindowSize scaling
mode and Center origin are used: For odd window width or height, the
origin of the coordinate system is placed in the middle of a pixel at
some .5 offset.

Avoid this issue by rounding the half width and height values.
  • Loading branch information
neocturne committed Mar 3, 2022
commit 6fa4d1adba7510f076e9f75e9ed0856c8919aa9c
10 changes: 6 additions & 4 deletions crates/bevy_render/src/camera/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ impl CameraProjection for OrthographicProjection {
fn update(&mut self, width: f32, height: f32) {
match (&self.scaling_mode, &self.window_origin) {
(ScalingMode::WindowSize, WindowOrigin::Center) => {
let half_width = width / 2.0;
let half_height = height / 2.0;
self.left = -half_width;
let half_width = (width / 2.0).round();
let half_height = (height / 2.0).round();
// Assign left and bottom such that (right-left)==width and (top-bottom)==height
// still hold with with rounded half_width and half_height
self.left = half_width - width;
neocturne marked this conversation as resolved.
Show resolved Hide resolved
self.right = half_width;
self.top = half_height;
self.bottom = -half_height;
self.bottom = half_height - height;
neocturne marked this conversation as resolved.
Show resolved Hide resolved
}
(ScalingMode::WindowSize, WindowOrigin::BottomLeft) => {
self.left = 0.0;
Expand Down