mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
Add Camera::viewport_to_world_2d
(#6557)
# Objective Add a simpler and less expensive 2D variant of `viewport_to_world`. Co-authored-by: devil-ira <justthecooldude@gmail.com>
This commit is contained in:
parent
39e14a4a40
commit
6b4795c428
1 changed files with 19 additions and 0 deletions
|
@ -248,6 +248,25 @@ impl Camera {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a 2D world position computed from a position on this [`Camera`]'s viewport.
|
||||||
|
///
|
||||||
|
/// Useful for 2D cameras and other cameras with an orthographic projection pointing along the Z axis.
|
||||||
|
///
|
||||||
|
/// To get the world space coordinates with Normalized Device Coordinates, you should use
|
||||||
|
/// [`ndc_to_world`](Self::ndc_to_world).
|
||||||
|
pub fn viewport_to_world_2d(
|
||||||
|
&self,
|
||||||
|
camera_transform: &GlobalTransform,
|
||||||
|
viewport_position: Vec2,
|
||||||
|
) -> Option<Vec2> {
|
||||||
|
let target_size = self.logical_viewport_size()?;
|
||||||
|
let ndc = viewport_position * 2. / target_size - Vec2::ONE;
|
||||||
|
|
||||||
|
let world_near_plane = self.ndc_to_world(camera_transform, ndc.extend(1.))?;
|
||||||
|
|
||||||
|
Some(world_near_plane.truncate())
|
||||||
|
}
|
||||||
|
|
||||||
/// Given a position in world space, use the camera's viewport to compute the Normalized Device Coordinates.
|
/// Given a position in world space, use the camera's viewport to compute the Normalized Device Coordinates.
|
||||||
///
|
///
|
||||||
/// When the position is within the viewport the values returned will be between -1.0 and 1.0 on the X and Y axes,
|
/// When the position is within the viewport the values returned will be between -1.0 and 1.0 on the X and Y axes,
|
||||||
|
|
Loading…
Reference in a new issue