Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
6.7 KiB

  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Adds SteamVR render support to existing camera objects
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. using System.Reflection;
  9. using Valve.VR;
  10. [RequireComponent(typeof(Camera))]
  11. public class SteamVR_Camera : MonoBehaviour
  12. {
  13. [SerializeField]
  14. private Transform _head;
  15. public Transform head { get { return _head; } }
  16. public Transform offset { get { return _head; } } // legacy
  17. public Transform origin { get { return _head.parent; } }
  18. public new Camera camera { get; private set; }
  19. [SerializeField]
  20. private Transform _ears;
  21. public Transform ears { get { return _ears; } }
  22. public Ray GetRay()
  23. {
  24. return new Ray(_head.position, _head.forward);
  25. }
  26. public bool wireframe = false;
  27. static public float sceneResolutionScale
  28. {
  29. get { return UnityEngine.XR.XRSettings.eyeTextureResolutionScale; }
  30. set { UnityEngine.XR.XRSettings.eyeTextureResolutionScale = value; }
  31. }
  32. #region Enable / Disable
  33. void OnDisable()
  34. {
  35. SteamVR_Render.Remove(this);
  36. }
  37. void OnEnable()
  38. {
  39. // Bail if no hmd is connected
  40. var vr = SteamVR.instance;
  41. if (vr == null)
  42. {
  43. if (head != null)
  44. {
  45. head.GetComponent<SteamVR_TrackedObject>().enabled = false;
  46. }
  47. enabled = false;
  48. return;
  49. }
  50. // Convert camera rig for native OpenVR integration.
  51. var t = transform;
  52. if (head != t)
  53. {
  54. Expand();
  55. t.parent = origin;
  56. while (head.childCount > 0)
  57. head.GetChild(0).parent = t;
  58. // Keep the head around, but parent to the camera now since it moves with the hmd
  59. // but existing content may still have references to this object.
  60. head.parent = t;
  61. head.localPosition = Vector3.zero;
  62. head.localRotation = Quaternion.identity;
  63. head.localScale = Vector3.one;
  64. head.gameObject.SetActive(false);
  65. _head = t;
  66. }
  67. if (ears == null)
  68. {
  69. var e = transform.GetComponentInChildren<SteamVR_Ears>();
  70. if (e != null)
  71. _ears = e.transform;
  72. }
  73. if (ears != null)
  74. ears.GetComponent<SteamVR_Ears>().vrcam = this;
  75. SteamVR_Render.Add(this);
  76. }
  77. #endregion
  78. #region Functionality to ensure SteamVR_Camera component is always the last component on an object
  79. void Awake()
  80. {
  81. camera = GetComponent<Camera>(); // cached to avoid runtime lookup
  82. ForceLast();
  83. }
  84. static Hashtable values;
  85. public void ForceLast()
  86. {
  87. if (values != null)
  88. {
  89. // Restore values on new instance
  90. foreach (DictionaryEntry entry in values)
  91. {
  92. var f = entry.Key as FieldInfo;
  93. f.SetValue(this, entry.Value);
  94. }
  95. values = null;
  96. }
  97. else
  98. {
  99. // Make sure it's the last component
  100. var components = GetComponents<Component>();
  101. // But first make sure there aren't any other SteamVR_Cameras on this object.
  102. for (int i = 0; i < components.Length; i++)
  103. {
  104. var c = components[i] as SteamVR_Camera;
  105. if (c != null && c != this)
  106. {
  107. DestroyImmediate(c);
  108. }
  109. }
  110. components = GetComponents<Component>();
  111. if (this != components[components.Length - 1])
  112. {
  113. // Store off values to be restored on new instance
  114. values = new Hashtable();
  115. var fields = GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
  116. foreach (var f in fields)
  117. if (f.IsPublic || f.IsDefined(typeof(SerializeField), true))
  118. values[f] = f.GetValue(this);
  119. var go = gameObject;
  120. DestroyImmediate(this);
  121. go.AddComponent<SteamVR_Camera>().ForceLast();
  122. }
  123. }
  124. }
  125. #endregion
  126. #region Expand / Collapse object hierarchy
  127. #if UNITY_EDITOR
  128. public bool isExpanded { get { return head != null && transform.parent == head; } }
  129. #endif
  130. const string eyeSuffix = " (eye)";
  131. const string earsSuffix = " (ears)";
  132. const string headSuffix = " (head)";
  133. const string originSuffix = " (origin)";
  134. public string baseName { get { return name.EndsWith(eyeSuffix) ? name.Substring(0, name.Length - eyeSuffix.Length) : name; } }
  135. // Object hierarchy creation to make it easy to parent other objects appropriately,
  136. // otherwise this gets called on demand at runtime. Remaining initialization is
  137. // performed at startup, once the hmd has been identified.
  138. public void Expand()
  139. {
  140. var _origin = transform.parent;
  141. if (_origin == null)
  142. {
  143. _origin = new GameObject(name + originSuffix).transform;
  144. _origin.localPosition = transform.localPosition;
  145. _origin.localRotation = transform.localRotation;
  146. _origin.localScale = transform.localScale;
  147. }
  148. if (head == null)
  149. {
  150. _head = new GameObject(name + headSuffix, typeof(SteamVR_TrackedObject)).transform;
  151. head.parent = _origin;
  152. head.position = transform.position;
  153. head.rotation = transform.rotation;
  154. head.localScale = Vector3.one;
  155. head.tag = tag;
  156. }
  157. if (transform.parent != head)
  158. {
  159. transform.parent = head;
  160. transform.localPosition = Vector3.zero;
  161. transform.localRotation = Quaternion.identity;
  162. transform.localScale = Vector3.one;
  163. while (transform.childCount > 0)
  164. transform.GetChild(0).parent = head;
  165. #if !UNITY_2017_2_OR_NEWER
  166. var guiLayer = GetComponent<GUILayer>();
  167. if (guiLayer != null)
  168. {
  169. DestroyImmediate(guiLayer);
  170. head.gameObject.AddComponent<GUILayer>();
  171. }
  172. #endif
  173. var audioListener = GetComponent<AudioListener>();
  174. if (audioListener != null)
  175. {
  176. DestroyImmediate(audioListener);
  177. _ears = new GameObject(name + earsSuffix, typeof(SteamVR_Ears)).transform;
  178. ears.parent = _head;
  179. ears.localPosition = Vector3.zero;
  180. ears.localRotation = Quaternion.identity;
  181. ears.localScale = Vector3.one;
  182. }
  183. }
  184. if (!name.EndsWith(eyeSuffix))
  185. name += eyeSuffix;
  186. }
  187. public void Collapse()
  188. {
  189. transform.parent = null;
  190. // Move children and components from head back to camera.
  191. while (head.childCount > 0)
  192. head.GetChild(0).parent = transform;
  193. #if !UNITY_2017_2_OR_NEWER
  194. var guiLayer = head.GetComponent<GUILayer>();
  195. if (guiLayer != null)
  196. {
  197. DestroyImmediate(guiLayer);
  198. gameObject.AddComponent<GUILayer>();
  199. }
  200. #endif
  201. if (ears != null)
  202. {
  203. while (ears.childCount > 0)
  204. ears.GetChild(0).parent = transform;
  205. DestroyImmediate(ears.gameObject);
  206. _ears = null;
  207. gameObject.AddComponent(typeof(AudioListener));
  208. }
  209. if (origin != null)
  210. {
  211. // If we created the origin originally, destroy it now.
  212. if (origin.name.EndsWith(originSuffix))
  213. {
  214. // Reparent any children so we don't accidentally delete them.
  215. var _origin = origin;
  216. while (_origin.childCount > 0)
  217. _origin.GetChild(0).parent = _origin.parent;
  218. DestroyImmediate(_origin.gameObject);
  219. }
  220. else
  221. {
  222. transform.parent = origin;
  223. }
  224. }
  225. DestroyImmediate(head.gameObject);
  226. _head = null;
  227. if (name.EndsWith(eyeSuffix))
  228. name = name.Substring(0, name.Length - eyeSuffix.Length);
  229. }
  230. #endregion
  231. }