x


Is there an easy way to get on-screen render size (bounds)?

I need to quickly find the on-screen bounding box of a given object or mesh. Do I have to iterate convert all the points to world space and compute my own?

more ▼

asked Mar 01 '11 at 12:34 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

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

2 answers: sort newest

If anyone is interested in Matt's solution, here is my implementation.

public Rect BoundsToScreenRect(Bounds bounds)
{
    // Get mesh origin and farthest extent (this works best with simple convex meshes)
    Vector3 origin = Camera.main.WorldToScreenPoint(new Vector3(bounds.min.x, bounds.max.y, 0f));
    Vector3 extent = Camera.main.WorldToScreenPoint(new Vector3(bounds.max.x, bounds.min.y, 0f));

    // Create rect in screen space and return - does not account for camera perspective
    return new Rect(origin.x, Screen.height - origin.y, extent.x - origin.x, origin.y - extent.y);
}
more ▼

answered May 07 '12 at 03:25 PM

karl_ gravatar image

karl_
2.4k 41 53 70

Many thanks, Have pinched the fragment entire just to save me rebuilding this particular wheel

Nov 02 '12 at 02:05 PM Duke Euphoria

...annnnd as an extension method :

using UnityEngine;
using System.Collections;

public static class BoundsExtensions  
{
    public static Rect ToScreenSpace(this Bounds bounds, Camera camera)
    {
        var origin = camera.WorldToScreenPoint(new Vector3(bounds.min.x, bounds.min.y, 0.0f));
        var extents = camera.WorldToScreenPoint(new Vector3(bounds.max.x, bounds.min.y, 0.0f));

        return new Rect(origin.x, Screen.height - origin.y, extents.x - origin.x, origin.y - extents.y);
    }
}
Apr 07 at 02:41 AM duke
(comments are locked)
10|3000 characters needed characters left

You could get the bounding volume using Renderer.bounds, then convert those coordinates to screen coordinates. The min and max of the screen X & Y coordinates will be a bounding box.

However, this is only an approximation. Because it is axis-aligned, AND because the camera angle will distort the shape further. You might end up with a box that's bigger than necessary depending on the shape. It will work better for compact, convex objects.

more ▼

answered Mar 01 '11 at 12:55 AM

Bampf gravatar image

Bampf
5k 8 19 49

Right, I thought of that, and passed on it for those reasons. What I'm going for is the old 'trombone' shot used by Steven Spielberg and the like, where the 'target object' stays the same size on screen, but the camera moves way back while zooming in at the same time (or vice versa). That approximation would mess a bit with the actual extent of the rendered object. I guess I should try it, but I think for my purposes it will be too distorted.

Mar 02 '11 at 12:09 AM DaveA

Actually, this effect was invented by Alfred Hitchcock in Vertigo.

Apr 05 '12 at 02:47 PM PatHightree
(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:

x1366
x488
x161
x96

asked: Mar 01 '11 at 12:34 AM

Seen: 4718 times

Last Updated: Apr 07 at 02:42 AM