From ab4743fdb16789e0b17b8f3bd916737f98afa11a Mon Sep 17 00:00:00 2001 From: ExMatics HydrogenC <33123710+HydrogenC@users.noreply.github.com> Date: Wed, 5 Jun 2024 18:20:38 +0800 Subject: [PATCH] Add more comditional compiling --- Source/Editor/Gizmo/SelectionOutline.cs | 6 +- Source/Editor/Viewport/EditorViewport.cs | 5 + Source/Engine/Core/Config.h | 3 - Source/Engine/Core/Math/BoundingFrustum.cpp | 17 +- Source/Engine/Core/Math/BoundingFrustum.cs | 16 ++ Source/Engine/Core/Math/Matrix.cpp | 28 ++- Source/Engine/Core/Math/Matrix.cs | 159 ++++++++++-------- .../Tools/Flax.Build/Build/ProjectTarget.cs | 4 + 8 files changed, 152 insertions(+), 86 deletions(-) diff --git a/Source/Editor/Gizmo/SelectionOutline.cs b/Source/Editor/Gizmo/SelectionOutline.cs index 59859be5f..92bc7825f 100644 --- a/Source/Editor/Gizmo/SelectionOutline.cs +++ b/Source/Editor/Gizmo/SelectionOutline.cs @@ -145,7 +145,11 @@ namespace FlaxEditor.Gizmo _material.SetParameterValue("OutlineColor0", _color0); _material.SetParameterValue("OutlineColor1", _color1); _material.SetParameterValue("CustomDepth", customDepth); - _material.SetParameterValue("ViewInfo", new Float4(1.0f / projection.M11, 1.0f / projection.M22, near / (far - near), (far * near) / (far - near) / far)); +#if FLAX_REVERSE_Z + _material.SetParameterValue("ViewInfo", new Float4(1.0f / projection.M11, 1.0f / projection.M22, -near / (far - near), (far * near) / (far - near) / far)); +#else + _material.SetParameterValue("ViewInfo", new Float4(1.0f / projection.M11, 1.0f / projection.M22, far / (far - near), -(far * near) / (far - near) / far)); +#endif Renderer.DrawPostFxMaterial(context, ref renderContext, _material, output, input.View()); // Cleanup diff --git a/Source/Editor/Viewport/EditorViewport.cs b/Source/Editor/Viewport/EditorViewport.cs index 4af0a3ccf..b2c5f50e2 100644 --- a/Source/Editor/Viewport/EditorViewport.cs +++ b/Source/Editor/Viewport/EditorViewport.cs @@ -1399,8 +1399,13 @@ namespace FlaxEditor.Viewport ivp.Invert(); // Create near and far points, with device depth of 1 and 0 respectively +#if FLAX_REVERSE_Z var nearPoint = new Vector3(mousePosition, 1.0f); var farPoint = new Vector3(mousePosition, 0.0f); +#else + var nearPoint = new Vector3(mousePosition, 0.0f); + var farPoint = new Vector3(mousePosition, 1.0f); +#endif viewport.Unproject(ref nearPoint, ref ivp, out nearPoint); viewport.Unproject(ref farPoint, ref ivp, out farPoint); diff --git a/Source/Engine/Core/Config.h b/Source/Engine/Core/Config.h index d2b4d666d..dc4d76290 100644 --- a/Source/Engine/Core/Config.h +++ b/Source/Engine/Core/Config.h @@ -41,9 +41,6 @@ // Enable/disable assertion for Engine low layers #define ENABLE_ASSERTION_LOW_LAYERS ENABLE_ASSERTION && (BUILD_DEBUG || FLAX_TESTS) -// Enable reverse z -#define FLAX_REVERSE_Z 1 - // Scripting API defines (see C++ scripting documentation for more info) #define API_ENUM(...) #define API_CLASS(...) diff --git a/Source/Engine/Core/Math/BoundingFrustum.cpp b/Source/Engine/Core/Math/BoundingFrustum.cpp index 6b8854859..38d54a95e 100644 --- a/Source/Engine/Core/Math/BoundingFrustum.cpp +++ b/Source/Engine/Core/Math/BoundingFrustum.cpp @@ -46,7 +46,21 @@ void BoundingFrustum::SetMatrix(const Matrix& matrix) _pBottom.D = matrix.M44 + matrix.M42; _pBottom.Normalize(); - // TODO: Fix this for reverse Z +#if FLAX_REVERSE_Z + // Far plane + _pFar.Normal.X = matrix.M13; + _pFar.Normal.Y = matrix.M23; + _pFar.Normal.Z = matrix.M33; + _pFar.D = matrix.M43; + _pFar.Normalize(); + + // Near plane + _pNear.Normal.X = matrix.M14 - matrix.M13; + _pNear.Normal.Y = matrix.M24 - matrix.M23; + _pNear.Normal.Z = matrix.M34 - matrix.M33; + _pNear.D = matrix.M44 - matrix.M43; + _pNear.Normalize(); +#else // Near plane _pNear.Normal.X = matrix.M13; _pNear.Normal.Y = matrix.M23; @@ -60,6 +74,7 @@ void BoundingFrustum::SetMatrix(const Matrix& matrix) _pFar.Normal.Z = matrix.M34 - matrix.M33; _pFar.D = matrix.M44 - matrix.M43; _pFar.Normalize(); +#endif } Plane BoundingFrustum::GetPlane(int32 index) const diff --git a/Source/Engine/Core/Math/BoundingFrustum.cs b/Source/Engine/Core/Math/BoundingFrustum.cs index 160129f7f..b45160cf1 100644 --- a/Source/Engine/Core/Math/BoundingFrustum.cs +++ b/Source/Engine/Core/Math/BoundingFrustum.cs @@ -230,6 +230,21 @@ namespace FlaxEngine bottom.D = matrix.M44 + matrix.M42; bottom.Normalize(); +#if FLAX_REVERSE_Z + // Far plane + far.Normal.X = matrix.M13; + far.Normal.Y = matrix.M23; + far.Normal.Z = matrix.M33; + far.D = matrix.M43; + far.Normalize(); + + // Near plane + near.Normal.X = matrix.M14 - matrix.M13; + near.Normal.Y = matrix.M24 - matrix.M23; + near.Normal.Z = matrix.M34 - matrix.M33; + near.D = matrix.M44 - matrix.M43; + near.Normalize(); +#else // Near plane near.Normal.X = matrix.M13; near.Normal.Y = matrix.M23; @@ -243,6 +258,7 @@ namespace FlaxEngine far.Normal.Z = matrix.M34 - matrix.M33; far.D = matrix.M44 - matrix.M43; far.Normalize(); +#endif } private static Vector3 Get3PlanesInterPoint(ref Plane p1, ref Plane p2, ref Plane p3) diff --git a/Source/Engine/Core/Math/Matrix.cpp b/Source/Engine/Core/Math/Matrix.cpp index 872468134..afa94b149 100644 --- a/Source/Engine/Core/Math/Matrix.cpp +++ b/Source/Engine/Core/Math/Matrix.cpp @@ -40,15 +40,15 @@ float Matrix::GetDeterminant() const const float temp4 = M31 * M44 - M34 * M41; const float temp5 = M31 * M43 - M33 * M41; const float temp6 = M31 * M42 - M32 * M41; - return M11 * (M22 * temp1 - M23 * temp2 + M24 * temp3) - M12 * (M21 * temp1 -M23 * temp4 + M24 * temp5) + M13 * (M21 * temp2 - M22 * temp4 + M24 * temp6) - M14 * (M21 * temp3 - M22 * temp5 + M23 * temp6); + return M11 * (M22 * temp1 - M23 * temp2 + M24 * temp3) - M12 * (M21 * temp1 - M23 * temp4 + M24 * temp5) + M13 * (M21 * temp2 - M22 * temp4 + M24 * temp6) - M14 * (M21 * temp3 - M22 * temp5 + M23 * temp6); } float Matrix::RotDeterminant() const { return - Values[0][0] * (Values[1][1] * Values[2][2] - Values[1][2] * Values[2][1]) - - Values[1][0] * (Values[0][1] * Values[2][2] - Values[0][2] * Values[2][1]) + - Values[2][0] * (Values[0][1] * Values[1][2] - Values[0][2] * Values[1][1]); + Values[0][0] * (Values[1][1] * Values[2][2] - Values[1][2] * Values[2][1]) - + Values[1][0] * (Values[0][1] * Values[2][2] - Values[0][2] * Values[2][1]) + + Values[2][0] * (Values[0][1] * Values[1][2] - Values[0][2] * Values[1][1]); } void Matrix::NormalizeScale() @@ -506,10 +506,15 @@ void Matrix::OrthoOffCenter(float left, float right, float bottom, float top, fl result = Identity; result.M11 = 2.0f / (right - left); result.M22 = 2.0f / (top - bottom); - result.M33 = -zRange; result.M41 = (left + right) / (left - right); result.M42 = (top + bottom) / (bottom - top); +#if FLAX_REVERSE_Z + result.M33 = -zRange; result.M43 = zFar * zRange; +#else + result.M33 = zRange; + result.M43 = -zNear * zRange; +#endif } void Matrix::PerspectiveFov(float fov, float aspect, float zNear, float zFar, Matrix& result) @@ -525,16 +530,21 @@ void Matrix::PerspectiveFov(float fov, float aspect, float zNear, float zFar, Ma void Matrix::PerspectiveOffCenter(float left, float right, float bottom, float top, float zNear, float zFar, Matrix& result) { - const float zRange = zNear / (zFar - zNear); + const float zRange = 1.0 / (zFar - zNear); result = Zero; result.M11 = 2.0f * zNear / (right - left); result.M22 = 2.0f * zNear / (top - bottom); result.M31 = (left + right) / (left - right); result.M32 = (top + bottom) / (bottom - top); - result.M33 = -zRange; - result.M34 = 1.0f; - result.M43 = zFar * zRange; + result.M34 = 1.0f; +#if FLAX_REVERSE_Z + result.M33 = -zNear * zRange; + result.M43 = zFar * zNear * zRange; +#else + result.M33 = zFar * zRange; + result.M43 = -zFar * zNear * zRange; +#endif } void Matrix::RotationX(float angle, Matrix& result) diff --git a/Source/Engine/Core/Math/Matrix.cs b/Source/Engine/Core/Math/Matrix.cs index 34dcc9c52..154373180 100644 --- a/Source/Engine/Core/Math/Matrix.cs +++ b/Source/Engine/Core/Math/Matrix.cs @@ -519,22 +519,22 @@ namespace FlaxEngine { switch (index) { - case 0: return M11; - case 1: return M12; - case 2: return M13; - case 3: return M14; - case 4: return M21; - case 5: return M22; - case 6: return M23; - case 7: return M24; - case 8: return M31; - case 9: return M32; - case 10: return M33; - case 11: return M34; - case 12: return M41; - case 13: return M42; - case 14: return M43; - case 15: return M44; + case 0: return M11; + case 1: return M12; + case 2: return M13; + case 3: return M14; + case 4: return M21; + case 5: return M22; + case 6: return M23; + case 7: return M24; + case 8: return M31; + case 9: return M32; + case 10: return M33; + case 11: return M34; + case 12: return M41; + case 13: return M42; + case 14: return M43; + case 15: return M44; } throw new ArgumentOutOfRangeException(nameof(index), "Indices for Matrix run from 0 to 15, inclusive."); } @@ -543,55 +543,55 @@ namespace FlaxEngine { switch (index) { - case 0: - M11 = value; - break; - case 1: - M12 = value; - break; - case 2: - M13 = value; - break; - case 3: - M14 = value; - break; - case 4: - M21 = value; - break; - case 5: - M22 = value; - break; - case 6: - M23 = value; - break; - case 7: - M24 = value; - break; - case 8: - M31 = value; - break; - case 9: - M32 = value; - break; - case 10: - M33 = value; - break; - case 11: - M34 = value; - break; - case 12: - M41 = value; - break; - case 13: - M42 = value; - break; - case 14: - M43 = value; - break; - case 15: - M44 = value; - break; - default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Matrix run from 0 to 15, inclusive."); + case 0: + M11 = value; + break; + case 1: + M12 = value; + break; + case 2: + M13 = value; + break; + case 3: + M14 = value; + break; + case 4: + M21 = value; + break; + case 5: + M22 = value; + break; + case 6: + M23 = value; + break; + case 7: + M24 = value; + break; + case 8: + M31 = value; + break; + case 9: + M32 = value; + break; + case 10: + M33 = value; + break; + case 11: + M34 = value; + break; + case 12: + M41 = value; + break; + case 13: + M42 = value; + break; + case 14: + M43 = value; + break; + case 15: + M44 = value; + break; + default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Matrix run from 0 to 15, inclusive."); } } } @@ -2176,10 +2176,15 @@ namespace FlaxEngine result = Identity; result.M11 = 2.0f / (right - left); result.M22 = 2.0f / (top - bottom); - result.M33 = -zRange; result.M41 = (left + right) / (left - right); result.M42 = (top + bottom) / (bottom - top); +#if FLAX_REVERSE_Z + result.M33 = -zRange; result.M43 = zfar * zRange; +#else + result.M33 = zRange; + result.M43 = -znear * zRange; +#endif } /// @@ -2238,14 +2243,19 @@ namespace FlaxEngine public static void PerspectiveFov(float fov, float aspect, float znear, float zfar, out Matrix result) { var yScale = (float)(1.0f / Math.Tan(fov * 0.5f)); - var zRange = znear / (zfar - znear); + var zRange = 1.0f / (zfar - znear); result = new Matrix { M11 = yScale / aspect, M22 = yScale, - M33 = -zRange, M34 = 1.0f, - M43 = zRange * zfar, +#if FLAX_REVERSE_Z + M33 = -znear * zRange, + M43 = znear * zfar * zRange, +#else + M33 = zfar * zRange, + M43 = -znear * zfar * zRange, +#endif }; } @@ -2275,16 +2285,21 @@ namespace FlaxEngine /// When the method completes, contains the created projection matrix. public static void PerspectiveOffCenter(float left, float right, float bottom, float top, float znear, float zfar, out Matrix result) { - float zRange = znear / (zfar - znear); + float zRange = 1.0f / (zfar - znear); result = new Matrix { M11 = 2.0f * znear / (right - left), M22 = 2.0f * znear / (top - bottom), M31 = (left + right) / (left - right), M32 = (top + bottom) / (bottom - top), - M33 = -zRange, M34 = 1.0f, - M43 = zfar * zRange, +#if FLAX_REVERSE_Z + M33 = -znear * zRange, + M43 = znear * zfar * zRange, +#else + M33 = zfar * zRange, + M43 = -znear * zfar * zRange, +#endif }; } diff --git a/Source/Tools/Flax.Build/Build/ProjectTarget.cs b/Source/Tools/Flax.Build/Build/ProjectTarget.cs index 817ce8458..0640e30c7 100644 --- a/Source/Tools/Flax.Build/Build/ProjectTarget.cs +++ b/Source/Tools/Flax.Build/Build/ProjectTarget.cs @@ -80,6 +80,10 @@ namespace Flax.Build options.ScriptingAPI.Defines.Add("USE_LARGE_WORLDS"); } + // Add reverse-z definitions + options.CompileEnv.PreprocessorDefinitions.Add("FLAX_REVERSE_Z"); + options.ScriptingAPI.Defines.Add("FLAX_REVERSE_Z"); + // Add include paths for this and all referenced projects sources foreach (var project in Project.GetAllProjects()) {