Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity function to access the 2D box immediately from the 3D pipeline?

In Unity, say you have a 3D object,

enter image description here

Of course, it's trivial to get the AABB, Unity has direct functions for that,

enter image description here

(You might have to "add up all the bounding boxes of the renderers" in the usual way, no issue.)

So Unity does indeed have a direct function to give you the 3D AABB box instantly, out of the internal mesh/render pipeline every frame.

Now, for the Camera in question, as positioned, that AABB indeed covers a certain 2D bounding box ...

enter image description here

In fact ... is there some sort of built-in direct way to find that orange 2D box in Unity??

Question - does Unity have a function which immediately gives that 2D frustrum box from the pipeline?

(Note that to do it manually you just make rays (or use world to screen space as Draco mentions, same) for the 8 points of the AABB; encapsulate those in 2D to make the orange box.)

I don't need a manual solution, I'm asking if the engine gives this somehow from the pipeline every frame?

Is there a call?

(Indeed, it would be even better to have this ...)

enter image description here

My feeling is that one or all of the

  • occlusion system in particular
  • the shaders
  • the renderer

would surely know the orange box, and perhaps even the blue box inside the pipeline, right off the graphics card, just as it knows the AABB for a given mesh.

We know that Unity lets you tap the AABB 3D box instantly every frame for a given mesh: In fact does Unity give the "2D frustrum bound" as shown here?

like image 721
Fattie Avatar asked Aug 18 '18 06:08

Fattie


1 Answers

As far as I am aware, there is no built in for this.

However, finding the extremes yourself is really pretty easy. Getting the mesh's bounding box (the cuboid shown in the screenshot) is just how this is done, you're just doing it in a transformed space.

  1. Loop through all the verticies of the mesh, doing the following:
  2. Transform the point from local to world space (this handles dealing with scale and rotation)
  3. Transform the point from world space to screen space
  4. Determine if the new point's X and Y are above/below the stored min/max values, if so, update the stored min/max with the new value
  5. After looping over all vertices, you'll have 4 values: min-X, min-Y, max-X, and max-Y. Now you can construct your bounding rectangle

You may also wish to first perform a Gift Wrapping of the model first, and only deal with the resulting convex hull (as no points not part of the convex hull will ever be outside the bounds of the convex hull). If you intend to draw this screen space rectangle while the model moves, scales, or rotates on screen, and have to recompute the bounding box, then you'll want to do this and cache the result.

Note that this does not work if the model animates (e.g. if your humanoid stands up and does jumping jacks). Solving for the animated case is much more difficult, as you would have to treat every frame of every animation as part of the original mesh for the purposes of the convex hull solving (to insure that none of your animations ever move a part of the mesh outside the convex hull), increasing the complexity by a power.

like image 124
Draco18s no longer trusts SE Avatar answered Nov 01 '22 00:11

Draco18s no longer trusts SE