Skip to content

Instantly share code, notes, and snippets.

@alexanderameye
Created July 15, 2022 15:05
Show Gist options
  • Save alexanderameye/74f3fc87efc162607b5bde1c365c1bbc to your computer and use it in GitHub Desktop.
Save alexanderameye/74f3fc87efc162607b5bde1c365c1bbc to your computer and use it in GitHub Desktop.
ScriptableRendererFeature templae
// ScriptableRendererFeature template created for URP 12 and Unity 2022.2
// Made by Alexander Ameye
// https://alexanderameye.github.io/
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class MyScriptableRendererFeature : ScriptableRendererFeature
{
class MyScriptableRenderPass : ScriptableRenderPass
{
// The profiler tag that will show up in the frame debugger.
const string ProfilerTag = "Template Pass";
// We will store our pass settings in this variable.
MyScriptableRenderPassSettings settings;
public MyScriptableRenderPass(MyScriptableRenderPassSettings settings)
{
this.settings = settings;
// Set the render pass event.
renderPassEvent = settings.renderPassEvent;
}
// Gets called by the renderer before executing the pass.
// Can be used to configure render targets and their clearing state.
// Can be user to create temporary render target textures.
// If this method is not overriden, the render pass will render to the active camera render target.
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
}
// The actual execution of the pass. This is where custom rendering occurs.
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
// Grab a command buffer. We put the actual execution of the pass inside of a profiling scope.
CommandBuffer cmd = CommandBufferPool.Get();
using (new ProfilingScope(cmd, new ProfilingSampler(ProfilerTag)))
{
// rendering code
}
// Execute the command buffer and release it.
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
// Called when the camera has finished rendering.
// Here we release/cleanup any allocated resources that were created by this pass.
// Gets called for all cameras i na camera stack.
public override void OnCameraCleanup(CommandBuffer cmd)
{
}
}
// Settings for the pass.
[System.Serializable]
public class MyScriptableRenderPassSettings
{
// Where/when the render pass should be injected during the rendering process.
public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
// Used for any potential down-sampling we will do in the pass.
[Range(1,4)] public int downsample = 1;
// A variable that's specific to the use case of our pass.
[Range(0, 20)] public int blurStrength = 5;
// additional properties ...
}
// References to our pass and its settings.
MyScriptableRenderPass pass;
public MyScriptableRenderPassSettings passSettings = new();
// Gets called every time serialization happens.
// Gets called when you enable/disable the renderer feature.
// Gets called when you change a property in the inspector of the renderer feature.
public override void Create()
{
// Pass the settings as a parameter to the constructor of the pass.
pass = new MyScriptableRenderPass(passSettings);
}
// Injects one or multiple render passes in the renderer.
// Gets called when setting up the renderer, once per-camera.
// Gets called every frame, once per-camera.
// Will not be called if the renderer feature is disabled in the renderer inspector.
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
// Here you can queue up multiple passes after each other.
renderer.EnqueuePass(pass);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment