From 49d407f624625051e760955516659f7d804f1e25 Mon Sep 17 00:00:00 2001 From: copygirl Date: Sat, 30 Dec 2023 18:22:12 +0100 Subject: [PATCH] Only calculate inverse transform --- objects/Grid.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/objects/Grid.cs b/objects/Grid.cs index 55e4184..40e25a9 100644 --- a/objects/Grid.cs +++ b/objects/Grid.cs @@ -72,21 +72,20 @@ public partial class Grid : Area3D /// public Transform3D Snap(Transform3D transform, Vector3 normal, Item item) { - var inverseTransform = GlobalTransform.AffineInverse(); - var inverseBasis = GlobalBasis.Inverse(); - var halfSize = (Vector3)item.Size * StepSize / 2; + var halfSize = (Vector3)item.Size * StepSize / 2; // Get grid-local values of the global transform and normal. - var pos = inverseTransform * transform.Origin; - var basis = inverseBasis * transform.Basis; - normal = inverseBasis * normal; + var inverse = GlobalTransform.AffineInverse(); + var pos = inverse * transform.Origin; + var basis = inverse.Basis * transform.Basis; + normal = inverse.Basis * normal; // Snap rotation to nearest axis. basis = Basis.FromEuler(basis.GetEuler().Snapped(Tau / 4)); // Offset / "push out" by half of the item's size. - var offsetAxis = (int)(normal * basis).Abs().MaxAxisIndex(); - pos += halfSize[offsetAxis] * normal; + var axis = (normal * basis).Abs().MaxAxisIndex(); + pos += halfSize[(int)axis] * normal; // Snap the position to the grid. var halfOff = basis * halfSize.PosMod(1.0f);