SteamVR_ExternalCamera.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. using Valve.VR;
  7. public class SteamVR_ExternalCamera : MonoBehaviour
  8. {
  9. public void ReadConfig()
  10. {
  11. try
  12. {
  13. HmdMatrix34_t pose = default(HmdMatrix34_t);
  14. bool flag = false;
  15. object obj = this.config;
  16. string[] array = File.ReadAllLines(this.configPath);
  17. foreach (string text in array)
  18. {
  19. string[] array3 = text.Split(new char[]
  20. {
  21. '='
  22. });
  23. if (array3.Length == 2)
  24. {
  25. string text2 = array3[0];
  26. if (text2 == "m")
  27. {
  28. string[] array4 = array3[1].Split(new char[]
  29. {
  30. ','
  31. });
  32. if (array4.Length == 12)
  33. {
  34. pose.m0 = float.Parse(array4[0]);
  35. pose.m1 = float.Parse(array4[1]);
  36. pose.m2 = float.Parse(array4[2]);
  37. pose.m3 = float.Parse(array4[3]);
  38. pose.m4 = float.Parse(array4[4]);
  39. pose.m5 = float.Parse(array4[5]);
  40. pose.m6 = float.Parse(array4[6]);
  41. pose.m7 = float.Parse(array4[7]);
  42. pose.m8 = float.Parse(array4[8]);
  43. pose.m9 = float.Parse(array4[9]);
  44. pose.m10 = float.Parse(array4[10]);
  45. pose.m11 = float.Parse(array4[11]);
  46. flag = true;
  47. }
  48. }
  49. else if (text2 == "disableStandardAssets")
  50. {
  51. FieldInfo field = obj.GetType().GetField(text2);
  52. if (field != null)
  53. {
  54. field.SetValue(obj, bool.Parse(array3[1]));
  55. }
  56. }
  57. else
  58. {
  59. FieldInfo field2 = obj.GetType().GetField(text2);
  60. if (field2 != null)
  61. {
  62. field2.SetValue(obj, float.Parse(array3[1]));
  63. }
  64. }
  65. }
  66. }
  67. this.config = (SteamVR_ExternalCamera.Config)obj;
  68. if (flag)
  69. {
  70. SteamVR_Utils.RigidTransform rigidTransform = new SteamVR_Utils.RigidTransform(pose);
  71. this.config.x = rigidTransform.pos.x;
  72. this.config.y = rigidTransform.pos.y;
  73. this.config.z = rigidTransform.pos.z;
  74. Vector3 eulerAngles = rigidTransform.rot.eulerAngles;
  75. this.config.rx = eulerAngles.x;
  76. this.config.ry = eulerAngles.y;
  77. this.config.rz = eulerAngles.z;
  78. }
  79. }
  80. catch
  81. {
  82. }
  83. }
  84. public void AttachToCamera(SteamVR_Camera vrcam)
  85. {
  86. if (this.target == vrcam.head)
  87. {
  88. return;
  89. }
  90. this.target = vrcam.head;
  91. Transform parent = base.transform.parent;
  92. Transform parent2 = vrcam.head.parent;
  93. parent.parent = parent2;
  94. parent.localPosition = Vector3.zero;
  95. parent.localRotation = Quaternion.identity;
  96. parent.localScale = Vector3.one;
  97. vrcam.enabled = false;
  98. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(vrcam.gameObject);
  99. vrcam.enabled = true;
  100. gameObject.name = "camera";
  101. UnityEngine.Object.DestroyImmediate(gameObject.GetComponent<SteamVR_Camera>());
  102. UnityEngine.Object.DestroyImmediate(gameObject.GetComponent<SteamVR_CameraFlip>());
  103. this.cam = gameObject.GetComponent<Camera>();
  104. this.cam.fieldOfView = this.config.fov;
  105. this.cam.useOcclusionCulling = false;
  106. this.cam.enabled = false;
  107. this.colorMat = new Material(Shader.Find("Custom/SteamVR_ColorOut"));
  108. this.alphaMat = new Material(Shader.Find("Custom/SteamVR_AlphaOut"));
  109. this.clipMaterial = new Material(Shader.Find("Custom/SteamVR_ClearAll"));
  110. Transform transform = gameObject.transform;
  111. transform.parent = base.transform;
  112. transform.localPosition = new Vector3(this.config.x, this.config.y, this.config.z);
  113. transform.localRotation = Quaternion.Euler(this.config.rx, this.config.ry, this.config.rz);
  114. transform.localScale = Vector3.one;
  115. while (transform.childCount > 0)
  116. {
  117. UnityEngine.Object.DestroyImmediate(transform.GetChild(0).gameObject);
  118. }
  119. this.clipQuad = GameObject.CreatePrimitive(PrimitiveType.Quad);
  120. this.clipQuad.name = "ClipQuad";
  121. UnityEngine.Object.DestroyImmediate(this.clipQuad.GetComponent<MeshCollider>());
  122. MeshRenderer component = this.clipQuad.GetComponent<MeshRenderer>();
  123. component.material = this.clipMaterial;
  124. component.shadowCastingMode = ShadowCastingMode.Off;
  125. component.receiveShadows = false;
  126. component.lightProbeUsage = LightProbeUsage.Off;
  127. component.reflectionProbeUsage = ReflectionProbeUsage.Off;
  128. Transform transform2 = this.clipQuad.transform;
  129. transform2.parent = transform;
  130. transform2.localScale = new Vector3(1000f, 1000f, 1f);
  131. transform2.localRotation = Quaternion.identity;
  132. this.clipQuad.SetActive(false);
  133. }
  134. public float GetTargetDistance()
  135. {
  136. if (this.target == null)
  137. {
  138. return this.config.near + 0.01f;
  139. }
  140. Transform transform = this.cam.transform;
  141. Vector3 vector = new Vector3(transform.forward.x, 0f, transform.forward.z);
  142. Vector3 normalized = vector.normalized;
  143. Vector3 position = this.target.position;
  144. Vector3 vector2 = new Vector3(this.target.forward.x, 0f, this.target.forward.z);
  145. Vector3 inPoint = position + vector2.normalized * this.config.hmdOffset;
  146. Plane plane = new Plane(normalized, inPoint);
  147. float value = -plane.GetDistanceToPoint(transform.position);
  148. return Mathf.Clamp(value, this.config.near + 0.01f, this.config.far - 0.01f);
  149. }
  150. public void RenderNear()
  151. {
  152. int num = Screen.width / 2;
  153. int num2 = Screen.height / 2;
  154. if (this.cam.targetTexture == null || this.cam.targetTexture.width != num || this.cam.targetTexture.height != num2)
  155. {
  156. this.cam.targetTexture = new RenderTexture(num, num2, 24, RenderTextureFormat.ARGB32);
  157. this.cam.targetTexture.antiAliasing = ((QualitySettings.antiAliasing != 0) ? QualitySettings.antiAliasing : 1);
  158. }
  159. this.cam.nearClipPlane = this.config.near;
  160. this.cam.farClipPlane = this.config.far;
  161. CameraClearFlags clearFlags = this.cam.clearFlags;
  162. Color backgroundColor = this.cam.backgroundColor;
  163. this.cam.clearFlags = CameraClearFlags.Color;
  164. this.cam.backgroundColor = Color.clear;
  165. float d = Mathf.Clamp(this.GetTargetDistance() + this.config.nearOffset, this.config.near, this.config.far);
  166. Transform parent = this.clipQuad.transform.parent;
  167. this.clipQuad.transform.position = parent.position + parent.forward * d;
  168. MonoBehaviour[] array = null;
  169. bool[] array2 = null;
  170. if (this.config.disableStandardAssets)
  171. {
  172. array = this.cam.gameObject.GetComponents<MonoBehaviour>();
  173. array2 = new bool[array.Length];
  174. for (int i = 0; i < array.Length; i++)
  175. {
  176. MonoBehaviour monoBehaviour = array[i];
  177. if (monoBehaviour.enabled && monoBehaviour.GetType().ToString().StartsWith("UnityStandardAssets."))
  178. {
  179. monoBehaviour.enabled = false;
  180. array2[i] = true;
  181. }
  182. }
  183. }
  184. this.clipQuad.SetActive(true);
  185. this.cam.Render();
  186. this.clipQuad.SetActive(false);
  187. if (array != null)
  188. {
  189. for (int j = 0; j < array.Length; j++)
  190. {
  191. if (array2[j])
  192. {
  193. array[j].enabled = true;
  194. }
  195. }
  196. }
  197. this.cam.clearFlags = clearFlags;
  198. this.cam.backgroundColor = backgroundColor;
  199. Graphics.DrawTexture(new Rect(0f, 0f, (float)num, (float)num2), this.cam.targetTexture, this.colorMat);
  200. Graphics.DrawTexture(new Rect((float)num, 0f, (float)num, (float)num2), this.cam.targetTexture, this.alphaMat);
  201. }
  202. public void RenderFar()
  203. {
  204. this.cam.nearClipPlane = this.config.near;
  205. this.cam.farClipPlane = this.config.far;
  206. this.cam.Render();
  207. int num = Screen.width / 2;
  208. int num2 = Screen.height / 2;
  209. Graphics.DrawTexture(new Rect(0f, (float)num2, (float)num, (float)num2), this.cam.targetTexture, this.colorMat);
  210. }
  211. private void OnGUI()
  212. {
  213. }
  214. private void OnEnable()
  215. {
  216. this.cameras = UnityEngine.Object.FindObjectsOfType<Camera>();
  217. if (this.cameras != null)
  218. {
  219. int num = this.cameras.Length;
  220. this.cameraRects = new Rect[num];
  221. for (int i = 0; i < num; i++)
  222. {
  223. Camera camera = this.cameras[i];
  224. this.cameraRects[i] = camera.rect;
  225. if (!(camera == this.cam))
  226. {
  227. if (!(camera.targetTexture != null))
  228. {
  229. if (!(camera.GetComponent<SteamVR_Camera>() != null))
  230. {
  231. camera.rect = new Rect(0.5f, 0f, 0.5f, 0.5f);
  232. }
  233. }
  234. }
  235. }
  236. }
  237. if (this.config.sceneResolutionScale > 0f)
  238. {
  239. this.sceneResolutionScale = SteamVR_Camera.sceneResolutionScale;
  240. SteamVR_Camera.sceneResolutionScale = this.config.sceneResolutionScale;
  241. }
  242. }
  243. private void OnDisable()
  244. {
  245. if (this.cameras != null)
  246. {
  247. int num = this.cameras.Length;
  248. for (int i = 0; i < num; i++)
  249. {
  250. Camera camera = this.cameras[i];
  251. if (camera != null)
  252. {
  253. camera.rect = this.cameraRects[i];
  254. }
  255. }
  256. this.cameras = null;
  257. this.cameraRects = null;
  258. }
  259. if (this.config.sceneResolutionScale > 0f)
  260. {
  261. SteamVR_Camera.sceneResolutionScale = this.sceneResolutionScale;
  262. }
  263. }
  264. public SteamVR_ExternalCamera.Config config;
  265. public string configPath;
  266. private Camera cam;
  267. private Transform target;
  268. private GameObject clipQuad;
  269. private Material clipMaterial;
  270. private Material colorMat;
  271. private Material alphaMat;
  272. private Camera[] cameras;
  273. private Rect[] cameraRects;
  274. private float sceneResolutionScale;
  275. public struct Config
  276. {
  277. public float x;
  278. public float y;
  279. public float z;
  280. public float rx;
  281. public float ry;
  282. public float rz;
  283. public float fov;
  284. public float near;
  285. public float far;
  286. public float sceneResolutionScale;
  287. public float frameSkip;
  288. public float nearOffset;
  289. public float farOffset;
  290. public float hmdOffset;
  291. public bool disableStandardAssets;
  292. }
  293. }