← All math tools

Combined Bounds Center

Combine world-space renderer bounds to find the center and full size of the smallest axis-aligned box that contains them.

View the Unity C# source on GitHub

3D view

Drag an orange handle to move it, or drag an arrow to move along one axis. Drag the background to orbit. Results are drawn in green.

Result

Inputs

Object position (used when there are no renderer bounds)

Renderer bounds

Center
Size
Center
Size

Unity C#

The same calculation as a Unity C# helper.

using UnityEngine;

public static class CombinedBounds
{
    public static Bounds Combine(Vector3 objectPosition, params Bounds[] rendererBounds)
    {
        if (rendererBounds.Length == 0)
            return new Bounds(objectPosition, Vector3.zero);

        Bounds result = rendererBounds[0];
        for (int i = 1; i < rendererBounds.Length; i++)
            result.Encapsulate(rendererBounds[i]);
        return result;
    }

    public static Bounds FromRenderers(Transform root)
    {
        Renderer[] renderers = root.GetComponentsInChildren<Renderer>();
        var bounds = new Bounds[renderers.Length];
        for (int i = 0; i < renderers.Length; i++)
            bounds[i] = renderers[i].bounds;
        return Combine(root.position, bounds);
    }
}