BoundingBoxRenderer.cs
2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*==============================================================================
Copyright (c) 2018 PTC Inc. All Rights Reserved.
Confidential and Proprietary - Protected under copyright and other laws.
Vuforia is a trademark of PTC Inc., registered in the United States and other
countries.
==============================================================================*/
using UnityEngine;
using Vuforia;
/// <summary>
/// A component that renders a bounding box using lines.
/// </summary>
public class BoundingBoxRenderer : MonoBehaviour
{
#region PRIVATE_MEMBERS
private Material mLineMaterial = null;
#endregion // PRIVATE_MEMBERS
private void OnRenderObject()
{
GL.PushMatrix();
GL.MultMatrix(transform.localToWorldMatrix);
if (mLineMaterial == null)
{
// We "borrow" the default material from a primitive.
// This ensures that, even on mobile platforms,
// we always get a valid material at runtime,
// as on mobile Unity can strip away unused shaders at build-time.
var tempObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
var cubeRenderer = tempObj.GetComponent<MeshRenderer>();
mLineMaterial = new Material(cubeRenderer.material);
mLineMaterial.color = Color.white;
Destroy(tempObj);
}
mLineMaterial.SetPass(0);
mLineMaterial.color = Color.white;
GL.Begin(GL.LINES);
// Bottom XZ quad
GL.Vertex3(-0.5f, -0.5f, -0.5f);
GL.Vertex3( 0.5f, -0.5f, -0.5f);
GL.Vertex3(0.5f, -0.5f, -0.5f);
GL.Vertex3(0.5f, -0.5f, 0.5f);
GL.Vertex3( 0.5f, -0.5f, 0.5f);
GL.Vertex3(-0.5f, -0.5f, 0.5f);
GL.Vertex3(-0.5f, -0.5f, 0.5f);
GL.Vertex3(-0.5f, -0.5f, -0.5f);
// Top XZ quad
GL.Vertex3(-0.5f, 0.5f, -0.5f);
GL.Vertex3(0.5f, 0.5f, -0.5f);
GL.Vertex3(0.5f, 0.5f, -0.5f);
GL.Vertex3(0.5f, 0.5f, 0.5f);
GL.Vertex3(0.5f, 0.5f, 0.5f);
GL.Vertex3(-0.5f, 0.5f, 0.5f);
GL.Vertex3(-0.5f, 0.5f, 0.5f);
GL.Vertex3(-0.5f, 0.5f, -0.5f);
// Side lines
GL.Vertex3(-0.5f, -0.5f, -0.5f);
GL.Vertex3(-0.5f, 0.5f, -0.5f);
GL.Vertex3(0.5f, -0.5f, -0.5f);
GL.Vertex3(0.5f, 0.5f, -0.5f);
GL.Vertex3(0.5f, -0.5f, 0.5f);
GL.Vertex3(0.5f, 0.5f, 0.5f);
GL.Vertex3(-0.5f, -0.5f, 0.5f);
GL.Vertex3(-0.5f, 0.5f, 0.5f);
GL.End();
GL.PopMatrix();
}
}