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

Resolved SkinnedMeshRenderer.localBounds runtime manipulation

Discussion in 'Scripting' started by Mangonels, Apr 5, 2022.

  1. Mangonels

    Mangonels

    Joined:
    May 8, 2018
    Posts:
    20
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. [RequireComponent(typeof(SkinnedMeshRenderer))]
    4. public class SkinnedMeshRendererSetup : MonoBehaviour
    5. {
    6.     void Start()
    7.     {
    8.         SkinnedMeshRenderer smr = GetComponent<SkinnedMeshRenderer>();
    9.         Bounds bounds = smr.localBounds;
    10.         //bounds.size = new Vector3(10f, 10f, 10f);
    11.         //bounds.Expand(10f);
    12.         //bounds.extents = new Vector3(10f, 10f, 10f);
    13.         //bounds.SetMinMax(new Vector3(-10f, -10f, -10f), new Vector3(10f, 10f, 10f));
    14.     }
    15. }
    16.  
    I'm trying to change the size of a SkinnedMeshRenderer's Bounds.

    I tried doing it in 4 (commented) diferent ways, none of them work.

    Aparently an internal value does change, but I don't see this reflected in scene view's bounds, or on the inspector. What am I doing wrong?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    In ten years using Unity I have actually never seen anything useful come out of getting any of the
    bounds
    properties on any of the renderers. It always ends up being not useful or it doesn't work or it works in a weird way, or it's incorrect in editor, or it messes up with two cameras, etc.

    I always just set my own notion of whatever I need for "bounds," perhaps with a simple radius, or else an axis-aligned bounds check, and moved onto the next problem. What exactly do you want with these bounds?
     
  3. Mangonels

    Mangonels

    Joined:
    May 8, 2018
    Posts:
    20
    I'm trying to fix an issue where SkinnedMeshRenderer objects marked with Update When Offscreen false, cull out of view while still within Game camera view. This is due to certain animations causing the actual object to detatch from the renderer's bounds. Once the camera stops seeing the bounds, the SkinnedMeshRenderer object can sometimes still be seen since it's detatched/slightly separated, resulting in popping in and out of view. I'm sure this is a common isue, I just can't find the answer to it.

    idk if I made myself clear with the issue though, but basically, editing the bounds manually is not a solution, because I have many of these objects. So it came to actually editing it in runtime.

    Other olutions I thought of:

    - Making the camera account for a wider "angle" before culling objects out of view. But I do not believe this is an option.
    - Trying to refresh the Bound's proportions. But aparently, this is only posible with a regular MeshRenderer through a MeshFilter.
    - Editing all bounds manually (may resort to it if desperate, which I'm kinda nearing).
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,531
    Uhm, you know that Bounds is a struct and therefore a value type? When you want to change the bounds of a Mesh or a Renderer, you have to set the bounds property to a Bounds value. In your code you only read the bounds of the renderer and then you manipulate the Bounds value but you never assign it back to the renderer.

    I also have never manipulated the bounds of a renderer, however I have changed the bounds of the rendered mesh in the past. So you should be able to do:

    Code (CSharp):
    1. SkinnedMeshRenderer smr = GetComponent<SkinnedMeshRenderer>();
    2. Bounds bounds = smr.localBounds;
    3. bounds.Expand(10f);
    4. smr.localBounds = bounds;
    This would increas the original bounds by 10 units in each direction.
     
    Mangonels and Kurt-Dekker like this.