Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ArrowRenderer.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using UnityEngine;
5 
6 namespace droid.Runtime.Utilities.Misc.Drawing {
7 // Put this script on a Camera
11  [ExecuteInEditMode]
12  public class ArrowRenderer : MonoBehaviour {
13  // Fill/drag these in from the editor
14 
15  // Choose the Unlit/Color shader in the Material Settings
16  // You can change that color, to change the color of the connecting lines
19  [SerializeField]
20  Material _line_mat = null;
21 
24  [SerializeField]
25  GameObject _main_point = null;
26 
27  [SerializeField] float _offset = 0.3f;
28 
31  [SerializeField]
32  GameObject[] _points = null;
33 
34  [SerializeField] Vector3[] _vec3_points = null;
35 
36  // Connect all of the `points` to the `main_point_pos`
37  void DrawConnectingLines(IReadOnlyCollection<Tuple<Vector3, Vector3>> vec_pairs) {
38  if (vec_pairs.Count > 0) {
39  // Loop through each point to connect to the mainPoint
40  foreach (var point in vec_pairs) {
41  var main_point_pos = point.Item1;
42  var point_pos = point.Item2;
43 
44  GL.Begin(GL.LINES);
45  this._line_mat.SetPass(0);
46  GL.Color(new Color(this._line_mat.color.r,
47  this._line_mat.color.g,
48  this._line_mat.color.b,
49  this._line_mat.color.a));
50  GL.Vertex3(main_point_pos.x, main_point_pos.y, main_point_pos.z);
51  GL.Vertex3(point_pos.x, point_pos.y, point_pos.z);
52  //
53  GL.Vertex3(point_pos.x - this._offset, point_pos.y, point_pos.z);
54  GL.Vertex3(point_pos.x, point_pos.y - this._offset, point_pos.z);
55  GL.Vertex3(point_pos.x, point_pos.y, point_pos.z - this._offset);
56  GL.Vertex3(point_pos.x + this._offset, point_pos.y, point_pos.z);
57  GL.Vertex3(point_pos.x, point_pos.y + this._offset, point_pos.z);
58  GL.Vertex3(point_pos.x, point_pos.y, point_pos.z + this._offset);
59  //
60  GL.End();
61  }
62  }
63  }
64 
65  // To show the lines in the game window whne it is running
66  void OnPostRender() {
67  this._vec3_points = this._points.Select(v => v.transform.position).ToArray();
68  var s = this._vec3_points
69  .Select(v => new Tuple<Vector3, Vector3>(this._main_point.transform.position, v)).ToArray();
70  this.DrawConnectingLines(s);
71  }
72 
73  // To show the lines in the editor
74  void OnDrawGizmos() {
75  this._vec3_points = this._points.Select(v => v.transform.position).ToArray();
76  var s = this._vec3_points
77  .Select(v => new Tuple<Vector3, Vector3>(this._main_point.transform.position, v)).ToArray();
78  this.DrawConnectingLines(s);
79  }
80  }
81 }