SvrEye.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class SvrEye : MonoBehaviour, IComparable<SvrEye>
  6. {
  7. public static List<SvrEye> Instances = new List<SvrEye>();
  8. public enum eSide
  9. {
  10. Left = 1,
  11. Right = 2,
  12. Both = 3,
  13. [HideInInspector]
  14. Count = Both
  15. };
  16. public enum eType
  17. {
  18. RenderTexture = 0,
  19. StandardTexture = 1,
  20. EglTexture = 2,
  21. };
  22. public delegate void OnPostRenderCallback(int sideMask, int layerMask);
  23. public OnPostRenderCallback OnPostRenderListener;
  24. public delegate void OnPreRenderCallback(int sideMask, int textureId, int previousId);
  25. public OnPreRenderCallback OnPreRenderListener;
  26. [Tooltip("Layer depth stack")]
  27. public int layerDepth = 0;
  28. [Tooltip("Image display transform")]
  29. public Camera imageCamera;
  30. [Tooltip("Image type: 0/Camera render target, 1/Texture 2d, 2/External egl")]
  31. public eType imageType = eType.RenderTexture;
  32. [Tooltip("Image texture used when ImageType is StandardTexture")]
  33. public Texture imageTexture;
  34. [Tooltip("Image transform for scale, rotation and position (optional)")]
  35. public Transform imageTransform;
  36. [Tooltip("Image display region (camera clip space)")]
  37. public Vector4 clipLowerLeft = new Vector4(-1, -1, 0, 1);
  38. public Vector4 clipUpperLeft = new Vector4(-1, 1, 0, 1);
  39. public Vector4 clipUpperRight = new Vector4(1, 1, 0, 1);
  40. public Vector4 clipLowerRight = new Vector4(1, -1, 0, 1);
  41. [Tooltip("Image source region (texture uv space)")]
  42. public Vector2 uvLowerLeft = new Vector2(0, 0);
  43. public Vector2 uvUpperLeft = new Vector2(0, 1);
  44. public Vector2 uvUpperRight = new Vector2(1, 1);
  45. public Vector2 uvLowerRight = new Vector2(1, 0);
  46. [Tooltip("Side mask")]
  47. public eSide side = eSide.Both;
  48. private float fovMargin = 0f;
  49. private RenderTextureFormat format = RenderTextureFormat.Default;
  50. private Vector2 resolution = new Vector2(1920, 1080);
  51. private float resolutionScaleFactor = 1.0f;
  52. private int antiAliasing = 1;
  53. private int depth = 24;
  54. private int frustumType = 0;
  55. private const int bufferCount = 3;
  56. private RenderTexture[] eyeTextures = new RenderTexture[bufferCount];
  57. private int[] eyeTextureIds = new int[bufferCount];
  58. private int currentTextureIndex = 0;
  59. private Camera[] mainCameras = null;
  60. private bool dirty = false;
  61. private Coroutine recreateBuffersCoroutine = null;
  62. public int CompareTo(SvrEye that)
  63. {
  64. return this.layerDepth.CompareTo(that.layerDepth);
  65. }
  66. public float FovMargin
  67. {
  68. get { return fovMargin; }
  69. set { fovMargin = value; }
  70. }
  71. public int FrustumType
  72. {
  73. get { return frustumType; }
  74. set { frustumType = value; }
  75. }
  76. public void SetImage(Texture2D texture)
  77. {
  78. imageTexture = texture;
  79. InitializeBuffers();
  80. }
  81. public eType ImageType
  82. {
  83. get { return imageType; }
  84. set { imageType = value; }
  85. }
  86. public eSide Side
  87. {
  88. get { return side; }
  89. set { side = value; }
  90. }
  91. public RenderTextureFormat Format
  92. {
  93. get { return format; }
  94. set { SetDirty(format != value); format = value; }
  95. }
  96. public int AntiAliasing
  97. {
  98. get { return antiAliasing; }
  99. set { SetDirty(antiAliasing != value); antiAliasing = value; }
  100. }
  101. public int Depth
  102. {
  103. get { return depth; }
  104. set { SetDirty(depth != value); depth = value; }
  105. }
  106. public Vector2 Resolution
  107. {
  108. get { return resolution; }
  109. set { SetDirty(!Mathf.Approximately(resolution.x, value.x) || !Mathf.Approximately(resolution.y, value.y)); resolution = value; }
  110. }
  111. public float ResolutionScaleFactor
  112. {
  113. get { return resolutionScaleFactor; }
  114. set { SetDirty(!Mathf.Approximately(resolutionScaleFactor, value)); resolutionScaleFactor = value; }
  115. }
  116. void SetDirty(bool value)
  117. {
  118. dirty = dirty == true ? true : value;
  119. }
  120. public int TextureId
  121. {
  122. get { return eyeTextureIds[currentTextureIndex]; }
  123. set { eyeTextureIds[currentTextureIndex] = value; }
  124. }
  125. public int PreviousId
  126. {
  127. get { return eyeTextureIds[(currentTextureIndex + bufferCount - 1) % bufferCount]; }
  128. }
  129. public Texture TexturePtr
  130. {
  131. get { return (imageTexture != null ? imageTexture : (Texture)eyeTextures[currentTextureIndex]); }
  132. }
  133. void Awake()
  134. {
  135. Instances.Add(this);
  136. AcquireComponents();
  137. InitializeCoords();
  138. }
  139. void OnDestroy()
  140. {
  141. Instances.Remove(this);
  142. }
  143. void AcquireComponents()
  144. {
  145. if (imageCamera == null) imageCamera = gameObject.GetComponent<Camera>();
  146. Debug.Assert(imageCamera != null, "ImageCamera object required");
  147. mainCameras = imageCamera.GetComponentsInChildren<Camera>();
  148. }
  149. void Start()
  150. {
  151. //Initialize(); Called by SvrManager.InitializeEyes()
  152. }
  153. void LateUpdate()
  154. {
  155. UpdateCoords();
  156. }
  157. public void Initialize()
  158. {
  159. InitializeBuffers();
  160. InitializeCameras();
  161. }
  162. void InitializeBuffers()
  163. {
  164. for (int i = 0; i < bufferCount; ++i)
  165. {
  166. if (eyeTextures[i] != null)
  167. eyeTextures[i].Release();
  168. switch(imageType)
  169. {
  170. case eType.RenderTexture:
  171. eyeTextures[i] = new RenderTexture((int)(resolution.x * resolutionScaleFactor), (int)(resolution.y * resolutionScaleFactor), depth, format);
  172. eyeTextures[i].antiAliasing = antiAliasing;
  173. eyeTextures[i].Create();
  174. eyeTextureIds[i] = eyeTextures[i].GetNativeTexturePtr().ToInt32();
  175. //Debug.Log("Create Render Texture with ID: " + eyeTextureIds[i] + " Width: " + eyeTextures[i].width + " Height: " + eyeTextures[i].height + " AA: " + eyeTextures[i].antiAliasing);
  176. break;
  177. case eType.StandardTexture:
  178. if (imageTexture) eyeTextureIds[i] = imageTexture.GetNativeTexturePtr().ToInt32();
  179. break;
  180. case eType.EglTexture:
  181. eyeTextureIds[i] = 0;
  182. break;
  183. }
  184. }
  185. dirty = false;
  186. }
  187. void InitializeCameras()
  188. {
  189. var deviceInfo = SvrPlugin.Instance.deviceInfo;
  190. var deviceFov = new Vector2(deviceInfo.targetFovXRad, deviceInfo.targetFovYRad) * Mathf.Rad2Deg;
  191. var frustum = side == eSide.Right ? deviceInfo.targetFrustumRight : deviceInfo.targetFrustumLeft;
  192. foreach (var mainCamera in mainCameras)
  193. {
  194. if (frustumType == (int)SvrManager.SvrSettings.eFrustumType.Camera)
  195. {
  196. mainCamera.fieldOfView = deviceFov.y + FovMargin * deviceFov.y;
  197. mainCamera.aspect = deviceFov.x / deviceFov.y;
  198. }
  199. else if (frustumType == (int)SvrManager.SvrSettings.eFrustumType.Device)
  200. {
  201. mainCamera.fieldOfView = deviceFov.y;
  202. mainCamera.aspect = deviceFov.x / deviceFov.y;
  203. mainCamera.projectionMatrix = SvrManager.Perspective(frustum.left, frustum.right, frustum.bottom, frustum.top, frustum.near, mainCamera.farClipPlane);
  204. }
  205. }
  206. }
  207. void InitializeCoords()
  208. {
  209. clipLowerLeft.Set(-1, -1, 0, 1);
  210. clipUpperLeft.Set(-1, 1, 0, 1);
  211. clipUpperRight.Set(1, 1, 0, 1);
  212. clipLowerRight.Set(1, -1, 0, 1);
  213. }
  214. void UpdateCoords()
  215. {
  216. if (imageTransform == null)
  217. return;
  218. var viewCamera = mainCameras[0];
  219. if (viewCamera == null)
  220. return;
  221. var extents = 0.5f * Vector3.one;
  222. var center = Vector3.zero;
  223. var worldLowerLeft = new Vector4(center.x - extents.x, center.y - extents.y, 0, 1);
  224. var worldUpperLeft = new Vector4(center.x - extents.x, center.y + extents.y, 0, 1);
  225. var worldUpperRight = new Vector4(center.x + extents.x, center.y + extents.y, 0, 1);
  226. var worldLowerRight = new Vector4(center.x + extents.x, center.y - extents.y, 0, 1);
  227. Matrix4x4 MVP = viewCamera.projectionMatrix * viewCamera.worldToCameraMatrix * imageTransform.localToWorldMatrix;
  228. clipLowerLeft = MVP * worldLowerLeft;
  229. clipUpperLeft = MVP * worldUpperLeft;
  230. clipUpperRight = MVP * worldUpperRight;
  231. clipLowerRight = MVP * worldLowerRight;
  232. }
  233. void OnPreRender()
  234. {
  235. if (imageType != eType.RenderTexture) return;
  236. SwapBuffers();
  237. if (OnPreRenderListener != null)
  238. {
  239. OnPreRenderListener((int)side, TextureId, PreviousId);
  240. }
  241. }
  242. void SwapBuffers()
  243. {
  244. if (imageType != eType.RenderTexture) return;
  245. currentTextureIndex = ++currentTextureIndex % bufferCount;
  246. var targetTexture = eyeTextures[currentTextureIndex];
  247. if (targetTexture == null) return;
  248. for (int i = 0; i < mainCameras.Length; i++)
  249. {
  250. mainCameras[i].targetTexture = targetTexture;
  251. }
  252. targetTexture.DiscardContents();
  253. }
  254. void OnPostRender()
  255. {
  256. RecreateBuffersIfDirty();
  257. if (OnPostRenderListener != null)
  258. {
  259. OnPostRenderListener((int)side, 0x0);
  260. }
  261. }
  262. void RecreateBuffersIfDirty()
  263. {
  264. if (dirty)
  265. {
  266. if (recreateBuffersCoroutine != null)
  267. {
  268. StopCoroutine(recreateBuffersCoroutine);
  269. recreateBuffersCoroutine = null;
  270. }
  271. recreateBuffersCoroutine = StartCoroutine(RecreateBuffersDeferred());
  272. dirty = false;
  273. }
  274. }
  275. IEnumerator RecreateBuffersDeferred()
  276. {
  277. int i = 0;
  278. while (i < bufferCount)
  279. {
  280. int index = currentTextureIndex - 1;
  281. index = index >= 0 ? index : bufferCount - 1;
  282. if (eyeTextures[index] != null)
  283. eyeTextures[index].Release();
  284. switch (imageType)
  285. {
  286. case eType.RenderTexture:
  287. eyeTextures[index] = new RenderTexture((int)(resolution.x * resolutionScaleFactor), (int)(resolution.y * resolutionScaleFactor), depth, format);
  288. eyeTextures[index].antiAliasing = antiAliasing;
  289. eyeTextures[index].Create();
  290. eyeTextureIds[index] = eyeTextures[index].GetNativeTexturePtr().ToInt32();
  291. Debug.Log("Re-create Render Texture with ID: " + eyeTextureIds[index] + " Width: " + eyeTextures[index].width + " Height: " + eyeTextures[index].height + " AA: " + eyeTextures[index].antiAliasing);
  292. break;
  293. case eType.StandardTexture:
  294. if (imageTexture) eyeTextureIds[index] = imageTexture.GetNativeTexturePtr().ToInt32();
  295. break;
  296. case eType.EglTexture:
  297. eyeTextureIds[index] = 0;
  298. break;
  299. }
  300. int prevTextureIndex = currentTextureIndex;
  301. yield return new WaitUntil(() => currentTextureIndex != prevTextureIndex);
  302. i++;
  303. }
  304. yield break;
  305. }
  306. }