@@ -8,7 +8,7 @@ use bevy_asset::Handle;
88use bevy_derive:: Deref ;
99use bevy_ecs:: { component:: Component , entity:: Entity , reflect:: ReflectComponent } ;
1010use bevy_image:: Image ;
11- use bevy_math:: { ops, Dir3 , FloatOrd , Mat4 , Ray3d , Rect , URect , UVec2 , Vec2 , Vec3 } ;
11+ use bevy_math:: { ops, Dir3 , FloatOrd , Mat4 , Ray3d , Rect , URect , UVec2 , Vec2 , Vec3 , Vec3A } ;
1212use bevy_reflect:: prelude:: * ;
1313use bevy_transform:: components:: { GlobalTransform , Transform } ;
1414use bevy_window:: { NormalizedWindowRef , WindowRef } ;
@@ -509,10 +509,10 @@ impl Camera {
509509 . ok_or ( ViewportConversionError :: InvalidData ) ?;
510510 // NDC z-values outside of 0 < z < 1 are outside the (implicit) camera frustum and are thus not in viewport-space
511511 if ndc_space_coords. z < 0.0 {
512- return Err ( ViewportConversionError :: PastNearPlane ) ;
512+ return Err ( ViewportConversionError :: PastFarPlane ) ;
513513 }
514514 if ndc_space_coords. z > 1.0 {
515- return Err ( ViewportConversionError :: PastFarPlane ) ;
515+ return Err ( ViewportConversionError :: PastNearPlane ) ;
516516 }
517517
518518 // Flip the Y co-ordinate origin from the bottom to the top.
@@ -547,10 +547,10 @@ impl Camera {
547547 . ok_or ( ViewportConversionError :: InvalidData ) ?;
548548 // NDC z-values outside of 0 < z < 1 are outside the (implicit) camera frustum and are thus not in viewport-space
549549 if ndc_space_coords. z < 0.0 {
550- return Err ( ViewportConversionError :: PastNearPlane ) ;
550+ return Err ( ViewportConversionError :: PastFarPlane ) ;
551551 }
552552 if ndc_space_coords. z > 1.0 {
553- return Err ( ViewportConversionError :: PastFarPlane ) ;
553+ return Err ( ViewportConversionError :: PastNearPlane ) ;
554554 }
555555
556556 // Stretching ndc depth to value via near plane and negating result to be in positive room again.
@@ -686,10 +686,10 @@ impl Camera {
686686 Ok ( world_near_plane. truncate ( ) )
687687 }
688688
689- /// Given a position in world space, use the camera's viewport to compute the Normalized Device Coordinates.
689+ /// Given a point in world space, use the camera's viewport to compute the Normalized Device Coordinates of the point .
690690 ///
691- /// When the position is within the viewport the values returned will be between -1.0 and 1.0 on the X and Y axes,
692- /// and between 0.0 and 1.0 on the Z axis.
691+ /// When the point is within the viewport the values returned will be between -1.0 (bottom left) and 1.0 (top right)
692+ /// on the X and Y axes, and between 0.0 (far) and 1.0 (near) on the Z axis.
693693 /// To get the coordinates in the render target's viewport dimensions, you should use
694694 /// [`world_to_viewport`](Self::world_to_viewport).
695695 ///
@@ -699,17 +699,16 @@ impl Camera {
699699 /// # Panics
700700 ///
701701 /// Will panic if the `camera_transform` contains `NAN` and the `glam_assert` feature is enabled.
702- pub fn world_to_ndc (
702+ pub fn world_to_ndc < V : Into < Vec3A > + From < Vec3A > > (
703703 & self ,
704704 camera_transform : & GlobalTransform ,
705- world_position : Vec3 ,
706- ) -> Option < Vec3 > {
707- // Build a transformation matrix to convert from world space to NDC using camera data
708- let clip_from_world: Mat4 =
709- self . computed . clip_from_view * camera_transform. to_matrix ( ) . inverse ( ) ;
710- let ndc_space_coords: Vec3 = clip_from_world. project_point3 ( world_position) ;
705+ world_point : V ,
706+ ) -> Option < V > {
707+ let view_from_world = camera_transform. affine ( ) . inverse ( ) ;
708+ let view_point = view_from_world. transform_point3a ( world_point. into ( ) ) ;
709+ let ndc_point = self . computed . clip_from_view . project_point3a ( view_point) ;
711710
712- ( !ndc_space_coords . is_nan ( ) ) . then_some ( ndc_space_coords )
711+ ( !ndc_point . is_nan ( ) ) . then_some ( ndc_point . into ( ) )
713712 }
714713
715714 /// Given a position in Normalized Device Coordinates,
0 commit comments