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 d77ad33583
commit 7656cf541d
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>
/// Draws the gizmo.
/// </summary>

View File

@@ -89,5 +89,10 @@ namespace FlaxEditor.Gizmo
/// Gets a <see cref="FlaxEditor.Undo"/> object used by the gizmo owner.
/// </summary>
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 />
protected override void OnApplyTransformation(ref Vector3 translationDelta, ref Quaternion rotationDelta, ref Vector3 scaleDelta)
{

View File

@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using FlaxEditor.SceneGraph;
using FlaxEngine;
namespace FlaxEditor.Gizmo
@@ -398,15 +399,7 @@ namespace FlaxEditor.Gizmo
// Snap to ground
if (_activeAxis == Axis.None && SelectionCount != 0 && Owner.SnapToGround)
{
if (Physics.RayCast(Position, Vector3.Down, out var hit, float.MaxValue, uint.MaxValue, false))
{
StartTransforming();
var translationDelta = hit.Point - Position;
var rotationDelta = Quaternion.Identity;
var scaleDelta = Vector3.Zero;
OnApplyTransformation(ref translationDelta, ref rotationDelta, ref scaleDelta);
EndTransforming();
}
SnapToGround();
}
// Only when is active
else if (_isActive)
@@ -517,6 +510,39 @@ namespace FlaxEditor.Gizmo
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>
/// Gets a value indicating whether this tool can transform objects.
/// </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>
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>
/// Called when user starts transforming selected objects.
/// </summary>

View File

@@ -2,6 +2,7 @@
using System;
using FlaxEditor.Gizmo;
using FlaxEditor.SceneGraph;
using FlaxEditor.Tools.Foliage.Undo;
using FlaxEngine;
@@ -87,6 +88,12 @@ namespace FlaxEditor.Tools.Foliage
navigationDirty = false;
}
/// <inheritdoc />
protected override bool IsSelected(SceneGraphNode obj)
{
return false;
}
/// <inheritdoc />
protected override void OnStartTransforming()
{
@@ -231,6 +238,21 @@ namespace FlaxEditor.Tools.Foliage
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 />
public override void OnActivated()
{

View File

@@ -21,10 +21,12 @@ namespace FlaxEditor.Viewport
/// </summary>
/// <param name="task">The task.</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)
{
Undo = undo;
SceneGraphRoot = sceneGraphRoot;
SetUpdate(ref _update, OnUpdate);
}
@@ -73,6 +75,9 @@ namespace FlaxEditor.Viewport
/// <inheritdoc />
public Undo Undo { get; }
/// <inheritdoc />
public SceneGraph.RootNode SceneGraphRoot { get; }
/// <inheritdoc />
protected override bool IsControllingMouse => Gizmos.Active?.IsControllingMouse ?? false;

View File

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

View File

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