Inject a pass using a Scriptable Renderer Feature
This section describes how to create a Scriptable Renderer Feature for a URP Renderer. A Scriptable Renderer Feature enqueues a ScriptableRenderPass instance every frame.
You need to write a Scriptable Render Pass first.
This walkthrough contains the following sections:
- Create a scriptable Renderer Feature
- Add the Renderer Feature to the the Universal Renderer asset
- Enqueue the render pass in the custom renderer feature
- Complete code for the scripts in this example
Create a scriptable Renderer Feature
Create a new C# script and name it
MyRendererFeature.cs.In the script, remove the code that Unity inserted in the
MyRendererFeatureclass.Add the following
usingdirective:using UnityEngine.Rendering; using UnityEngine.Rendering.Universal;Create the
MyRendererFeatureclass that inherits from the ScriptableRendererFeature class.public class MyRendererFeature : ScriptableRendererFeatureIn the
MyRendererFeatureclass, implement the following methods:Create: Unity calls this method on the following events:When the Renderer Feature loads the first time.
When you enable or disable the Renderer Feature.
When you change a property in the inspector of the Renderer Feature.
AddRenderPasses: Unity calls this method every frame, once for each camera. This method lets you injectScriptableRenderPassinstances into the scriptable Renderer.
Now you have the custom MyRendererFeature Renderer Feature with its main methods.
Below is the complete code for this step.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.Universal;
public class MyRendererFeature : ScriptableRendererFeature
{
public override void Create()
{
}
public override void AddRenderPasses(ScriptableRenderer renderer,
ref RenderingData renderingData)
{
}
}
Add the Renderer Feature to the Universal Renderer asset
Add the Renderer Feature you created to the the Universal Renderer asset. For information on how to do this, refer to the page How to add a Renderer Feature to a Renderer.
Enqueue a render pass in the custom renderer feature
In this section, you instantiate a render pass in the Create method of the MyRendererFeature class, and enqueue it in the AddRenderPasses method.
This section uses the example RedTintRenderPass Scriptable Render Pass from the Write a Scriptable Render Pass page.
Declare the following fields:
[SerializeField] private Shader shader; private Material material; private RedTintRenderPass redTintRenderPass;In the
Createmethod, instantiate theRedTintRenderPassclass.In the method, use the
renderPassEventfield to specify when to execute the render pass.public override void Create() { if (shader == null) { return; } material = CoreUtils.CreateEngineMaterial(shader); redTintRenderPass = new RedTintRenderPass(material); redTintRenderPass.renderPassEvent = RenderPassEvent.AfterRenderingSkybox; }In the
AddRenderPassesmethod, enqueue the render pass with theEnqueuePassmethod.public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { if (renderingData.cameraData.cameraType == CameraType.Game) { renderer.EnqueuePass(redTintRenderPass); } }
You can now add the Scriptable Renderer Feature to the active URP asset.
For a full example, refer to Example of a complete Scriptable Renderer Feature.