Skip to content

Latest commit

 

History

History
163 lines (118 loc) · 5.3 KB

File metadata and controls

163 lines (118 loc) · 5.3 KB

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

  1. Create a new C# script and name it MyRendererFeature.cs.

  2. In the script, remove the code that Unity inserted in the MyRendererFeature class.

  3. Add the following using directive:

    using UnityEngine.Rendering;
    using UnityEngine.Rendering.Universal;
  4. Create the MyRendererFeature class that inherits from the ScriptableRendererFeature class.

    public class MyRendererFeature : ScriptableRendererFeature    
  5. 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 inject ScriptableRenderPass 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.

  1. Declare the following fields:

    [SerializeField] private Shader shader;
    private Material material;
    private RedTintRenderPass redTintRenderPass;
  2. In the Create method, instantiate the RedTintRenderPass 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);
    
        redTintRenderPass.renderPassEvent = RenderPassEvent.AfterRenderingSkybox;
    }
  3. In the AddRenderPasses method, enqueue the render pass with the EnqueuePass 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)
        {
            Debug.LogError("Ensure that you've set a shader in the Scriptable Renderer Feature.");
            return;
        }
        material = CoreUtils.CreateEngineMaterial(shader);
        redTintRenderPass = new RedTintRenderPass(material);
        
        redTintRenderPass.renderPassEvent = RenderPassEvent.AfterRenderingSkybox;
    }

    public override void AddRenderPasses(ScriptableRenderer renderer,
        ref RenderingData renderingData)
    {
        if (redTintRenderPass != null &&
            renderingData.cameraData.cameraType == CameraType.Game)
        {
            renderer.EnqueuePass(redTintRenderPass);
        }
    }
    protected override void Dispose(bool disposing)
    {
        CoreUtils.Destroy(material);
    }
}