void OnPreRender() { Profiler.BeginSample("AO - OnPreRender"); checkMaterials(true); if (m_targetCamera != null) { #if UNITY_EDITOR if ((m_targetCamera.cameraType == CameraType.SceneView) && (((PerPixelNormals == PerPixelNormalSource.GBuffer) && (m_targetCamera.orthographic == true)) || (PerPixelNormals == PerPixelNormalSource.Camera) && (SceneView.lastActiveSceneView.m_SceneLighting == false))) { PerPixelNormals = PerPixelNormalSource.None; } #endif bool deferredReflections = (GraphicsSettings.GetShaderMode(BuiltinShaderType.DeferredReflections) != BuiltinShaderMode.Disabled); if ((m_prevPerPixelNormals != PerPixelNormals) || (m_prevApplyMethod != ApplyMethod) || (m_prevDeferredReflections != deferredReflections) || (m_commandBuffer_Parameters.cmdBuffer == null) || (m_commandBuffer_Occlusion.cmdBuffer == null) || (m_commandBuffer_Apply.cmdBuffer == null) ) { CameraEvent cameraStage = CameraEvent.BeforeImageEffectsOpaque; if (ApplyMethod == ApplicationMethod.Deferred) { cameraStage = deferredReflections ? CameraEvent.BeforeReflections : CameraEvent.BeforeLighting; } createCommandBuffer(ref m_commandBuffer_Parameters, "AmplifyOcclusion_Parameters_" + m_myIDstring, cameraStage); createCommandBuffer(ref m_commandBuffer_Occlusion, "AmplifyOcclusion_Compute_" + m_myIDstring, cameraStage); createCommandBuffer(ref m_commandBuffer_Apply, "AmplifyOcclusion_Apply_" + m_myIDstring, cameraStage); m_prevPerPixelNormals = PerPixelNormals; m_prevApplyMethod = ApplyMethod; m_prevDeferredReflections = deferredReflections; m_paramsChanged = true; } if ((m_commandBuffer_Parameters.cmdBuffer != null) && (m_commandBuffer_Occlusion.cmdBuffer != null) && (m_commandBuffer_Apply.cmdBuffer != null)) { if (AmplifyOcclusionCommon.IsStereoMultiPassEnabled(m_targetCamera) == true) { uint curStepIdx = (m_sampleStep >> 1) & 1; uint curEyeIdx = (m_sampleStep & 1); m_curTemporalIdx = (curEyeIdx * 2) + (0 + curStepIdx); m_prevTemporalIdx = (curEyeIdx * 2) + (1 - curStepIdx); } else { uint curStepIdx = m_sampleStep & 1; m_curTemporalIdx = 0 + curStepIdx; m_prevTemporalIdx = 1 - curStepIdx; } m_commandBuffer_Parameters.cmdBuffer.Clear(); UpdateGlobalShaderConstants(m_commandBuffer_Parameters.cmdBuffer); UpdateGlobalShaderConstants_Matrices(m_commandBuffer_Parameters.cmdBuffer); UpdateGlobalShaderConstants_AmbientOcclusion(m_commandBuffer_Parameters.cmdBuffer); checkParamsChanged(); if (m_paramsChanged) { m_commandBuffer_Occlusion.cmdBuffer.Clear(); commandBuffer_FillComputeOcclusion(m_commandBuffer_Occlusion.cmdBuffer); } m_commandBuffer_Apply.cmdBuffer.Clear(); if (ApplyMethod == ApplicationMethod.Debug) { commandBuffer_FillApplyDebug(m_commandBuffer_Apply.cmdBuffer); } else { if (ApplyMethod == ApplicationMethod.PostEffect) { commandBuffer_FillApplyPostEffect(m_commandBuffer_Apply.cmdBuffer); } else { bool logTarget = !m_HDR; commandBuffer_FillApplyDeferred(m_commandBuffer_Apply.cmdBuffer, logTarget); } } updateParams(); m_sampleStep++; // No clamp, free running counter } } else { m_targetCamera = GetComponent <Camera>(); Update(); #if UNITY_EDITOR if (m_targetCamera.cameraType != CameraType.SceneView) { System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor"); UnityEngine.Object[] array = Resources.FindObjectsOfTypeAll(T); if (array.Length > 0) { EditorWindow gameView = (EditorWindow)array[0]; gameView.Focus(); } } #endif } Profiler.EndSample(); }
private void checkParamsChanged() { bool HDR = m_targetCamera.allowHDR; // && tier? bool MSAA = m_targetCamera.allowMSAA && m_targetCamera.actualRenderingPath != RenderingPath.DeferredLighting && m_targetCamera.actualRenderingPath != RenderingPath.DeferredShading && QualitySettings.antiAliasing >= 1; int antiAliasing = MSAA ? QualitySettings.antiAliasing : 1; if (m_occlusionDepthRT != null) { if ((m_occlusionDepthRT.width != m_target.width) || (m_occlusionDepthRT.height != m_target.height) || (m_prevMSAA != MSAA) || (!m_occlusionDepthRT.IsCreated()) || (m_temporalAccumRT != null && (!m_temporalAccumRT[0].IsCreated() || !m_temporalAccumRT[1].IsCreated())) #if UNITY_EDITOR || ((m_prevIsPlaying == true) && (EditorApplication.isPlaying == false)) #endif ) { AmplifyOcclusionCommon.SafeReleaseRT(ref m_occlusionDepthRT); AmplifyOcclusionCommon.SafeReleaseRT(ref m_depthMipmap); releaseTemporalRT(); m_paramsChanged = true; } else { if (m_prevFilterEnabled != FilterEnabled) { releaseTemporalRT(); } } } if (m_temporalAccumRT != null) { if (AmplifyOcclusionCommon.IsStereoMultiPassEnabled(m_targetCamera) == true) { if (m_temporalAccumRT.Length != 4) { m_temporalAccumRT = null; } } else { if (m_temporalAccumRT.Length != 2) { m_temporalAccumRT = null; } } } if (m_occlusionDepthRT == null) { m_occlusionDepthRT = AmplifyOcclusionCommon.SafeAllocateRT("_AO_OcclusionDepthTexture", m_target.width, m_target.height, m_occlusionRTFormat, RenderTextureReadWrite.Linear, FilterMode.Bilinear); } if (m_temporalAccumRT == null && FilterEnabled) { if (AmplifyOcclusionCommon.IsStereoMultiPassEnabled(m_targetCamera) == true) { m_temporalAccumRT = new RenderTexture[4]; } else { m_temporalAccumRT = new RenderTexture[2]; } for (int i = 0; i < m_temporalAccumRT.Length; i++) { m_temporalAccumRT[i] = AmplifyOcclusionCommon.SafeAllocateRT("_AO_TemporalAccum_" + i.ToString(), m_target.width, m_target.height, m_accumTemporalRTFormat, RenderTextureReadWrite.Linear, FilterMode.Bilinear, antiAliasing); } m_clearHistory = true; } if ((CacheAware == true) && (m_depthMipmap == null)) { m_depthMipmap = AmplifyOcclusionCommon.SafeAllocateRT("_AO_DepthMipmap", m_target.width >> 1, m_target.height >> 1, RenderTextureFormat.RFloat, RenderTextureReadWrite.Linear, FilterMode.Point, 1, true); int minSize = (int)Mathf.Min(m_target.width, m_target.height); m_numberMips = (int)(Mathf.Log((float)minSize, 2.0f) + 1.0f) - 1; m_tmpMipString = null; m_tmpMipString = new string[m_numberMips]; for (int i = 0; i < m_numberMips; i++) { m_tmpMipString[i] = "_AO_TmpMip_" + i.ToString(); } } else { if ((CacheAware == false) && (m_depthMipmap != null)) { AmplifyOcclusionCommon.SafeReleaseRT(ref m_depthMipmap); m_tmpMipString = null; } } if ((m_prevSampleCount != SampleCount) || (m_prevDownsample != Downsample) || (m_prevCacheAware != CacheAware) || (m_prevBlurEnabled != BlurEnabled) || (((m_prevBlurPasses != BlurPasses) || (m_prevBlurRadius != BlurRadius)) && (BlurEnabled == true)) || (m_prevFilterEnabled != FilterEnabled) || (m_prevHDR != HDR) || (m_prevMSAA != MSAA)) { m_clearHistory |= (m_prevHDR != HDR); m_clearHistory |= (m_prevMSAA != MSAA); m_HDR = HDR; m_MSAA = MSAA; m_paramsChanged = true; } #if UNITY_EDITOR m_prevIsPlaying = EditorApplication.isPlaying; #endif }