Skip to content

Instantly share code, notes, and snippets.

@elringus
Last active September 27, 2024 03:44
Show Gist options
  • Save elringus/69f0da9c71306f1ea0f575cc7568b31a to your computer and use it in GitHub Desktop.
Save elringus/69f0da9c71306f1ea0f575cc7568b31a to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

Thanks a lot! This is very usefull

@virtushda
Copy link

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

@ProblemMachine
Copy link

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
Copy link
Author

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
Copy link

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

@elringus
Copy link
Author

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

Indeed, that works. Thanks for sharing!

@AlwinJoshy
Copy link

has been very helpful.
thanks a lot.

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