From f287ed6c300184c5787e3f380fa6bbb882d6623a Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Tue, 24 Oct 2023 19:59:47 +0200 Subject: [PATCH 1/5] init --- Source/Engine/Core/Math/Float2.cs | 27 +++++++++++++++++++++++++++ Source/Engine/Core/Math/Vector3.cs | 15 +++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/Source/Engine/Core/Math/Float2.cs b/Source/Engine/Core/Math/Float2.cs index 3ca6f51d7..4ded4cf92 100644 --- a/Source/Engine/Core/Math/Float2.cs +++ b/Source/Engine/Core/Math/Float2.cs @@ -1273,6 +1273,33 @@ namespace FlaxEngine return result; } + /// + /// funcion for grid snaping snap to absolute world grid + ///
desined for snaping using a ray / draging object
+ ///
+ /// + /// + /// out = Ceil(((A - (GridSize * 0.5f)) / GridSize) * GridSize + public static Float2 SnapToGrid(Float2 Translation, Float2 GridSize) + { + Translation.X = Mathf.Ceil((Translation.X - (GridSize.X * 0.5f)) / GridSize.Y) * GridSize.X; + Translation.Y = Mathf.Ceil((Translation.Y - (GridSize.Y * 0.5f)) / GridSize.X) * GridSize.Y; + return Translation; + } + /// + /// funcion for grid snaping snap to absolute world grid + ///
desined for snaping using a ray / draging object
+ ///
+ /// + /// + /// out = Ceil(((A - (GridSize * 0.5f)) / GridSize) * GridSize + public static Float2 SnapToGrid(Float2 Translation, float GridSize) + { + Translation.X = Mathf.Ceil((Translation.X - (GridSize * 0.5f)) / GridSize) * GridSize; + Translation.Y = Mathf.Ceil((Translation.Y - (GridSize * 0.5f)) / GridSize) * GridSize; + return Translation; + } + /// /// Adds two vectors. /// diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index 280093619..b7cc396ee 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -1671,6 +1671,21 @@ namespace FlaxEngine return result; } + /// + /// funcion for grid snaping snap to absolute world grid + ///
desined for snaping using a ray / draging object
+ ///
+ /// + /// + /// out = Ceil(((A - (GridSize * 0.5f)) / GridSize) * GridSize + public static Vector3 SnapToGrid(Vector3 Translation, float GridSize) + { + Translation.X = Mathr.Ceil((Translation.X - (GridSize * 0.5f)) / GridSize) * GridSize; + Translation.Y = Mathr.Ceil((Translation.Y - (GridSize * 0.5f)) / GridSize) * GridSize; + Translation.Z = Mathr.Ceil((Translation.Z - (GridSize * 0.5f)) / GridSize) * GridSize; + return Translation; + } + /// /// Adds two vectors. /// From 39aebc6c851adc3a45750fa996abbdbcfca2aed9 Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Wed, 25 Oct 2023 12:10:23 +0200 Subject: [PATCH 2/5] Update Vector3.cs --- Source/Engine/Core/Math/Vector3.cs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index b7cc396ee..ad1797644 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -1678,12 +1678,26 @@ namespace FlaxEngine /// /// /// out = Ceil(((A - (GridSize * 0.5f)) / GridSize) * GridSize - public static Vector3 SnapToGrid(Vector3 Translation, float GridSize) + public static Vector3 SnapToGrid(Vector3 translation, float gridSize) { - Translation.X = Mathr.Ceil((Translation.X - (GridSize * 0.5f)) / GridSize) * GridSize; - Translation.Y = Mathr.Ceil((Translation.Y - (GridSize * 0.5f)) / GridSize) * GridSize; - Translation.Z = Mathr.Ceil((Translation.Z - (GridSize * 0.5f)) / GridSize) * GridSize; - return Translation; + translation.X = Mathr.Ceil((translation.X - (gridSize * 0.5f)) / gridSize) * gridSize; + translation.Y = Mathr.Ceil((translation.Y - (gridSize * 0.5f)) / gridSize) * gridSize; + translation.Z = Mathr.Ceil((translation.Z - (gridSize * 0.5f)) / gridSize) * gridSize; + return translation; + } + /// + /// funcion for grid snaping snap to absolute world grid + ///
desined for snaping using a ray / draging object
+ ///
+ /// + /// + /// out = Ceil(((A - (GridSize * 0.5f)) / GridSize) * GridSize + public static Vector3 SnapToGrid(Vector3 translation, Float3 gridSize) + { + translation.X = Mathr.Ceil((translation.X - (gridSize.X * 0.5f)) / gridSize.X) * gridSize.X; + translation.Y = Mathr.Ceil((translation.Y - (gridSize.Y * 0.5f)) / gridSize.Y) * gridSize.Y; + translation.Z = Mathr.Ceil((translation.Z - (gridSize.Z * 0.5f)) / gridSize.Z) * gridSize.Z; + return translation; } /// From 4fb35579b5e8199a081a21e800bb1ed4390f3080 Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Wed, 25 Oct 2023 12:12:22 +0200 Subject: [PATCH 3/5] Update Float2.cs --- Source/Engine/Core/Math/Float2.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Source/Engine/Core/Math/Float2.cs b/Source/Engine/Core/Math/Float2.cs index 4ded4cf92..d33cc7fea 100644 --- a/Source/Engine/Core/Math/Float2.cs +++ b/Source/Engine/Core/Math/Float2.cs @@ -1280,11 +1280,11 @@ namespace FlaxEngine /// /// /// out = Ceil(((A - (GridSize * 0.5f)) / GridSize) * GridSize - public static Float2 SnapToGrid(Float2 Translation, Float2 GridSize) + public static Float2 SnapToGrid(Float2 translation, Float2 gridSize) { - Translation.X = Mathf.Ceil((Translation.X - (GridSize.X * 0.5f)) / GridSize.Y) * GridSize.X; - Translation.Y = Mathf.Ceil((Translation.Y - (GridSize.Y * 0.5f)) / GridSize.X) * GridSize.Y; - return Translation; + translation.X = Mathf.Ceil((translation.X - (gridSize.X * 0.5f)) / gridSize.Y) * gridSize.X; + translation.Y = Mathf.Ceil((translation.Y - (gridSize.Y * 0.5f)) / gridSize.X) * gridSize.Y; + return translation; } /// /// funcion for grid snaping snap to absolute world grid @@ -1295,9 +1295,9 @@ namespace FlaxEngine /// out = Ceil(((A - (GridSize * 0.5f)) / GridSize) * GridSize public static Float2 SnapToGrid(Float2 Translation, float GridSize) { - Translation.X = Mathf.Ceil((Translation.X - (GridSize * 0.5f)) / GridSize) * GridSize; - Translation.Y = Mathf.Ceil((Translation.Y - (GridSize * 0.5f)) / GridSize) * GridSize; - return Translation; + translation.X = Mathf.Ceil((translation.X - (gridSize * 0.5f)) / gridSize) * gridSize; + translation.Y = Mathf.Ceil((translation.Y - (gridSize * 0.5f)) / gridSize) * gridSize; + return translation; } /// From ec0205871249c4e463269e135b06cf8fcfc7985b Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Wed, 25 Oct 2023 16:56:45 +0200 Subject: [PATCH 4/5] Update Float2.cs --- Source/Engine/Core/Math/Float2.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Core/Math/Float2.cs b/Source/Engine/Core/Math/Float2.cs index d33cc7fea..38fb7534a 100644 --- a/Source/Engine/Core/Math/Float2.cs +++ b/Source/Engine/Core/Math/Float2.cs @@ -1293,7 +1293,7 @@ namespace FlaxEngine /// /// /// out = Ceil(((A - (GridSize * 0.5f)) / GridSize) * GridSize - public static Float2 SnapToGrid(Float2 Translation, float GridSize) + public static Float2 SnapToGrid(Float2 translation, float gridSize) { translation.X = Mathf.Ceil((translation.X - (gridSize * 0.5f)) / gridSize) * gridSize; translation.Y = Mathf.Ceil((translation.Y - (gridSize * 0.5f)) / gridSize) * gridSize; From 04da732da9088353571492a71cc45dec7619f5e7 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 19 Feb 2024 23:04:59 +0100 Subject: [PATCH 5/5] Cleanup code #1795 and unify new api in vector types --- Source/Engine/Core/Math/Double2.cs | 13 ++++++++++++ Source/Engine/Core/Math/Double3.cs | 14 +++++++++++++ Source/Engine/Core/Math/Float2.cs | 30 ++++++++------------------- Source/Engine/Core/Math/Float3.cs | 14 +++++++++++++ Source/Engine/Core/Math/Vector2.cs | 13 ++++++++++++ Source/Engine/Core/Math/Vector3.cs | 33 ++++++++---------------------- 6 files changed, 71 insertions(+), 46 deletions(-) diff --git a/Source/Engine/Core/Math/Double2.cs b/Source/Engine/Core/Math/Double2.cs index 02669a869..9211d2a7b 100644 --- a/Source/Engine/Core/Math/Double2.cs +++ b/Source/Engine/Core/Math/Double2.cs @@ -1255,6 +1255,19 @@ namespace FlaxEngine return result; } + /// + /// Snaps the input position into the grid. + /// + /// The position to snap. + /// The size of the grid. + /// The position snapped to the grid. + public static Double2 SnapToGrid(Double2 pos, Double2 gridSize) + { + pos.X = Mathd.Ceil((pos.X - (gridSize.X * 0.5)) / gridSize.Y) * gridSize.X; + pos.Y = Mathd.Ceil((pos.Y - (gridSize.Y * 0.5)) / gridSize.X) * gridSize.Y; + return pos; + } + /// /// Adds two vectors. /// diff --git a/Source/Engine/Core/Math/Double3.cs b/Source/Engine/Core/Math/Double3.cs index ca5eb9cbd..ccb30d699 100644 --- a/Source/Engine/Core/Math/Double3.cs +++ b/Source/Engine/Core/Math/Double3.cs @@ -1536,6 +1536,20 @@ namespace FlaxEngine return result; } + /// + /// Snaps the input position into the grid. + /// + /// The position to snap. + /// The size of the grid. + /// The position snapped to the grid. + public static Double3 SnapToGrid(Double3 pos, Double3 gridSize) + { + pos.X = Mathd.Ceil((pos.X - (gridSize.X * 0.5)) / gridSize.X) * gridSize.X; + pos.Y = Mathd.Ceil((pos.Y - (gridSize.Y * 0.5)) / gridSize.Y) * gridSize.Y; + pos.Z = Mathd.Ceil((pos.Z - (gridSize.Z * 0.5)) / gridSize.Z) * gridSize.Z; + return pos; + } + /// /// Adds two vectors. /// diff --git a/Source/Engine/Core/Math/Float2.cs b/Source/Engine/Core/Math/Float2.cs index 38fb7534a..ccf8384c8 100644 --- a/Source/Engine/Core/Math/Float2.cs +++ b/Source/Engine/Core/Math/Float2.cs @@ -1274,30 +1274,16 @@ namespace FlaxEngine } /// - /// funcion for grid snaping snap to absolute world grid - ///
desined for snaping using a ray / draging object
+ /// Snaps the input position into the grid. ///
- /// - /// - /// out = Ceil(((A - (GridSize * 0.5f)) / GridSize) * GridSize - public static Float2 SnapToGrid(Float2 translation, Float2 gridSize) + /// The position to snap. + /// The size of the grid. + /// The position snapped to the grid. + public static Float2 SnapToGrid(Float2 pos, Float2 gridSize) { - translation.X = Mathf.Ceil((translation.X - (gridSize.X * 0.5f)) / gridSize.Y) * gridSize.X; - translation.Y = Mathf.Ceil((translation.Y - (gridSize.Y * 0.5f)) / gridSize.X) * gridSize.Y; - return translation; - } - /// - /// funcion for grid snaping snap to absolute world grid - ///
desined for snaping using a ray / draging object
- ///
- /// - /// - /// out = Ceil(((A - (GridSize * 0.5f)) / GridSize) * GridSize - public static Float2 SnapToGrid(Float2 translation, float gridSize) - { - translation.X = Mathf.Ceil((translation.X - (gridSize * 0.5f)) / gridSize) * gridSize; - translation.Y = Mathf.Ceil((translation.Y - (gridSize * 0.5f)) / gridSize) * gridSize; - return translation; + pos.X = Mathf.Ceil((pos.X - (gridSize.X * 0.5f)) / gridSize.Y) * gridSize.X; + pos.Y = Mathf.Ceil((pos.Y - (gridSize.Y * 0.5f)) / gridSize.X) * gridSize.Y; + return pos; } /// diff --git a/Source/Engine/Core/Math/Float3.cs b/Source/Engine/Core/Math/Float3.cs index 3ea95f0d7..9ee2bbea9 100644 --- a/Source/Engine/Core/Math/Float3.cs +++ b/Source/Engine/Core/Math/Float3.cs @@ -1520,6 +1520,20 @@ namespace FlaxEngine return result; } + /// + /// Snaps the input position into the grid. + /// + /// The position to snap. + /// The size of the grid. + /// The position snapped to the grid. + public static Float3 SnapToGrid(Float3 pos, Float3 gridSize) + { + pos.X = Mathf.Ceil((pos.X - (gridSize.X * 0.5f)) / gridSize.X) * gridSize.X; + pos.Y = Mathf.Ceil((pos.Y - (gridSize.Y * 0.5f)) / gridSize.Y) * gridSize.Y; + pos.Z = Mathf.Ceil((pos.Z - (gridSize.Z * 0.5f)) / gridSize.Z) * gridSize.Z; + return pos; + } + /// /// Adds two vectors. /// diff --git a/Source/Engine/Core/Math/Vector2.cs b/Source/Engine/Core/Math/Vector2.cs index 568c51764..f8e0b6c32 100644 --- a/Source/Engine/Core/Math/Vector2.cs +++ b/Source/Engine/Core/Math/Vector2.cs @@ -1381,6 +1381,19 @@ namespace FlaxEngine return result; } + /// + /// Snaps the input position into the grid. + /// + /// The position to snap. + /// The size of the grid. + /// The position snapped to the grid. + public static Vector2 SnapToGrid(Vector2 pos, Vector2 gridSize) + { + pos.X = Mathr.Ceil((pos.X - (gridSize.X * 0.5f)) / gridSize.Y) * gridSize.X; + pos.Y = Mathr.Ceil((pos.Y - (gridSize.Y * 0.5f)) / gridSize.X) * gridSize.Y; + return pos; + } + /// /// Adds two vectors. /// diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index ad1797644..554631849 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -1672,32 +1672,17 @@ namespace FlaxEngine } /// - /// funcion for grid snaping snap to absolute world grid - ///
desined for snaping using a ray / draging object
+ /// Snaps the input position into the grid. ///
- /// - /// - /// out = Ceil(((A - (GridSize * 0.5f)) / GridSize) * GridSize - public static Vector3 SnapToGrid(Vector3 translation, float gridSize) + /// The position to snap. + /// The size of the grid. + /// The position snapped to the grid. + public static Vector3 SnapToGrid(Vector3 pos, Vector3 gridSize) { - translation.X = Mathr.Ceil((translation.X - (gridSize * 0.5f)) / gridSize) * gridSize; - translation.Y = Mathr.Ceil((translation.Y - (gridSize * 0.5f)) / gridSize) * gridSize; - translation.Z = Mathr.Ceil((translation.Z - (gridSize * 0.5f)) / gridSize) * gridSize; - return translation; - } - /// - /// funcion for grid snaping snap to absolute world grid - ///
desined for snaping using a ray / draging object
- ///
- /// - /// - /// out = Ceil(((A - (GridSize * 0.5f)) / GridSize) * GridSize - public static Vector3 SnapToGrid(Vector3 translation, Float3 gridSize) - { - translation.X = Mathr.Ceil((translation.X - (gridSize.X * 0.5f)) / gridSize.X) * gridSize.X; - translation.Y = Mathr.Ceil((translation.Y - (gridSize.Y * 0.5f)) / gridSize.Y) * gridSize.Y; - translation.Z = Mathr.Ceil((translation.Z - (gridSize.Z * 0.5f)) / gridSize.Z) * gridSize.Z; - return translation; + pos.X = Mathr.Ceil((pos.X - (gridSize.X * 0.5f)) / gridSize.X) * gridSize.X; + pos.Y = Mathr.Ceil((pos.Y - (gridSize.Y * 0.5f)) / gridSize.Y) * gridSize.Y; + pos.Z = Mathr.Ceil((pos.Z - (gridSize.Z * 0.5f)) / gridSize.Z) * gridSize.Z; + return pos; } ///