VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_Utils.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using System.Linq;
5 
6 /// <summary>
7 /// Collection of generally useful functions.
8 /// </summary>
9 namespace AVR.Core.Utils {
10  /// <summary>
11  /// Geometry-centric utility functions.
12  /// </summary>
13  public static class Geom
14  {
15  /// <summary>
16  /// Creates a new, empty transform in the scene with a given parent, name and local coordinates
17  /// </summary>
18  /// <param name="parent"> Parent of the spawned transform. Pass null if there ought to be no parent. </param>
19  /// <param name="name"> Name of the newly created transform </param>
20  /// <param name="localCoords"> Local coordiantes of the newly spawned transform. </param>
21  /// <returns></returns>
22  public static Transform addEmptyTransform(Transform parent, string name="EmptyTransform", Vector3 localCoords=default(Vector3)) {
23  Transform T = new GameObject(name).transform;
24  T.SetParent(parent);
25  T.localPosition=localCoords;
26  return T;
27  }
28 
29  /// <summary>
30  /// Clamps the rotation to a quaternion to the given euler-angle bounds. Eg ClampQuaternionRotation(q, new Vector3(30, 0, 120)) will clamp q to a min/max rotation of +/- 30 deg around the x Axis etc.
31  /// </summary>
32  /// <param name="q">Quaternion to be clamped</param>
33  /// <param name="bounds">Euler-angle bounds. (Will count as both positive and negative.</param>
34  /// <returns></returns>
35  public static Quaternion ClampQuaternionRotation(Quaternion q, Vector3 bounds)
36  {
37  q.x /= q.w;
38  q.y /= q.w;
39  q.z /= q.w;
40  q.w = 1.0f;
41 
42  float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);
43  angleX = Mathf.Clamp(angleX, -bounds.x, bounds.x);
44  q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleX);
45 
46  float angleY = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.y);
47  angleY = Mathf.Clamp(angleY, -bounds.y, bounds.y);
48  q.y = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleY);
49 
50  float angleZ = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.z);
51  angleZ = Mathf.Clamp(angleZ, -bounds.z, bounds.z);
52  q.z = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleZ);
53 
54  return q;
55  }
56 
57  /// <summary>
58  /// Clamps the rotation to a quaternion to the given euler-angle bounds. Eg ClampQuaternionRotation(q, new Vector3(30, 0, 120)) will clamp q to a min/max rotation of +/- 30 deg around the x Axis etc.
59  /// </summary>
60  /// <param name="q">Quaternion to be clamped</param>
61  /// <param name="minbounds">Minimum euler-angle bounds</param>
62  /// <param name="maxbounds">Maximum euler-angle bounds</param>
63  /// <returns></returns>
64  public static Quaternion ClampQuaternionRotation(Quaternion q, Vector3 minbounds, Vector3 maxbounds)
65  {
66  q.x /= q.w;
67  q.y /= q.w;
68  q.z /= q.w;
69  q.w = 1.0f;
70 
71  float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);
72  angleX = Mathf.Clamp(angleX, -minbounds.x, maxbounds.x);
73  q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleX);
74 
75  float angleY = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.y);
76  angleY = Mathf.Clamp(angleY, -minbounds.y, maxbounds.y);
77  q.y = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleY);
78 
79  float angleZ = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.z);
80  angleZ = Mathf.Clamp(angleZ, -minbounds.z, maxbounds.z);
81  q.z = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleZ);
82 
83  return q;
84  }
85  }
86 
87  /// <summary>
88  /// Physics and raycast-centric utility functions.
89  /// </summary>
90  public static class Phys
91  {
92  /// <summary>
93  /// Perform linecasts along a spline of positions.
94  /// </summary>
95  /// <param name="positions"> List of positions. Linecasts will be performed for each pair of subsequent points </param>
96  /// <param name="hit"> RaycastHit of the first hit encountered. </param>
97  /// <returns> True if something is hit, otherwise false. </returns>
98  public static bool PathCast(Vector3[] positions, out RaycastHit hit)
99  {
100  for (int i = 0; i < positions.Length - 1; i++)
101  {
102  if (Physics.Linecast(positions[i], positions[i + 1], out hit))
103  {
104  return true;
105  }
106  }
107  hit = new RaycastHit();
108  return false;
109  }
110 
111  /// <summary>
112  /// Perform linecasts along a spline of positions.
113  /// </summary>
114  /// <param name="positions"> List of positions. Linecasts will be performed for each pair of subsequent points </param>
115  /// <param name="hit"> RaycastHit of the first hit encountered. </param>
116  /// <param name="mask"> Mask passed to the Linecast function. </param>
117  /// <returns> True if something is hit, otherwise false. </returns>
118  public static bool PathCast(Vector3[] positions, out RaycastHit hit, LayerMask mask)
119  {
120  for (int i = 0; i < positions.Length - 1; i++)
121  {
122  if (Physics.Linecast(positions[i], positions[i + 1], out hit, mask))
123  {
124  return true;
125  }
126  }
127  hit = new RaycastHit();
128  return false;
129  }
130 
131  /// <summary>
132  /// Perform linecasts along a spline of positions given in local space.
133  /// </summary>
134  /// <param name="positions"> List of positions in LOCAL space. Linecasts will be performed for each pair of subsequent points </param>
135  /// <param name="hit"> RaycastHit of the first hit encountered. </param>
136  /// <param name="localToWorld"> Transformation matrix to convert given positions from local to world space. </param>
137  /// <returns> True if something is hit, otherwise false. </returns>
138  public static bool PathCast(Vector3[] positions, out RaycastHit hit, Matrix4x4 localToWorld)
139  {
140  for (int i = 0; i < positions.Length - 1; i++)
141  {
142  if (Physics.Linecast(localToWorld * positions[i], localToWorld * positions[i + 1], out hit))
143  {
144  return true;
145  }
146  }
147  hit = new RaycastHit();
148  return false;
149  }
150 
151  public static bool LineCast(Vector3 start, Vector3 end, out RaycastHit hit)
152  {
153  return Physics.Linecast(start, end, out hit);
154  }
155 
156  public static bool LineCast(Vector3 start, Vector3 end, out RaycastHit hit, LayerMask mask)
157  {
158  return Physics.Linecast(start, end, out hit, mask);
159  }
160 
161  public static bool LineCast(Vector3 start, Vector3 end, out RaycastHit hit, Matrix4x4 localToWorld)
162  {
163  return Physics.Linecast(localToWorld * start, localToWorld * end, out hit);
164  }
165  }
166 
167  /// <summary>
168  /// Misc. utility functions.
169  /// </summary>
170  public static class Misc {
171  /// <summary>
172  /// Returns location within the hierarchy of a scene object.
173  /// </summary>
174  /// <param name="self"> Transform component of object in question </param>
175  /// <returns> Hierarchy location formated as a directory-string </returns>
176  public static string GetHierarchyPath(Transform self)
177  {
178  string path = self.gameObject.name;
179  Transform p = self.parent;
180  while (p != null)
181  {
182  path = p.name + "/" + path;
183  p = p.parent;
184  }
185  return path;
186  }
187 
188  /// <summary>
189  /// Finds an Object in the Scene by name and type including inactive ones
190  /// </summary>
191  /// <param name="name"> Name of object in question </param>
192  /// <param name="type"> Type of object in question </param>
193  /// <returns> First object found that has the given name and type. Null if none is found. </returns>
194  public static Object GlobalFind(string name, System.Type type)
195  {
196  Object[] objs = Resources.FindObjectsOfTypeAll(type);
197 
198  foreach (Object obj in objs)
199  {
200  if (obj.name == name)
201  {
202  return obj;
203  }
204  }
205 
206  return null;
207  }
208 
209  /// <summary>
210  /// Get custom System.Attribute from a given type
211  /// </summary>
212  /// <param name="type"> Type to get Attribute from </param>
213  /// <typeparam name="TAttribute">Attribute type to retrive</typeparam>
214  /// <returns>First attribute of given type attatched to given type.</returns>
215  public static TAttribute GetAttribute<TAttribute>(this System.Type type) where TAttribute : System.Attribute
216  {
217  return type.GetCustomAttributes(typeof(TAttribute), true).FirstOrDefault() as TAttribute;
218  }
219 
220  /// <summary>
221  /// Creates a Texture2D object from a given image path
222  /// </summary>
223  /// <param name="path"> Path (relative to assembly) where the image is located. Example: "Packages/com.avr.core/Package_Resources/avr_logo_vr.png" </param>
224  /// <returns></returns>
225  public static Texture2D Image2Texture(string path) {
226  var rawData = System.IO.File.ReadAllBytes(path);
227  Texture2D tex = new Texture2D(1, 1); // Size should not matter here, as LoadImage resets it.
228  tex.LoadImage(rawData);
229  return tex;
230  }
231 
232  /// <summary>
233  /// Creates an empty gameobject with specifed position and rotation. Returns its transform component.
234  /// </summary>
235  /// <param name="name">Name of the gameobject</param>
236  /// <param name="parent">Parent of the gameobject (null for no parent)</param>
237  /// <param name="worldPos">World-space position to instantiate the object at</param>
238  /// <param name="worldRot">World-space rotation of the gameobject</param>
239  public static Transform CreateEmptyGameObject(string name, Transform parent, Vector3 worldPos, Quaternion worldRot) {
240  GameObject o = new GameObject(name);
241  o.transform.SetParent(parent);
242  o.transform.SetPositionAndRotation(worldPos, worldRot);
243  return o.transform;
244  }
245 
246  /// <summary>
247  /// Creates an empty gameobject with a localposition and rotation of zero. Returns its transform component.
248  /// </summary>
249  /// <param name="name">Name of the gameobject</param>
250  /// <param name="parent">Parent of the gameobject (null for no parent)</param>
251  public static Transform CreateEmptyGameObject(string name = "EmptyTransform", Transform parent = null)
252  {
253  GameObject o = new GameObject(name);
254  o.transform.SetParent(parent);
255  o.transform.localPosition = Vector3.zero;
256  o.transform.localRotation = Quaternion.identity;
257  return o.transform;
258  }
259  }
260 }
static bool PathCast(Vector3[] positions, out RaycastHit hit, LayerMask mask)
Perform linecasts along a spline of positions.
Definition: AVR_Utils.cs:118
static Texture2D Image2Texture(string path)
Creates a Texture2D object from a given image path
Definition: AVR_Utils.cs:225
static Transform addEmptyTransform(Transform parent, string name="EmptyTransform", Vector3 localCoords=default(Vector3))
Creates a new, empty transform in the scene with a given parent, name and local coordinates ...
Definition: AVR_Utils.cs:22
static Transform CreateEmptyGameObject(string name="EmptyTransform", Transform parent=null)
Creates an empty gameobject with a localposition and rotation of zero. Returns its transform componen...
Definition: AVR_Utils.cs:251
static bool LineCast(Vector3 start, Vector3 end, out RaycastHit hit)
Definition: AVR_Utils.cs:151
static string GetHierarchyPath(Transform self)
Returns location within the hierarchy of a scene object.
Definition: AVR_Utils.cs:176
static Transform CreateEmptyGameObject(string name, Transform parent, Vector3 worldPos, Quaternion worldRot)
Creates an empty gameobject with specifed position and rotation. Returns its transform component...
Definition: AVR_Utils.cs:239
static Quaternion ClampQuaternionRotation(Quaternion q, Vector3 bounds)
Clamps the rotation to a quaternion to the given euler-angle bounds. Eg ClampQuaternionRotation(q, new Vector3(30, 0, 120)) will clamp q to a min/max rotation of +/- 30 deg around the x Axis etc.
Definition: AVR_Utils.cs:35
static Quaternion ClampQuaternionRotation(Quaternion q, Vector3 minbounds, Vector3 maxbounds)
Clamps the rotation to a quaternion to the given euler-angle bounds. Eg ClampQuaternionRotation(q, new Vector3(30, 0, 120)) will clamp q to a min/max rotation of +/- 30 deg around the x Axis etc.
Definition: AVR_Utils.cs:64
Misc. utility functions.
Definition: AVR_Utils.cs:170
static bool PathCast(Vector3[] positions, out RaycastHit hit)
Perform linecasts along a spline of positions.
Definition: AVR_Utils.cs:98
static bool PathCast(Vector3[] positions, out RaycastHit hit, Matrix4x4 localToWorld)
Perform linecasts along a spline of positions given in local space.
Definition: AVR_Utils.cs:138
static bool LineCast(Vector3 start, Vector3 end, out RaycastHit hit, Matrix4x4 localToWorld)
Definition: AVR_Utils.cs:161
Geometry-centric utility functions.
Definition: AVR_Utils.cs:13
Physics and raycast-centric utility functions.
Definition: AVR_Utils.cs:90
static Object GlobalFind(string name, System.Type type)
Finds an Object in the Scene by name and type including inactive ones
Definition: AVR_Utils.cs:194
static bool LineCast(Vector3 start, Vector3 end, out RaycastHit hit, LayerMask mask)
Definition: AVR_Utils.cs:156