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

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