OVROverlay.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.VR;
  4. public class OVROverlay : MonoBehaviour
  5. {
  6. public void OverrideOverlayTextureInfo(Texture srcTexture, IntPtr nativePtr, VRNode node)
  7. {
  8. int num = (node != VRNode.RightEye) ? 0 : 1;
  9. this.textures[num] = srcTexture;
  10. this.cachedTextures[num] = srcTexture;
  11. this.texNativePtrs[num] = nativePtr;
  12. }
  13. private void Awake()
  14. {
  15. Debug.Log("Overlay Awake");
  16. this.rend = base.GetComponent<Renderer>();
  17. for (int i = 0; i < 2; i++)
  18. {
  19. if (this.rend != null && this.textures[i] == null)
  20. {
  21. this.textures[i] = this.rend.material.mainTexture;
  22. }
  23. if (this.textures[i] != null)
  24. {
  25. this.cachedTextures[i] = this.textures[i];
  26. this.texNativePtrs[i] = this.textures[i].GetNativeTexturePtr();
  27. }
  28. }
  29. }
  30. private void OnEnable()
  31. {
  32. if (!OVRManager.isHmdPresent)
  33. {
  34. base.enabled = false;
  35. return;
  36. }
  37. this.OnDisable();
  38. for (int i = 0; i < 15; i++)
  39. {
  40. if (OVROverlay.instances[i] == null || OVROverlay.instances[i] == this)
  41. {
  42. this.layerIndex = i;
  43. OVROverlay.instances[i] = this;
  44. break;
  45. }
  46. }
  47. }
  48. private void OnDisable()
  49. {
  50. if (this.layerIndex != -1)
  51. {
  52. OVRPlugin.SetOverlayQuad(true, false, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, OVRPose.identity.ToPosef(), Vector3.one.ToVector3f(), this.layerIndex, OVRPlugin.OverlayShape.Quad);
  53. OVROverlay.instances[this.layerIndex] = null;
  54. }
  55. this.layerIndex = -1;
  56. }
  57. private void OnRenderObject()
  58. {
  59. if (!Camera.current.CompareTag("MainCamera") || Camera.current.cameraType != CameraType.Game || this.layerIndex == -1 || this.currentOverlayType == OVROverlay.OverlayType.None)
  60. {
  61. return;
  62. }
  63. if (this.currentOverlayShape == OVROverlay.OverlayShape.Cylinder)
  64. {
  65. Debug.LogWarning("Overlay shape " + this.currentOverlayShape + " is not supported on current platform");
  66. }
  67. for (int i = 0; i < 2; i++)
  68. {
  69. if (i < this.textures.Length)
  70. {
  71. if (this.textures[i] != this.cachedTextures[i])
  72. {
  73. this.cachedTextures[i] = this.textures[i];
  74. if (this.cachedTextures[i] != null)
  75. {
  76. this.texNativePtrs[i] = this.cachedTextures[i].GetNativeTexturePtr();
  77. }
  78. }
  79. if (this.currentOverlayShape == OVROverlay.OverlayShape.Cubemap && this.textures[i] != null && this.textures[i].GetType() != typeof(Cubemap))
  80. {
  81. Debug.LogError("Need Cubemap texture for cube map overlay");
  82. return;
  83. }
  84. }
  85. }
  86. if (this.cachedTextures[0] == null || this.texNativePtrs[0] == IntPtr.Zero)
  87. {
  88. return;
  89. }
  90. bool onTop = this.currentOverlayType == OVROverlay.OverlayType.Overlay;
  91. bool flag = false;
  92. Transform transform = base.transform;
  93. while (transform != null && !flag)
  94. {
  95. flag |= (transform == Camera.current.transform);
  96. transform = transform.parent;
  97. }
  98. OVRPose ovrpose = (!flag) ? base.transform.ToTrackingSpacePose() : base.transform.ToHeadSpacePose();
  99. Vector3 lossyScale = base.transform.lossyScale;
  100. for (int j = 0; j < 3; j++)
  101. {
  102. int index;
  103. lossyScale[index = j] = lossyScale[index] / Camera.current.transform.lossyScale[j];
  104. }
  105. if (this.currentOverlayShape == OVROverlay.OverlayShape.Cubemap)
  106. {
  107. ovrpose.position = Camera.current.transform.position;
  108. }
  109. if (this.currentOverlayShape == OVROverlay.OverlayShape.Cylinder)
  110. {
  111. float num = lossyScale.x / lossyScale.z / 3.14159274f * 180f;
  112. if (num > 180f)
  113. {
  114. Debug.LogError("Cylinder overlay's arc angle has to be below 180 degree, current arc angle is " + num + " degree.");
  115. return;
  116. }
  117. }
  118. bool flag2 = OVRPlugin.SetOverlayQuad(onTop, flag, this.texNativePtrs[0], this.texNativePtrs[1], IntPtr.Zero, ovrpose.flipZ().ToPosef(), lossyScale.ToVector3f(), this.layerIndex, (OVRPlugin.OverlayShape)this.currentOverlayShape);
  119. if (this.rend)
  120. {
  121. this.rend.enabled = !flag2;
  122. }
  123. }
  124. private const int maxInstances = 15;
  125. private static OVROverlay[] instances = new OVROverlay[15];
  126. public OVROverlay.OverlayType currentOverlayType = OVROverlay.OverlayType.Overlay;
  127. public OVROverlay.OverlayShape currentOverlayShape;
  128. public Texture[] textures = new Texture[2];
  129. private Texture[] cachedTextures = new Texture[2];
  130. private IntPtr[] texNativePtrs = new IntPtr[]
  131. {
  132. IntPtr.Zero,
  133. IntPtr.Zero
  134. };
  135. private int layerIndex = -1;
  136. private Renderer rend;
  137. public enum OverlayShape
  138. {
  139. Quad,
  140. Cylinder,
  141. Cubemap
  142. }
  143. public enum OverlayType
  144. {
  145. None,
  146. Underlay,
  147. Overlay,
  148. OverlayShowLod
  149. }
  150. }