-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Add zoom to orthographic example #2662
Add zoom to orthographic example #2662
Conversation
Should this zoom maths be added as a method on To clarify, I'm thinking something like: /// Center is relative to the screen plane
/// E.g. 0,0 is the bottom left corner
pub fn zoom_to(&mut self, center: Vec2, scale: f32);
pub fn scale_clipping(&mut self, scale: f32); |
I'm definitely in agreement with this :) Looks solid though! |
I'm not sure I understand exactly. In the example, zooming is performed by changing the scale of the projection and moving the camera itself. But we don't have access to the camera translation from within the projection. I can only see how it'd be possible to zoom in and out of the centre of the screen plane. |
Hmm yeah, true. The zoom method could return the relative transform based on the Vec2 as the fraction of the screen, then you'd update the camera's transform based on that. |
WDYT of the following signature |
Initial proposal:
Counter-proposal:
Changes:
|
Hi @alice-i-cecile and @DJMcNab , I have updated the code as per my interpretation of our previous discussion. Please take a look and let me know your thoughts. I've also added a rotation system to the example. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good for the happy path, but for many other cases this would be either badly wrong - or worse, subtly broken.
@@ -81,6 +82,19 @@ pub struct OrthographicProjection { | |||
pub depth_calculation: DepthCalculation, | |||
} | |||
|
|||
impl OrthographicProjection { | |||
pub fn zoom_to(&mut self, new_screen_center: Vec2, new_scale: f32, camera_transform: &mut Transform) { | |||
let screen_center_normalized = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not correctly handle the window_origin
property of self
.
let screen_center_normalized = | ||
new_screen_center * 2. - Vec2::ONE; | ||
let center = screen_center_normalized * Vec2::new(self.right, self.top); | ||
let world_pos = camera_transform.translation.truncate() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this ignores rotation of the camera, and moves it as if it were facing in the direction of the
I think the correct way to implement this would be to create a new Transform
with the translation corresponding to something like new_screen_center/self.size() * self.scale()
(taking into account the window_origin
correctly). You'd then apply this new transform to the passed in Transform
.
Correctly handling all this will lead to some gnarly code, but that's all the more reason to make this something we have built in.
(That is why I suggested the API which returns a Transform
, because it would have forced you to implement it correctly - I still think we should have this API, but the method with the easier name should do the application itself)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, having done some more reading I've realised I don't know enough about this to be sure. This might be correct here.
That is, the thing to do here is to add some tests which show it correctly handling all the available modes, with different rotations, scales and the like In theory, for a higher level test of this you should use |
Sorry for the delay, I have been very busy finishing up the quarter with work! Back and able to contribute with a bit more consistency. |
Hmm. I think that the smaller, scoped demo has clear value. Then, we can tackle a proper, general solution in another PR and update the example. |
@B-Janson I'm going to add the |
I want this but it seems stale. I recommend we close this an open a tracking issue for the example. |
Seconding that. Please close once an issue is up. |
This would conflict with #11022, we should probably just request an example there. |
Objective
Fixes #2580. Extends the orthogonal example to include zoom as suggested.
Solution