// Renders a single camera in a single texture with occlusion only from opaque objects. // This is the most performant option for mixed reality. // It does not support any transparency in the foreground layer. private void RenderOptimized() { bool debugClipPlane = SDKUtils.FeatureEnabled(inputFrame.features, FEATURES.DEBUG_CLIP_PLANE); bool renderComplexClipPlane = SDKUtils.FeatureEnabled(inputFrame.features, FEATURES.COMPLEX_CLIP_PLANE); bool renderGroundClipPlane = SDKUtils.FeatureEnabled(inputFrame.features, FEATURES.GROUND_CLIP_PLANE); SDKUtils.SetCamera(_cameraInstance, _cameraInstance.transform, _inputFrame, localToWorldMatrix, spectatorLayerMask); _cameraInstance.targetTexture = _optimizedRenderTexture; // Clear alpha channel _writeMaterial.SetInt(SDKShaders.LIV_COLOR_MASK, (int)ColorWriteMask.Alpha); _optimizedRenderingPass.commandBuffer.Blit(BuiltinRenderTextureType.None, BuiltinRenderTextureType.CurrentActive, _writeMaterial); // Render opaque pixels into alpha channel _writeOpaqueToAlphaMaterial.SetInt(SDKShaders.LIV_COLOR_MASK, (int)ColorWriteMask.Alpha); _optimizedRenderingPass.commandBuffer.DrawMesh(_clipPlaneMesh, Matrix4x4.identity, _writeOpaqueToAlphaMaterial, 0, 0); // Render clip plane Matrix4x4 clipPlaneTransform = localToWorldMatrix * (Matrix4x4)_inputFrame.clipPlane.transform; _optimizedRenderingPass.commandBuffer.DrawMesh(_clipPlaneMesh, clipPlaneTransform, GetClipPlaneMaterial(debugClipPlane, renderComplexClipPlane, ColorWriteMask.Alpha), 0, 0); // Render ground clip plane if (renderGroundClipPlane) { Matrix4x4 groundClipPlaneTransform = localToWorldMatrix * (Matrix4x4)_inputFrame.groundClipPlane.transform; _optimizedRenderingPass.commandBuffer.DrawMesh(_clipPlaneMesh, groundClipPlaneTransform, GetGroundClipPlaneMaterial(debugClipPlane, ColorWriteMask.Alpha), 0, 0); } SDKUniversalRenderFeature.AddPass(_optimizedRenderingPass); // TODO: this is just proprietary SDKShaders.StartRendering(); SDKShaders.StartBackgroundRendering(); InvokePreRenderBackground(); SendTextureToBridge(_optimizedRenderTexture, TEXTURE_ID.OPTIMIZED_COLOR_BUFFER_ID); _cameraInstance.Render(); InvokePostRenderBackground(); _cameraInstance.targetTexture = null; SDKShaders.StopBackgroundRendering(); SDKShaders.StopRendering(); _optimizedRenderingPass.commandBuffer.Clear(); SDKUniversalRenderFeature.ClearPasses(); }
// Default render without any special changes private void RenderBackground() { SDKUtils.SetCamera(_cameraInstance, _cameraInstance.transform, _inputFrame, localToWorldMatrix, spectatorLayerMask); _cameraInstance.targetTexture = _backgroundRenderTexture; RenderTexture tempRenderTexture = null; bool overridePostProcessing = SDKUtils.FeatureEnabled(inputFrame.features, FEATURES.OVERRIDE_POST_PROCESSING); if (overridePostProcessing) { tempRenderTexture = RenderTexture.GetTemporary(_backgroundRenderTexture.width, _backgroundRenderTexture.height, 0, _backgroundRenderTexture.format); #if UNITY_EDITOR tempRenderTexture.name = "LIV.TemporaryRenderTexture"; #endif _captureTexturePass.commandBuffer.Blit(BuiltinRenderTextureType.CurrentActive, tempRenderTexture); _applyTexturePass.commandBuffer.Blit(tempRenderTexture, BuiltinRenderTextureType.CurrentActive); SDKUniversalRenderFeature.AddPass(_captureTexturePass); SDKUniversalRenderFeature.AddPass(_applyTexturePass); } SDKShaders.StartRendering(); SDKShaders.StartBackgroundRendering(); InvokePreRenderBackground(); SendTextureToBridge(_backgroundRenderTexture, TEXTURE_ID.BACKGROUND_COLOR_BUFFER_ID); _cameraInstance.Render(); InvokePostRenderBackground(); _cameraInstance.targetTexture = null; SDKShaders.StopBackgroundRendering(); SDKShaders.StopRendering(); if (overridePostProcessing) { _captureTexturePass.commandBuffer.Clear(); _applyTexturePass.commandBuffer.Clear(); RenderTexture.ReleaseTemporary(tempRenderTexture); } SDKUniversalRenderFeature.ClearPasses(); }
// Extract the image which is in front of our clip plane // The compositing is heavily relying on the alpha channel, therefore we want to make sure it does // not get corrupted by the postprocessing or any shader private void RenderForeground() { bool debugClipPlane = SDKUtils.FeatureEnabled(inputFrame.features, FEATURES.DEBUG_CLIP_PLANE); bool renderComplexClipPlane = SDKUtils.FeatureEnabled(inputFrame.features, FEATURES.COMPLEX_CLIP_PLANE); bool renderGroundClipPlane = SDKUtils.FeatureEnabled(inputFrame.features, FEATURES.GROUND_CLIP_PLANE); bool overridePostProcessing = SDKUtils.FeatureEnabled(inputFrame.features, FEATURES.OVERRIDE_POST_PROCESSING); bool fixPostEffectsAlpha = SDKUtils.FeatureEnabled(inputFrame.features, FEATURES.FIX_FOREGROUND_ALPHA) | _liv.fixPostEffectsAlpha; MonoBehaviour[] behaviours = null; bool[] wasBehaviourEnabled = null; if (disableStandardAssets) { SDKUtils.DisableStandardAssets(_cameraInstance, ref behaviours, ref wasBehaviourEnabled); } // Capture camera defaults CameraClearFlags capturedClearFlags = _cameraInstance.clearFlags; Color capturedBgColor = _cameraInstance.backgroundColor; Color capturedFogColor = RenderSettings.fogColor; // Make sure that fog does not corrupt alpha channel RenderSettings.fogColor = new Color(capturedFogColor.r, capturedFogColor.g, capturedFogColor.b, 0f); SDKUtils.SetCamera(_cameraInstance, _cameraInstance.transform, _inputFrame, localToWorldMatrix, spectatorLayerMask); _cameraInstance.clearFlags = CameraClearFlags.Color; _cameraInstance.backgroundColor = Color.clear; _cameraInstance.targetTexture = _foregroundRenderTexture; RenderTexture capturedAlphaRenderTexture = RenderTexture.GetTemporary(_foregroundRenderTexture.width, _foregroundRenderTexture.height, 0, _foregroundRenderTexture.format); #if UNITY_EDITOR capturedAlphaRenderTexture.name = "LIV.CapturedAlphaRenderTexture"; #endif // Render opaque pixels into alpha channel _clipPlanePass.commandBuffer.DrawMesh(_clipPlaneMesh, Matrix4x4.identity, _writeOpaqueToAlphaMaterial, 0, 0); // Render clip plane Matrix4x4 clipPlaneTransform = localToWorldMatrix * (Matrix4x4)_inputFrame.clipPlane.transform; _clipPlanePass.commandBuffer.DrawMesh(_clipPlaneMesh, clipPlaneTransform, GetClipPlaneMaterial(debugClipPlane, renderComplexClipPlane, ColorWriteMask.All), 0, 0); // Render ground clip plane if (renderGroundClipPlane) { Matrix4x4 groundClipPlaneTransform = localToWorldMatrix * (Matrix4x4)_inputFrame.groundClipPlane.transform; _clipPlanePass.commandBuffer.DrawMesh(_clipPlaneMesh, groundClipPlaneTransform, GetGroundClipPlaneMaterial(debugClipPlane, ColorWriteMask.All), 0, 0); } // Copy alpha in to texture _clipPlanePass.commandBuffer.Blit(BuiltinRenderTextureType.CurrentActive, capturedAlphaRenderTexture); _clipPlanePass.commandBuffer.SetRenderTarget(_cameraColorTextureIdentifier); SDKUniversalRenderFeature.AddPass(_clipPlanePass); // Fix alpha corruption by post processing RenderTexture tempRenderTexture = null; if (overridePostProcessing || fixPostEffectsAlpha) { tempRenderTexture = RenderTexture.GetTemporary(_foregroundRenderTexture.width, _foregroundRenderTexture.height, 0, _foregroundRenderTexture.format); #if UNITY_EDITOR tempRenderTexture.name = "LIV.TemporaryRenderTexture"; #endif _captureTexturePass.commandBuffer.Blit(BuiltinRenderTextureType.CurrentActive, tempRenderTexture); SDKUniversalRenderFeature.AddPass(_captureTexturePass); _writeMaterial.SetInt(SDKShaders.LIV_COLOR_MASK, overridePostProcessing ? (int)ColorWriteMask.All : (int)ColorWriteMask.Alpha); _applyTexturePass.commandBuffer.Blit(tempRenderTexture, BuiltinRenderTextureType.CurrentActive, _writeMaterial); SDKUniversalRenderFeature.AddPass(_applyTexturePass); } // Combine captured alpha with result alpha _combineAlphaMaterial.SetInt(SDKShaders.LIV_COLOR_MASK, (int)ColorWriteMask.Alpha); _combineAlphaPass.commandBuffer.Blit(capturedAlphaRenderTexture, BuiltinRenderTextureType.CurrentActive, _combineAlphaMaterial); SDKUniversalRenderFeature.AddPass(_combineAlphaPass); if (useDeferredRendering) { SDKUtils.ForceForwardRendering(cameraInstance, _clipPlaneMesh, _forceForwardRenderingMaterial); } SDKShaders.StartRendering(); SDKShaders.StartForegroundRendering(); InvokePreRenderForeground(); SendTextureToBridge(_foregroundRenderTexture, TEXTURE_ID.FOREGROUND_COLOR_BUFFER_ID); _cameraInstance.Render(); InvokePostRenderForeground(); _cameraInstance.targetTexture = null; SDKShaders.StopForegroundRendering(); SDKShaders.StopRendering(); if (overridePostProcessing || fixPostEffectsAlpha) { _captureTexturePass.commandBuffer.Clear(); _applyTexturePass.commandBuffer.Clear(); RenderTexture.ReleaseTemporary(tempRenderTexture); } RenderTexture.ReleaseTemporary(capturedAlphaRenderTexture); _clipPlanePass.commandBuffer.Clear(); _combineAlphaPass.commandBuffer.Clear(); SDKUniversalRenderFeature.ClearPasses(); // Revert camera defaults _cameraInstance.clearFlags = capturedClearFlags; _cameraInstance.backgroundColor = capturedBgColor; RenderSettings.fogColor = capturedFogColor; SDKUtils.RestoreStandardAssets(ref behaviours, ref wasBehaviourEnabled); }