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