- Home /
how to get 8 vertices from bounds properties?
i want to render boundingbox of the collider and i know about properties of bounds like size,extents and center but i dont know how to get 8 vertices that make bounding box.
any help here from somebody experienced?
Answer by MrSkiz · Jun 05, 2013 at 11:43 AM
Hi,
I know this topic is pretty old but here is my take on how to get the 8 points of a bounding cube : just use the Bounds.min and Bounds.max variables to get the two extreme points, then alternate between min.x/y/z and max.x/y/z to construct the 6 other Vector3.
boundPoint1 = collider.bounds.min;
boundPoint2 = collider.bounds.max;
boundPoint3 = Vector3(boundPoint1.x, boundPoint1.y, boundPoint2.z);
boundPoint4 = Vector3(boundPoint1.x, boundPoint2.y, boundPoint1.z);
boundPoint5 = Vector3(boundPoint2.x, boundPoint1.y, boundPoint1.z);
boundPoint6 = Vector3(boundPoint1.x, boundPoint2.y, boundPoint2.z);
boundPoint7 = Vector3(boundPoint2.x, boundPoint1.y, boundPoint2.z);
boundPoint8 = Vector3(boundPoint2.x, boundPoint2.y, boundPoint1.z);
Easy, quite typo-free.
//if you want to visualize the box you can add to $$anonymous$$rSkiz's code:
Color lineColor = Color.green;
// rectangular cuboid
// top of rectangular cuboid (6-2-8-4)
Debug.DrawLine (boundPoint6, boundPoint2, lineColor);
Debug.DrawLine (boundPoint2, boundPoint8, lineColor);
Debug.DrawLine (boundPoint8, boundPoint4, lineColor);
Debug.DrawLine (boundPoint4, boundPoint6, lineColor);
// bottom of rectangular cuboid (3-7-5-1)
Debug.DrawLine (boundPoint3, boundPoint7, lineColor);
Debug.DrawLine (boundPoint7, boundPoint5, lineColor);
Debug.DrawLine (boundPoint5, boundPoint1, lineColor);
Debug.DrawLine (boundPoint1, boundPoint3, lineColor);
// legs (6-3, 2-7, 8-5, 4-1)
Debug.DrawLine (boundPoint6, boundPoint3, lineColor);
Debug.DrawLine (boundPoint2, boundPoint7, lineColor);
Debug.DrawLine (boundPoint8, boundPoint5, lineColor);
Debug.DrawLine (boundPoint4, boundPoint1, lineColor);
// if you want make a cube
var bounds$$anonymous$$esh = go.AddComponent<$$anonymous$$eshFilter>(); ;
bounds$$anonymous$$esh.mesh = new $$anonymous$$esh();
bounds$$anonymous$$esh.mesh.vertices = new [] { boundPoint1, boundPoint2, boundPoint3, boundPoint4, boundPoint5, boundPoint6, boundPoint7, boundPoint8 };
bounds$$anonymous$$esh.mesh.triangles = new[]
{
0,7,4,
0,3,7,
5,1,3,
3,1,7,
7,1,4,
4,1,6,
5,3,2,
2,3,0,
0,4,2,
2,4,6,
1,5,2,
6,1,2
};
Answer by Bampf · Oct 11, 2010 at 02:52 PM
First I'll answer the question, then I'll mention two alternatives that don't involve computing the 8 vertices.
1) The extents (x, y, and z) are half of the size in those directions. So you can get one corner by subtracting the extents from the center, and the opposite corner by adding the extents:
Vector pt1 = collider.bounds.center - collider.bound.extents;
Vector pt2 = collider.bounds.center + collider.bound.extents;
The other corners can be found by adding some extents, subtracting others. Something like:
Vector pt3 = New Vector3(collider.bounds.x - collider.bound.extents.x,
collider.bounds.y + collider.bound.extents.y,
collider.bounds.z + collider.bound.extents.z);
This time I subtracted x, added y, added z. There are 8 possible combinations of additions and subtractions. Those are your 8 vertices.
2) But maybe you don't need the 8 vertices. You could instead create a GameObject that's a 1x1x1 cube, then move it to the center of the collision, and set the scale of the cube to match the size of the collider. I didn't test this, but it would look something like this:
GameObject myCube = GameObject.CreatePrimitive(PrimitiveType.Cube); // or, instantiate a prefab cube
myCube.tranform.position = collider.bounds.center;
myCube.tranform.localScale = collider.bounds.size;
(And instead of creating a primitive cube, you could instantiate a prefab cube, which would have whatever material, scripts, and other components you wanted to put on it.)
3) If you just want to draw it in the Scene View for debugging purposes, you can simply call Gizmos.DrawCube or Gizmos.DrawWireCube, passing it bounds.center and bounds.size as the arguments.
Answer by hoodymong · Jan 05, 2017 at 10:19 AM
// if you want make a cube mesh
var boundsMesh = go.AddComponent<MeshFilter>();
boundsMesh.mesh = new Mesh();
boundsMesh.mesh.vertices = new [] { boundPoint1, boundPoint2, boundPoint3, boundPoint4, boundPoint5, boundPoint6, boundPoint7, boundPoint8 };
boundsMesh.mesh.triangles = new[]
{
0,7,4,
0,3,7,
5,1,3,
3,1,7,
7,1,4,
4,1,6,
5,3,2,
2,3,0,
0,4,2,
2,4,6,
1,5,2,
6,1,2
};
Your answer
Follow this Question
Related Questions
Click and Drag - How to? 2 Answers
LodGroup manually set Bounds 0 Answers
Unity's Bounding Box 0 Answers
2D bounding box in screen space from a 3D object 0 Answers
What exactly is the bounding box? 0 Answers