Merge branch 'boxcollider-autosize' of https://github.com/Menotdan/FlaxEngine into Menotdan-boxcollider-autosize

This commit is contained in:
Wojtek Figat
2023-12-14 10:49:46 +01:00
2 changed files with 89 additions and 0 deletions

View File

@@ -8,8 +8,33 @@ using Real = System.Single;
using FlaxEngine; using FlaxEngine;
#if FLAX_EDITOR
using FlaxEditor.CustomEditors.Dedicated;
using FlaxEditor.CustomEditors;
#endif
namespace FlaxEditor.SceneGraph.Actors namespace FlaxEditor.SceneGraph.Actors
{ {
#if FLAX_EDITOR
/// <summary>
/// Dedicated custom editor for BoxCollider objects.
/// </summary>
[CustomEditor(typeof(BoxCollider)), DefaultEditor]
public class BoxColliderEditor : ActorEditor
{
/// <inheritdoc />
public override void Initialize(LayoutElementsContainer layout)
{
base.Initialize(layout);
layout.Space(20f);
var autoResizeButton = layout.Button("Resize to Fit", "Resize the box collider to fit it's parent's bounds.");
BoxCollider collider = Values[0] as BoxCollider;
autoResizeButton.Button.Clicked += collider.AutoResize;
}
}
#endif
/// <summary> /// <summary>
/// Scene tree node for <see cref="BoxCollider"/> actor type. /// Scene tree node for <see cref="BoxCollider"/> actor type.
/// </summary> /// </summary>
@@ -37,5 +62,13 @@ namespace FlaxEditor.SceneGraph.Actors
return base.RayCastSelf(ref ray, out distance, out normal); return base.RayCastSelf(ref ray, out distance, out normal);
} }
/// <inheritdoc />
public override void PostSpawn()
{
base.PostSpawn();
BoxCollider boxCollider = Actor as BoxCollider;
boxCollider.AutoResize();
}
} }
} }

View File

@@ -0,0 +1,56 @@
#if FLAX_EDITOR
using FlaxEditor.CustomEditors;
using FlaxEditor.CustomEditors.Dedicated;
#endif
namespace FlaxEngine
{
partial class BoxCollider
{
private void BoxExcluding(Actor target, ref BoundingBox output, Actor excluded)
{
foreach (Actor child in target.Children)
{
if (child == excluded)
{
continue;
}
output = BoundingBox.Merge(output, child.Box);
BoxExcluding(child, ref output, excluded);
}
}
/// <summary>
/// Resizes the box collider based on the bounds of it's parent.
/// </summary>
public void AutoResize()
{
if (Parent is Scene)
{
return;
}
LocalPosition = Vector3.Zero;
Vector3 parentScale = Parent.Scale;
BoundingBox parentBox = Parent.Box;
BoxExcluding(Parent, ref parentBox, this);
Vector3 parentSize = parentBox.Size;
Vector3 parentCenter = parentBox.Center - Parent.Position;
// Avoid division by zero
if (parentScale.X == 0 || parentScale.Y == 0 || parentScale.Z == 0)
{
return;
}
Size = parentSize / parentScale;
Center = parentCenter / parentScale;
// Undo Rotation
Orientation *= Quaternion.Invert(Orientation);
}
}
}