SteamVR_Camera.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using System;
  2. using System.Collections;
  3. using System.Reflection;
  4. using UnityEngine;
  5. using UnityEngine.VR;
  6. [RequireComponent(typeof(Camera))]
  7. public class SteamVR_Camera : MonoBehaviour
  8. {
  9. public Transform head
  10. {
  11. get
  12. {
  13. return this._head;
  14. }
  15. }
  16. public Transform offset
  17. {
  18. get
  19. {
  20. return this._head;
  21. }
  22. }
  23. public Transform origin
  24. {
  25. get
  26. {
  27. return this._head.parent;
  28. }
  29. }
  30. public Transform ears
  31. {
  32. get
  33. {
  34. return this._ears;
  35. }
  36. }
  37. public Ray GetRay()
  38. {
  39. return new Ray(this._head.position, this._head.forward);
  40. }
  41. public static float sceneResolutionScale
  42. {
  43. get
  44. {
  45. return VRSettings.renderScale;
  46. }
  47. set
  48. {
  49. VRSettings.renderScale = value;
  50. }
  51. }
  52. private void OnDisable()
  53. {
  54. SteamVR_Render.Remove(this);
  55. }
  56. private void OnEnable()
  57. {
  58. if (SteamVR.instance == null)
  59. {
  60. if (this.head != null)
  61. {
  62. this.head.GetComponent<SteamVR_GameView>().enabled = false;
  63. this.head.GetComponent<SteamVR_TrackedObject>().enabled = false;
  64. }
  65. if (this.flip != null)
  66. {
  67. this.flip.enabled = false;
  68. }
  69. base.enabled = false;
  70. return;
  71. }
  72. Transform transform = base.transform;
  73. if (this.head != transform)
  74. {
  75. this.Expand();
  76. transform.parent = this.origin;
  77. while (this.head.childCount > 0)
  78. {
  79. this.head.GetChild(0).parent = transform;
  80. }
  81. this.head.parent = transform;
  82. this.head.localPosition = Vector3.zero;
  83. this.head.localRotation = Quaternion.identity;
  84. this.head.localScale = Vector3.one;
  85. this.head.gameObject.SetActive(false);
  86. this._head = transform;
  87. }
  88. if (this.flip != null)
  89. {
  90. UnityEngine.Object.DestroyImmediate(this.flip);
  91. this.flip = null;
  92. }
  93. if (this.ears == null)
  94. {
  95. SteamVR_Ears componentInChildren = base.transform.GetComponentInChildren<SteamVR_Ears>();
  96. if (componentInChildren != null)
  97. {
  98. this._ears = componentInChildren.transform;
  99. }
  100. }
  101. if (this.ears != null)
  102. {
  103. this.ears.GetComponent<SteamVR_Ears>().vrcam = this;
  104. }
  105. SteamVR_Render.Add(this);
  106. }
  107. private void Awake()
  108. {
  109. this.ForceLast();
  110. }
  111. public void ForceLast()
  112. {
  113. if (SteamVR_Camera.values != null)
  114. {
  115. IDictionaryEnumerator enumerator = SteamVR_Camera.values.GetEnumerator();
  116. try
  117. {
  118. while (enumerator.MoveNext())
  119. {
  120. object obj = enumerator.Current;
  121. DictionaryEntry dictionaryEntry = (DictionaryEntry)obj;
  122. FieldInfo fieldInfo = dictionaryEntry.Key as FieldInfo;
  123. fieldInfo.SetValue(this, dictionaryEntry.Value);
  124. }
  125. }
  126. finally
  127. {
  128. IDisposable disposable;
  129. if ((disposable = (enumerator as IDisposable)) != null)
  130. {
  131. disposable.Dispose();
  132. }
  133. }
  134. SteamVR_Camera.values = null;
  135. }
  136. else
  137. {
  138. Component[] components = base.GetComponents<Component>();
  139. for (int i = 0; i < components.Length; i++)
  140. {
  141. SteamVR_Camera steamVR_Camera = components[i] as SteamVR_Camera;
  142. if (steamVR_Camera != null && steamVR_Camera != this)
  143. {
  144. if (steamVR_Camera.flip != null)
  145. {
  146. UnityEngine.Object.DestroyImmediate(steamVR_Camera.flip);
  147. }
  148. UnityEngine.Object.DestroyImmediate(steamVR_Camera);
  149. }
  150. }
  151. components = base.GetComponents<Component>();
  152. if (this != components[components.Length - 1])
  153. {
  154. SteamVR_Camera.values = new Hashtable();
  155. FieldInfo[] fields = base.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  156. foreach (FieldInfo fieldInfo2 in fields)
  157. {
  158. if (fieldInfo2.IsPublic || fieldInfo2.IsDefined(typeof(SerializeField), true))
  159. {
  160. SteamVR_Camera.values[fieldInfo2] = fieldInfo2.GetValue(this);
  161. }
  162. }
  163. GameObject gameObject = base.gameObject;
  164. UnityEngine.Object.DestroyImmediate(this);
  165. gameObject.AddComponent<SteamVR_Camera>().ForceLast();
  166. }
  167. }
  168. }
  169. public string baseName
  170. {
  171. get
  172. {
  173. return (!base.name.EndsWith(" (eye)")) ? base.name : base.name.Substring(0, base.name.Length - " (eye)".Length);
  174. }
  175. }
  176. public void Expand()
  177. {
  178. Transform transform = base.transform.parent;
  179. if (transform == null)
  180. {
  181. transform = new GameObject(base.name + " (origin)").transform;
  182. transform.localPosition = base.transform.localPosition;
  183. transform.localRotation = base.transform.localRotation;
  184. transform.localScale = base.transform.localScale;
  185. }
  186. if (this.head == null)
  187. {
  188. this._head = new GameObject(base.name + " (head)", new Type[]
  189. {
  190. typeof(SteamVR_GameView),
  191. typeof(SteamVR_TrackedObject)
  192. }).transform;
  193. this.head.parent = transform;
  194. this.head.position = base.transform.position;
  195. this.head.rotation = base.transform.rotation;
  196. this.head.localScale = Vector3.one;
  197. this.head.tag = base.tag;
  198. Camera component = this.head.GetComponent<Camera>();
  199. component.clearFlags = CameraClearFlags.Nothing;
  200. component.cullingMask = 0;
  201. component.eventMask = 0;
  202. component.orthographic = true;
  203. component.orthographicSize = 1f;
  204. component.nearClipPlane = 0f;
  205. component.farClipPlane = 1f;
  206. component.useOcclusionCulling = false;
  207. }
  208. if (base.transform.parent != this.head)
  209. {
  210. base.transform.parent = this.head;
  211. base.transform.localPosition = Vector3.zero;
  212. base.transform.localRotation = Quaternion.identity;
  213. base.transform.localScale = Vector3.one;
  214. while (base.transform.childCount > 0)
  215. {
  216. base.transform.GetChild(0).parent = this.head;
  217. }
  218. GUILayer component2 = base.GetComponent<GUILayer>();
  219. if (component2 != null)
  220. {
  221. UnityEngine.Object.DestroyImmediate(component2);
  222. this.head.gameObject.AddComponent<GUILayer>();
  223. }
  224. AudioListener component3 = base.GetComponent<AudioListener>();
  225. if (component3 != null)
  226. {
  227. UnityEngine.Object.DestroyImmediate(component3);
  228. this._ears = new GameObject(base.name + " (ears)", new Type[]
  229. {
  230. typeof(SteamVR_Ears)
  231. }).transform;
  232. this.ears.parent = this._head;
  233. this.ears.localPosition = Vector3.zero;
  234. this.ears.localRotation = Quaternion.identity;
  235. this.ears.localScale = Vector3.one;
  236. }
  237. }
  238. if (!base.name.EndsWith(" (eye)"))
  239. {
  240. base.name += " (eye)";
  241. }
  242. }
  243. public void Collapse()
  244. {
  245. base.transform.parent = null;
  246. while (this.head.childCount > 0)
  247. {
  248. this.head.GetChild(0).parent = base.transform;
  249. }
  250. GUILayer component = this.head.GetComponent<GUILayer>();
  251. if (component != null)
  252. {
  253. UnityEngine.Object.DestroyImmediate(component);
  254. base.gameObject.AddComponent<GUILayer>();
  255. }
  256. if (this.ears != null)
  257. {
  258. while (this.ears.childCount > 0)
  259. {
  260. this.ears.GetChild(0).parent = base.transform;
  261. }
  262. UnityEngine.Object.DestroyImmediate(this.ears.gameObject);
  263. this._ears = null;
  264. base.gameObject.AddComponent(typeof(AudioListener));
  265. }
  266. if (this.origin != null)
  267. {
  268. if (this.origin.name.EndsWith(" (origin)"))
  269. {
  270. Transform origin = this.origin;
  271. while (origin.childCount > 0)
  272. {
  273. origin.GetChild(0).parent = origin.parent;
  274. }
  275. UnityEngine.Object.DestroyImmediate(origin.gameObject);
  276. }
  277. else
  278. {
  279. base.transform.parent = this.origin;
  280. }
  281. }
  282. UnityEngine.Object.DestroyImmediate(this.head.gameObject);
  283. this._head = null;
  284. if (base.name.EndsWith(" (eye)"))
  285. {
  286. base.name = base.name.Substring(0, base.name.Length - " (eye)".Length);
  287. }
  288. }
  289. [SerializeField]
  290. private Transform _head;
  291. [SerializeField]
  292. private Transform _ears;
  293. public bool wireframe;
  294. [SerializeField]
  295. private SteamVR_CameraFlip flip;
  296. public static Material blitMaterial;
  297. private static Hashtable values;
  298. private const string eyeSuffix = " (eye)";
  299. private const string earsSuffix = " (ears)";
  300. private const string headSuffix = " (head)";
  301. private const string originSuffix = " (origin)";
  302. }