Add improvements for objects spawning and snapping in Editor to include object bounds
This commit is contained in:
@@ -85,6 +85,56 @@ namespace FlaxEditor.Gizmo
|
||||
return node;
|
||||
}
|
||||
|
||||
/// <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
|
||||
bool isSelected = false;
|
||||
for (var e = hit; e != null && !isSelected; e = e.ParentNode)
|
||||
isSelected |= IsSelected(e);
|
||||
if (isSelected)
|
||||
{
|
||||
GetSelectedObjectsBounds(out var selectionBounds, out _);
|
||||
ray.Position = ray.GetPoint(selectionBounds.Size.Y * 0.5f);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Include objects bounds into target snap location
|
||||
var editorBounds = BoundingBox.Empty;
|
||||
var bottomToCenter = 100000.0f;
|
||||
for (int i = 0; i < _selectionParents.Count; i++)
|
||||
{
|
||||
if (_selectionParents[i] is ActorNode actorNode)
|
||||
{
|
||||
var b = actorNode.Actor.EditorBoxChildren;
|
||||
BoundingBox.Merge(ref editorBounds, ref b, out editorBounds);
|
||||
bottomToCenter = Mathf.Min(bottomToCenter, actorNode.Actor.Position.Y - editorBounds.Minimum.Y);
|
||||
}
|
||||
}
|
||||
var newPosition = ray.GetPoint(distance) + new Vector3(0, bottomToCenter, 0);
|
||||
|
||||
// Snap
|
||||
StartTransforming();
|
||||
var translationDelta = newPosition - Position;
|
||||
var rotationDelta = Quaternion.Identity;
|
||||
var scaleDelta = Vector3.Zero;
|
||||
OnApplyTransformation(ref translationDelta, ref rotationDelta, ref scaleDelta);
|
||||
EndTransforming();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Pick()
|
||||
{
|
||||
@@ -211,10 +261,7 @@ namespace FlaxEditor.Gizmo
|
||||
if (_selectionParents[i] is ActorNode actorNode)
|
||||
{
|
||||
bounds = BoundingBox.Merge(bounds, actorNode.Actor.BoxWithChildren);
|
||||
if (actorNode.AffectsNavigationWithChildren)
|
||||
{
|
||||
navigationDirty |= actorNode.Actor.HasStaticFlag(StaticFlags.Navigation);
|
||||
}
|
||||
navigationDirty |= actorNode.AffectsNavigationWithChildren;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -510,42 +510,6 @@ 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
|
||||
bool isSelected = false;
|
||||
for (var e = hit; e != null && !isSelected; e = e.ParentNode)
|
||||
isSelected |= IsSelected(e);
|
||||
if (isSelected)
|
||||
{
|
||||
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>
|
||||
|
||||
Reference in New Issue
Block a user