Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.5 KiB

  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
  4. the Utilities SDK except in compliance with the License, which is provided at the time of installation
  5. or download, or which otherwise accompanies this software in either electronic or hard copy form.
  6. You may obtain a copy of the License at
  7. https://developer.oculus.com/licenses/utilities-1.31
  8. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
  9. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  10. ANY KIND, either express or implied. See the License for the specific language governing
  11. permissions and limitations under the License.
  12. ************************************************************************************/
  13. using UnityEngine;
  14. using System.Collections.Generic;
  15. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || UNITY_ANDROID
  16. internal class OVRCompositionUtil {
  17. public static void SafeDestroy(GameObject obj)
  18. {
  19. if (Application.isPlaying)
  20. {
  21. GameObject.Destroy(obj);
  22. }
  23. else
  24. {
  25. GameObject.DestroyImmediate(obj);
  26. }
  27. }
  28. public static void SafeDestroy(ref GameObject obj)
  29. {
  30. if (obj != null)
  31. {
  32. SafeDestroy(obj);
  33. obj = null;
  34. }
  35. }
  36. public static OVRPlugin.CameraDevice ConvertCameraDevice(OVRManager.CameraDevice cameraDevice)
  37. {
  38. if (cameraDevice == OVRManager.CameraDevice.WebCamera0)
  39. {
  40. return OVRPlugin.CameraDevice.WebCamera0;
  41. }
  42. else if (cameraDevice == OVRManager.CameraDevice.WebCamera1)
  43. {
  44. return OVRPlugin.CameraDevice.WebCamera1;
  45. }
  46. else if (cameraDevice == OVRManager.CameraDevice.ZEDCamera)
  47. {
  48. return OVRPlugin.CameraDevice.ZEDCamera;
  49. }
  50. else
  51. {
  52. return OVRPlugin.CameraDevice.None;
  53. }
  54. }
  55. public static OVRBoundary.BoundaryType ToBoundaryType(OVRManager.VirtualGreenScreenType type)
  56. {
  57. if (type == OVRManager.VirtualGreenScreenType.OuterBoundary)
  58. {
  59. return OVRBoundary.BoundaryType.OuterBoundary;
  60. }
  61. else if (type == OVRManager.VirtualGreenScreenType.PlayArea)
  62. {
  63. return OVRBoundary.BoundaryType.PlayArea;
  64. }
  65. else
  66. {
  67. Debug.LogWarning("Unmatched VirtualGreenScreenType");
  68. return OVRBoundary.BoundaryType.OuterBoundary;
  69. }
  70. }
  71. public static Vector3 GetWorldPosition(Vector3 trackingSpacePosition)
  72. {
  73. OVRPose tsPose;
  74. tsPose.position = trackingSpacePosition;
  75. tsPose.orientation = Quaternion.identity;
  76. OVRPose wsPose = OVRExtensions.ToWorldSpacePose(tsPose);
  77. Vector3 pos = wsPose.position;
  78. return pos;
  79. }
  80. public static float GetMaximumBoundaryDistance(Camera camera, OVRBoundary.BoundaryType boundaryType)
  81. {
  82. if (!OVRManager.boundary.GetConfigured())
  83. {
  84. return float.MaxValue;
  85. }
  86. Vector3[] geometry = OVRManager.boundary.GetGeometry(boundaryType);
  87. if (geometry.Length == 0)
  88. {
  89. return float.MaxValue;
  90. }
  91. float maxDistance = -float.MaxValue;
  92. foreach (Vector3 v in geometry)
  93. {
  94. Vector3 pos = GetWorldPosition(v);
  95. float distance = Vector3.Dot(camera.transform.forward, pos);
  96. if (maxDistance < distance)
  97. {
  98. maxDistance = distance;
  99. }
  100. }
  101. return maxDistance;
  102. }
  103. public static Mesh BuildBoundaryMesh(OVRBoundary.BoundaryType boundaryType, float topY, float bottomY)
  104. {
  105. if (!OVRManager.boundary.GetConfigured())
  106. {
  107. return null;
  108. }
  109. List<Vector3> geometry = new List<Vector3>(OVRManager.boundary.GetGeometry(boundaryType));
  110. if (geometry.Count == 0)
  111. {
  112. return null;
  113. }
  114. geometry.Add(geometry[0]);
  115. int numPoints = geometry.Count;
  116. Vector3[] vertices = new Vector3[numPoints * 2];
  117. Vector2[] uvs = new Vector2[numPoints * 2];
  118. for (int i = 0; i < numPoints; ++i)
  119. {
  120. Vector3 v = geometry[i];
  121. vertices[i] = new Vector3(v.x, bottomY, v.z);
  122. vertices[i + numPoints] = new Vector3(v.x, topY, v.z);
  123. uvs[i] = new Vector2((float)i / (numPoints - 1), 0.0f);
  124. uvs[i + numPoints] = new Vector2(uvs[i].x, 1.0f);
  125. }
  126. int[] triangles = new int[(numPoints - 1) * 2 * 3];
  127. for (int i = 0; i < numPoints - 1; ++i)
  128. {
  129. // the geometry is built clockwised. only the back faces should be rendered in the camera frame mask
  130. triangles[i * 6 + 0] = i;
  131. triangles[i * 6 + 1] = i + numPoints;
  132. triangles[i * 6 + 2] = i + 1 + numPoints;
  133. triangles[i * 6 + 3] = i;
  134. triangles[i * 6 + 4] = i + 1 + numPoints;
  135. triangles[i * 6 + 5] = i + 1;
  136. }
  137. Mesh mesh = new Mesh();
  138. mesh.vertices = vertices;
  139. mesh.uv = uvs;
  140. mesh.triangles = triangles;
  141. return mesh;
  142. }
  143. }
  144. #endif