Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example for adding custom render passes via renderer features for lightweight render pipeline (LWRP)
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.LWRP;
// Inheriting from `ScriptableRendererFeature` will add it to the
// `Renderer Features` list of the custom LWRP renderer data asset.
public class RenderMyCustomPass : ScriptableRendererFeature
{
private class MyCustomPass : ScriptableRenderPass
{
// Just a tag used to pick up a buffer from the pool.
private const string commandBufferName = nameof(MyCustomPass);
// Corresponds to `Tags { "LightMode" = "MyCustomPass" }` in the shaders.
// You have to add this tag for the corresponding shaders to associate them with this pass.
private static readonly ShaderTagId shaderTag = new ShaderTagId(nameof(MyCustomPass));
// An arbitrary name to store temporary render texture.
private static readonly int tempRTPropertyId = Shader.PropertyToID("_TempRT");
// Name of the grab texture used in the shaders.
private static readonly int grabTexturePropertyId = Shader.PropertyToID("_MyGrabTexture");
public MyCustomPass ()
{
renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
}
public override void Execute (ScriptableRenderContext context, ref RenderingData renderingData)
{
// Grab screen texture and assign it to a global texture property.
var cmd = CommandBufferPool.Get(commandBufferName);
cmd.GetTemporaryRT(tempRTPropertyId, renderingData.cameraData.cameraTargetDescriptor);
cmd.Blit(BuiltinRenderTextureType.RenderTexture, tempRTPropertyId);
cmd.SetGlobalTexture(grabTexturePropertyId, tempRTPropertyId);
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
CommandBufferPool.Release(cmd);
// Draw the objects that are using materials associated with this pass.
var drawingSettings = CreateDrawingSettings(shaderTag, ref renderingData, SortingCriteria.CommonTransparent);
var filteringSettings = new FilteringSettings(RenderQueueRange.transparent);
context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref filteringSettings);
}
public override void FrameCleanup (CommandBuffer cmd)
{
base.FrameCleanup(cmd);
cmd.ReleaseTemporaryRT(tempRTPropertyId);
}
}
private MyCustomPass grabScreenPass;
public override void Create ()
{
grabScreenPass = new MyCustomPass();
}
public override void AddRenderPasses (ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(grabScreenPass);
}
}
@Elringus

This comment has been minimized.

Copy link
Owner Author

@Elringus Elringus commented May 28, 2019

How to use

  1. Drop the script to the project;
  2. Create ForwardRendererData asset with Create -> Rendering -> Lightweight Render Pipeline -> Forward Renderer;
  3. Select the created asset and add our custom renderer feature:
  4. Override the renderer type used in LWRP asset with the created ForwardRendererData asset :
@antoinefortin

This comment has been minimized.

Copy link

@antoinefortin antoinefortin commented Jul 10, 2020

Thanks a lot! This is very usefull

@virtushda

This comment has been minimized.

Copy link

@virtushda virtushda commented Sep 9, 2020

You've saved us from Unity's terrible documentation, thank you!!! <3

@ProblemMachine

This comment has been minimized.

Copy link

@ProblemMachine ProblemMachine commented Dec 22, 2020

I've used this to develop a special effect and it works perfectly in the game view but fails to render at all in the scene view. Is this normal? Any idea what might be going on?

Other than that issue this is perfect, though, thanks!

@Elringus

This comment has been minimized.

Copy link
Owner Author

@Elringus Elringus commented Dec 23, 2020

I've used this to develop a special effect and it works perfectly in the game view but fails to render at all in the scene view. Is this normal? Any idea what might be going on?

Other than that issue this is perfect, though, thanks!

I remember it wasn't drawing in scene for me as well. Not sure if it's a bug or I'm missing something.

@ProblemMachine

This comment has been minimized.

Copy link

@ProblemMachine ProblemMachine commented Dec 27, 2020

Changing "BuiltinRenderTextureType.CameraTarget" to "BuiltinRenderTextureType.RenderTexture" seems to have fixed it

@Elringus

This comment has been minimized.

Copy link
Owner Author

@Elringus Elringus commented Dec 27, 2020

Changing "BuiltinRenderTextureType.CameraTarget" to "BuiltinRenderTextureType.RenderTexture" seems to have fixed it

Indeed, that works. Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment