Upload project files
This commit is contained in:
88
Editor/PrefabPainter/Utilities/BoundUtilities.cs
Normal file
88
Editor/PrefabPainter/Utilities/BoundUtilities.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Misaki.ArtToolEditor
|
||||
{
|
||||
internal class BoundUtilities
|
||||
{
|
||||
internal struct SphereBounds
|
||||
{
|
||||
internal Vector3 position;
|
||||
internal float radius;
|
||||
|
||||
/// <summary>
|
||||
/// Create a sphere bound based on a position and a radius.
|
||||
/// </summary>
|
||||
/// <param name="p">Position</param>
|
||||
/// <param name="r">Radius</param>
|
||||
internal SphereBounds(Vector3 p, float r)
|
||||
{
|
||||
position = p;
|
||||
radius = r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return true if the other sphere is in the radius of this sphere bound.
|
||||
/// </summary>
|
||||
/// <param name="other"></param>
|
||||
/// <returns></returns>
|
||||
internal bool Intersects(SphereBounds other)
|
||||
{
|
||||
return Vector3.Distance(position, other.position) < (radius + other.radius);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a spherical bounds inside of a Renderer bounds.
|
||||
/// It'll take every meshes found in the hierarchy into account.
|
||||
/// </summary>
|
||||
/// <param name="go">GameObject having Renderer component(s).</param>
|
||||
/// <param name="bounds">Sphere bounds instance in which sphere information will be store.</param>
|
||||
/// <returns>True if bounds data has been set.</returns>
|
||||
internal static bool GetSphereBounds(GameObject go, out SphereBounds bounds)
|
||||
{
|
||||
var entireObjectBounds = GetHierarchyBounds(go);
|
||||
|
||||
bounds = default(SphereBounds);
|
||||
|
||||
if (entireObjectBounds.size == Vector3.zero)
|
||||
return false;
|
||||
|
||||
bounds.position = entireObjectBounds.center;
|
||||
bounds.radius = Mathf.Max(entireObjectBounds.extents.x, entireObjectBounds.extents.z);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create bounds based on meshes found in the given GameObject hierarchy.
|
||||
/// </summary>
|
||||
/// <param name="parent">Root object.</param>
|
||||
/// <returns>New bounds around the given GameObject.
|
||||
/// If GameObject has no Renderer, bounds will have a size of zero.</returns>
|
||||
internal static Bounds GetHierarchyBounds(GameObject parent)
|
||||
{
|
||||
var renderers = parent.GetComponentsInChildren<Renderer>();
|
||||
|
||||
var bounds = default(Bounds);
|
||||
|
||||
if (renderers.Length == 0)
|
||||
return bounds;
|
||||
|
||||
for (var i = 0; i < renderers.Length; ++i)
|
||||
{
|
||||
var it = renderers[i].bounds;
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
// World position
|
||||
bounds.center = it.center;
|
||||
}
|
||||
|
||||
bounds.Encapsulate(it.max);
|
||||
bounds.Encapsulate(it.min);
|
||||
}
|
||||
|
||||
return bounds;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user