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

Apply "snap 2D transforms to pixel" to viewport #93786

Merged
merged 2 commits into from
Sep 25, 2024

Conversation

alvinhochun
Copy link
Contributor

@alvinhochun alvinhochun commented Jun 30, 2024

We shall not leave the viewport transform to be rounded by the code for rounding canvas items. Since the viewport transform is inverse to the camera transform, we get incorrect rounding at the halfway point that misaligns the viewport and the canvas item which the camera is following.

Instead, reintroduce viewport rounding, but do it in a way that matches the rounding of canvas items. Also take into account the half-pixel offset of the centre point when viewport dimension is not divisible by two. For CanvasLayers that follows viewport, take into account the scale when rounding. Overall this should work better compared to the rounding in Godot 4.2 (and earlier).


Fixes #93712

This reimplements the viewport rounding removed from #87297.

I've tested this with the MRP there, with odd and even viewport sizes, and with a bunch of other scenarios with CanvasLayer following viewport and scaled.

@alvinhochun alvinhochun requested a review from a team as a code owner June 30, 2024 16:22
@Chaosus Chaosus added this to the 4.3 milestone Jun 30, 2024
@markdibarry
Copy link
Contributor

markdibarry commented Jul 16, 2024

Tested and can confirm that there's no jitter with odd-sized viewports. This does re-introduce the jitter for Parallax2D, though. Changing _camera_moved() to something like this fixes it, but if someone has a better suggestion I'd like to hear it. (Always annoying to add on more logic per frame if it's not necessary)

void Parallax2D::_camera_moved(const Transform2D &p_transform, const Point2 &p_screen_offset, const Point2 &p_adj_screen_pos) {
	if (!ignore_camera_scroll) {
		if (get_viewport() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) {
			Size2 vps = get_viewport_rect().size;
			Vector2 offset;
			offset.x = ((int)vps.width % 2) ? 0.0 : 0.5;
			offset.y = ((int)vps.height % 2) ? 0.0 : 0.5;
			set_screen_offset((p_adj_screen_pos + offset).floor());
		} else {
			set_screen_offset(p_adj_screen_pos);
		}
	}
}

@clayjohn clayjohn modified the milestones: 4.3, 4.4 Jul 24, 2024
@clayjohn
Copy link
Member

@alvinhochun Can you apply the change from Marks comment? After that, this should be good to merge.

@AThousandShips AThousandShips changed the title Apply snap 2D transforms to pixel to viewport Apply "snap 2D transforms to pixel" to viewport Sep 21, 2024
alvinhochun and others added 2 commits September 23, 2024 20:34
We shall not leave the viewport transform to be rounded by the code for
rounding canvas items. Since the viewport transform is inverse to the
camera transform, we get incorrect rounding at the halfway point that
misaligns the viewport and the canvas item which the camera is
following.

Instead, reintroduce viewport rounding, but do it in a way that matches
the rounding of canvas items. Also take into account the half-pixel
offset of the centre point when viewport dimension is not divisible by
two.  For `CanvasLayer`s that follows viewport, take into account the
scale when rounding. Overall this should work better compared to the
rounding in Godot 4.2 (and earlier).
@clayjohn
Copy link
Member

@markdibarry Can you confirm that this no longer introduces a regression in your testing project?

@markdibarry
Copy link
Contributor

Sorry for the late reply. I didn't notice any regressions with the test project. No jitter for odd sized viewports, parallax or no.
Of course, jitter still remains for drag margins and camera smoothing, but this is a good step forward!

@akien-mga akien-mga merged commit 73bf121 into godotengine:master Sep 25, 2024
19 checks passed
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2D Camera viewport rounding error with subpixel position of 0.5 with "Snap 2D Transforms to Pixel" enabled
5 participants