private bool CreateLayer(int mipLevels, int sampleCount, OVRPlugin.EyeTextureFormat etFormat, int flags, OVRPlugin.Sizei size, OVRPlugin.OverlayShape shape) { if (!this.layerIdHandle.IsAllocated || this.layerIdPtr == IntPtr.Zero) { this.layerIdHandle = GCHandle.Alloc(this.layerId, GCHandleType.Pinned); this.layerIdPtr = this.layerIdHandle.AddrOfPinnedObject(); } if (this.layerIndex == -1) { for (int i = 0; i < 15; i++) { if (OVROverlay.instances[i] == null || OVROverlay.instances[i] == this) { this.layerIndex = i; OVROverlay.instances[i] = this; break; } } } if (!this.isOverridePending && this.layerDesc.MipLevels == mipLevels && this.layerDesc.SampleCount == sampleCount && this.layerDesc.Format == etFormat && this.layerDesc.LayerFlags == flags && this.layerDesc.TextureSize.Equals(size) && this.layerDesc.Shape == shape) { return(false); } OVRPlugin.LayerDesc desc = OVRPlugin.CalculateLayerDesc(shape, this.layout, size, mipLevels, sampleCount, etFormat, flags); OVRPlugin.EnqueueSetupLayer(desc, this.layerIdPtr); this.layerId = (int)this.layerIdHandle.Target; if (this.layerId > 0) { this.layerDesc = desc; this.stageCount = OVRPlugin.GetLayerTextureStageCount(this.layerId); } this.isOverridePending = false; return(true); }
private OVRPlugin.LayerDesc GetCurrentLayerDesc() { OVRPlugin.LayerDesc newDesc = new OVRPlugin.LayerDesc() { Format = OVRPlugin.EyeTextureFormat.R8G8B8A8_sRGB, LayerFlags = (int)OVRPlugin.LayerFlags.TextureOriginAtBottomLeft, Layout = layout, MipLevels = 1, SampleCount = 1, Shape = (OVRPlugin.OverlayShape)currentOverlayShape, TextureSize = new OVRPlugin.Sizei() { w = textures[0].width, h = textures[0].height } }; var tex2D = textures[0] as Texture2D; if (tex2D != null) { if (tex2D.format == TextureFormat.RGBAHalf || tex2D.format == TextureFormat.RGBAFloat) { newDesc.Format = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; } newDesc.MipLevels = tex2D.mipmapCount; } var texCube = textures[0] as Cubemap; if (texCube != null) { if (texCube.format == TextureFormat.RGBAHalf || texCube.format == TextureFormat.RGBAFloat) { newDesc.Format = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; } newDesc.MipLevels = texCube.mipmapCount; } var rt = textures[0] as RenderTexture; if (rt != null) { newDesc.SampleCount = rt.antiAliasing; if (rt.format == RenderTextureFormat.ARGBHalf || rt.format == RenderTextureFormat.ARGBFloat || rt.format == RenderTextureFormat.RGB111110Float) { newDesc.Format = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; } } if (isProtectedContent) { newDesc.LayerFlags |= (int)OVRPlugin.LayerFlags.ProtectedContent; } return(newDesc); }
private bool CreateLayer(int mipLevels, int sampleCount, OVRPlugin.EyeTextureFormat etFormat, int flags, OVRPlugin.Sizei size, OVRPlugin.OverlayShape shape) { if (!layerIdHandle.IsAllocated || layerIdPtr == IntPtr.Zero) { layerIdHandle = GCHandle.Alloc(layerId, GCHandleType.Pinned); layerIdPtr = layerIdHandle.AddrOfPinnedObject(); } if (layerIndex == -1) { for (int i = 0; i < maxInstances; ++i) { if (instances[i] == null || instances[i] == this) { layerIndex = i; instances[i] = this; break; } } } bool needsSetup = ( isOverridePending || layerDesc.MipLevels != mipLevels || layerDesc.SampleCount != sampleCount || layerDesc.Format != etFormat || layerDesc.Layout != layout || layerDesc.LayerFlags != flags || !layerDesc.TextureSize.Equals(size) || layerDesc.Shape != shape || layerCompositionDepth != compositionDepth); if (!needsSetup) { return(false); } OVRPlugin.LayerDesc desc = OVRPlugin.CalculateLayerDesc(shape, layout, size, mipLevels, sampleCount, etFormat, flags); OVRPlugin.EnqueueSetupLayer(desc, compositionDepth, layerIdPtr); layerId = (int)layerIdHandle.Target; if (layerId > 0) { layerDesc = desc; layerCompositionDepth = compositionDepth; if (isExternalSurface) { stageCount = 1; } else { stageCount = OVRPlugin.GetLayerTextureStageCount(layerId); } } isOverridePending = false; return(true); }
private void LateUpdate() { if (this.currentOverlayType == OVROverlay.OverlayType.None || this.textures.Length < this.texturesPerStage || this.textures[0] == null) { return; } if (Time.frameCount <= this.prevFrameIndex) { return; } this.prevFrameIndex = Time.frameCount; OVRPose identity = OVRPose.identity; Vector3 one = Vector3.one; bool overlay = false; bool headLocked = false; if (!this.ComputeSubmit(ref identity, ref one, ref overlay, ref headLocked)) { return; } OVRPlugin.LayerDesc currentLayerDesc = this.GetCurrentLayerDesc(); bool isHdr = currentLayerDesc.Format == OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; bool flag = this.CreateLayer(currentLayerDesc.MipLevels, currentLayerDesc.SampleCount, currentLayerDesc.Format, currentLayerDesc.LayerFlags, currentLayerDesc.TextureSize, currentLayerDesc.Shape); if (this.layerIndex == -1 || this.layerId <= 0) { return; } bool useMipmaps = currentLayerDesc.MipLevels > 1; flag |= this.CreateLayerTextures(useMipmaps, currentLayerDesc.TextureSize, isHdr); if (this.layerTextures[0].appTexture as RenderTexture != null) { this.isDynamic = true; } if (!this.LatchLayerTextures()) { return; } if (!this.PopulateLayer(currentLayerDesc.MipLevels, isHdr, currentLayerDesc.TextureSize, currentLayerDesc.SampleCount)) { return; } bool flag2 = this.SubmitLayer(overlay, headLocked, identity, one); if (this.rend) { this.rend.enabled = !flag2; } }
private OVRPlugin.LayerDesc GetCurrentLayerDesc() { OVRPlugin.LayerDesc result = new OVRPlugin.LayerDesc { Format = OVRPlugin.EyeTextureFormat.Default, LayerFlags = 8, Layout = this.layout, MipLevels = 1, SampleCount = 1, Shape = (OVRPlugin.OverlayShape) this.currentOverlayShape, TextureSize = new OVRPlugin.Sizei { w = this.textures[0].width, h = this.textures[0].height } }; Texture2D texture2D = this.textures[0] as Texture2D; if (texture2D != null) { if (texture2D.format == TextureFormat.RGBAHalf || texture2D.format == TextureFormat.RGBAFloat) { result.Format = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; } result.MipLevels = texture2D.mipmapCount; } Cubemap cubemap = this.textures[0] as Cubemap; if (cubemap != null) { if (cubemap.format == TextureFormat.RGBAHalf || cubemap.format == TextureFormat.RGBAFloat) { result.Format = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; } result.MipLevels = cubemap.mipmapCount; } RenderTexture renderTexture = this.textures[0] as RenderTexture; if (renderTexture != null) { result.SampleCount = renderTexture.antiAliasing; if (renderTexture.format == RenderTextureFormat.ARGBHalf || renderTexture.format == RenderTextureFormat.ARGBFloat || renderTexture.format == RenderTextureFormat.RGB111110Float) { result.Format = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; } } return(result); }
private void DestroyLayer() { if (layerIndex != -1) { // Turn off the overlay if it was on. OVRPlugin.EnqueueSubmitLayer(true, false, IntPtr.Zero, IntPtr.Zero, -1, 0, OVRPose.identity.ToPosef(), Vector3.one.ToVector3f(), layerIndex, (OVRPlugin.OverlayShape)prevOverlayShape); instances[layerIndex] = null; layerIndex = -1; } if (layerIdPtr != IntPtr.Zero) { OVRPlugin.EnqueueDestroyLayer(layerIdPtr); layerIdPtr = IntPtr.Zero; layerIdHandle.Free(); layerId = 0; } layerDesc = new OVRPlugin.LayerDesc(); }
void LateUpdate() { if (!OVRManager.OVRManagerinitialized) { return; } if (!xrDeviceConstructed) { InitOVROverlay(); } if (OVRManager.loadedXRDevice != constructedOverlayXRDevice) { 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."); return; } // The overlay must be specified every eye frame, because it is positioned relative to the // current head location. If frames are dropped, it will be time warped appropriately, // just like the eye buffers. if (currentOverlayType == OverlayType.None || ((textures.Length < texturesPerStage || textures[0] == null) && !isExternalSurface)) { return; } OVRPose pose = OVRPose.identity; Vector3 scale = Vector3.one; bool overlay = false; bool headLocked = false; if (!ComputeSubmit(ref pose, ref scale, ref overlay, ref headLocked)) { return; } if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) { if (currentOverlayShape == OverlayShape.Quad) { OpenVROverlayUpdate(scale, pose); } //No more Overlay processing is required if we're on OpenVR return; } OVRPlugin.LayerDesc newDesc = GetCurrentLayerDesc(); bool isHdr = (newDesc.Format == OVRPlugin.EyeTextureFormat.R16G16B16A16_FP); // If the layer and textures are created but sizes differ, force re-creating them if (!layerDesc.TextureSize.Equals(newDesc.TextureSize) && layerId > 0) { DestroyLayerTextures(); DestroyLayer(); } bool createdLayer = CreateLayer(newDesc.MipLevels, newDesc.SampleCount, newDesc.Format, newDesc.LayerFlags, newDesc.TextureSize, newDesc.Shape); if (layerIndex == -1 || layerId <= 0) { return; } bool useMipmaps = (newDesc.MipLevels > 1); createdLayer |= CreateLayerTextures(useMipmaps, newDesc.TextureSize, isHdr); if (!isExternalSurface && (layerTextures[0].appTexture as RenderTexture != null)) { isDynamic = true; } if (!LatchLayerTextures()) { return; } // Don't populate the same frame image twice. if (frameIndex > prevFrameIndex) { int stage = frameIndex % stageCount; if (!PopulateLayer(newDesc.MipLevels, isHdr, newDesc.TextureSize, newDesc.SampleCount, stage)) { return; } } bool isOverlayVisible = SubmitLayer(overlay, headLocked, noDepthBufferTesting, pose, scale, frameIndex); prevFrameIndex = frameIndex; if (isDynamic) { ++frameIndex; } // Backward compatibility: show regular renderer if overlay isn't visible. if (rend) { rend.enabled = !isOverlayVisible; } }
private OVRPlugin.LayerDesc GetCurrentLayerDesc() { OVRPlugin.Sizei textureSize = new OVRPlugin.Sizei() { w = 0, h = 0 }; if (isExternalSurface) { textureSize.w = externalSurfaceWidth; textureSize.h = externalSurfaceHeight; } else { if (textures[0] == null) { Debug.LogWarning("textures[0] hasn't been set"); } textureSize.w = textures[0] ? textures[0].width : 0; textureSize.h = textures[0] ? textures[0].height : 0; } OVRPlugin.LayerDesc newDesc = new OVRPlugin.LayerDesc() { Format = OVRPlugin.EyeTextureFormat.R8G8B8A8_sRGB, LayerFlags = isExternalSurface ? 0 : (int)OVRPlugin.LayerFlags.TextureOriginAtBottomLeft, Layout = layout, MipLevels = 1, SampleCount = 1, Shape = (OVRPlugin.OverlayShape)currentOverlayShape, TextureSize = textureSize }; var tex2D = textures[0] as Texture2D; if (tex2D != null) { if (tex2D.format == TextureFormat.RGBAHalf || tex2D.format == TextureFormat.RGBAFloat) { newDesc.Format = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; } newDesc.MipLevels = tex2D.mipmapCount; } var texCube = textures[0] as Cubemap; if (texCube != null) { if (texCube.format == TextureFormat.RGBAHalf || texCube.format == TextureFormat.RGBAFloat) { newDesc.Format = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; } newDesc.MipLevels = texCube.mipmapCount; } var rt = textures[0] as RenderTexture; if (rt != null) { newDesc.SampleCount = rt.antiAliasing; if (rt.format == RenderTextureFormat.ARGBHalf || rt.format == RenderTextureFormat.ARGBFloat || rt.format == RenderTextureFormat.RGB111110Float) { newDesc.Format = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; } } if (isProtectedContent) { newDesc.LayerFlags |= (int)OVRPlugin.LayerFlags.ProtectedContent; } if (isExternalSurface) { newDesc.LayerFlags |= (int)OVRPlugin.LayerFlags.AndroidSurfaceSwapChain; } return(newDesc); }
void Awake() { Debug.Log("Overlay Awake"); if (tex2DMaterial == null) { tex2DMaterial = new Material(Shader.Find("Oculus/Texture2D Blit")); } if (cubeMaterial == null) { cubeMaterial = new Material(Shader.Find("Oculus/Cubemap Blit")); } rend = GetComponent <Renderer>(); if (textures.Length == 0) { textures = new Texture[] { null } } ; // Backward compatibility if (rend != null && textures[0] == null) { textures[0] = rend.material.mainTexture; } } void OnEnable() { if (!OVRManager.isHmdPresent) { enabled = false; return; } } void OnDisable() { if ((gameObject.hideFlags & HideFlags.DontSaveInBuild) != 0) { return; } DestroyLayerTextures(); DestroyLayer(); } void OnDestroy() { DestroyLayerTextures(); DestroyLayer(); } bool ComputeSubmit(ref OVRPose pose, ref Vector3 scale, ref bool overlay, ref bool headLocked) { Camera headCamera = Camera.main; overlay = (currentOverlayType == OverlayType.Overlay); headLocked = false; for (var t = transform; t != null && !headLocked; t = t.parent) { headLocked |= (t == headCamera.transform); } pose = (headLocked) ? transform.ToHeadSpacePose(headCamera) : transform.ToTrackingSpacePose(headCamera); scale = transform.lossyScale; for (int i = 0; i < 3; ++i) { scale[i] /= headCamera.transform.lossyScale[i]; } if (currentOverlayShape == OverlayShape.Cubemap) { #if UNITY_ANDROID && !UNITY_EDITOR //HACK: VRAPI cubemaps assume are yawed 180 degrees relative to LibOVR. pose.orientation = pose.orientation * Quaternion.AngleAxis(180, Vector3.up); #endif pose.position = headCamera.transform.position; } // Pack the offsetCenter directly into pose.position for offcenterCubemap if (currentOverlayShape == OverlayShape.OffcenterCubemap) { pose.position = transform.position; if (pose.position.magnitude > 1.0f) { Debug.LogWarning("Your cube map center offset's magnitude is greater than 1, which will cause some cube map pixel always invisible ."); return(false); } } // Cylinder overlay sanity checking if (currentOverlayShape == OverlayShape.Cylinder) { float arcAngle = scale.x / scale.z / (float)Math.PI * 180.0f; if (arcAngle > 180.0f) { Debug.LogWarning("Cylinder overlay's arc angle has to be below 180 degree, current arc angle is " + arcAngle + " degree."); return(false); } } return(true); } void LateUpdate() { // The overlay must be specified every eye frame, because it is positioned relative to the // current head location. If frames are dropped, it will be time warped appropriately, // just like the eye buffers. if (currentOverlayType == OverlayType.None || textures.Length < texturesPerStage || textures[0] == null) { return; } OVRPose pose = OVRPose.identity; Vector3 scale = Vector3.one; bool overlay = false; bool headLocked = false; if (!ComputeSubmit(ref pose, ref scale, ref overlay, ref headLocked)) { return; } OVRPlugin.LayerDesc newDesc = GetCurrentLayerDesc(); bool isHdr = (newDesc.Format == OVRPlugin.EyeTextureFormat.R16G16B16A16_FP); bool createdLayer = CreateLayer(newDesc.MipLevels, newDesc.SampleCount, newDesc.Format, newDesc.LayerFlags, newDesc.TextureSize, newDesc.Shape); if (layerIndex == -1 || layerId <= 0) { return; } bool useMipmaps = (newDesc.MipLevels > 1); createdLayer |= CreateLayerTextures(useMipmaps, newDesc.TextureSize, isHdr); if (layerTextures[0].appTexture as RenderTexture != null) { isDynamic = true; } if (!LatchLayerTextures()) { return; } // Don't populate the same frame image twice. if (frameIndex > prevFrameIndex) { int stage = frameIndex % stageCount; if (!PopulateLayer(newDesc.MipLevels, isHdr, newDesc.TextureSize, newDesc.SampleCount, stage)) { return; } } bool isOverlayVisible = SubmitLayer(overlay, headLocked, pose, scale, frameIndex); prevFrameIndex = frameIndex; if (isDynamic) { ++frameIndex; } // Backward compatibility: show regular renderer if overlay isn't visible. if (rend) { rend.enabled = !isOverlayVisible; } } #endregion }
void Awake() { Debug.Log("Overlay Awake"); if (premultiplyMaterial == null) { premultiplyMaterial = new Material(Shader.Find("Oculus/Alpha Premultiply")); } rend = GetComponent <Renderer>(); if (textures.Length == 0) { textures = new Texture[] { null } } ; // Backward compatibility if (rend != null && textures[0] == null) { textures[0] = rend.material.mainTexture; } if (textures[0] != null) { cachedTextures[0] = textures[0]; texNativePtrs[0] = textures[0].GetNativeTexturePtr(); } #if UNITY_ANDROID && !UNITY_EDITOR if (textures.Length == 2 && textures[1] != null) { layout = (isMultiviewEnabled) ? OVRPlugin.LayerLayout.Array : OVRPlugin.LayerLayout.Stereo; } texturesPerStage = (layout == OVRPlugin.LayerLayout.Stereo) ? 2 : 1; #endif } void OnEnable() { if (!OVRManager.isHmdPresent) { enabled = false; return; } OnDisable(); for (int i = 0; i < maxInstances; ++i) { if (instances[i] == null || instances[i] == this) { layerIndex = i; instances[i] = this; break; } } layerIdHandle = GCHandle.Alloc(layerId, GCHandleType.Pinned); layerIdPtr = layerIdHandle.AddrOfPinnedObject(); } void OnDisable() { if (layerIndex != -1) { // Turn off the overlay if it was on. OVRPlugin.EnqueueSubmitLayer(true, false, IntPtr.Zero, IntPtr.Zero, -1, 0, OVRPose.identity.ToPosef(), Vector3.one.ToVector3f(), layerIndex, (OVRPlugin.OverlayShape)_prevOverlayShape); instances[layerIndex] = null; } if (layerIdPtr != IntPtr.Zero) { OVRPlugin.EnqueueDestroyLayer(layerIdPtr); layerIdPtr = IntPtr.Zero; layerIdHandle.Free(); } layerIndex = -1; } int prevFrameIndex = -1; void OnRenderObject() { // The overlay must be specified every eye frame, because it is positioned relative to the // current head location. If frames are dropped, it will be time warped appropriately, // just like the eye buffers. if (!Camera.current.CompareTag("MainCamera") || Camera.current.cameraType != CameraType.Game || layerIndex == -1 || currentOverlayType == OverlayType.None || textures.Length < texturesPerStage) { return; } // Don't submit the same frame twice. if (Time.frameCount <= prevFrameIndex) { return; } prevFrameIndex = Time.frameCount; #if !UNITY_ANDROID || UNITY_EDITOR if (currentOverlayShape == OverlayShape.OffcenterCubemap) { Debug.LogWarning("Overlay shape " + currentOverlayShape + " is not supported on current platform"); } #endif for (int i = 0; i < texturesPerStage; ++i) { if (textures[i] != cachedTextures[i]) { cachedTextures[i] = textures[i]; if (cachedTextures[i] != null) { texNativePtrs[i] = cachedTextures[i].GetNativeTexturePtr(); } } if (currentOverlayShape == OverlayShape.Cubemap) { if (textures[i] != null && textures[i].GetType() != typeof(Cubemap)) { Debug.LogError("Need Cubemap texture for cube map overlay"); return; } } } if (cachedTextures[0] == null || texNativePtrs[0] == IntPtr.Zero) { return; } bool overlay = (currentOverlayType == OverlayType.Overlay); bool headLocked = false; for (var t = transform; t != null && !headLocked; t = t.parent) { headLocked |= (t == Camera.current.transform); } OVRPose pose = (headLocked) ? transform.ToHeadSpacePose() : transform.ToTrackingSpacePose(); Vector3 scale = transform.lossyScale; for (int i = 0; i < 3; ++i) { scale[i] /= Camera.current.transform.lossyScale[i]; } #if !UNITY_ANDROID if (currentOverlayShape == OverlayShape.Cubemap) { pose.position = Camera.current.transform.position; } #endif // Pack the offsetCenter directly into pose.position for offcenterCubemap if (currentOverlayShape == OverlayShape.OffcenterCubemap) { pose.position = transform.position; if (pose.position.magnitude > 1.0f) { Debug.LogWarning("your cube map center offset's magnitude is greater than 1, which will cause some cube map pixel always invisible ."); } } // Cylinder overlay sanity checking if (currentOverlayShape == OverlayShape.Cylinder) { float arcAngle = scale.x / scale.z / (float)Math.PI * 180.0f; if (arcAngle > 180.0f) { Debug.LogError("Cylinder overlay's arc angle has to be below 180 degree, current arc angle is " + arcAngle + " degree."); return; } } OVRPlugin.Sizei size = new OVRPlugin.Sizei() { w = textures[0].width, h = textures[0].height }; int flags = (int)OVRPlugin.LayerFlags.TextureOriginAtBottomLeft; int mipLevels = 1; int sampleCount = 1; TextureFormat txFormat = TextureFormat.BGRA32; OVRPlugin.EyeTextureFormat etFormat = OVRPlugin.EyeTextureFormat.B8G8R8A8_sRGB; RenderTextureFormat rtFormat = RenderTextureFormat.BGRA32; var tex2D = textures[0] as Texture2D; if (tex2D != null) { if (tex2D.format == TextureFormat.RGBAHalf || tex2D.format == TextureFormat.RGBAFloat) { txFormat = TextureFormat.RGBAHalf; etFormat = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; rtFormat = RenderTextureFormat.ARGBHalf; } } var rt = textures[0] as RenderTexture; if (rt != null) { sampleCount = rt.antiAliasing; if (rt.format == RenderTextureFormat.ARGBHalf) { txFormat = TextureFormat.RGBAHalf; etFormat = OVRPlugin.EyeTextureFormat.R16G16B16A16_FP; rtFormat = RenderTextureFormat.ARGBHalf; } } bool needsSetup = ( !layerDesc.TextureSize.Equals(size) || layerDesc.SampleCount != sampleCount || layerDesc.LayerFlags != flags || layerDesc.Shape != (OVRPlugin.OverlayShape)currentOverlayShape || layerDesc.Layout != layout || layerDesc.Format != etFormat); OVRPlugin.LayerDesc desc = new OVRPlugin.LayerDesc(); if (layerIdPtr != IntPtr.Zero && needsSetup) { if ((int)layerIdHandle.Target != 0) { OVRPlugin.EnqueueDestroyLayer(layerIdPtr); } desc = OVRPlugin.CalculateLayerDesc((OVRPlugin.OverlayShape)currentOverlayShape, layout, size, mipLevels, sampleCount, etFormat, flags); OVRPlugin.EnqueueSetupLayer(desc, layerIdPtr); layerId = (int)layerIdHandle.Target; if (layerId > 0) { layerDesc = desc; } } if (layerId > 0) { // For newer SDKs, blit directly to the surface that will be used in compositing. int stageCount = OVRPlugin.GetLayerTextureStageCount(layerId); if (externalTextures == null) { frameIndex = 0; externalTextures = new Texture[texturesPerStage][]; } for (int eyeId = 0; eyeId < texturesPerStage; ++eyeId) { if (externalTextures[eyeId] == null) { externalTextures[eyeId] = new Texture[stageCount]; } int stage = frameIndex % stageCount; IntPtr externalTex = OVRPlugin.GetLayerTexture(layerId, stage, (OVRPlugin.Eye)eyeId); if (externalTex == IntPtr.Zero) { continue; } bool needsCopy = isDynamic; Texture et = externalTextures[eyeId][stage]; if (et == null) { bool isSrgb = (etFormat == OVRPlugin.EyeTextureFormat.B8G8R8A8_sRGB || etFormat == OVRPlugin.EyeTextureFormat.R8G8B8A8_sRGB); if (currentOverlayShape != OverlayShape.Cubemap && currentOverlayShape != OverlayShape.OffcenterCubemap) { et = Texture2D.CreateExternalTexture(size.w, size.h, txFormat, mipLevels > 1, isSrgb, externalTex); } #if UNITY_2017_1_OR_NEWER else { //et = Cubemap.CreateExternalTexture(size.w, size.h, txFormat, mipLevels > 1, isSrgb, externalTex); et = Cubemap.CreateExternalTexture(size.w, txFormat, isSrgb, externalTex); } #endif externalTextures[eyeId][stage] = et; needsCopy = true; } if (needsCopy) { // The compositor uses premultiplied alpha, so multiply it here. if (currentOverlayShape != OverlayShape.Cubemap && currentOverlayShape != OverlayShape.OffcenterCubemap) { var tempRT = RenderTexture.GetTemporary(size.w, size.h, 0, rtFormat, RenderTextureReadWrite.Default, sampleCount); #if UNITY_ANDROID && !UNITY_EDITOR Graphics.Blit(textures[eyeId], tempRT); //Resolve, decompress, swizzle, etc not handled by simple CopyTexture. #else Graphics.Blit(textures[eyeId], tempRT, premultiplyMaterial); #endif Graphics.CopyTexture(tempRT, 0, 0, et, 0, 0); RenderTexture.ReleaseTemporary(tempRT); } #if UNITY_2017_1_OR_NEWER else { var tempRTSrc = RenderTexture.GetTemporary(size.w, size.h, 0, rtFormat, RenderTextureReadWrite.Default, sampleCount); var tempRTDst = RenderTexture.GetTemporary(size.w, size.h, 0, rtFormat, RenderTextureReadWrite.Default, sampleCount); for (int face = 0; face < 6; ++face) { //HACK: It would be much more efficient to blit directly from textures[eyeId] to et, but Unity's API doesn't support that. //Suggest using a native plugin to render directly to a cubemap layer for 360 video, etc. Graphics.CopyTexture(textures[eyeId], face, 0, tempRTSrc, 0, 0); Graphics.Blit(tempRTSrc, tempRTDst, premultiplyMaterial); Graphics.CopyTexture(tempRTDst, 0, 0, et, face, 0); } RenderTexture.ReleaseTemporary(tempRTSrc); RenderTexture.ReleaseTemporary(tempRTDst); } #endif } } bool isOverlayVisible = OVRPlugin.EnqueueSubmitLayer(overlay, headLocked, texNativePtrs[0], texNativePtrs[1], layerId, frameIndex, pose.flipZ().ToPosef(), scale.ToVector3f(), layerIndex, (OVRPlugin.OverlayShape)currentOverlayShape); if (isDynamic) { ++frameIndex; } _prevOverlayShape = currentOverlayShape; if (rend) { rend.enabled = !isOverlayVisible; } } } }
void Awake() { Debug.Log("Overlay Awake"); if (premultiplyMaterial == null) { premultiplyMaterial = new Material(Shader.Find("Oculus/Alpha Premultiply")); } rend = GetComponent <Renderer>(); if (textures.Length == 0) { textures = new Texture[] { null } } ; // Backward compatibility if (rend != null && textures[0] == null) { textures[0] = rend.material.mainTexture; } #if UNITY_ANDROID && !UNITY_EDITOR if (textures.Length == 2 && textures[1] != null) { layout = OVRPlugin.LayerLayout.Stereo; } #endif } void OnEnable() { if (!OVRManager.isHmdPresent) { enabled = false; return; } } void OnDisable() { DestroyLayerTextures(); DestroyLayer(); } void OnDestroy() { DestroyLayerTextures(); DestroyLayer(); } bool ComputeSubmit(ref OVRPose pose, ref Vector3 scale, ref bool overlay, ref bool headLocked) { overlay = (currentOverlayType == OverlayType.Overlay); headLocked = false; for (var t = transform; t != null && !headLocked; t = t.parent) { headLocked |= (t == Camera.current.transform); } pose = (headLocked) ? transform.ToHeadSpacePose() : transform.ToTrackingSpacePose(); scale = transform.lossyScale; for (int i = 0; i < 3; ++i) { scale[i] /= Camera.current.transform.lossyScale[i]; } if (currentOverlayShape == OverlayShape.Cubemap) { pose.position = Camera.current.transform.position; } // Pack the offsetCenter directly into pose.position for offcenterCubemap if (currentOverlayShape == OverlayShape.OffcenterCubemap) { pose.position = transform.position; if (pose.position.magnitude > 1.0f) { Debug.LogWarning("Your cube map center offset's magnitude is greater than 1, which will cause some cube map pixel always invisible ."); return(false); } } // Cylinder overlay sanity checking if (currentOverlayShape == OverlayShape.Cylinder) { float arcAngle = scale.x / scale.z / (float)Math.PI * 180.0f; if (arcAngle > 180.0f) { Debug.LogWarning("Cylinder overlay's arc angle has to be below 180 degree, current arc angle is " + arcAngle + " degree."); return(false); } } return(true); } void OnRenderObject() { // The overlay must be specified every eye frame, because it is positioned relative to the // current head location. If frames are dropped, it will be time warped appropriately, // just like the eye buffers. if (!Camera.current.CompareTag("MainCamera") || Camera.current.cameraType != UnityEngine.CameraType.Game) { return; } if (currentOverlayType == OverlayType.None || textures.Length < texturesPerStage) { return; } // Don't submit the same frame twice. if (Time.frameCount <= prevFrameIndex) { return; } prevFrameIndex = Time.frameCount; OVRPose pose = OVRPose.identity; Vector3 scale = Vector3.one; bool overlay = false; bool headLocked = false; if (!ComputeSubmit(ref pose, ref scale, ref overlay, ref headLocked)) { return; } OVRPlugin.LayerDesc newDesc = GetCurrentLayerDesc(); bool isHdr = (newDesc.Format == OVRPlugin.EyeTextureFormat.R16G16B16A16_FP); bool createdLayer = CreateLayer(newDesc.MipLevels, newDesc.SampleCount, newDesc.Format, newDesc.LayerFlags, newDesc.TextureSize, newDesc.Shape); if (layerIndex == -1 || layerId <= 0) { return; } bool isSrgb = (newDesc.Format == OVRPlugin.EyeTextureFormat.B8G8R8A8_sRGB || newDesc.Format == OVRPlugin.EyeTextureFormat.R8G8B8A8_sRGB); bool useMipmaps = (newDesc.MipLevels > 1); createdLayer |= CreateLayerTextures(isSrgb, useMipmaps, newDesc.TextureSize, isHdr); if (layerTextures[0].appTexture as RenderTexture != null) { isDynamic = true; } if (!isDynamic && !createdLayer) { return; } if (!LatchLayerTextures()) { return; } if (!PopulateLayer(newDesc.MipLevels, isHdr, newDesc.TextureSize, newDesc.SampleCount)) { return; } bool isOverlayVisible = SubmitLayer(overlay, headLocked, pose, scale); // Backward compatibility: show regular renderer if overlay isn't visible. if (rend) { rend.enabled = !isOverlayVisible; } } #endregion }