- Home /
Draw borders cube
Hi I need to draw the borders of a cube/rectangle, as a wireframe. But I don't want to draw the lines forming a triangle, just the borders. Any ideias?
When run in the editor or does it have to work for a build? What line drawing solution were you doing to use?
Run: I need to draw when run in game, not editor. Solution: anything that works. GL, LineRenderer... But with Unity free.
Answer by Kaz_Yamof · May 22, 2014 at 04:48 PM
Thanks @Cherno, I will take a look at this later.
I solved using the BoundBox script at the asset store. It's free, and use GL too.
Thanks everybody.
Answer by robertbu · May 21, 2014 at 04:35 PM
There is a answer that draws the bounds using Debug.DrawLine(). Substitute your favorite line drawing solution:
http://answers.unity3d.com/questions/461588/drawing-a-bounding-box-similar-to-box-collider.html
Answer by Kaz_Yamof · May 21, 2014 at 04:49 PM
Actually I already tried this solution. Works perfectly in editor, but I didn't figure out how to convert this to GL. Here my attempt:
//Attached to objects (cubes)
public class ShowMeshBounds : MonoBehaviour
{
private Vector3 v3FrontTopLeft;
private Vector3 v3FrontTopRight;
private Vector3 v3FrontBottomLeft;
private Vector3 v3FrontBottomRight;
private Vector3 v3BackTopLeft;
private Vector3 v3BackTopRight;
private Vector3 v3BackBottomLeft;
private Vector3 v3BackBottomRight;
private Vector3[] list = null;
void Update()
{
CalcPositons();
}
void CalcPositons()
{
if (list == null)
{
list = new Vector3[24];
Bounds bounds;
BoxCollider bc = GetComponent<BoxCollider>();
if (bc != null)
bounds = bc.bounds;
else
return;
Vector3 v3Center = bounds.center;
Vector3 v3Extents = bounds.extents;
v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);
v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);
v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);
v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);
v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);
v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);
list[0] = (v3FrontTopLeft);
list[1] = (v3FrontTopRight);
list[2] = (v3FrontTopRight);
list[3] = (v3FrontBottomRight);
list[4] = (v3FrontBottomRight);
list[5] = (v3FrontBottomLeft);
list[6] = (v3FrontBottomLeft);
list[7] = (v3FrontTopLeft);
list[8] = (v3BackTopLeft);
list[9] = (v3BackTopRight);
list[10] = (v3BackTopRight);
list[11] = (v3BackBottomRight);
list[12] = (v3BackBottomRight);
list[13] = (v3BackBottomLeft);
list[14] = (v3BackBottomLeft);
list[15] = (v3BackTopLeft);
list[16] = (v3FrontTopLeft);
list[17] = (v3BackTopLeft);
list[18] = (v3FrontTopRight);
list[19] = (v3BackTopRight);
list[20] = (v3FrontBottomRight);
list[21] = (v3BackBottomRight);
list[22] = (v3FrontBottomLeft);
list[23] = (v3BackBottomLeft);
Camera.mainCamera.GetComponent<DrawLineBounds>().cubes.Add(list);
}
}
}
//Attached to Camera main
public class DrawLineBounds : MonoBehaviour
{
public List<Vector3[]> cubes = new List<Vector3[]>();
void OnPostRender()
{
GL.PushMatrix();
GL.Begin(GL.LINES);
GL.Color(Color.blue);
foreach (Vector3[] lista in cubes)
{
for (int i = 1; i < lista.Length; i++)
{
GL.Vertex(lista[i - 1]);
GL.Vertex(lista[i]);
}
}
GL.End();
GL.PopMatrix();
}
}
I try to manage a list of Vector3[] on camera main, every cube sending his 'vertices' to there, and rendering that list of vectors 3 with GL lines.
I have little experience with GL. Early on I purchased Vectorosity and use it for all my line drawing.
Vectrosity is really amazing, but I need to use free resources... For my own learning...
Answer by Cherno · May 22, 2014 at 03:43 AM
There is a nice grid-drawing script here on Answers, I used it myself with some customizing. You can use it to draw a whole 3d grid, 2d grid, single slice of a 3d grid, or even a single cube, you just need to dive into the code and change the coordinates :)
http://answers.unity3d.com/questions/482128/draw-grid-lines-in-game-view.html