OVROverlay.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. ref Vector3 ptr = ref lossyScale;
  103. int index;
  104. lossyScale[index = j] = ptr[index] / Camera.current.transform.lossyScale[j];
  105. }
  106. if (this.currentOverlayShape == OVROverlay.OverlayShape.Cubemap)
  107. {
  108. ovrpose.position = Camera.current.transform.position;
  109. }
  110. if (this.currentOverlayShape == OVROverlay.OverlayShape.Cylinder)
  111. {
  112. float num = lossyScale.x / lossyScale.z / 3.14159274f * 180f;
  113. if (num > 180f)
  114. {
  115. Debug.LogError("Cylinder overlay's arc angle has to be below 180 degree, current arc angle is " + num + " degree.");
  116. return;
  117. }
  118. }
  119. 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);
  120. if (this.rend)
  121. {
  122. this.rend.enabled = !flag2;
  123. }
  124. }
  125. private const int maxInstances = 15;
  126. private static OVROverlay[] instances = new OVROverlay[15];
  127. public OVROverlay.OverlayType currentOverlayType = OVROverlay.OverlayType.Overlay;
  128. public OVROverlay.OverlayShape currentOverlayShape;
  129. public Texture[] textures = new Texture[2];
  130. private Texture[] cachedTextures = new Texture[2];
  131. private IntPtr[] texNativePtrs = new IntPtr[]
  132. {
  133. IntPtr.Zero,
  134. IntPtr.Zero
  135. };
  136. private int layerIndex = -1;
  137. private Renderer rend;
  138. public enum OverlayShape
  139. {
  140. Quad,
  141. Cylinder,
  142. Cubemap
  143. }
  144. public enum OverlayType
  145. {
  146. None,
  147. Underlay,
  148. Overlay,
  149. OverlayShowLod
  150. }
  151. }