public Bounds CalculateBounds()
{
IReadOnlyList<SpawnableInfo> children = GetComponentsInChildren<SpawnableInfo>();
bool initialized = false;
Bounds result = default;
foreach (SpawnableInfo child in children)
{
if (child == this)
continue;
Bounds childBounds = child switch
{
MeowEditorPrimitive primitive => primitive.GetBoundingBox(),
MeowEditorLight light => light.GetBoundingBox(),
MeowEditorCapybara capybara => capybara.GetBoundingBox(),
MeowEditorCulling nested => nested.CalculateBounds(),
_ => default
};
if (childBounds.size == Vector3.zero)
{
if (!Extensions.TryGetColliderBounds(child, out childBounds)
&& !Extensions.TryGetRendererBounds(child, out childBounds)
&& !Extensions.TryGetMeshFilterBounds(child, out childBounds))
{
#if !EDITOR
Vector3 fallbackCenter = child.Position;
Vector3 fallbackSize = child.Scale;
#else
Transform childTransform = child.transform;
Vector3 fallbackCenter = childTransform.position;
Vector3 fallbackSize = childTransform.lossyScale;
#endif
if (fallbackSize == Vector3.zero)
fallbackSize = Vector3.one;
childBounds = new Bounds(fallbackCenter, fallbackSize);
}
}
if (!initialized)
{
result = childBounds;
initialized = true;
}
else
{
result.Encapsulate(childBounds);
}
}
if (!initialized)
{
if (!Extensions.TryGetColliderBounds(this, out result)
&& !Extensions.TryGetRendererBounds(this, out result)
&& !Extensions.TryGetMeshFilterBounds(this, out result))
{
Vector3 size = transform.lossyScale;
if (size == Vector3.zero)
size = Vector3.one;
result = new Bounds(transform.position, size);
}
initialized = true;
}
if (initialized && BoundsPadding != Vector3.zero)
{
Vector3 extents = result.extents + BoundsPadding;
result.extents = new Vector3(
Mathf.Max(extents.x, 0f),
Mathf.Max(extents.y, 0f),
Mathf.Max(extents.z, 0f)
);
}
return result;
}