In my URP project using Unity 2021.3.6, after upgrading to Unity 6, I first modified the compatibility mode RenderFeature according to the documentation, but the blur effect stopped working. In the Frame Debug, I can’t see the corresponding pass. Even after completely following the documentation to write the new renderGraph version, there’s still no effect. I’m not sure what went wrong.
This is my radial blur shader
Shader "Hidden/CustomPostEffect/RadialBlur"
{
Properties
{
_MainTex ("Texture", 2D) = "white" { }
}
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
uniform half4 _Params;
#define _BlurRadius _Params.x
#define _Iteration _Params.y
#define _RadialCenter _Params.zw
TEXTURE2D (_MainTex);
SAMPLER(sampler_MainTex);
struct appdata
{
float4 vertex: POSITION;
float2 uv: TEXCOORD0;
};
struct VaryingsDefault
{
float4 pos: SV_POSITION;
float2 uv: TEXCOORD0;
};
VaryingsDefault VertDefault(appdata v)
{
VaryingsDefault o;
o.pos = TransformObjectToHClip(v.vertex.xyz);
o.uv = v.uv;
return o;
}
half4 RadialBlur(VaryingsDefault i)
{
float2 blurVector = (_RadialCenter - i.uv.xy) * _BlurRadius;
half4 acumulateColor = half4(0, 0, 0, 0);
[unroll(30)]
for (int j = 0; j < _Iteration; j ++)
{
acumulateColor += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
i.uv.xy += blurVector;
}
return acumulateColor / _Iteration;
}
half4 Frag(VaryingsDefault i): SV_Target
{
return RadialBlur(i);
}
ENDHLSL
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment Frag
ENDHLSL
}
}
}
Here's my RendererFeatureimplementation
public class RadialBlurRenderFeature : ScriptableRendererFeature
{
public Shader radialBlurShader;
private Material radialBlurMaterial;
private RadialBlurRenderPass radialBlurRenderPass;
public override void Create()
{
if (radialBlurShader == null)
{
return;
}
radialBlurMaterial = CoreUtils.CreateEngineMaterial(radialBlurShader);
radialBlurRenderPass = new RadialBlurRenderPass(radialBlurMaterial);
radialBlurRenderPass.renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
if (radialBlurRenderPass == null)
{
return;
}
//if (renderingData.cameraData.cameraType == CameraType.Game)
renderer.EnqueuePass(radialBlurRenderPass);
//Debug.Log("加入渲染队列");
}
protected override void Dispose(bool disposing)
{
if (Application.isPlaying)
{
Destroy(radialBlurMaterial);
}
else
{
DestroyImmediate(radialBlurMaterial);
}
}
}
This is my RenderPass line of sight
public class RadialBlurRenderPass : ScriptableRenderPass
{
private static readonly int ParamsId = Shader.PropertyToID("_Params");
private const string k_RadialBlurTextureName = "_RadialBlurTexture";
private const string k_RadialBlurPassName = "RadialBlurRenderPass";
private Material radialBlurMaterial;
private RenderTextureDescriptor radialBlurTextureDescriptor;
public RadialBlurRenderPass(Material material)
{
this.radialBlurMaterial = material;
radialBlurTextureDescriptor = new RenderTextureDescriptor(Screen.width, Screen.height,
RenderTextureFormat.Default, 0);
}
private void UpdateRadialBlurSettings()
{
if (radialBlurMaterial == null)
return;
var volumeComponent = VolumeManager.instance.stack.GetComponent<RadialBlur>();
if (volumeComponent == null)
return;
Vector4 blurParams = new Vector4(
volumeComponent.BlurRadius.value * 0.02f,
volumeComponent.Iteration.value,
volumeComponent.RadialCenterX.value,
volumeComponent.RadialCenterY.value
);
radialBlurMaterial.SetVector(ParamsId, blurParams);
}
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
{
UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
// The following line ensures that the render pass doesn't blit
// from the back buffer.
if (resourceData.isActiveTargetBackBuffer)
return;
// Set the blur texture size to be the same as the camera target size.
radialBlurTextureDescriptor.width = cameraData.cameraTargetDescriptor.width;
radialBlurTextureDescriptor.height = cameraData.cameraTargetDescriptor.height;
radialBlurTextureDescriptor.depthBufferBits = 0;
TextureHandle srcCamColor = resourceData.activeColorTexture;
TextureHandle dst = UniversalRenderer.CreateRenderGraphTexture(renderGraph,
radialBlurTextureDescriptor, k_RadialBlurTextureName, false);
// Update the blur settings in the material
UpdateRadialBlurSettings();
// This check is to avoid an error from the material preview in the scene
if (!srcCamColor.IsValid() || !dst.IsValid())
return;
// The AddBlitPass method adds a vertical blur render graph pass that blits from the source texture (camera color in this case) to the destination texture using the first shader pass (the shader pass is defined in the last parameter).
RenderGraphUtils.BlitMaterialParameters paraVertical = new(srcCamColor, dst, radialBlurMaterial, 0);
renderGraph.AddBlitPass(paraVertical, k_RadialBlurPassName);
//Debug.Log("Ìí¼ÓPass");
}
}