x


How to determine bounding box of scene?

My objective is to determine the total dimensions of a scene.

Will I need to write a script to iterate through each object in the scene and continue adding each to an iteratively expanded "box", or is there a more elegant method available?

Thanks,

-Aubrey

more ▼

asked Dec 06 '09 at 05:51 PM

Aubrey Falconer gravatar image

Aubrey Falconer
657 45 53 68

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I don't think there is a more elegant method, but then, that script should be pretty short:

Bounds b = new Bounds(Vector3.zero, Vector3.zero);
foreach (Renderer r in FindObjectsOfType(typeof(Renderer))
    b = b.Encapsulate(r.bounds);

If you want to do this every frame, you may consider caching the array of renderers, since FindObjectsOfType is slow, though.

more ▼

answered Dec 06 '09 at 05:57 PM

jonas echterhoff gravatar image

jonas echterhoff ♦♦
9.8k 7 23 104

Excellent! Far more elegant than the script I was envisioning :) Thanks again!

Yes, I only plan to run this script once

Dec 06 '09 at 06:04 PM Aubrey Falconer

if you have objects that they have colliders without renderers, you should take care of them

Dec 06 '09 at 06:49 PM Ashkan_gc

You need to set the initial value of Bounds b based on the first Renderer. Otherwise, b.min values might stay zero if there are no negative coords. Similar for b.max if there aren't positive coords.

Sep 08 '10 at 12:09 AM HeywoodFloyd
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5084
x2089
x718

asked: Dec 06 '09 at 05:51 PM

Seen: 2453 times

Last Updated: Dec 06 '09 at 05:51 PM