OVRCompositionUtil.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  4. internal class OVRCompositionUtil {
  5. public static void SafeDestroy(GameObject obj)
  6. {
  7. if (Application.isPlaying)
  8. {
  9. GameObject.Destroy(obj);
  10. }
  11. else
  12. {
  13. GameObject.DestroyImmediate(obj);
  14. }
  15. }
  16. public static void SafeDestroy(ref GameObject obj)
  17. {
  18. SafeDestroy(obj);
  19. obj = null;
  20. }
  21. public static OVRPlugin.CameraDevice ConvertCameraDevice(OVRManager.CameraDevice cameraDevice)
  22. {
  23. if (cameraDevice == OVRManager.CameraDevice.WebCamera0)
  24. {
  25. return OVRPlugin.CameraDevice.WebCamera0;
  26. }
  27. else if (cameraDevice == OVRManager.CameraDevice.WebCamera1)
  28. {
  29. return OVRPlugin.CameraDevice.WebCamera1;
  30. }
  31. else if (cameraDevice == OVRManager.CameraDevice.ZEDCamera)
  32. {
  33. return OVRPlugin.CameraDevice.ZEDCamera;
  34. }
  35. else
  36. {
  37. return OVRPlugin.CameraDevice.None;
  38. }
  39. }
  40. public static OVRBoundary.BoundaryType ToBoundaryType(OVRManager.VirtualGreenScreenType type)
  41. {
  42. if (type == OVRManager.VirtualGreenScreenType.OuterBoundary)
  43. {
  44. return OVRBoundary.BoundaryType.OuterBoundary;
  45. }
  46. else if (type == OVRManager.VirtualGreenScreenType.PlayArea)
  47. {
  48. return OVRBoundary.BoundaryType.PlayArea;
  49. }
  50. else
  51. {
  52. Debug.LogWarning("Unmatched VirtualGreenScreenType");
  53. return OVRBoundary.BoundaryType.OuterBoundary;
  54. }
  55. }
  56. public static Vector3 GetWorldPosition(Vector3 trackingSpacePosition)
  57. {
  58. OVRPose tsPose;
  59. tsPose.position = trackingSpacePosition;
  60. tsPose.orientation = Quaternion.identity;
  61. OVRPose wsPose = OVRExtensions.ToWorldSpacePose(tsPose);
  62. Vector3 pos = wsPose.position;
  63. return pos;
  64. }
  65. public static float GetMaximumBoundaryDistance(Camera camera, OVRBoundary.BoundaryType boundaryType)
  66. {
  67. if (!OVRManager.boundary.GetConfigured())
  68. {
  69. return float.MaxValue;
  70. }
  71. Vector3[] geometry = OVRManager.boundary.GetGeometry(boundaryType);
  72. if (geometry.Length == 0)
  73. {
  74. return float.MaxValue;
  75. }
  76. float maxDistance = -float.MaxValue;
  77. foreach (Vector3 v in geometry)
  78. {
  79. Vector3 pos = GetWorldPosition(v);
  80. float distance = Vector3.Dot(camera.transform.forward, pos);
  81. if (maxDistance < distance)
  82. {
  83. maxDistance = distance;
  84. }
  85. }
  86. return maxDistance;
  87. }
  88. public static Mesh BuildBoundaryMesh(OVRBoundary.BoundaryType boundaryType, float topY, float bottomY)
  89. {
  90. if (!OVRManager.boundary.GetConfigured())
  91. {
  92. return null;
  93. }
  94. List<Vector3> geometry = new List<Vector3>(OVRManager.boundary.GetGeometry(boundaryType));
  95. if (geometry.Count == 0)
  96. {
  97. return null;
  98. }
  99. geometry.Add(geometry[0]);
  100. int numPoints = geometry.Count;
  101. Vector3[] vertices = new Vector3[numPoints * 2];
  102. Vector2[] uvs = new Vector2[numPoints * 2];
  103. for (int i = 0; i < numPoints; ++i)
  104. {
  105. Vector3 v = geometry[i];
  106. vertices[i] = new Vector3(v.x, bottomY, v.z);
  107. vertices[i + numPoints] = new Vector3(v.x, topY, v.z);
  108. uvs[i] = new Vector2((float)i / (numPoints - 1), 0.0f);
  109. uvs[i + numPoints] = new Vector2(uvs[i].x, 1.0f);
  110. }
  111. int[] triangles = new int[(numPoints - 1) * 2 * 3];
  112. for (int i = 0; i < numPoints - 1; ++i)
  113. {
  114. // the geometry is built clockwised. only the back faces should be rendered in the camera frame mask
  115. triangles[i * 6 + 0] = i;
  116. triangles[i * 6 + 1] = i + numPoints;
  117. triangles[i * 6 + 2] = i + 1 + numPoints;
  118. triangles[i * 6 + 3] = i;
  119. triangles[i * 6 + 4] = i + 1 + numPoints;
  120. triangles[i * 6 + 5] = i + 1;
  121. }
  122. Mesh mesh = new Mesh();
  123. mesh.vertices = vertices;
  124. mesh.uv = uvs;
  125. mesh.triangles = triangles;
  126. return mesh;
  127. }
  128. }
  129. #endif