diff --git a/Source/Editor/Viewport/EditorViewport.cs b/Source/Editor/Viewport/EditorViewport.cs
index c7b845c89..4af0a3ccf 100644
--- a/Source/Editor/Viewport/EditorViewport.cs
+++ b/Source/Editor/Viewport/EditorViewport.cs
@@ -1398,9 +1398,9 @@ namespace FlaxEditor.Viewport
Matrix.Multiply(ref v, ref p, out var ivp);
ivp.Invert();
- // Create near and far points
- var nearPoint = new Vector3(mousePosition, _nearPlane);
- var farPoint = new Vector3(mousePosition, _farPlane);
+ // Create near and far points, with device depth of 1 and 0 respectively
+ var nearPoint = new Vector3(mousePosition, 1.0f);
+ var farPoint = new Vector3(mousePosition, 0.0f);
viewport.Unproject(ref nearPoint, ref ivp, out nearPoint);
viewport.Unproject(ref farPoint, ref ivp, out farPoint);
diff --git a/Source/Engine/Core/Math/Viewport.cs b/Source/Engine/Core/Math/Viewport.cs
index cf3bff29c..ec5c61ef8 100644
--- a/Source/Engine/Core/Math/Viewport.cs
+++ b/Source/Engine/Core/Math/Viewport.cs
@@ -296,15 +296,13 @@ namespace FlaxEngine
///
/// Converts a screen space point into a corresponding point in world space.
///
- /// The vector to project.
+ /// The vector to project, screen uv and device depth.
/// The projection matrix.
/// The view matrix.
- /// The world matrix.
/// The unprojected Vector.
- public Vector3 Unproject(Vector3 source, Matrix projection, Matrix view, Matrix world)
+ public Vector3 Unproject(Vector3 source, Matrix projection, Matrix view)
{
- Matrix.Multiply(ref world, ref view, out Matrix matrix);
- Matrix.Multiply(ref matrix, ref projection, out matrix);
+ Matrix.Multiply(ref view, ref projection, out Matrix matrix);
Matrix.Invert(ref matrix, out matrix);
Unproject(ref source, ref matrix, out Vector3 vector);
@@ -314,8 +312,8 @@ namespace FlaxEngine
///
/// Converts a screen space point into a corresponding point in world space.
///
- /// The vector to project.
- /// An inverted combined WorldViewProjection matrix.
+ /// The vector to project, screen uv and device depth.
+ /// An inverted combined ViewProjection matrix.
/// The unprojected vector.
public void Unproject(ref Vector3 source, ref Matrix matrix, out Vector3 vector)
{
diff --git a/Source/Shaders/DebugDraw.shader b/Source/Shaders/DebugDraw.shader
index d10dbbf06..de4e079be 100644
--- a/Source/Shaders/DebugDraw.shader
+++ b/Source/Shaders/DebugDraw.shader
@@ -43,7 +43,7 @@ float4 PS(VS2PS input) : SV_Target
{
float sceneDepthDeviceZ = SceneDepthTexture.Load(int3(input.Position.xy, 0)).r;
float interpolatedDeviceZ = input.Position.z;
- clip(sceneDepthDeviceZ - interpolatedDeviceZ);
+ clip(interpolatedDeviceZ - sceneDepthDeviceZ);
}
#endif