- Home /
Trouble utilizing MeshRenderer bounds in a way that is independent of object rotation / orientation.
I am attempting to use MeshRender.Bounds to identify a number of points on my Plane object. My end goal is to RayCast from a 'grid' of points on that plane outwards to identify whether that point is being covered by a specific object. Using each of these points, I want to calculate a rough percentage of how much of the plane has been covered by the specific objects. I had this system working smoothly for a plane that faced the camera, but soon realized that the same setup on an identical plane rotated in another direction showed many points on the grid being off.
My grid scheme made a couple of assumptions, the two main ones being that MeshRenderer.bounds.min would always yield the bottom left corner point of the mesh, and that MeshRenderer.bounds.max would always yield the top-right point of the mesh. Each intermittent grid point was based on this assumption ( which worked correctly for the camera-facing plane).
I decided to do a little test using bounds.min / bounds.max on identical objects(planes) facing different directions - with the following results: Note here that bounds.min is being drawn in white, center = red, and max = blue. The bounds.center point(red) on each object seems to be as expected, though the min and max results seem to vary pretty greatly.
Here is a snippet of how I am showing these rays:
Bounds bounds = GetComponent<MeshRenderer>().bounds;
Vector3 botLeft = bounds.min;//bottom left
Debug.DrawLine (botLeft, botLeft + transform.up * 15, Color.white);
Vector3 center = bounds.center;//center
Debug.DrawLine (center, center + transform.up * 15, Color.red);
Vector3 topRight = bounds.max;//top Right
Debug.DrawLine (topRight, topRight + transform.up * 15, Color.blue);
I have also tried some alternative methods of obtaining the bottom left/ top right points of the mesh, such as botLeft = bounds.center - bounds.extent; // topRight = bounds.center + bounds.extent; or using bounds.size/2f in place of extent. Each with very similar results.
I have also tried converting these points from local to world space using transform.TransformPoint - but these results are much further off.
Anyway my question is basically, is there a way for me to manipulate MeshRender.bounds.min / max to yield the bottom left/ top right corners of the mesh regardless of it's object's rotation. Any suggestions would be greatly appreciated.
p.s I do have an alternate solution to this problem which involves using many, manually placed child objects which will determine the position to raycast from, or using a number of child box colliders to detect collisions with the specified objects such as : but this seems a little excessive and overall less expandable than what I want to achieve.
Answer by samizzo · Jun 20, 2018 at 03:39 AM
Note that the bounds are axis-aligned bounding boxes (AABBs), which means they are aligned to the world space coordinate system. That is, they aren't aligned to your object's orientation; they aren't oriented bounding boxes (OBBs).
This means that as you rotate your object, the AABB will grow and shrink in size. Are you taking this into account? It looks like you aren't, because you seem to be assuming that they are OBBs judging from this line:
Debug.DrawLine (botLeft, botLeft + transform.up * 15, Color.white);
To draw the AABB for the object, you can do this:
private void OnDrawGizmos()
{
Gizmos.DrawWireCube(m_bounds.center, m_bounds.size);
}