Fix editor viewport camera orbiting issues

#354
This commit is contained in:
Wojtek Figat
2021-03-18 18:23:11 +01:00
parent 66c1b73635
commit f32ea92336
4 changed files with 43 additions and 51 deletions

View File

@@ -154,15 +154,16 @@ namespace FlaxEditor.Viewport.Cameras
private void ShowSphere(ref BoundingSphere sphere, ref Quaternion orientation)
{
Vector3 position;
if (Viewport.UseOrthographicProjection)
{
position = sphere.Center + Vector3.Backward * orientation * (sphere.Radius * 5.0f);
Viewport.OrthographicScale = Vector3.Distance(position, sphere.Center) / 1000;
}
else
{
position = sphere.Center - Vector3.Forward * orientation * (sphere.Radius * 2.5f);
TargetPoint = position;
}
TargetPoint = sphere.Center;
MoveViewport(position, orientation);
}
@@ -212,12 +213,12 @@ namespace FlaxEditor.Viewport.Cameras
Viewport.GetInput(out var input);
Viewport.GetPrevInput(out var prevInput);
var mainViewport = Viewport as MainEditorGizmoViewport;
bool isUsingGizmo = mainViewport != null && mainViewport.TransformGizmo.ActiveAxis != TransformGizmoBase.Axis.None;
var transformGizmo = (Viewport as EditorGizmoViewport)?.Gizmos.Active as TransformGizmoBase;
var isUsingGizmo = transformGizmo != null && transformGizmo.ActiveAxis != TransformGizmoBase.Axis.None;
// Get current view properties
float yaw = Viewport.Yaw;
float pitch = Viewport.Pitch;
var yaw = Viewport.Yaw;
var pitch = Viewport.Pitch;
var position = Viewport.ViewPosition;
var rotation = Viewport.ViewOrientation;
@@ -271,7 +272,7 @@ namespace FlaxEditor.Viewport.Cameras
position += forward * (Viewport.MouseWheelZoomSpeedFactor * input.MouseWheelDelta * 25.0f);
if (input.IsAltDown)
{
position += forward * (Viewport.MouseSpeed * 40 * Viewport.MouseDeltaRight.ValuesSum);
position += forward * (Viewport.MouseSpeed * 40 * Viewport.MousePositionDelta.ValuesSum);
}
}
@@ -279,7 +280,7 @@ namespace FlaxEditor.Viewport.Cameras
if (input.IsOrbiting && isUsingGizmo)
{
centerMouse = false;
Viewport.ViewPosition += mainViewport.TransformGizmo.LastDelta.Translation;
Viewport.ViewPosition += transformGizmo.LastDelta.Translation;
return;
}
@@ -288,7 +289,7 @@ namespace FlaxEditor.Viewport.Cameras
Viewport.Pitch = pitch;
if (input.IsOrbiting)
{
float orbitRadius = Vector3.Distance(ref position, ref TargetPoint);
float orbitRadius = Mathf.Max(Vector3.Distance(ref position, ref TargetPoint), 0.0001f);
Vector3 localPosition = Viewport.ViewDirection * (-1 * orbitRadius);
Viewport.ViewPosition = TargetPoint + localPosition;
}

View File

@@ -62,7 +62,7 @@ namespace FlaxEditor.Viewport
public bool SnapToGround => Editor.Instance.Options.Options.Input.SnapToGround.Process(Root);
/// <inheritdoc />
public Vector2 MouseDelta => _mouseDeltaLeft * 1000;
public Vector2 MouseDelta => _mouseDelta * 1000;
/// <inheritdoc />
public bool UseSnapping => Root.GetKey(KeyboardKeys.Control);

View File

@@ -140,7 +140,7 @@ namespace FlaxEditor.Viewport
private bool _isControllingMouse;
private int _deltaFilteringStep;
private Vector2 _startPos;
private Vector2 _mouseDeltaRightLast;
private Vector2 _mouseDeltaLast;
private Vector2[] _deltaFilteringBuffer = new Vector2[FpsCameraFilteringFrames];
/// <summary>
@@ -159,14 +159,9 @@ namespace FlaxEditor.Viewport
protected Vector2 _viewMousePos;
/// <summary>
/// The mouse delta (right button down).
/// The mouse position delta.
/// </summary>
protected Vector2 _mouseDeltaRight;
/// <summary>
/// The mouse delta (left button down).
/// </summary>
protected Vector2 _mouseDeltaLeft;
protected Vector2 _mouseDelta;
// Camera
@@ -213,14 +208,9 @@ namespace FlaxEditor.Viewport
}
/// <summary>
/// Gets the mouse movement delta for the right button (user press and move).
/// Gets the mouse movement position delta (user press and move).
/// </summary>
public Vector2 MouseDeltaRight => _mouseDeltaRight;
/// <summary>
/// Gets the mouse movement delta for the left button (user press and move).
/// </summary>
public Vector2 MouseDeltaLeft => _mouseDeltaLeft;
public Vector2 MousePositionDelta => _mouseDelta;
/// <summary>
/// Camera's pitch angle clamp range (in degrees).
@@ -1018,6 +1008,7 @@ namespace FlaxEditor.Viewport
if (_isControllingMouse)
{
var rmbWheel = false;
// Gather input
{
bool isAltDown = _input.IsAltDown;
@@ -1099,23 +1090,21 @@ namespace FlaxEditor.Viewport
moveDelta *= 0.3f;
// Calculate smooth mouse delta not dependant on viewport size
Vector2 offset = _viewMousePos - _startPos;
if (_input.IsZooming && !_input.IsMouseRightDown && !_input.IsMouseLeftDown && !_input.IsMouseMiddleDown && !_isOrtho && !rmbWheel)
{
offset = Vector2.Zero;
}
offset.X = offset.X > 0 ? Mathf.Floor(offset.X) : Mathf.Ceil(offset.X);
offset.Y = offset.Y > 0 ? Mathf.Floor(offset.Y) : Mathf.Ceil(offset.Y);
_mouseDeltaRight = offset / size;
_mouseDeltaRight.Y *= size.Y / size.X;
_mouseDelta = offset / size;
_mouseDelta.Y *= size.Y / size.X;
Vector2 mouseDelta = Vector2.Zero;
if (_useMouseFiltering)
{
// Update delta filtering buffer
_deltaFilteringBuffer[_deltaFilteringStep] = _mouseDeltaRight;
_deltaFilteringBuffer[_deltaFilteringStep] = _mouseDelta;
_deltaFilteringStep++;
// If the step is too far, zero
@@ -1129,14 +1118,16 @@ namespace FlaxEditor.Viewport
mouseDelta /= FpsCameraFilteringFrames;
}
else
mouseDelta = _mouseDeltaRight;
{
mouseDelta = _mouseDelta;
}
if (_useMouseAcceleration)
{
// Accelerate the delta
var currentDelta = mouseDelta;
mouseDelta += _mouseDeltaRightLast * _mouseAccelerationScale;
_mouseDeltaRightLast = currentDelta;
mouseDelta += _mouseDeltaLast * _mouseAccelerationScale;
_mouseDeltaLast = currentDelta;
}
// Update
@@ -1161,7 +1152,20 @@ namespace FlaxEditor.Viewport
}
else
{
_mouseDeltaRight = _mouseDeltaRightLast = Vector2.Zero;
if (_input.IsMouseLeftDown || _input.IsMouseRightDown)
{
// Calculate smooth mouse delta not dependant on viewport size
Vector2 offset = _viewMousePos - _startPos;
offset.X = offset.X > 0 ? Mathf.Floor(offset.X) : Mathf.Ceil(offset.X);
offset.Y = offset.Y > 0 ? Mathf.Floor(offset.Y) : Mathf.Ceil(offset.Y);
_mouseDelta = offset / size;
_startPos = _viewMousePos;
}
else
{
_mouseDelta = Vector2.Zero;
}
_mouseDeltaLast = Vector2.Zero;
if (ContainsFocus)
{
@@ -1198,19 +1202,6 @@ namespace FlaxEditor.Viewport
UpdateView(dt, ref moveDelta, ref mouseDelta, out _);
}
}
if (_input.IsMouseLeftDown)
{
// Calculate smooth mouse delta not dependant on viewport size
Vector2 offset = _viewMousePos - _startPos;
offset.X = offset.X > 0 ? Mathf.Floor(offset.X) : Mathf.Ceil(offset.X);
offset.Y = offset.Y > 0 ? Mathf.Floor(offset.Y) : Mathf.Ceil(offset.Y);
_mouseDeltaLeft = offset / size;
_startPos = _viewMousePos;
}
else
{
_mouseDeltaLeft = Vector2.Zero;
}
_input.MouseWheelDelta = 0;
}

View File

@@ -342,7 +342,7 @@ namespace FlaxEditor.Viewport
public bool SnapToGround => false;
/// <inheritdoc />
public Vector2 MouseDelta => _mouseDeltaLeft * 1000;
public Vector2 MouseDelta => _mouseDelta * 1000;
/// <inheritdoc />
public bool UseSnapping => Root.GetKey(KeyboardKeys.Control);