Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
Draw3DBoundingBox.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 using UnityEngine;
3 
4 namespace droid.Runtime.Utilities.GameObjects.BoundingBoxes {
8  [RequireComponent(typeof(Camera))]
9  [ExecuteInEditMode]
10  public class Draw3DBoundingBox : MonoBehaviour {
11  List<Color> _colors = new List<Color>();
12  [SerializeField] Material _line_material;
13  List<GameObject> _names = new List<GameObject>();
14 
15  List<Vector3[,]> _outlines = new List<Vector3[,]>();
16 
17  //List<Vector3[,]> _triangles = new List<Vector3[,]>();
18  //[SerializeField] GUISkin _gui_skin;
19 
20  Camera _camera;
21  [SerializeField] bool _draw_label = true;
22  BoundingBox[] _bounding_boxes;
23  [SerializeField] bool _cacheBoundingBoxes = true;
24 
25  void Awake() {
26  if (!this._line_material) {
27  var shader = Shader.Find("Unlit/Color");
28  this._line_material = new Material(shader);
29  }
30 
31  //GUI.skin = this._gui_skin;
32 
33  this._camera = this.GetComponent<Camera>();
34  }
35 
36  void OnPostRender() {
37  if (this._outlines == null) {
38  return;
39  }
40 
41  if (this._line_material) {
42  this._line_material.SetPass(0);
43  }
44 
45  GL.Begin(GL.LINES);
46  for (var j = 0; j < this._outlines.Count; j++) {
47  GL.Color(this._colors[j]);
48  for (var i = 0; i < this._outlines[j].GetLength(0); i++) {
49  GL.Vertex(this._outlines[j][i, 0]);
50  GL.Vertex(this._outlines[j][i, 1]);
51  }
52  }
53 
54  GL.End();
55 /*
56  GL.Begin(GL.TRIANGLES);
57 
58  for (var j = 0; j < this._triangles.Count; j++) {
59  GL.Color(this._colors[j]);
60  for (var i = 0; i < this._triangles[j].GetLength(0); i++) {
61  GL.Vertex(this._triangles[j][i, 0]);
62  GL.Vertex(this._triangles[j][i, 1]);
63  GL.Vertex(this._triangles[j][i, 2]);
64  }
65  }
66 
67  GL.End();
68  */
69  }
70 
76  public void SetOutlines(Vector3[,] new_outlines, Color new_color, GameObject game_object) {
77  if (new_outlines == null) {
78  return;
79  }
80 
81  if (this._outlines == null) {
82  return;
83  }
84 
85  if (new_outlines.GetLength(0) > 0) {
86  this._outlines.Add(new_outlines);
87  this._colors.Add(new_color);
88  this._names.Add(game_object);
89  }
90  }
91 /*
97  public void SetTriangles(Vector3[,] new_triangles, Color new_color, GameObject game_object) {
98  if (new_triangles == null) {
99  return;
100  }
101 
102  if (this._triangles == null) {
103  return;
104  }
105 
106  if (new_triangles.GetLength(0) > 0) {
107  this._colors.Add(new_color);
108  this._triangles.Add(new_triangles);
109  this._names.Add(game_object);
110  }
111  }
112 */
113 
114  void OnPreRender() {
115  this._outlines.Clear();
116  this._colors.Clear();
117  this._names.Clear();
118  //this._triangles.Clear();
119  if (!this._cacheBoundingBoxes || this._bounding_boxes == null || this._bounding_boxes.Length == 0) {
120  this._bounding_boxes = FindObjectsOfType<BoundingBox>();
121  }
122 
123  foreach (var bb in this._bounding_boxes) {
124  if (bb) {
125  this.SetOutlines(bb.Lines, bb.EditorPreviewLineColor, bb.gameObject);
126  }
127  }
128  }
129 
130  const float _padding = 4;
131 
132  void OnGUI() {
133  if (this._draw_label) {
134  var i = 0;
135  foreach (var t in this._outlines) {
136  var point = t[0, 0];
137  var box_position = this._camera.WorldToScreenPoint(point);
138  box_position.y = Screen.height - box_position.y;
139 
140  var text = this._names[i].name;
141 
142  var content = GUI.skin.box.CalcSize(new GUIContent(text));
143  content.x = content.x + _padding;
144  content.y = content.y + _padding;
145  var rect = new Rect(box_position.x - content.x / 2,
146  box_position.y - content.y / 2,
147  content.x,
148  content.y);
149  GUI.Box(rect, text);
150  //GUI.Label(this._rect, text);
151  i++;
152  }
153  }
154  }
155  }
156 }
void SetOutlines(Vector3[,] new_outlines, Color new_color, GameObject game_object)