Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
CylinderLineRenderer.cs
Go to the documentation of this file.
1 using UnityEngine;
2 
3 namespace droid.Runtime.Utilities.Misc.Drawing {
4  public class CylinderLineRenderer : MonoBehaviour {
5  // Fill in this with the default Unity Cylinder mesh
6  // We will account for the cylinder pivot/origin being in the middle.
9  public Mesh _CylinderMesh;
10 
11  // Material used for the connecting lines
12  public Material _LineMat;
13 
14  // Connect all of the `points` to the `mainPoint`
15  public GameObject _MainPoint;
16  public GameObject[] _Points;
17 
18  public float _Radius = 0.05f;
19 
20  GameObject[] _ring_game_objects;
21 
22  // Use this for initialization
23  void Start() {
24  this._ring_game_objects = new GameObject[this._Points.Length];
25  //this.connectingRings = new ProceduralRing[points.Length];
26  for (var i = 0; i < this._Points.Length; i++) {
27  // Make a game_object that we will put the ring on
28  // And then put it as a child on the game_object that has this Command and Control script
29  this._ring_game_objects[i] = new GameObject {name = "Connecting ring #" + i};
30  this._ring_game_objects[i].transform.parent = this.gameObject.transform;
31 
32  // We make a offset game_object to counteract the default cylindermesh pivot/origin being in the middle
33  var ring_offset_cylinder_mesh_object = new GameObject();
34  ring_offset_cylinder_mesh_object.transform.parent = this._ring_game_objects[i].transform;
35 
36  // Offset the cylinder so that the pivot/origin is at the bottom in relation to the outer ring game_object.
37  ring_offset_cylinder_mesh_object.transform.localPosition = new Vector3(0f, 1f, 0f);
38  // Set the radius
39  ring_offset_cylinder_mesh_object.transform.localScale = new Vector3(this._Radius, 1f, this._Radius);
40 
41  // Create the the Mesh and renderer to show the connecting ring
42  var ring_mesh = ring_offset_cylinder_mesh_object.AddComponent<MeshFilter>();
43  ring_mesh.mesh = this._CylinderMesh;
44 
45  var ring_renderer = ring_offset_cylinder_mesh_object.AddComponent<MeshRenderer>();
46  ring_renderer.material = this._LineMat;
47  }
48  }
49 
50  // Update is called once per frame
51  void Update() {
52  for (var i = 0; i < this._Points.Length; i++) {
53  // Move the ring to the point
54  this._ring_game_objects[i].transform.position = this._Points[i].transform.position;
55 
56  // Match the scale to the distance
57  var cylinder_distance =
58  0.5f * Vector3.Distance(this._Points[i].transform.position, this._MainPoint.transform.position);
59  this._ring_game_objects[i].transform.localScale =
60  new Vector3(this._ring_game_objects[i].transform.localScale.x,
61  cylinder_distance,
62  this._ring_game_objects[i].transform.localScale.z);
63 
64  // Make the cylinder look at the main point.
65  // Since the cylinder is pointing up(y) and the forward is z, we need to offset by 90 degrees.
66  this._ring_game_objects[i].transform.LookAt(this._MainPoint.transform, Vector3.up);
67  this._ring_game_objects[i].transform.rotation *= Quaternion.Euler(90, 0, 0);
68  }
69  }
70  }
71 
75  public class ConnectPointsWithCubeMesh : MonoBehaviour {
76  // Fill in this with the default Unity Cube mesh
77  // We will account for the cube pivot/origin being in the middle.
78  public Mesh _CubeMesh;
79 
80  // Material used for the connecting lines
81  public Material _LineMat;
82 
83  // Connect all of the `points` to the `mainPoint`
84  public GameObject _MainPoint;
85  public GameObject[] _Points;
86 
87  public float _Radius = 0.05f;
88 
89  GameObject[] _ring_game_objects;
90 
91  // Use this for initialization
92  void Start() {
93  this._ring_game_objects = new GameObject[this._Points.Length];
94  //this.connectingRings = new ProceduralRing[points.Length];
95  for (var i = 0; i < this._Points.Length; i++) {
96  // Make a game_object that we will put the ring on
97  // And then put it as a child on the game_object that has this Command and Control script
98  this._ring_game_objects[i] = new GameObject();
99  this._ring_game_objects[i].name = "Connecting ring #" + i;
100  this._ring_game_objects[i].transform.parent = this.gameObject.transform;
101 
102  // We make a offset game_object to counteract the default cubemesh pivot/origin being in the middle
103  var ring_offset_cube_mesh_object = new GameObject();
104  ring_offset_cube_mesh_object.transform.parent = this._ring_game_objects[i].transform;
105 
106  // Offset the cube so that the pivot/origin is at the bottom in relation to the outer ring game_object.
107  ring_offset_cube_mesh_object.transform.localPosition = new Vector3(0f, 1f, 0f);
108  // Set the radius
109  ring_offset_cube_mesh_object.transform.localScale = new Vector3(this._Radius, 1f, this._Radius);
110 
111  // Create the the Mesh and renderer to show the connecting ring
112  var ring_mesh = ring_offset_cube_mesh_object.AddComponent<MeshFilter>();
113  ring_mesh.mesh = this._CubeMesh;
114 
115  var ring_renderer = ring_offset_cube_mesh_object.AddComponent<MeshRenderer>();
116  ring_renderer.material = this._LineMat;
117  }
118  }
119 
120  // Update is called once per frame
121  void Update() {
122  for (var i = 0; i < this._Points.Length; i++) {
123  // Move the ring to the point
124  this._ring_game_objects[i].transform.position = this._Points[i].transform.position;
125 
126  this._ring_game_objects[i].transform.position =
127  0.5f * (this._Points[i].transform.position + this._MainPoint.transform.position);
128  var delta = this._Points[i].transform.position - this._MainPoint.transform.position;
129  this._ring_game_objects[i].transform.position += delta;
130 
131  // Match the scale to the distance
132  var cube_distance =
133  Vector3.Distance(this._Points[i].transform.position, this._MainPoint.transform.position);
134  this._ring_game_objects[i].transform.localScale =
135  new Vector3(this._ring_game_objects[i].transform.localScale.x,
136  cube_distance,
137  this._ring_game_objects[i].transform.localScale.z);
138 
139  // Make the cube look at the main point.
140  // Since the cube is pointing up(y) and the forward is z, we need to offset by 90 degrees.
141  this._ring_game_objects[i].transform.LookAt(this._MainPoint.transform, Vector3.up);
142  this._ring_game_objects[i].transform.rotation *= Quaternion.Euler(90, 0, 0);
143  }
144  }
145  }
146 }