- Home /
Renderer Bounds
how would I set a renderers bounds (center and extents) in script? the inspector provides access to it, but I know its read-only when I try to do the same in a script. Thanks.
Answer by Bunny83 · Jul 30, 2012 at 10:32 AM
Renderer.bounds just tells you the bounds of the renderer. This depends on the object it is rendering. You can't set the bounds, why would you? The bounds are a result of the objects position / scale / rotation.
there are some issues with the model/rig that require manual adjustments (and unfortunately fixing the rig is not an option)
Well, if you want to set the bounds manually, you have to set
$$anonymous$$esh.bounds, not Renderer.bounds. The renderer is just a component which renders the mesh. The $$anonymous$$esh represents the actual data.
Again, Renderer.bounds is deter$$anonymous$$ed at runtime. It's not stored anywhere. It represents the axis-aligned-bounding-box (AABB) that encloses the rotated mesh.bounds. mesh.bound can be changed manually, but Renderer.bound is always an AABB, so when the object is rotated, it will be larger.
How does your object look like? If the mesh is already rotated in local space, the mesh.bounds will already be too large. The bounds can't be rotated to match your object shape. $$anonymous$$esh.bounds is a local AABB and Renderer.bounds a worldspace AABB.
Don't give up. I've just written this little helper script(Show$$anonymous$$eshBounds.cs) which can visualize the $$anonymous$$esh.bounds and the Renderer.bounds as transparent cubes (mesh.bounds == the red cube, renderer.bounds == green cube).
Just attach the script to your model (whereever your meshfilter or skinnedmeshrenderer is)
I just found a bug in my old script(Show$$anonymous$$eshBounds.cs) ;) I've fixed it.
I've also written another little script(Set$$anonymous$$eshBounds.cs) which allows you to set the bounds of a mesh at runtime. You can tweak the values in the inspector. Together with the visualization script you should be able to fix your bounds problems ;)
Answer by Kryptos · Jul 30, 2012 at 10:37 AM
If you are creating a mesh a runtime, then you might be looking for Mesh.RecalculateBounds. Otherwise, as @Bunny83 said, there is not need to set the bounds yourself.
thanks for the replies, but there are issues with the model and rig I received leading to my need to do some manual adjustments. I was hoping to automate this, but I guess that isn't possible (and neither if fixing the rig).
I tried that but since the model/rig are at fault, it just recalculates the same bad bounds and fixing the rig is not currently an option.
Answer by ProbePLayer · Aug 15, 2013 at 10:20 PM
as everyone else stated, modifying Mesh.bounds would probably be your solution. my addition to this would be in case the bounds.expand function doesn't seem to work, modify it through manual expansion, like this:
//expand the bounding box so that it keeps up with the mesh
//otherwise the mesh wont show when the mesh is in camera frustum and the bounds are not
Bounds bounds = new Bounds(mesh.bounds.center, mesh.bounds.size+new Vector3(waveSpeed,waveSpeed,waveSpeed));
mesh.bounds = bounds;