Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
DrawBoundingBoxes.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 using UnityEngine;
3 
4 namespace droid.Runtime.Utilities.GameObjects.BoundingBoxes.Experimental {
8  [ExecuteInEditMode]
9  public class ShowBoundingBoxes : MonoBehaviour {
10  public Color _Color = Color.green;
11  public GameObject _Line_Object;
12  Dictionary<GameObject, GameObject> _lines = new Dictionary<GameObject, GameObject>();
13 
14  MeshFilter[] _mesh_filter_objects;
15 
16  void ReallocateLineRenderers() {
17  this._mesh_filter_objects = FindObjectsOfType<MeshFilter>();
18  this._lines.Clear();
19  }
20 
21  // void OnWillRenderObject() { throw new System.NotImplementedException(); }
22 
23  void Update() {
24  if (this._lines == null || this._mesh_filter_objects == null) {
25  this.ReallocateLineRenderers();
26  }
27 
28  this.CalcPositionsAndDrawBoxes();
29  }
30 
31  void CalcPositionsAndDrawBoxes() {
32  foreach (var mesh_filter_object in this._mesh_filter_objects) {
33  if (mesh_filter_object.gameObject.CompareTag("Target")) {
34  GameObject liner;
35  if (!this._lines.ContainsKey(mesh_filter_object.gameObject)) {
36  liner = Instantiate(this._Line_Object, this._Line_Object.transform);
37  this._lines.Add(mesh_filter_object.gameObject, liner);
38  } else {
39  Debug.Log("found Target");
40  liner = this._lines[mesh_filter_object.gameObject];
41  }
42 
43  var bounds = mesh_filter_object.mesh.bounds;
44 
45  //Bounds bounds;
46  //BoxCollider bc = GetComponent<BoxCollider>();
47  //if (bc != null)
48  // bounds = bc.bounds;
49  //else
50  //return;
51 
52  var v3_center = bounds.center;
53  var v3_extents = bounds.extents;
54 
55  var corners = Corners.ExtractCorners(v3_center, v3_extents, mesh_filter_object.transform);
56 
57  liner.GetComponent<LineRenderer>().SetPosition(0, corners[4]);
58  liner.GetComponent<LineRenderer>().SetPosition(1, corners[5]);
59 
60  Corners.DrawBox(corners[0],
61  corners[1],
62  corners[2],
63  corners[3],
64  corners[4],
65  corners[5],
66  corners[6],
67  corners[7],
68  this._Color);
69  }
70  }
71  }
72  }
73 }