From 6dcadb5131805fdcf6a8149070539e0ddb9e1a72 Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Wed, 8 Nov 2023 18:34:17 +0100 Subject: [PATCH 1/3] Compacted UpdateTransform Matrix math --- Source/Engine/UI/GUI/Control.Bounds.cs | 53 +++++++++++++++++++------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/Source/Engine/UI/GUI/Control.Bounds.cs b/Source/Engine/UI/GUI/Control.Bounds.cs index c6260b648..272b1c5f1 100644 --- a/Source/Engine/UI/GUI/Control.Bounds.cs +++ b/Source/Engine/UI/GUI/Control.Bounds.cs @@ -487,9 +487,12 @@ namespace FlaxEngine.GUI public void UpdateTransform() { // Actual pivot and negative pivot - Float2.Multiply(ref _pivot, ref _bounds.Size, out var v1); - Float2.Negate(ref v1, out var v2); - Float2.Add(ref v1, ref _bounds.Location, out v1); + //Float2.Multiply(ref _pivot, ref _bounds.Size, out var v1); + //Float2.Negate(ref v1, out var v2); + //Float2.Add(ref v1, ref _bounds.Location, out v1); + var v1 = _pivot * _bounds.Size; + var v2 = -v1; + v1 += _bounds.Location; // ------ Matrix3x3 based version: @@ -518,18 +521,42 @@ namespace FlaxEngine.GUI // ------ Matrix2x2 based version: // 2D transformation - Matrix2x2.Scale(ref _scale, out Matrix2x2 m1); - Matrix2x2.Shear(ref _shear, out Matrix2x2 m2); - Matrix2x2.Multiply(ref m1, ref m2, out m1); - Matrix2x2.Rotation(Mathf.DegreesToRadians * _rotation, out m2); - Matrix2x2.Multiply(ref m1, ref m2, out m1); + + //Matrix2x2.Scale(ref _scale, out Matrix2x2 m1); + //Matrix2x2.Shear(ref _shear, out Matrix2x2 m2); + //Matrix2x2.Multiply(ref m1, ref m2, out m1); + + // Scale and Shear + Matrix3x3 m1 = new Matrix3x3 + ( + _scale.X, + _scale.X * (_shear.X == 0 ? 0 : (1.0f / Mathf.Tan(Mathf.DegreesToRadians * (90 - Mathf.Clamp(_shear.X, -89.0f, 89.0f))))), + 0, + _scale.Y * (_shear.Y == 0 ? 0 : (1.0f / Mathf.Tan(Mathf.DegreesToRadians * (90 - Mathf.Clamp(_shear.Y, -89.0f, 89.0f))))), + _scale.Y, + 0, 0, 0, 1 + ); + + + //Matrix2x2.Rotation(Mathf.DegreesToRadians * _rotation, out m2); + float sin = Mathf.Sin(Mathf.DegreesToRadians * _rotation); + float cos = Mathf.Cos(Mathf.DegreesToRadians * _rotation); + + //Matrix2x2.Multiply(ref m1, ref m2, out m1); + m1.M11 = (_scale.X * cos) + (m1.M12 * -sin); + m1.M12 = (_scale.X * sin) + (m1.M12 * cos); + m1.M21 = (m1.M21 * cos) + (_scale.Y * -sin); + m1.M22 = (m1.M21 * sin) + (_scale.Y * cos); // Mix all the stuff - Matrix3x3.Translation2D(ref v2, out Matrix3x3 m3); - Matrix3x3 m4 = (Matrix3x3)m1; - Matrix3x3.Multiply(ref m3, ref m4, out m3); - Matrix3x3.Translation2D(ref v1, out m4); - Matrix3x3.Multiply(ref m3, ref m4, out _cachedTransform); + //Matrix3x3.Translation2D(ref v2, out Matrix3x3 m3); + //Matrix3x3 m4 = (Matrix3x3)m1; + //Matrix3x3.Multiply(ref m3, ref m4, out m3); + //Matrix3x3.Translation2D(ref v1, out Matrix3x3 m4); + //Matrix3x3.Multiply(ref m3, ref m4, out _cachedTransform); + m1.M31 = (v2.X * m1.M11) + (v2.Y * m1.M21) + v1.X; + m1.M32 = (v2.X * m1.M12) + (v2.Y * m1.M22) + v1.Y; + _cachedTransform = m1; // Cache inverted transform Matrix3x3.Invert(ref _cachedTransform, out _cachedTransformInv); From ea3f02f810b04c60becc22b8d6acb7ef3260a690 Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Thu, 9 Nov 2023 17:34:07 +0100 Subject: [PATCH 2/3] Fix rotacion sheers the UI element --- Source/Engine/UI/GUI/Control.Bounds.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Engine/UI/GUI/Control.Bounds.cs b/Source/Engine/UI/GUI/Control.Bounds.cs index 272b1c5f1..c9d20bfae 100644 --- a/Source/Engine/UI/GUI/Control.Bounds.cs +++ b/Source/Engine/UI/GUI/Control.Bounds.cs @@ -545,14 +545,14 @@ namespace FlaxEngine.GUI //Matrix2x2.Multiply(ref m1, ref m2, out m1); m1.M11 = (_scale.X * cos) + (m1.M12 * -sin); m1.M12 = (_scale.X * sin) + (m1.M12 * cos); - m1.M21 = (m1.M21 * cos) + (_scale.Y * -sin); + float m21 = (m1.M21 * cos) + (_scale.Y * -sin); m1.M22 = (m1.M21 * sin) + (_scale.Y * cos); - + m1.M21 = m21; // Mix all the stuff //Matrix3x3.Translation2D(ref v2, out Matrix3x3 m3); //Matrix3x3 m4 = (Matrix3x3)m1; //Matrix3x3.Multiply(ref m3, ref m4, out m3); - //Matrix3x3.Translation2D(ref v1, out Matrix3x3 m4); + //Matrix3x3.Translation2D(ref v1, out m4); //Matrix3x3.Multiply(ref m3, ref m4, out _cachedTransform); m1.M31 = (v2.X * m1.M11) + (v2.Y * m1.M21) + v1.X; m1.M32 = (v2.X * m1.M12) + (v2.Y * m1.M22) + v1.Y; From 307129b4a185c7838a56567bde0b4bf7c35d0d26 Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Thu, 16 Nov 2023 11:50:44 +0100 Subject: [PATCH 3/3] added c++ PingPong and flipped sheer --- Source/Engine/Core/Math/Math.h | 11 +++++++++++ Source/Engine/UI/GUI/Control.Bounds.cs | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Core/Math/Math.h b/Source/Engine/Core/Math/Math.h index 276f64884..49a052771 100644 --- a/Source/Engine/Core/Math/Math.h +++ b/Source/Engine/Core/Math/Math.h @@ -890,6 +890,17 @@ namespace Math { return Lerp(a, b, alpha < 0.5f ? InterpCircularIn(0.f, 1.f, alpha * 2.f) * 0.5f : InterpCircularOut(0.f, 1.f, alpha * 2.f - 1.f) * 0.5f + 0.5f); } + /// + /// PingPongs the value , so that it is never larger than and never smaller than 0. + /// + /// + /// + /// + template + static FORCE_INLINE T PingPong(const T& t, T length) + { + return length - Abs(Repeat(t, length * 2.0f) - length); + } // Rotates position about the given axis by the given angle, in radians, and returns the offset to position Vector3 FLAXENGINE_API RotateAboutAxis(const Vector3& normalizedRotationAxis, float angle, const Vector3& positionOnAxis, const Vector3& position); diff --git a/Source/Engine/UI/GUI/Control.Bounds.cs b/Source/Engine/UI/GUI/Control.Bounds.cs index c9d20bfae..52a4f78f7 100644 --- a/Source/Engine/UI/GUI/Control.Bounds.cs +++ b/Source/Engine/UI/GUI/Control.Bounds.cs @@ -530,9 +530,9 @@ namespace FlaxEngine.GUI Matrix3x3 m1 = new Matrix3x3 ( _scale.X, - _scale.X * (_shear.X == 0 ? 0 : (1.0f / Mathf.Tan(Mathf.DegreesToRadians * (90 - Mathf.Clamp(_shear.X, -89.0f, 89.0f))))), + _scale.X * (_shear.Y == 0 ? 0 : (1.0f / Mathf.Tan(Mathf.DegreesToRadians * (90 - Mathf.Clamp(_shear.Y, -89.0f, 89.0f))))), 0, - _scale.Y * (_shear.Y == 0 ? 0 : (1.0f / Mathf.Tan(Mathf.DegreesToRadians * (90 - Mathf.Clamp(_shear.Y, -89.0f, 89.0f))))), + _scale.Y * (_shear.X == 0 ? 0 : (1.0f / Mathf.Tan(Mathf.DegreesToRadians * (90 - Mathf.Clamp(_shear.X, -89.0f, 89.0f))))), _scale.Y, 0, 0, 0, 1 );