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.CameraTarget, 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 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 :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.