Last active
December 31, 2023 19:11
-
-
Save IwakuraRein/a80dcb739741df71953979a09e313125 to your computer and use it in GitHub Desktop.
URP Exapmle Depth Only Pass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Rendering.Universal; | |
using UnityEngine.Rendering; | |
/// <summary> | |
/// Render all meshes of a certain layer mask to a custom depth buffer | |
/// </summary> | |
public class MyDepthPass : ScriptableRenderPass | |
{ | |
LayerMask layerMask; | |
int MyDepthBufferID = Shader.PropertyToID("_MyDepthBuffer"); | |
FilteringSettings filteringSettings; | |
List<ShaderTagId> shaderTagIdList = new List<ShaderTagId>(); | |
RTHandle myDepthTex; | |
public MyDepthPass(RenderPassEvent renderPassEvent, LayerMask layerMask) | |
{ | |
this.renderPassEvent = renderPassEvent; | |
this.layerMask = layerMask; | |
filteringSettings = new FilteringSettings(RenderQueueRange.all, layerMask); | |
shaderTagIdList.Add(new ShaderTagId("DepthOnly")); // Only render DepthOnly pass | |
} | |
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData) | |
{ | |
RenderTextureDescriptor descriptor = renderingData.cameraData.cameraTargetDescriptor; | |
// descriptor.colorFormat = RenderTextureFormat.RFloat; | |
// descriptor.depthBufferBits = 0; | |
descriptor.colorFormat = RenderTextureFormat.Depth; | |
descriptor.msaaSamples = 1; | |
RenderingUtils.ReAllocateIfNeeded(ref myDepthTex, Vector2.one, descriptor, FilterMode.Point, TextureWrapMode.Clamp, name: "_MyDepthBuffer"); | |
ConfigureTarget(myDepthTex, myDepthTex); // When disabling color target, pass two depth RTHandle. | |
ConfigureClear(ClearFlag.All, Color.black); | |
} | |
public override void OnCameraCleanup(CommandBuffer cmd) | |
{ | |
// no need to release when using RTHandle | |
//cmd.ReleaseTemporaryRT(MyDepthBufferID); | |
} | |
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) | |
{ | |
SortingCriteria sortingCriteria = SortingCriteria.None; | |
DrawingSettings drawingSettings = CreateDrawingSettings(shaderTagIdList, ref renderingData, sortingCriteria); | |
drawingSettings.perObjectData = PerObjectData.None; | |
CommandBuffer cmd = CommandBufferPool.Get(); | |
using (new ProfilingScope(cmd, new ProfilingSampler("Draw My Depth"))) | |
{ | |
context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref filteringSettings); | |
cmd.SetGlobalTexture(Shader.PropertyToID("_MyDepthBuffer"), myDepthTex); | |
} | |
context.ExecuteCommandBuffer(cmd); | |
CommandBufferPool.Release(cmd); | |
} | |
public void Dispose() | |
{ | |
myDepthTex?.Release(); | |
} | |
} | |
public class MyDepthRendererFeature : ScriptableRendererFeature | |
{ | |
public LayerMask Layer; | |
public RenderPassEvent InjectionPoint = RenderPassEvent.BeforeRenderingPrePasses; | |
MyDepthPass renderPass = null; | |
public override void AddRenderPasses(ScriptableRenderer renderer, | |
ref RenderingData renderingData) | |
{ | |
if (renderingData.cameraData.cameraType == CameraType.Game) | |
renderer.EnqueuePass(renderPass); | |
} | |
public override void Create() | |
{ | |
renderPass = new MyDepthPass(InjectionPoint, Layer); | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
renderPass.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment