using UnityEngine; using UnityEngine.Rendering; using UnityEngine.Rendering.Universal; namespace Framework.RenderFeature { public class GaussBlurPass : ScriptableRenderPass { private GaussBlurFeature.Setting _setting; int tmpId1; RenderTargetIdentifier tmpRT1; // RTHandle rth_tempTex; RTHandle cameraColorTarget; string profilerTag; private static readonly int blurOffset = Shader.PropertyToID ("_blurOffset"); private static readonly int grayOffset = Shader.PropertyToID ("_grayOffset"); private static readonly int grayOffsetR = Shader.PropertyToID ("_grayOffsetR"); private static readonly int grayOffsetG = Shader.PropertyToID ("_grayOffsetG"); private static readonly int grayOffsetB = Shader.PropertyToID ("_grayOffsetB"); private static readonly int overlyColor = Shader.PropertyToID ("_overlyColor"); public GaussBlurPass Init (GaussBlurFeature.Setting setting, string cmdName) { this._setting = setting; this.profilerTag = cmdName; return this; } public override void OnCameraSetup (CommandBuffer cmd, ref RenderingData renderingData) { if (this._setting is not { enable: true } || this.cameraColorTarget == null) return; RenderTextureDescriptor opaqueDesc = renderingData.cameraData.cameraTargetDescriptor; opaqueDesc.depthBufferBits = 0; //Color and depth cannot be combined in RTHandles // RenderingUtils.ReAllocateIfNeeded (ref rth_tempTex, opaqueDesc, FilterMode.Bilinear, TextureWrapMode.Clamp, name: "_TempTex"); var width = opaqueDesc.width / 2; var height = opaqueDesc.height / 2; tmpId1 = Shader.PropertyToID ("tmpBlurRT1"); cmd.GetTemporaryRT (tmpId1, width, height, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32); // cmd.GetTemporaryRT(this.cameraColorTarget, width, height, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32); tmpRT1 = new RenderTargetIdentifier (tmpId1); // (父类函数)指定pass的render target ConfigureTarget (this.cameraColorTarget); } public override void Execute (ScriptableRenderContext context, ref RenderingData renderingData) { #if UNITY_EDITOR if (!UnityEditor.EditorApplication.isPlaying) { return; } #endif if (!this._setting.enable) return; if (this.cameraColorTarget == null || this.cameraColorTarget.rt == null) { return; } CommandBuffer cmd = CommandBufferPool.Get (profilerTag); if (this._setting.isBlur && this._setting.blurProgress > 0 && this._setting.blurMaterial != null) { this._setting.blurMaterial.SetFloat (blurOffset, this._setting.blurProgress); for (int i = 1; i < this._setting.blurPasses; ++i) { cmd.Blit (cameraColorTarget , tmpRT1 , _setting.blurMaterial); cmd.Blit (tmpRT1 , cameraColorTarget); } } if (this._setting.isGray) { this._setting.grayMaterial.SetFloat (grayOffset, this._setting.grayScale); this._setting.grayMaterial.SetFloat (grayOffsetR, this._setting.colorChannel_R); this._setting.grayMaterial.SetFloat (grayOffsetG, this._setting.colorChannel_G); this._setting.grayMaterial.SetFloat (grayOffsetB, this._setting.colorChannel_B); this._setting.grayMaterial.SetColor (overlyColor, this._setting.grayColor); cmd.Blit (cameraColorTarget , tmpRT1 , _setting.grayMaterial); cmd.Blit (tmpRT1 , cameraColorTarget); } context.ExecuteCommandBuffer (cmd); cmd.Clear (); CommandBufferPool.Release (cmd); } public void SetTarget (RTHandle rendererCameraColorTargetHandle) { this.cameraColorTarget = rendererCameraColorTargetHandle; } } }