Search Unity

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

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Recalculating renderer.bounds

Discussion in 'Editor & General Support' started by AndrewBrechin, Feb 22, 2011.

  1. AndrewBrechin

    AndrewBrechin

    Joined:
    Jan 11, 2010
    Posts:
    8
    Hi,

    I am instantiating an animating object and setting the local scale to zero and scaling up over time.
    When I zoom into the object (move the camera closer) it disappears.
    If I leave out the scaling up and create it at the right size the zooming works correctly.

    I discovered that it is due to the renderer.bounds being zero and culling the object when going off screen.

    How do I recalculate renderer.bounds when my object has scaled up to the desired size?

    I have tried using mesh.RecalculateBounds() and the mesh bounds is always correct, just not the renderer.bounds.

    Cheers

    Andrew
     
  2. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    1,877
    Well I have perhaps a similar issue with child objects being culled far too early because of [presumably] innacurate bounding box calculations
    I posted a bug for it back in December (#386906).

    You can tick the 'Update When Offscreen' box on the Skinned Mesh Render and see if that fixes it, but this seems a hack...

    Hoping fix in next update...
     
  3. AndrewBrechin

    AndrewBrechin

    Joined:
    Jan 11, 2010
    Posts:
    8
    Setting 'Update When Off screen' flag does fix this but it is impractical to use as I am wanting a lot of objects to use this "appear" method in my scene.
     
  4. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    1,877
    Exactly... Hoping Unity fix this, but Ive had no feedback on any bugs posted...
     
  5. AndrewBrechin

    AndrewBrechin

    Joined:
    Jan 11, 2010
    Posts:
    8
    Does anyone know what else triggers it to recalculate if there is no direct function?
     
    CloudyVR likes this.
  6. AndrewBrechin

    AndrewBrechin

    Joined:
    Jan 11, 2010
    Posts:
    8
    I have discovered a TEMPORARY fix, if I set updateWhenOffscreen to true then change it back to false when I have scaled to the desired size the renderer.bounds are correct.
     
  7. KingSharaman

    KingSharaman

    Joined:
    Aug 19, 2011
    Posts:
    4
    Use RecalculateBounds() after you assign the mesh to a MeshFilter component. So for example in C#:

    GetComponent<MeshFilter>().mesh = aMeshObject;
    aMeshObject.RecalculateBounds();

    This way it will recalculate renderer bounds.
     
  8. -brainer-

    -brainer-

    Joined:
    Sep 8, 2013
    Posts:
    2
    Bug still persists. Calling RecalculateBounds on meshes does not update the RendererBounds.
     
  9. ChristmasEve

    ChristmasEve

    Joined:
    Apr 21, 2015
    Posts:
    41
    I'm running into this exact problem right now. Have you found a solution yet since 2013? I cannot see my procedurally created mesh and I'm assuming it is because it thinks my bounds are 0 x 0 x 0. As you said, calling RecalculateBounds() on the attached mesh does *not* update the Renderer Bounds. This really sucks. I cannot generate a mesh procedurally because of this bug :(
     
  10. unity_PLHfJs-vCd4KHA

    unity_PLHfJs-vCd4KHA

    Joined:
    Jan 17, 2018
    Posts:
    2
    I'm running into this exact problem... But it's 2018...
     
  11. januszbadziej1965

    januszbadziej1965

    Joined:
    Mar 5, 2018
    Posts:
    13
    Same here... are they still fixing it it... or what's happening?
     
  12. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    28,885
    Post case numbers in case it is a related but not same issue.
     
  13. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,184
    The issue still exists, however setting updateWhenOffscreen to false before setting/changing the mesh, then setting updateWhenOffscreen to true seems to work.
     
  14. JustinAtSama

    JustinAtSama

    Joined:
    Mar 22, 2019
    Posts:
    5
    @hippocoder
    5/7/2019

    I have to say the problem still exists.I may have a similar but different problem however. When I have a parent transform with no mesh and a sphere collider encapuslating children transforms with their own meshes, forcing a recalculate works fine. When I rotate the parent transform, the recalculate gives a bigger bounds thus making my collider much bigger than it should be.
     
    bradley_newman_aofl likes this.
  15. JustinAtSama

    JustinAtSama

    Joined:
    Mar 22, 2019
    Posts:
    5
    Well I solved my own issue. If you have a parent object that is setting the collider size by encapsulting its children mesh bounds, every time you end up changing the scale of the children and want to reflect that change to the collider on the parent, you must rotate the parent back to its original rotation; encapsulate all children meshes again; then rotate the parent back.

    Code (CSharp):
    1. protected virtual void RefreshColliderSize() {
    2.         // cache the rotation
    3.         cacheRotation = transform.rotation;
    4.         // rotate to original
    5.         transform.rotation = initialRotation;
    6.  
    7.         Bounds b = new Bounds(transform.position, Vector3.zero);
    8.         int index = transform.childCount;
    9.         while (index > 0) {
    10.             index--;
    11.             MeshRenderer meshRenderer = transform.GetChild(index).GetComponent<MeshRenderer>();
    12.             if (meshRenderer.enabled) {
    13.                 b.Encapsulate(meshRenderer.bounds);
    14.             }
    15.         }
    16.         // get the max extents
    17.         float maxExtents = 0f;
    18.         if (b.extents.x > maxExtents) { maxExtents = b.extents.x; }
    19.         if (b.extents.y > maxExtents) { maxExtents = b.extents.y; }
    20.         if (b.extents.z > maxExtents) { maxExtents = b.extents.z; }
    21.         // since this object's parent is scaled down we need to reverse it
    22.         sphereCollider.radius = maxExtents / transform.lossyScale.x;
    23.         // rotate back
    24.         transform.rotation = cacheRotation;
    25.     }
    In this case initialRotation is Quaternion.Identity. Hope this helps!
     
    Last edited: May 8, 2019
    FlightOfOne likes this.
unityunity