1 using System.Collections;
2 using System.Collections.Generic;
9 namespace AVR.Core.Utils {
22 public static Transform
addEmptyTransform(Transform parent,
string name=
"EmptyTransform", Vector3 localCoords=
default(Vector3)) {
23 Transform T =
new GameObject(name).transform;
25 T.localPosition=localCoords;
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);
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);
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);
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);
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);
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);
98 public static bool PathCast(Vector3[] positions, out RaycastHit hit)
100 for (
int i = 0; i < positions.Length - 1; i++)
102 if (Physics.Linecast(positions[i], positions[i + 1], out hit))
107 hit =
new RaycastHit();
118 public static bool PathCast(Vector3[] positions, out RaycastHit hit, LayerMask mask)
120 for (
int i = 0; i < positions.Length - 1; i++)
122 if (Physics.Linecast(positions[i], positions[i + 1], out hit, mask))
127 hit =
new RaycastHit();
138 public static bool PathCast(Vector3[] positions, out RaycastHit hit, Matrix4x4 localToWorld)
140 for (
int i = 0; i < positions.Length - 1; i++)
142 if (Physics.Linecast(localToWorld * positions[i], localToWorld * positions[i + 1], out hit))
147 hit =
new RaycastHit();
151 public static bool LineCast(Vector3 start, Vector3 end, out RaycastHit hit)
153 return Physics.Linecast(start, end, out hit);
156 public static bool LineCast(Vector3 start, Vector3 end, out RaycastHit hit, LayerMask mask)
158 return Physics.Linecast(start, end, out hit, mask);
161 public static bool LineCast(Vector3 start, Vector3 end, out RaycastHit hit, Matrix4x4 localToWorld)
163 return Physics.Linecast(localToWorld * start, localToWorld * end, out hit);
178 string path = self.gameObject.name;
179 Transform p = self.parent;
182 path = p.name +
"/" + path;
194 public static Object
GlobalFind(
string name, System.Type type)
196 Object[] objs = Resources.FindObjectsOfTypeAll(type);
198 foreach (Object obj
in objs)
200 if (obj.name == name)
215 public static TAttribute GetAttribute<TAttribute>(
this System.Type type) where TAttribute : System.Attribute
217 return type.GetCustomAttributes(typeof(TAttribute),
true).FirstOrDefault() as TAttribute;
226 var rawData = System.IO.File.ReadAllBytes(path);
227 Texture2D tex =
new Texture2D(1, 1);
228 tex.LoadImage(rawData);
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);
253 GameObject o =
new GameObject(name);
254 o.transform.SetParent(parent);
255 o.transform.localPosition = Vector3.zero;
256 o.transform.localRotation = Quaternion.identity;
static bool PathCast(Vector3[] positions, out RaycastHit hit, LayerMask mask)
Perform linecasts along a spline of positions.
static Texture2D Image2Texture(string path)
Creates a Texture2D object from a given image path
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 ...
static Transform CreateEmptyGameObject(string name="EmptyTransform", Transform parent=null)
Creates an empty gameobject with a localposition and rotation of zero. Returns its transform componen...
static bool LineCast(Vector3 start, Vector3 end, out RaycastHit hit)
static string GetHierarchyPath(Transform self)
Returns location within the hierarchy of a scene object.
static Transform CreateEmptyGameObject(string name, Transform parent, Vector3 worldPos, Quaternion worldRot)
Creates an empty gameobject with specifed position and rotation. Returns its transform component...
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.
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.
static bool PathCast(Vector3[] positions, out RaycastHit hit)
Perform linecasts along a spline of positions.
static bool PathCast(Vector3[] positions, out RaycastHit hit, Matrix4x4 localToWorld)
Perform linecasts along a spline of positions given in local space.
static bool LineCast(Vector3 start, Vector3 end, out RaycastHit hit, Matrix4x4 localToWorld)
Geometry-centric utility functions.
Physics and raycast-centric utility functions.
static Object GlobalFind(string name, System.Type type)
Finds an Object in the Scene by name and type including inactive ones
static bool LineCast(Vector3 start, Vector3 end, out RaycastHit hit, LayerMask mask)