AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
Utils.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 
6 namespace HoloToolkit.Unity
7 {
11  public static class Utils
12  {
13  [System.Obsolete("Use GameObjectExtensions.SetLayerRecursively(gameObject, layer) instead;")]
14  public static void SetLayerRecursively(GameObject gameObject, int layer)
15  {
16  gameObject.SetLayerRecursively(layer);
17  }
18 
31  public static float Map(float minA, float maxA, float minB, float maxB, float value)
32  {
33  return (value - minA) * (maxB / maxA) + minB;
34  }
35 
48  public static float MapAndClamp(float minA, float maxA, float minB, float maxB, float value)
49  {
50  return Mathf.Clamp(Map(minA, maxA, minB, maxB, value), minB, maxB);
51  }
52 
60  public static void MoveObjectInFrontOfUser(Transform stageTransform, Transform tran, Vector3 offset, float yawOffset)
61  {
62  // have obj track head position with translation offset
63  Vector3 stageHeadPos = MathUtils.TransformPointFromTo(null, stageTransform, CameraCache.Main.transform.position);
64  Vector3 stageHeadDir = MathUtils.TransformDirectionFromTo(null, stageTransform, CameraCache.Main.transform.forward);
65  stageHeadDir.y = 0.0f; // ignore head pitch - use head position to set height
66  stageHeadDir.Normalize();
67  Vector3 sideDir = Vector3.Cross(stageHeadDir, Vector3.up).normalized;
68  Vector3 stageNewPos = stageHeadPos + stageHeadDir * offset.z + Vector3.up * offset.y + sideDir * offset.x;
69  tran.localPosition = MathUtils.TransformPointFromTo(stageTransform, tran.parent, stageNewPos);
70 
71  // also track head yaw
72  Vector3 toDir = MathUtils.TransformDirectionFromTo(stageTransform, tran.parent, stageHeadDir);
73  Quaternion rot = Quaternion.FromToRotation(Vector3.forward, toDir.normalized) * Quaternion.Euler(0.0f, yawOffset, 0.0f);
74  tran.localRotation = rot;
75  }
76 
84  public static void ConnectCylinderBetweenPoints(Transform endPointSpace, Vector3 a, Vector3 b, Transform cylTransform)
85  {
86  cylTransform.localPosition = MathUtils.TransformPointFromTo(endPointSpace, cylTransform.parent, 0.5f * (a + b));
87  Vector3 dir = MathUtils.TransformDirectionFromTo(endPointSpace, cylTransform.parent, (b - a).normalized);
88  cylTransform.localRotation = Quaternion.LookRotation(dir) * Quaternion.FromToRotation(Vector3.up, Vector3.forward);
89  Vector3 scale = cylTransform.localScale;
90  scale.y = (a - b).magnitude * 0.5f;
91  cylTransform.localScale = scale;
92  }
93 
99  public static void SetMaterialRecursive(Transform t, Material mat)
100  {
101  if (t.gameObject && t.gameObject.GetComponent<Renderer>())
102  {
103  t.gameObject.GetComponent<Renderer>().material = mat;
104  }
105 
106  for (int ii = 0; ii < t.childCount; ++ii)
107  {
108  SetMaterialRecursive(t.GetChild(ii), mat);
109  }
110  }
111 
123  public static void SetMaterialRecursiveForName(Transform t, Material mat, string nameToTest)
124  {
125  if (t.gameObject && t.gameObject.GetComponent<Renderer>() && t.gameObject.name == nameToTest)
126  {
127  t.gameObject.GetComponent<Renderer>().material = mat;
128  }
129 
130  for (int ii = 0; ii < t.childCount; ++ii)
131  {
132  SetMaterialRecursiveForName(t.GetChild(ii), mat, nameToTest);
133  }
134 
135  Resources.UnloadUnusedAssets();
136  }
137 
142  public static bool IsInEditor()
143  {
144 #if UNITY_WSA && !UNITY_EDITOR
145  return false;
146 #else
147  return true;
148 #endif
149  }
150 
157  public static Transform GetChildRecursive(Transform t, string name)
158  {
159  int numChildren = t.childCount;
160  for (int ii = 0; ii < numChildren; ++ii)
161  {
162  Transform child = t.GetChild(ii);
163  if (child.name == name)
164  {
165  return child;
166  }
167  Transform foundIt = GetChildRecursive(child, name);
168  if (foundIt != null)
169  {
170  return foundIt;
171  }
172  }
173  return null;
174  }
175  }
176 }
static bool IsInEditor()
helper for detecting if running in editor
Definition: Utils.cs:142
static void SetLayerRecursively(GameObject gameObject, int layer)
Definition: Utils.cs:14
Miscellaneous utility methods.
Definition: Utils.cs:11
static Vector3 TransformDirectionFromTo(Transform from, Transform to, Vector3 dirInFrom)
Takes a direction in the coordinate space specified by the "from" transform and transforms it to be t...
Definition: MathUtils.cs:59
static void SetMaterialRecursiveForName(Transform t, Material mat, string nameToTest)
Change material for every object in hierarchy which has a name equal to nameToTest. WARNING! See Community Answer This function automatically instantiates the materials and makes them unique to this renderer. It is your responsibility to destroy the materials when the game object is being destroyed. Resources.UnloadUnusedAssets also destroys the materials but it is usually only called when loading a new level. See Unity Documentation
Definition: Utils.cs:123
static Transform GetChildRecursive(Transform t, string name)
walk hierarchy looking for named transform
Definition: Utils.cs:157
static void ConnectCylinderBetweenPoints(Transform endPointSpace, Vector3 a, Vector3 b, Transform cylTransform)
Given two points and the transform that they are in, set position, rotation, and scale of a cylinder ...
Definition: Utils.cs:84
The purpose of this class is to provide a cached reference to the main camera. Calling Camera...
Definition: CameraCache.cs:12
static Camera Main
Returns a cached reference to the main camera and uses Camera.main if it hasn&#39;t been cached yet...
Definition: CameraCache.cs:20
Math Utilities class.
Definition: MathUtils.cs:13
static float Map(float minA, float maxA, float minB, float maxB, float value)
This takes a value, whose proportions are defined by minA and maxA, and scales it to a space defined ...
Definition: Utils.cs:31
static void MoveObjectInFrontOfUser(Transform stageTransform, Transform tran, Vector3 offset, float yawOffset)
Position transform along the gaze direction and orient yaw to match, with the specified offset ...
Definition: Utils.cs:60
static float MapAndClamp(float minA, float maxA, float minB, float maxB, float value)
This wrapper performs a map, but sets the results upper and lower limit based on the upper and lower ...
Definition: Utils.cs:48
static void SetMaterialRecursive(Transform t, Material mat)
Change material for every object in hierarchy
Definition: Utils.cs:99
static Vector3 TransformPointFromTo(Transform from, Transform to, Vector3 ptInFrom)
Takes a point in the coordinate space specified by the "from" transform and transforms it to be the c...
Definition: MathUtils.cs:44