| 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; |
| 19 | using System.Collections; |
| 20 | using System.Runtime.InteropServices; |
| 21 | |
| 22 | #if UNITY_2017_2_OR_NEWER |
| 23 | using Settings = UnityEngine.XR.XRSettings; |
| 24 | #else |
| 25 | using Settings = UnityEngine.VR.VRSettings; |
| 26 | #endif |
| 27 | |
| 28 | /// <summary> |
| 29 | /// Add OVROverlay script to an object with an optional mesh primitive |
| 30 | /// rendered as a TimeWarp overlay instead by drawing it into the eye buffer. |
| 31 | /// This will take full advantage of the display resolution and avoid double |
| 32 | /// resampling of the texture. |
| 33 | /// |
| 34 | /// We support 3 types of Overlay shapes right now |
| 35 | /// 1. Quad : This is most common overlay type , you render a quad in Timewarp space. |
| 36 | /// 2. Cylinder: [Mobile Only][Experimental], Display overlay as partial surface of a cylinder |
| 37 | /// * The cylinder's center will be your game object's center |
| 38 | /// * We encoded the cylinder's parameters in transform.scale, |
| 39 | /// **[scale.z] is the radius of the cylinder |
| 40 | /// **[scale.y] is the height of the cylinder |
| 41 | /// **[scale.x] is the length of the arc of cylinder |
| 42 | /// * Limitations |
| 43 | /// **Only the half of the cylinder can be displayed, which means the arc angle has to be smaller than 180 degree, [scale.x] / [scale.z] <= PI |
| 44 | /// **Your camera has to be inside of the inscribed sphere of the cylinder, the overlay will be faded out automatically when the camera is close to the inscribed sphere's surface. |
| 45 | /// **Translation only works correctly with vrDriver 1.04 or above |
| 46 | /// 3. Cubemap: Display overlay as a cube map |
| 47 | /// 4. OffcenterCubemap: [Mobile Only] Display overlay as a cube map with a texture coordinate offset |
| 48 | /// * The actually sampling will looks like [color = texture(cubeLayerSampler, normalize(direction) + offset)] instead of [color = texture( cubeLayerSampler, direction )] |
| 49 | /// * The extra center offset can be feed from transform.position |
| 50 | /// * Note: if transform.position's magnitude is greater than 1, which will cause some cube map pixel always invisible |
| 51 | /// Which is usually not what people wanted, we don't kill the ability for developer to do so here, but will warn out. |
| 52 | /// 5. Equirect: Display overlay as a 360-degree equirectangular skybox. |
| 53 | /// </summary> |
| 54 | public class OVROverlay : MonoBehaviour |
| 55 | { |
| 56 | #region Interface |
| 57 | |
| 58 | /// <summary> |
| 59 | /// Determines the on-screen appearance of a layer. |
| 60 | /// </summary> |
| 61 | public enum OverlayShape |
| 62 | { |
| 63 | Quad = OVRPlugin.OverlayShape.Quad, |
| 64 | Cylinder = OVRPlugin.OverlayShape.Cylinder, |
| 65 | Cubemap = OVRPlugin.OverlayShape.Cubemap, |
| 66 | OffcenterCubemap = OVRPlugin.OverlayShape.OffcenterCubemap, |
| 67 | Equirect = OVRPlugin.OverlayShape.Equirect, |
| 68 | } |
| 69 | |
| 70 | /// <summary> |
| 71 | /// Whether the layer appears behind or infront of other content in the scene. |
| 72 | /// </summary> |
| 73 | public enum OverlayType |
| 74 | { |
| 75 | None, |
| 76 | Underlay, |
| 77 | Overlay, |
| 78 | }; |
| 79 | |
| 80 | /// <summary> |
| 81 | /// Specify overlay's type |
| 82 | /// </summary> |
| 83 | [Tooltip("Specify overlay's type")] |
| 84 | public OverlayType currentOverlayType = OverlayType.Overlay; |
| 85 | |
| 86 | /// <summary> |
| 87 | /// If true, the texture's content is copied to the compositor each frame. |
| 88 | /// </summary> |
| 89 | [Tooltip("If true, the texture's content is copied to the compositor each frame.")] |
| 90 | public bool isDynamic = false; |
| 91 | |
| 92 | /// <summary> |
| 93 | /// If true, the layer would be used to present protected content (e.g. HDCP). The flag is effective only on PC. |
| 94 | /// </summary> |
| 95 | [Tooltip("If true, the layer would be used to present protected content (e.g. HDCP). The flag is effective only on PC.")] |
| 96 | public bool isProtectedContent = false; |
| 97 | |
| 98 | //Source and dest rects |
| 99 | public Rect srcRectLeft = new Rect(); |
| 100 | public Rect srcRectRight = new Rect(); |
| 101 | public Rect destRectLeft = new Rect(); |
| 102 | public Rect destRectRight = new Rect(); |
| 103 | |
| 104 | private OVRPlugin.TextureRectMatrixf textureRectMatrix = OVRPlugin.TextureRectMatrixf.zero; |
| 105 | |
| 106 | public bool overrideTextureRectMatrix = false; |
| 107 | |
| 108 | public bool overridePerLayerColorScaleAndOffset = false; |
| 109 | |
| 110 | public Vector4 colorScale = Vector4.one; |
| 111 | |
| 112 | public Vector4 colorOffset = Vector4.zero; |
| 113 | |
| 114 | //Warning: Developers should only use this supersample setting if they absolutely have the budget and need for it. It is extremely expensive, and will not be relevant for most developers. |
| 115 | public bool useExpensiveSuperSample = false; |
| 116 | |
| 117 | /// <summary> |
| 118 | /// If true, the layer will be created as an external surface. externalSurfaceObject contains the Surface object. It's effective only on Android. |
| 119 | /// </summary> |
| 120 | [Tooltip("If true, the layer will be created as an external surface. externalSurfaceObject contains the Surface object. It's effective only on Android.")] |
| 121 | public bool isExternalSurface = false; |
| 122 | |
| 123 | /// <summary> |
| 124 | /// The width which will be used to create the external surface. It's effective only on Android. |
| 125 | /// </summary> |
| 126 | [Tooltip("The width which will be used to create the external surface. It's effective only on Android.")] |
| 127 | public int externalSurfaceWidth = 0; |
| 128 | |
| 129 | /// <summary> |
| 130 | /// The height which will be used to create the external surface. It's effective only on Android. |
| 131 | /// </summary> |
| 132 | [Tooltip("The height which will be used to create the external surface. It's effective only on Android.")] |
| 133 | public int externalSurfaceHeight = 0; |
| 134 | |
| 135 | /// <summary> |
| 136 | /// The compositionDepth defines the order of the OVROverlays in composition. The overlay/underlay with smaller compositionDepth would be composited in the front of the overlay/underlay with larger compositionDepth. |
| 137 | /// </summary> |
| 138 | [Tooltip("The compositionDepth defines the order of the OVROverlays in composition. The overlay/underlay with smaller compositionDepth would be composited in the front of the overlay/underlay with larger compositionDepth.")] |
| 139 | public int compositionDepth = 0; |
| 140 | |
| 141 | /// <summary> |
| 142 | /// The noDepthBufferTesting will stop layer's depth buffer compositing even if the engine has "Depth buffer sharing" enabled on Rift. |
| 143 | /// </summary> |
| 144 | [Tooltip("The noDepthBufferTesting will stop layer's depth buffer compositing even if the engine has \"Shared Depth Buffer\" enabled")] |
| 145 | public bool noDepthBufferTesting = false; |
| 146 | |
| 147 | /// <summary> |
| 148 | /// Specify overlay's shape |
| 149 | /// </summary> |
| 150 | [Tooltip("Specify overlay's shape")] |
| 151 | public OverlayShape currentOverlayShape = OverlayShape.Quad; |
| 152 | private OverlayShape prevOverlayShape = OverlayShape.Quad; |
| 153 | |
| 154 | /// <summary> |
| 155 | /// The left- and right-eye Textures to show in the layer. |
| 156 | /// \note If you need to change the texture on a per-frame basis, please use OverrideOverlayTextureInfo(..) to avoid caching issues. |
| 157 | /// </summary> |
| 158 | [Tooltip("The left- and right-eye Textures to show in the layer.")] |
| 159 | public Texture[] textures = new Texture[] { null, null }; |
| 160 | |
| 161 | protected IntPtr[] texturePtrs = new IntPtr[] { IntPtr.Zero, IntPtr.Zero }; |
| 162 | |
| 163 | /// <summary> |
| 164 | /// The Surface object (Android only). |
| 165 | /// </summary> |
| 166 | public System.IntPtr externalSurfaceObject; |
| 167 | |
| 168 | public delegate void ExternalSurfaceObjectCreated(); |
| 169 | /// <summary> |
| 170 | /// Will be triggered after externalSurfaceTextueObject get created. |
| 171 | /// </summary> |
| 172 | public ExternalSurfaceObjectCreated externalSurfaceObjectCreated; |
| 173 | |
| 174 | /// <summary> |
| 175 | /// Use this function to set texture and texNativePtr when app is running |
| 176 | /// GetNativeTexturePtr is a slow behavior, the value should be pre-cached |
| 177 | /// </summary> |
| 178 | #if UNITY_2017_2_OR_NEWER |
| 179 | public void OverrideOverlayTextureInfo(Texture srcTexture, IntPtr nativePtr, UnityEngine.XR.XRNode node) |
| 180 | #else |
| 181 | public void OverrideOverlayTextureInfo(Texture srcTexture, IntPtr nativePtr, UnityEngine.VR.VRNode node) |
| 182 | #endif |
| 183 | { |
| 184 | #if UNITY_2017_2_OR_NEWER |
| 185 | int index = (node == UnityEngine.XR.XRNode.RightEye) ? 1 : 0; |
| 186 | #else |
| 187 | int index = (node == UnityEngine.VR.VRNode.RightEye) ? 1 : 0; |
| 188 | #endif |
| 189 | |
| 190 | if (textures.Length <= index) |
| 191 | return; |
| 192 | |
| 193 | textures[index] = srcTexture; |
| 194 | texturePtrs[index] = nativePtr; |
| 195 | |
| 196 | isOverridePending = true; |
| 197 | } |
| 198 | |
| 199 | protected bool isOverridePending; |
| 200 | |
| 201 | internal const int maxInstances = 15; |
| 202 | public static OVROverlay[] instances = new OVROverlay[maxInstances]; |
| 203 | |
| 204 | #endregion |
| 205 | |
| 206 | private static Material tex2DMaterial; |
| 207 | private static Material cubeMaterial; |
| 208 | |
| 209 | private OVRPlugin.LayerLayout layout { |
| 210 | get { |
| 211 | #if UNITY_ANDROID && !UNITY_EDITOR |
| 212 | if (textures.Length == 2 && textures[1] != null) |
| 213 | return OVRPlugin.LayerLayout.Stereo; |
| 214 | #endif |
| 215 | return OVRPlugin.LayerLayout.Mono; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | private struct LayerTexture { |
| 220 | public Texture appTexture; |
| 221 | public IntPtr appTexturePtr; |
| 222 | public Texture[] swapChain; |
| 223 | public IntPtr[] swapChainPtr; |
| 224 | }; |
| 225 | private LayerTexture[] layerTextures; |
| 226 | |
| 227 | private OVRPlugin.LayerDesc layerDesc; |
| 228 | private int stageCount = -1; |
| 229 | |
| 230 | private int layerIndex = -1; // Controls the composition order based on wake-up time. |
| 231 | |
| 232 | private int layerId = 0; // The layer's internal handle in the compositor. |
| 233 | private GCHandle layerIdHandle; |
| 234 | private IntPtr layerIdPtr = IntPtr.Zero; |
| 235 | |
| 236 | private int frameIndex = 0; |
| 237 | private int prevFrameIndex = -1; |
| 238 | |
| 239 | private Renderer rend; |
| 240 | |
| 241 | private int texturesPerStage { get { return (layout == OVRPlugin.LayerLayout.Stereo) ? 2 : 1; } } |
| 242 | |
| 243 | private bool CreateLayer(int mipLevels, int sampleCount, OVRPlugin.EyeTextureFormat etFormat, int flags, OVRPlugin.Sizei size, OVRPlugin.OverlayShape shape) |
| 244 | { |
| 245 | if (!layerIdHandle.IsAllocated || layerIdPtr == IntPtr.Zero) |
| 246 | { |
| 247 | layerIdHandle = GCHandle.Alloc(layerId, GCHandleType.Pinned); |
| 248 | layerIdPtr = layerIdHandle.AddrOfPinnedObject(); |
| 249 | } |
| 250 | |
| 251 | if (layerIndex == -1) |
| 252 | { |
| 253 | for (int i = 0; i < maxInstances; ++i) |
| 254 | { |
| 255 | if (instances[i] == null || instances[i] == this) |
| 256 | { |
| 257 | layerIndex = i; |
| 258 | instances[i] = this; |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | bool needsSetup = ( |
| 265 | isOverridePending || |
| 266 | layerDesc.MipLevels != mipLevels || |
| 267 | layerDesc.SampleCount != sampleCount || |
| 268 | layerDesc.Format != etFormat || |
| 269 | layerDesc.Layout != layout || |
| 270 | layerDesc.LayerFlags != flags || |
| 271 | !layerDesc.TextureSize.Equals(size) || |
| 272 | layerDesc.Shape != shape); |
| 273 | |
| 274 | if (!needsSetup) |
| 275 | return false; |
| 276 | |
| 277 | OVRPlugin.LayerDesc desc = OVRPlugin.CalculateLayerDesc(shape, layout, size, mipLevels, sampleCount, etFormat, flags); |
| 278 | OVRPlugin.EnqueueSetupLayer(desc, compositionDepth, layerIdPtr); |
| 279 | layerId = (int)layerIdHandle.Target; |
| 280 | |
| 281 | if (layerId > 0) |
| 282 | { |
| 283 | layerDesc = desc; |
| 284 | if (isExternalSurface) |
| 285 | { |
| 286 | stageCount = 1; |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | stageCount = OVRPlugin.GetLayerTextureStageCount(layerId); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | isOverridePending = false; |
| 295 | |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | private bool CreateLayerTextures(bool useMipmaps, OVRPlugin.Sizei size, bool isHdr) |
| 300 | { |
| 301 | if (isExternalSurface) |
| 302 | { |
| 303 | if (externalSurfaceObject == System.IntPtr.Zero) |
| 304 | { |
| 305 | externalSurfaceObject = OVRPlugin.GetLayerAndroidSurfaceObject(layerId); |
| 306 | if (externalSurfaceObject != System.IntPtr.Zero) |
| 307 | { |
| 308 | Debug.LogFormat("GetLayerAndroidSurfaceObject returns {0}", externalSurfaceObject); |
| 309 | if (externalSurfaceObjectCreated != null) |
| 310 | { |
| 311 | externalSurfaceObjectCreated(); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | bool needsCopy = false; |
| 319 | |
| 320 | if (stageCount <= 0) |
| 321 | return false; |
| 322 | |
| 323 | // For newer SDKs, blit directly to the surface that will be used in compositing. |
| 324 | |
| 325 | if (layerTextures == null) |
| 326 | layerTextures = new LayerTexture[texturesPerStage]; |
| 327 | |
| 328 | for (int eyeId = 0; eyeId < texturesPerStage; ++eyeId) |
| 329 | { |
| 330 | if (layerTextures[eyeId].swapChain == null) |
| 331 | layerTextures[eyeId].swapChain = new Texture[stageCount]; |
| 332 | |
| 333 | if (layerTextures[eyeId].swapChainPtr == null) |
| 334 | layerTextures[eyeId].swapChainPtr = new IntPtr[stageCount]; |
| 335 | |
| 336 | for (int stage = 0; stage < stageCount; ++stage) |
| 337 | { |
| 338 | Texture sc = layerTextures[eyeId].swapChain[stage]; |
| 339 | IntPtr scPtr = layerTextures[eyeId].swapChainPtr[stage]; |
| 340 | |
| 341 | if (sc != null && scPtr != IntPtr.Zero && size.w == sc.width && size.h == sc.height) |
| 342 | continue; |
| 343 | |
| 344 | if (scPtr == IntPtr.Zero) |
| 345 | scPtr = OVRPlugin.GetLayerTexture(layerId, stage, (OVRPlugin.Eye)eyeId); |
| 346 | |
| 347 | if (scPtr == IntPtr.Zero) |
| 348 | continue; |
| 349 | |
| 350 | var txFormat = (isHdr) ? TextureFormat.RGBAHalf : TextureFormat.RGBA32; |
| 351 | |
| 352 | if (currentOverlayShape != OverlayShape.Cubemap && currentOverlayShape != OverlayShape.OffcenterCubemap) |
| 353 | sc = Texture2D.CreateExternalTexture(size.w, size.h, txFormat, useMipmaps, true, scPtr); |
| 354 | #if UNITY_2017_1_OR_NEWER |
| 355 | else |
| 356 | sc = Cubemap.CreateExternalTexture(size.w, txFormat, useMipmaps, scPtr); |
| 357 | #endif |
| 358 | |
| 359 | layerTextures[eyeId].swapChain[stage] = sc; |
| 360 | layerTextures[eyeId].swapChainPtr[stage] = scPtr; |
| 361 | |
| 362 | needsCopy = true; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return needsCopy; |
| 367 | } |
| 368 | |
| 369 | private void DestroyLayerTextures() |
| 370 | { |
| 371 | if (isExternalSurface) |
| 372 | { |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | for (int eyeId = 0; layerTextures != null && eyeId < texturesPerStage; ++eyeId) |
| 377 | { |
| 378 | if (layerTextures[eyeId].swapChain != null) |
| 379 | { |
| 380 | for (int stage = 0; stage < stageCount; ++stage) |
| 381 | DestroyImmediate(layerTextures[eyeId].swapChain[stage]); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | layerTextures = null; |
| 386 | } |
| 387 | |
| 388 | private void DestroyLayer() |
| 389 | { |
| 390 | if (layerIndex != -1) |
| 391 | { |
| 392 | // Turn off the overlay if it was on. |
| 393 | OVRPlugin.EnqueueSubmitLayer(true, false, false, IntPtr.Zero, IntPtr.Zero, -1, 0, OVRPose.identity.ToPosef(), Vector3.one.ToVector3f(), layerIndex, (OVRPlugin.OverlayShape)prevOverlayShape); |
| 394 | instances[layerIndex] = null; |
| 395 | layerIndex = -1; |
| 396 | } |
| 397 | |
| 398 | if (layerIdPtr != IntPtr.Zero) |
| 399 | { |
| 400 | OVRPlugin.EnqueueDestroyLayer(layerIdPtr); |
| 401 | layerIdPtr = IntPtr.Zero; |
| 402 | layerIdHandle.Free(); |
| 403 | layerId = 0; |
| 404 | } |
| 405 | |
| 406 | layerDesc = new OVRPlugin.LayerDesc(); |
| 407 | |
| 408 | frameIndex = 0; |
| 409 | prevFrameIndex = -1; |
| 410 | } |
| 411 | |
| 412 | /// <summary> |
| 413 | /// Sets the source and dest rects for both eyes. Source explains what portion of the source texture is used, and |
| 414 | /// dest is what portion of the destination texture is rendered into. |
| 415 | /// </summary> |
| 416 | public void SetSrcDestRects(Rect srcLeft, Rect srcRight, Rect destLeft, Rect destRight) |
| 417 | { |
| 418 | srcRectLeft = srcLeft; |
| 419 | srcRectRight = srcRight; |
| 420 | destRectLeft = destLeft; |
| 421 | destRectRight = destRight; |
| 422 | } |
| 423 | |
| 424 | public void UpdateTextureRectMatrix() |
| 425 | { |
| 426 | Rect srcRectLeftConverted = new Rect(srcRectLeft.x, 1 - srcRectLeft.y - srcRectLeft.height, srcRectLeft.width, srcRectLeft.height); |
| 427 | Rect srcRectRightConverted = new Rect(srcRectRight.x, 1 - srcRectRight.y - srcRectRight.height, srcRectRight.width, srcRectRight.height); |
| 428 | textureRectMatrix.leftRect = srcRectLeftConverted; |
| 429 | textureRectMatrix.rightRect = srcRectRightConverted; |
| 430 | float leftWidthFactor = srcRectLeftConverted.width / destRectLeft.width; |
| 431 | float leftHeightFactor = srcRectLeftConverted.height / destRectLeft.height; |
| 432 | textureRectMatrix.leftScaleBias = new Vector4(leftWidthFactor, leftHeightFactor, srcRectLeftConverted.x - destRectLeft.x * leftWidthFactor, srcRectLeftConverted.y - destRectLeft.y * leftHeightFactor); |
| 433 | float rightWidthFactor = srcRectRightConverted.width / destRectRight.width; |
| 434 | float rightHeightFactor = srcRectRightConverted.height / destRectRight.height; |
| 435 | textureRectMatrix.rightScaleBias = new Vector4(rightWidthFactor, rightHeightFactor, srcRectRightConverted.x - destRectRight.x * rightWidthFactor, srcRectRightConverted.y - destRectRight.y * rightHeightFactor); |
| 436 | } |
| 437 | |
| 438 | public void SetPerLayerColorScaleAndOffset(Vector4 scale, Vector4 offset) |
| 439 | { |
| 440 | colorScale = scale; |
| 441 | colorOffset = offset; |
| 442 | } |
| 443 | |
| 444 | private bool LatchLayerTextures() |
| 445 | { |
| 446 | if (isExternalSurface) |
| 447 | { |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | for (int i = 0; i < texturesPerStage; ++i) |
| 452 | { |
| 453 | if (textures[i] != layerTextures[i].appTexture || layerTextures[i].appTexturePtr == IntPtr.Zero) |
| 454 | { |
| 455 | if (textures[i] != null) |
| 456 | { |
| 457 | #if UNITY_EDITOR |
| 458 | var assetPath = UnityEditor.AssetDatabase.GetAssetPath(textures[i]); |
| 459 | var importer = (UnityEditor.TextureImporter)UnityEditor.TextureImporter.GetAtPath(assetPath); |
| 460 | if (importer && importer.textureType != UnityEditor.TextureImporterType.Default) |
| 461 | { |
| 462 | Debug.LogError("Need Default Texture Type for overlay"); |
| 463 | return false; |
| 464 | } |
| 465 | #endif |
| 466 | var rt = textures[i] as RenderTexture; |
| 467 | if (rt && !rt.IsCreated()) |
| 468 | rt.Create(); |
| 469 | |
| 470 | layerTextures[i].appTexturePtr = (texturePtrs[i] != IntPtr.Zero) ? texturePtrs[i] : textures[i].GetNativeTexturePtr(); |
| 471 | |
| 472 | if (layerTextures[i].appTexturePtr != IntPtr.Zero) |
| 473 | layerTextures[i].appTexture = textures[i]; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (currentOverlayShape == OverlayShape.Cubemap) |
| 478 | { |
| 479 | if (textures[i] as Cubemap == null) |
| 480 | { |
| 481 | Debug.LogError("Need Cubemap texture for cube map overlay"); |
| 482 | return false; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | #if !UNITY_ANDROID || UNITY_EDITOR |
| 488 | if (currentOverlayShape == OverlayShape.OffcenterCubemap) |
| 489 | { |
| 490 | Debug.LogWarning("Overlay shape " + currentOverlayShape + " is not supported on current platform"); |
| 491 | return false; |
| 492 | } |
| 493 | #endif |
| 494 | |
| 495 | if (layerTextures[0].appTexture == null || layerTextures[0].appTexturePtr == IntPtr.Zero) |
| 496 | return false; |
| 497 | |
| 498 | return true; |
| 499 | } |
| 500 | |
| 501 | private OVRPlugin.LayerDesc GetCurrentLayerDesc() |
| 502 | { |
| 503 | OVRPlugin.Sizei textureSize = new OVRPlugin.Sizei() { w = 0, h = 0 }; |
| 504 | |
| 505 | if (isExternalSurface) |
| 506 | { |
| 507 | textureSize.w = externalSurfaceWidth; |
| 508 | textureSize.h = externalSurfaceHeight; |
| 509 | } |
| 510 | else |
| 511 | { |
| 512 | if (textures[0] == null) |
| 513 | { |
| 514 | Debug.LogWarning("textures[0] hasn't been set"); |
| 515 | } |
| 516 | textureSize.w = textures[0] ? textures[0].width : 0; |
| 517 | textureSize.h = textures[0] ? textures[0].height : 0; |
| 518 | } |
| 519 | |
| 520 | OVRPlugin.LayerDesc newDesc = new OVRPlugin.LayerDesc() { |
| 521 | Format = OVRPlugin.EyeTextureFormat.R8G8B8A8_sRGB, |
| 522 | LayerFlags = isExternalSurface ? 0 : (int)OVRPlugin.LayerFlags.TextureOriginAtBottomLeft, |
| 523 | Layout = layout, |
| 524 | MipLevels = 1, |
| 525 | SampleCount = 1, |
| 526 | Shape = (OVRPlugin.OverlayShape)currentOverlayShape, |
| 527 | TextureSize = textureSize |
| 528 | }; |
| 529 | |
| 530 | var tex2D = textures[0] as Texture2D; |
| 531 | if (tex2D != null) |
| 532 | { |
| 533 | if (tex2D.format == TextureFormat.RGBAHalf || tex2D.format == TextureFormat.RGBAFloat) |
| 534 | newDesc.Format = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; |
| 535 | |
| 536 | newDesc.MipLevels = tex2D.mipmapCount; |
| 537 | } |
| 538 | |
| 539 | var texCube = textures[0] as Cubemap; |
| 540 | if (texCube != null) |
| 541 | { |
| 542 | if (texCube.format == TextureFormat.RGBAHalf || texCube.format == TextureFormat.RGBAFloat) |
| 543 | newDesc.Format = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; |
| 544 | |
| 545 | newDesc.MipLevels = texCube.mipmapCount; |
| 546 | } |
| 547 | |
| 548 | var rt = textures[0] as RenderTexture; |
| 549 | if (rt != null) |
| 550 | { |
| 551 | newDesc.SampleCount = rt.antiAliasing; |
| 552 | |
| 553 | if (rt.format == RenderTextureFormat.ARGBHalf || rt.format == RenderTextureFormat.ARGBFloat || rt.format == RenderTextureFormat.RGB111110Float) |
| 554 | newDesc.Format = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; |
| 555 | } |
| 556 | |
| 557 | if (isProtectedContent) |
| 558 | { |
| 559 | newDesc.LayerFlags |= (int)OVRPlugin.LayerFlags.ProtectedContent; |
| 560 | } |
| 561 | |
| 562 | if (isExternalSurface) |
| 563 | { |
| 564 | newDesc.LayerFlags |= (int)OVRPlugin.LayerFlags.AndroidSurfaceSwapChain; |
| 565 | } |
| 566 | |
| 567 | return newDesc; |
| 568 | } |
| 569 | |
| 570 | private bool PopulateLayer(int mipLevels, bool isHdr, OVRPlugin.Sizei size, int sampleCount, int stage) |
| 571 | { |
| 572 | if (isExternalSurface) |
| 573 | { |
| 574 | return true; |
| 575 | } |
| 576 | |
| 577 | bool ret = false; |
| 578 | |
| 579 | RenderTextureFormat rtFormat = (isHdr) ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32; |
| 580 | |
| 581 | for (int eyeId = 0; eyeId < texturesPerStage; ++eyeId) |
| 582 | { |
| 583 | Texture et = layerTextures[eyeId].swapChain[stage]; |
| 584 | if (et == null) |
| 585 | continue; |
| 586 | |
| 587 | for (int mip = 0; mip < mipLevels; ++mip) |
| 588 | { |
| 589 | int width = size.w >> mip; |
| 590 | if (width < 1) width = 1; |
| 591 | int height = size.h >> mip; |
| 592 | if (height < 1) height = 1; |
| 593 | #if UNITY_2017_1_1 || UNITY_2017_2_OR_NEWER |
| 594 | RenderTextureDescriptor descriptor = new RenderTextureDescriptor(width, height, rtFormat, 0); |
| 595 | descriptor.msaaSamples = sampleCount; |
| 596 | descriptor.useMipMap = true; |
| 597 | descriptor.autoGenerateMips = false; |
| 598 | descriptor.sRGB = false; |
| 599 | |
| 600 | var tempRTDst = RenderTexture.GetTemporary(descriptor); |
| 601 | #else |
| 602 | var tempRTDst = RenderTexture.GetTemporary(width, height, 0, rtFormat, RenderTextureReadWrite.Linear, sampleCount); |
| 603 | #endif |
| 604 | |
| 605 | if (!tempRTDst.IsCreated()) |
| 606 | tempRTDst.Create(); |
| 607 | |
| 608 | tempRTDst.DiscardContents(); |
| 609 | |
| 610 | bool dataIsLinear = isHdr || (QualitySettings.activeColorSpace == ColorSpace.Linear); |
| 611 | |
| 612 | #if !UNITY_2017_1_OR_NEWER |
| 613 | var rt = textures[eyeId] as RenderTexture; |
| 614 | dataIsLinear |= rt != null && rt.sRGB; //HACK: Unity 5.6 and earlier convert to linear on read from sRGB RenderTexture. |
| 615 | #endif |
| 616 | #if UNITY_ANDROID && !UNITY_EDITOR |
| 617 | dataIsLinear = true; //HACK: Graphics.CopyTexture causes linear->srgb conversion on target write with D3D but not GLES. |
| 618 | #endif |
| 619 | |
| 620 | if (currentOverlayShape != OverlayShape.Cubemap && currentOverlayShape != OverlayShape.OffcenterCubemap) |
| 621 | { |
| 622 | tex2DMaterial.SetInt("_linearToSrgb", (!isHdr && dataIsLinear) ? 1 : 0); |
| 623 | |
| 624 | //Resolve, decompress, swizzle, etc not handled by simple CopyTexture. |
| 625 | #if !UNITY_ANDROID || UNITY_EDITOR |
| 626 | // The PC compositor uses premultiplied alpha, so multiply it here. |
| 627 | tex2DMaterial.SetInt("_premultiply", 1); |
| 628 | #endif |
| 629 | Graphics.Blit(textures[eyeId], tempRTDst, tex2DMaterial); |
| 630 | Graphics.CopyTexture(tempRTDst, 0, 0, et, 0, mip); |
| 631 | } |
| 632 | #if UNITY_2017_1_OR_NEWER |
| 633 | else // Cubemap |
| 634 | { |
| 635 | for (int face = 0; face < 6; ++face) |
| 636 | { |
| 637 | cubeMaterial.SetInt("_linearToSrgb", (!isHdr && dataIsLinear) ? 1 : 0); |
| 638 | |
| 639 | #if !UNITY_ANDROID || UNITY_EDITOR |
| 640 | // The PC compositor uses premultiplied alpha, so multiply it here. |
| 641 | cubeMaterial.SetInt("_premultiply", 1); |
| 642 | #endif |
| 643 | cubeMaterial.SetInt("_face", face); |
| 644 | //Resolve, decompress, swizzle, etc not handled by simple CopyTexture. |
| 645 | Graphics.Blit(textures[eyeId], tempRTDst, cubeMaterial); |
| 646 | Graphics.CopyTexture(tempRTDst, 0, 0, et, face, mip); |
| 647 | } |
| 648 | } |
| 649 | #endif |
| 650 | RenderTexture.ReleaseTemporary(tempRTDst); |
| 651 | |
| 652 | ret = true; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | private bool SubmitLayer(bool overlay, bool headLocked, bool noDepthBufferTesting, OVRPose pose, Vector3 scale, int frameIndex) |
| 660 | { |
| 661 | int rightEyeIndex = (texturesPerStage >= 2) ? 1 : 0; |
| 662 | if (overrideTextureRectMatrix) |
| 663 | { |
| 664 | UpdateTextureRectMatrix(); |
| 665 | } |
| 666 | bool isOverlayVisible = OVRPlugin.EnqueueSubmitLayer(overlay, headLocked, noDepthBufferTesting, |
| 667 | isExternalSurface ? System.IntPtr.Zero : layerTextures[0].appTexturePtr, |
| 668 | isExternalSurface ? System.IntPtr.Zero : layerTextures[rightEyeIndex].appTexturePtr, |
| 669 | layerId, frameIndex, pose.flipZ().ToPosef(), scale.ToVector3f(), layerIndex, (OVRPlugin.OverlayShape)currentOverlayShape, |
| 670 | overrideTextureRectMatrix, textureRectMatrix, overridePerLayerColorScaleAndOffset, colorScale, colorOffset, useExpensiveSuperSample); |
| 671 | |
| 672 | prevOverlayShape = currentOverlayShape; |
| 673 | |
| 674 | return isOverlayVisible; |
| 675 | } |
| 676 | |
| 677 | #region Unity Messages |
| 678 | |
| 679 | void Awake() |
| 680 | { |
| 681 | Debug.Log("Overlay Awake"); |
| 682 | |
| 683 | if (tex2DMaterial == null) |
| 684 | tex2DMaterial = new Material(Shader.Find("Oculus/Texture2D Blit")); |
| 685 | |
| 686 | if (cubeMaterial == null) |
| 687 | cubeMaterial = new Material(Shader.Find("Oculus/Cubemap Blit")); |
| 688 | |
| 689 | rend = GetComponent<Renderer>(); |
| 690 | |
| 691 | if (textures.Length == 0) |
| 692 | textures = new Texture[] { null }; |
| 693 | |
| 694 | // Backward compatibility |
| 695 | if (rend != null && textures[0] == null) |
| 696 | textures[0] = rend.material.mainTexture; |
| 697 | } |
| 698 | |
| 699 | static public string OpenVROverlayKey { get { return "unity:" + Application.companyName + "." + Application.productName; } } |
| 700 | private ulong OpenVROverlayHandle = OVR.OpenVR.OpenVR.k_ulOverlayHandleInvalid; |
| 701 | |
| 702 | void OnEnable() |
| 703 | { |
| 704 | if (OVRManager.OVRManagerinitialized) |
| 705 | InitOVROverlay(); |
| 706 | } |
| 707 | |
| 708 | void InitOVROverlay() |
| 709 | { |
| 710 | if (!OVRManager.isHmdPresent) |
| 711 | { |
| 712 | enabled = false; |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | constructedOverlayXRDevice = OVRManager.XRDevice.Unknown; |
| 717 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) |
| 718 | { |
| 719 | OVR.OpenVR.CVROverlay overlay = OVR.OpenVR.OpenVR.Overlay; |
| 720 | if (overlay != null) |
| 721 | { |
| 722 | OVR.OpenVR.EVROverlayError error = overlay.CreateOverlay(OpenVROverlayKey + transform.name, gameObject.name, ref OpenVROverlayHandle); |
| 723 | if (error != OVR.OpenVR.EVROverlayError.None) |
| 724 | { |
| 725 | enabled = false; |
| 726 | return; |
| 727 | } |
| 728 | } |
| 729 | else |
| 730 | { |
| 731 | enabled = false; |
| 732 | return; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | constructedOverlayXRDevice = OVRManager.loadedXRDevice; |
| 737 | xrDeviceConstructed = true; |
| 738 | } |
| 739 | |
| 740 | void OnDisable() |
| 741 | { |
| 742 | if ((gameObject.hideFlags & HideFlags.DontSaveInBuild) != 0) |
| 743 | return; |
| 744 | |
| 745 | if (!OVRManager.OVRManagerinitialized) |
| 746 | return; |
| 747 | |
| 748 | if (OVRManager.loadedXRDevice != constructedOverlayXRDevice) |
| 749 | return; |
| 750 | |
| 751 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 752 | { |
| 753 | DestroyLayerTextures(); |
| 754 | DestroyLayer(); |
| 755 | } |
| 756 | else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) |
| 757 | { |
| 758 | if (OpenVROverlayHandle != OVR.OpenVR.OpenVR.k_ulOverlayHandleInvalid) |
| 759 | { |
| 760 | OVR.OpenVR.CVROverlay overlay = OVR.OpenVR.OpenVR.Overlay; |
| 761 | if (overlay != null) |
| 762 | { |
| 763 | overlay.DestroyOverlay(OpenVROverlayHandle); |
| 764 | } |
| 765 | OpenVROverlayHandle = OVR.OpenVR.OpenVR.k_ulOverlayHandleInvalid; |
| 766 | } |
| 767 | } |
| 768 | constructedOverlayXRDevice = OVRManager.XRDevice.Unknown; |
| 769 | xrDeviceConstructed = false; |
| 770 | } |
| 771 | |
| 772 | void OnDestroy() |
| 773 | { |
| 774 | DestroyLayerTextures(); |
| 775 | DestroyLayer(); |
| 776 | } |
| 777 | |
| 778 | bool ComputeSubmit(ref OVRPose pose, ref Vector3 scale, ref bool overlay, ref bool headLocked) |
| 779 | { |
| 780 | Camera headCamera = Camera.main; |
| 781 | |
| 782 | overlay = (currentOverlayType == OverlayType.Overlay); |
| 783 | headLocked = false; |
| 784 | for (var t = transform; t != null && !headLocked; t = t.parent) |
| 785 | headLocked |= (t == headCamera.transform); |
| 786 | |
| 787 | pose = (headLocked) ? transform.ToHeadSpacePose(headCamera) : transform.ToTrackingSpacePose(headCamera); |
| 788 | scale = transform.lossyScale; |
| 789 | for (int i = 0; i < 3; ++i) |
| 790 | scale[i] /= headCamera.transform.lossyScale[i]; |
| 791 | |
| 792 | if (currentOverlayShape == OverlayShape.Cubemap) |
| 793 | { |
| 794 | #if UNITY_ANDROID && !UNITY_EDITOR |
| 795 | //HACK: VRAPI cubemaps assume are yawed 180 degrees relative to LibOVR. |
| 796 | pose.orientation = pose.orientation * Quaternion.AngleAxis(180, Vector3.up); |
| 797 | #endif |
| 798 | pose.position = headCamera.transform.position; |
| 799 | } |
| 800 | |
| 801 | // Pack the offsetCenter directly into pose.position for offcenterCubemap |
| 802 | if (currentOverlayShape == OverlayShape.OffcenterCubemap) |
| 803 | { |
| 804 | pose.position = transform.position; |
| 805 | if (pose.position.magnitude > 1.0f) |
| 806 | { |
| 807 | Debug.LogWarning("Your cube map center offset's magnitude is greater than 1, which will cause some cube map pixel always invisible ."); |
| 808 | return false; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | // Cylinder overlay sanity checking |
| 813 | if (currentOverlayShape == OverlayShape.Cylinder) |
| 814 | { |
| 815 | float arcAngle = scale.x / scale.z / (float)Math.PI * 180.0f; |
| 816 | if (arcAngle > 180.0f) |
| 817 | { |
| 818 | Debug.LogWarning("Cylinder overlay's arc angle has to be below 180 degree, current arc angle is " + arcAngle + " degree." ); |
| 819 | return false; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | return true; |
| 824 | } |
| 825 | |
| 826 | void OpenVROverlayUpdate(Vector3 scale, OVRPose pose) |
| 827 | { |
| 828 | OVR.OpenVR.CVROverlay overlayRef = OVR.OpenVR.OpenVR.Overlay; |
| 829 | if (overlayRef == null) |
| 830 | return; |
| 831 | |
| 832 | Texture overlayTex = textures[0]; |
| 833 | |
| 834 | if (overlayTex != null) |
| 835 | { |
| 836 | OVR.OpenVR.EVROverlayError error = overlayRef.ShowOverlay(OpenVROverlayHandle); |
| 837 | if (error == OVR.OpenVR.EVROverlayError.InvalidHandle || error == OVR.OpenVR.EVROverlayError.UnknownOverlay) |
| 838 | { |
| 839 | if (overlayRef.FindOverlay(OpenVROverlayKey + transform.name, ref OpenVROverlayHandle) != OVR.OpenVR.EVROverlayError.None) |
| 840 | return; |
| 841 | } |
| 842 | |
| 843 | OVR.OpenVR.Texture_t tex = new OVR.OpenVR.Texture_t(); |
| 844 | tex.handle = overlayTex.GetNativeTexturePtr(); |
| 845 | tex.eType = SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL") ? OVR.OpenVR.ETextureType.OpenGL : OVR.OpenVR.ETextureType.DirectX; |
| 846 | tex.eColorSpace = OVR.OpenVR.EColorSpace.Auto; |
| 847 | overlayRef.SetOverlayTexture(OpenVROverlayHandle, ref tex); |
| 848 | |
| 849 | OVR.OpenVR.VRTextureBounds_t textureBounds = new OVR.OpenVR.VRTextureBounds_t(); |
| 850 | textureBounds.uMin = (0 + OpenVRUVOffsetAndScale.x) * OpenVRUVOffsetAndScale.z; |
| 851 | textureBounds.vMin = (1 + OpenVRUVOffsetAndScale.y) * OpenVRUVOffsetAndScale.w; |
| 852 | textureBounds.uMax = (1 + OpenVRUVOffsetAndScale.x) * OpenVRUVOffsetAndScale.z; |
| 853 | textureBounds.vMax = (0 + OpenVRUVOffsetAndScale.y) * OpenVRUVOffsetAndScale.w; |
| 854 | |
| 855 | overlayRef.SetOverlayTextureBounds(OpenVROverlayHandle, ref textureBounds); |
| 856 | |
| 857 | OVR.OpenVR.HmdVector2_t vecMouseScale = new OVR.OpenVR.HmdVector2_t(); |
| 858 | vecMouseScale.v0 = OpenVRMouseScale.x; |
| 859 | vecMouseScale.v1 = OpenVRMouseScale.y; |
| 860 | overlayRef.SetOverlayMouseScale(OpenVROverlayHandle, ref vecMouseScale); |
| 861 | |
| 862 | overlayRef.SetOverlayWidthInMeters(OpenVROverlayHandle, scale.x); |
| 863 | |
| 864 | Matrix4x4 mat44 = Matrix4x4.TRS(pose.position, pose.orientation, Vector3.one); |
| 865 | |
| 866 | OVR.OpenVR.HmdMatrix34_t pose34 = mat44.ConvertToHMDMatrix34(); |
| 867 | |
| 868 | overlayRef.SetOverlayTransformAbsolute(OpenVROverlayHandle, OVR.OpenVR.ETrackingUniverseOrigin.TrackingUniverseStanding, ref pose34); |
| 869 | |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | private Vector4 OpenVRUVOffsetAndScale = new Vector4(0, 0, 1.0f, 1.0f); |
| 874 | private Vector2 OpenVRMouseScale = new Vector2(1, 1); |
| 875 | private OVRManager.XRDevice constructedOverlayXRDevice; |
| 876 | private bool xrDeviceConstructed = false; |
| 877 | |
| 878 | void LateUpdate() |
| 879 | { |
| 880 | if (!OVRManager.OVRManagerinitialized) |
| 881 | return; |
| 882 | if (!xrDeviceConstructed) |
| 883 | { |
| 884 | InitOVROverlay(); |
| 885 | } |
| 886 | |
| 887 | if (OVRManager.loadedXRDevice != constructedOverlayXRDevice) |
| 888 | { |
| 889 | Debug.LogError("Warning-XR Device was switched during runtime with overlays still enabled. When doing so, all overlays constructed with the previous XR device must first be disabled."); |
| 890 | return; |
| 891 | } |
| 892 | // The overlay must be specified every eye frame, because it is positioned relative to the |
| 893 | // current head location. If frames are dropped, it will be time warped appropriately, |
| 894 | // just like the eye buffers. |
| 895 | if (currentOverlayType == OverlayType.None || ((textures.Length < texturesPerStage || textures[0] == null) && !isExternalSurface)) |
| 896 | return; |
| 897 | |
| 898 | OVRPose pose = OVRPose.identity; |
| 899 | Vector3 scale = Vector3.one; |
| 900 | bool overlay = false; |
| 901 | bool headLocked = false; |
| 902 | if (!ComputeSubmit(ref pose, ref scale, ref overlay, ref headLocked)) |
| 903 | return; |
| 904 | |
| 905 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) |
| 906 | { |
| 907 | if (currentOverlayShape == OverlayShape.Quad) |
| 908 | OpenVROverlayUpdate(scale, pose); |
| 909 | //No more Overlay processing is required if we're on OpenVR |
| 910 | return; |
| 911 | } |
| 912 | |
| 913 | OVRPlugin.LayerDesc newDesc = GetCurrentLayerDesc(); |
| 914 | bool isHdr = (newDesc.Format == OVRPlugin.EyeTextureFormat.R16G16B16A16_FP); |
| 915 | |
| 916 | // If the layer and textures are created but sizes differ, force re-creating them |
| 917 | if (!layerDesc.TextureSize.Equals(newDesc.TextureSize) && layerId > 0) |
| 918 | { |
| 919 | DestroyLayerTextures(); |
| 920 | DestroyLayer(); |
| 921 | } |
| 922 | |
| 923 | bool createdLayer = CreateLayer(newDesc.MipLevels, newDesc.SampleCount, newDesc.Format, newDesc.LayerFlags, newDesc.TextureSize, newDesc.Shape); |
| 924 | |
| 925 | if (layerIndex == -1 || layerId <= 0) |
| 926 | return; |
| 927 | |
| 928 | bool useMipmaps = (newDesc.MipLevels > 1); |
| 929 | |
| 930 | createdLayer |= CreateLayerTextures(useMipmaps, newDesc.TextureSize, isHdr); |
| 931 | |
| 932 | if (!isExternalSurface && (layerTextures[0].appTexture as RenderTexture != null)) |
| 933 | isDynamic = true; |
| 934 | |
| 935 | if (!LatchLayerTextures()) |
| 936 | return; |
| 937 | |
| 938 | // Don't populate the same frame image twice. |
| 939 | if (frameIndex > prevFrameIndex) |
| 940 | { |
| 941 | int stage = frameIndex % stageCount; |
| 942 | if (!PopulateLayer (newDesc.MipLevels, isHdr, newDesc.TextureSize, newDesc.SampleCount, stage)) |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | bool isOverlayVisible = SubmitLayer(overlay, headLocked, noDepthBufferTesting, pose, scale, frameIndex); |
| 947 | |
| 948 | prevFrameIndex = frameIndex; |
| 949 | if (isDynamic) |
| 950 | ++frameIndex; |
| 951 | |
| 952 | // Backward compatibility: show regular renderer if overlay isn't visible. |
| 953 | if (rend) |
| 954 | rend.enabled = !isOverlayVisible; |
| 955 | } |
| 956 | |
| 957 | #endregion |
| 958 | } |