Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Broken bounds calculation

Discussion in 'Scripting' started by ProtagonistKun, Jan 17, 2023.

  1. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    Could someone please tell me what the hell is going on with this bounding box I tried to generate... It's way too big on the right side and that makes no sense. There are no hidden objects in the target. I am trying to calculate the bounding box myself, because I need the bounds of the entire thing, not a single meshrenderer

    Code (CSharp):
    1. var parentPosition = ARManager.Instance.instanceController.sceneRoot.transform.position;
    2.         var parentRotation = ARManager.Instance.instanceController.sceneRoot.transform.rotation;
    3.        
    4.         ARManager.Instance.instanceController.sceneRoot.transform.position = Vector3.zero;
    5.         ARManager.Instance.instanceController.sceneRoot.transform.rotation = Quaternion.identity;
    6.        
    7.         Renderer[] renderers = target.GetComponentsInChildren<Renderer>();
    8.         var bounds = renderers[0].bounds;
    9.         foreach (Renderer renderer in renderers.Where(x => x.gameObject.activeInHierarchy))
    10.         {
    11.             bounds.Encapsulate(renderer.bounds);
    12.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,707
  3. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Try using mesh.bounds instead. Those are in the local space of the mesh, instead of in worldspace
    https://docs.unity3d.com/ScriptReference/Mesh-bounds.html
     
  5. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    Using mesh.bounds is better, but then the other side is slightly wrong. Is there no way to calculate the actual dimensions of the model as it is displayed? as in, from the left-most vertex to the right-most vertex? (without reading the meshdata preferably)
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    Over here I've drawn a diagram how the actual mesh, the local bounds (aka mesh.bounds) and the worldspace bounds (aka renderer.bounds) essentially belong together. Depending on your actual mesh layout and how it's oriented in local space, the resulting AABB may not be what you have in mind. An AABB is always axis aligned. So getting the AABB of a rotated bounds would always end up larger. If you need a tighter bounds, you would need to encapsulate the vertices manually to actually construct a fitting bounding box for a certain orientation. Just always keep in mind that the bounds of the mesh lives in local space of the mesh while the renderer bounds is the encapsulation of the rotated mesh bounds. The better the mesh bounds fits the actual mesh, the better. Though keep in mind that both are always axis aligned. So if ou have a diagonal placed mesh in localspace (like in my example drawing), the bounds would never nicely match.

     
    Kurt-Dekker and halley like this.
  7. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,859
    And if you're frustrated by the whole point of this AABB nonsense, it's the fastest way to see if any part of the mesh could possibly be inside your viewing frustum or raycast. It's meant to act as a very fast/inexpensive screening check. AABBs work really well with octrees to do fast geometry searches. If it tries the test and says no hit, great, we know really quickly. If it says the test hit the AABB, then it will go ahead and do the necessary expensive extra work to see if it really hit the collider or whatever.