From 969db604e5ff6b4ccdafaabf3d17bb3ab11e7698 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 28 May 2021 11:01:13 +0200 Subject: [PATCH] Fix gizmo in top down view on Z-axis movement (and other combination of perpendicular views) Fix gizmo stability when using single axis #530 --- Source/Editor/Gizmo/TransformGizmoBase.cs | 59 +++++++++++++++-------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/Source/Editor/Gizmo/TransformGizmoBase.cs b/Source/Editor/Gizmo/TransformGizmoBase.cs index e123bd3f6..170bcb5b1 100644 --- a/Source/Editor/Gizmo/TransformGizmoBase.cs +++ b/Source/Editor/Gizmo/TransformGizmoBase.cs @@ -204,24 +204,43 @@ namespace FlaxEditor.Gizmo switch (_activeAxis) { - case Axis.XY: case Axis.X: { - var plane = new Plane(Vector3.Backward, Vector3.Transform(Position, invRotationMatrix).Z); + var plane = new Plane(Position, Vector3.Normalize(ray.Position - Position)); if (ray.Intersects(ref plane, out float intersection)) { _intersectPosition = ray.Position + ray.Direction * intersection; if (_lastIntersectionPosition != Vector3.Zero) _tDelta = _intersectPosition - _lastIntersectionPosition; - delta = _activeAxis == Axis.X - ? new Vector3(_tDelta.X, 0, 0) - : new Vector3(_tDelta.X, _tDelta.Y, 0); + delta = new Vector3(_tDelta.X, 0, 0); + } + break; + } + case Axis.Y: + { + var plane = new Plane(Position, Vector3.Normalize(ray.Position - Position)); + if (ray.Intersects(ref plane, out float intersection)) + { + _intersectPosition = ray.Position + ray.Direction * intersection; + if (_lastIntersectionPosition != Vector3.Zero) + _tDelta = _intersectPosition - _lastIntersectionPosition; + delta = new Vector3(0, _tDelta.Y, 0); } break; } case Axis.Z: + { + var plane = new Plane(Position, Vector3.Normalize(ray.Position - Position)); + if (ray.Intersects(ref plane, out float intersection)) + { + _intersectPosition = ray.Position + ray.Direction * intersection; + if (_lastIntersectionPosition != Vector3.Zero) + _tDelta = _intersectPosition - _lastIntersectionPosition; + delta = new Vector3(0, 0, _tDelta.Z); + } + break; + } case Axis.YZ: - case Axis.Y: { var plane = new Plane(Vector3.Left, Vector3.Transform(Position, invRotationMatrix).X); if (ray.Intersects(ref plane, out float intersection)) @@ -229,18 +248,19 @@ namespace FlaxEditor.Gizmo _intersectPosition = ray.Position + ray.Direction * intersection; if (_lastIntersectionPosition != Vector3.Zero) _tDelta = _intersectPosition - _lastIntersectionPosition; - switch (_activeAxis) - { - case Axis.Y: - delta = new Vector3(0, _tDelta.Y, 0); - break; - case Axis.Z: - delta = new Vector3(0, 0, _tDelta.Z); - break; - default: - delta = new Vector3(0, _tDelta.Y, _tDelta.Z); - break; - } + delta = new Vector3(0, _tDelta.Y, _tDelta.Z); + } + break; + } + case Axis.XY: + { + var plane = new Plane(Vector3.Backward, Vector3.Transform(Position, invRotationMatrix).Z); + if (ray.Intersects(ref plane, out float intersection)) + { + _intersectPosition = ray.Position + ray.Direction * intersection; + if (_lastIntersectionPosition != Vector3.Zero) + _tDelta = _intersectPosition - _lastIntersectionPosition; + delta = new Vector3(_tDelta.X, _tDelta.Y, 0); } break; } @@ -271,12 +291,11 @@ namespace FlaxEditor.Gizmo } } + // Modifiers if (isScaling) delta *= 0.01f; - if (Owner.IsAltKeyDown) delta *= 0.5f; - if ((isScaling ? ScaleSnapEnabled : TranslationSnapEnable) || Owner.UseSnapping) { float snapValue = isScaling ? ScaleSnapValue : TranslationSnapValue;