Fix snap to the ground to use scene graph query instead of physics only raycast

This commit is contained in:
Wojtek Figat
2021-08-12 13:59:30 +02:00
parent 98c43dba42
commit c3b9f61b9f
8 changed files with 92 additions and 11 deletions

View File

@@ -80,6 +80,13 @@ namespace FlaxEditor.Gizmo
{ {
} }
/// <summary>
/// Performs scene objects snapping to the ground.
/// </summary>
public virtual void SnapToGround()
{
}
/// <summary> /// <summary>
/// Draws the gizmo. /// Draws the gizmo.
/// </summary> /// </summary>

View File

@@ -89,5 +89,10 @@ namespace FlaxEditor.Gizmo
/// Gets a <see cref="FlaxEditor.Undo"/> object used by the gizmo owner. /// Gets a <see cref="FlaxEditor.Undo"/> object used by the gizmo owner.
/// </summary> /// </summary>
Undo Undo { get; } Undo Undo { get; }
/// <summary>
/// Gets the root tree node for the scene graph.
/// </summary>
SceneGraph.RootNode SceneGraphRoot { get; }
} }
} }

View File

@@ -218,6 +218,12 @@ namespace FlaxEditor.Gizmo
} }
} }
/// <inheritdoc />
protected override bool IsSelected(SceneGraphNode obj)
{
return _selection.Contains(obj);
}
/// <inheritdoc /> /// <inheritdoc />
protected override void OnApplyTransformation(ref Vector3 translationDelta, ref Quaternion rotationDelta, ref Vector3 scaleDelta) protected override void OnApplyTransformation(ref Vector3 translationDelta, ref Quaternion rotationDelta, ref Vector3 scaleDelta)
{ {

View File

@@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using FlaxEditor.SceneGraph;
using FlaxEngine; using FlaxEngine;
namespace FlaxEditor.Gizmo namespace FlaxEditor.Gizmo
@@ -398,15 +399,7 @@ namespace FlaxEditor.Gizmo
// Snap to ground // Snap to ground
if (_activeAxis == Axis.None && SelectionCount != 0 && Owner.SnapToGround) if (_activeAxis == Axis.None && SelectionCount != 0 && Owner.SnapToGround)
{ {
if (Physics.RayCast(Position, Vector3.Down, out var hit, float.MaxValue, uint.MaxValue, false)) SnapToGround();
{
StartTransforming();
var translationDelta = hit.Point - Position;
var rotationDelta = Quaternion.Identity;
var scaleDelta = Vector3.Zero;
OnApplyTransformation(ref translationDelta, ref rotationDelta, ref scaleDelta);
EndTransforming();
}
} }
// Only when is active // Only when is active
else if (_isActive) else if (_isActive)
@@ -517,6 +510,39 @@ namespace FlaxEditor.Gizmo
UpdateMatrices(); UpdateMatrices();
} }
/// <inheritdoc />
public override void SnapToGround()
{
if (Owner.SceneGraphRoot == null)
return;
var ray = new Ray(Position, Vector3.Down);
while (true)
{
var view = new Ray(Owner.ViewPosition, Owner.ViewDirection);
var rayCastFlags = SceneGraphNode.RayCastData.FlagTypes.SkipEditorPrimitives;
var hit = Owner.SceneGraphRoot.RayCast(ref ray, ref view, out var distance, out _, rayCastFlags);
if (hit != null)
{
// Skip snapping selection to itself
if (IsSelected(hit))
{
GetSelectedObjectsBounds(out var selectionBounds, out _);
ray.Position = ray.GetPoint(selectionBounds.Size.Y * 0.5f);
continue;
}
// Snap
StartTransforming();
var translationDelta = ray.GetPoint(distance) - Position;
var rotationDelta = Quaternion.Identity;
var scaleDelta = Vector3.Zero;
OnApplyTransformation(ref translationDelta, ref rotationDelta, ref scaleDelta);
EndTransforming();
}
break;
}
}
/// <summary> /// <summary>
/// Gets a value indicating whether this tool can transform objects. /// Gets a value indicating whether this tool can transform objects.
/// </summary> /// </summary>
@@ -545,6 +571,13 @@ namespace FlaxEditor.Gizmo
/// <param name="navigationDirty">True if editing the selected objects transformations marks the navigation system area dirty (for auto-rebuild), otherwise skip update.</param> /// <param name="navigationDirty">True if editing the selected objects transformations marks the navigation system area dirty (for auto-rebuild), otherwise skip update.</param>
protected abstract void GetSelectedObjectsBounds(out BoundingBox bounds, out bool navigationDirty); protected abstract void GetSelectedObjectsBounds(out BoundingBox bounds, out bool navigationDirty);
/// <summary>
/// Checks if the specified object is selected.
/// </summary>
/// <param name="obj">The object to check.</param>
/// <returns>True if it's selected, otherwise false.</returns>
protected abstract bool IsSelected(SceneGraphNode obj);
/// <summary> /// <summary>
/// Called when user starts transforming selected objects. /// Called when user starts transforming selected objects.
/// </summary> /// </summary>

View File

@@ -2,6 +2,7 @@
using System; using System;
using FlaxEditor.Gizmo; using FlaxEditor.Gizmo;
using FlaxEditor.SceneGraph;
using FlaxEditor.Tools.Foliage.Undo; using FlaxEditor.Tools.Foliage.Undo;
using FlaxEngine; using FlaxEngine;
@@ -87,6 +88,12 @@ namespace FlaxEditor.Tools.Foliage
navigationDirty = false; navigationDirty = false;
} }
/// <inheritdoc />
protected override bool IsSelected(SceneGraphNode obj)
{
return false;
}
/// <inheritdoc /> /// <inheritdoc />
protected override void OnStartTransforming() protected override void OnStartTransforming()
{ {
@@ -231,6 +238,21 @@ namespace FlaxEditor.Tools.Foliage
Owner.Undo?.AddAction(action); Owner.Undo?.AddAction(action);
} }
/// <inheritdoc />
public override void SnapToGround()
{
if (Physics.RayCast(Position, Vector3.Down, out var hit, float.MaxValue, uint.MaxValue, false))
{
// Snap
StartTransforming();
var translationDelta = hit.Point - Position;
var rotationDelta = Quaternion.Identity;
var scaleDelta = Vector3.Zero;
OnApplyTransformation(ref translationDelta, ref rotationDelta, ref scaleDelta);
EndTransforming();
}
}
/// <inheritdoc /> /// <inheritdoc />
public override void OnActivated() public override void OnActivated()
{ {

View File

@@ -21,10 +21,12 @@ namespace FlaxEditor.Viewport
/// </summary> /// </summary>
/// <param name="task">The task.</param> /// <param name="task">The task.</param>
/// <param name="undo">The undo.</param> /// <param name="undo">The undo.</param>
public EditorGizmoViewport(SceneRenderTask task, Undo undo) /// <param name="sceneGraphRoot">The scene graph root.</param>
public EditorGizmoViewport(SceneRenderTask task, Undo undo, SceneGraph.RootNode sceneGraphRoot)
: base(task, new FPSCamera(), true) : base(task, new FPSCamera(), true)
{ {
Undo = undo; Undo = undo;
SceneGraphRoot = sceneGraphRoot;
SetUpdate(ref _update, OnUpdate); SetUpdate(ref _update, OnUpdate);
} }
@@ -73,6 +75,9 @@ namespace FlaxEditor.Viewport
/// <inheritdoc /> /// <inheritdoc />
public Undo Undo { get; } public Undo Undo { get; }
/// <inheritdoc />
public SceneGraph.RootNode SceneGraphRoot { get; }
/// <inheritdoc /> /// <inheritdoc />
protected override bool IsControllingMouse => Gizmos.Active?.IsControllingMouse ?? false; protected override bool IsControllingMouse => Gizmos.Active?.IsControllingMouse ?? false;

View File

@@ -184,7 +184,7 @@ namespace FlaxEditor.Viewport
/// </summary> /// </summary>
/// <param name="editor">Editor instance.</param> /// <param name="editor">Editor instance.</param>
public MainEditorGizmoViewport(Editor editor) public MainEditorGizmoViewport(Editor editor)
: base(Object.New<SceneRenderTask>(), editor.Undo) : base(Object.New<SceneRenderTask>(), editor.Undo, editor.Scene.Root)
{ {
_editor = editor; _editor = editor;
_dragAssets = new DragAssets<DragDropEventArgs>(ValidateDragItem); _dragAssets = new DragAssets<DragDropEventArgs>(ValidateDragItem);

View File

@@ -336,6 +336,9 @@ namespace FlaxEditor.Viewport
/// <inheritdoc /> /// <inheritdoc />
public Undo Undo { get; } public Undo Undo { get; }
/// <inheritdoc />
public RootNode SceneGraphRoot => _window.Graph.Root;
/// <inheritdoc /> /// <inheritdoc />
public EditorViewport Viewport => this; public EditorViewport Viewport => this;