2
0

SteamVR_Camera.cs 9.4 KB

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