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
MyRendererFeature
class.Add the following
using
directive:using UnityEngine.Rendering; using UnityEngine.Rendering.Universal;
Create the
MyRendererFeature
class that inherits from the ScriptableRendererFeature class.public class MyRendererFeature : ScriptableRendererFeature
In the
MyRendererFeature
class, 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 injectScriptableRenderPass
instances 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
Create
method, instantiate theRedTintRenderPass
class.In the method, use the
renderPassEvent
field to specify when to execute the render pass.public override void Create() { if (shader == null) { return; } material = CoreUtils.CreateEngineMaterial(shader); redTintRenderPass = new RedTintRenderPass(material); renderPassEvent = RenderPassEvent.AfterRenderingSkybox; }
In the
AddRenderPasses
method, enqueue the render pass with theEnqueuePass
method.public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { if (renderingData.cameraData.cameraType == CameraType.Game) { renderer.EnqueuePass(redTintRenderPass); } }
Custom Renderer Feature code
Below is the complete code for the custom Renderer Feature script.
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class MyRendererFeature : ScriptableRendererFeature
{
[SerializeField] private Shader shader;
private Material material;
private RedTintRenderPass redTintRenderPass;
public override void Create()
{
if (shader == null)
{
return;
}
material = CoreUtils.CreateEngineMaterial(shader);
redTintRenderPass = new RedTintRenderPass(material);
redTintRenderPass.renderPassEvent = RenderPassEvent.AfterRenderingSkybox;
}
public override void AddRenderPasses(ScriptableRenderer renderer,
ref RenderingData renderingData)
{
if (renderingData.cameraData.cameraType == CameraType.Game)
{
renderer.EnqueuePass(redTintRenderPass);
}
}
public override void Dispose(bool disposing)
{
CoreUtils.Destroy(material);
}
}