Guest User

ScriptableRenderPass

a guest
Aug 27th, 2019
519
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. using UnityEngine.Rendering.Universal;
  5.  
  6.  
  7. public class DepthNormalsPass : ScriptableRenderPass
  8. {
  9. int kDepthBufferBits = 32;
  10. private RenderTargetHandle depthAttachmentHandle { get; set; }
  11. internal RenderTextureDescriptor descriptor { get; private set; }
  12.  
  13. private FilteringSettings m_FilteringSettings;
  14. string m_ProfilerTag = "DepthNormals Prepass";
  15. ShaderTagId m_ShaderTagId = new ShaderTagId("DepthOnly");
  16.  
  17. private Material depthNormalsMaterial = null;
  18.  
  19. public DepthNormalsPass(RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask)
  20. {
  21. m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
  22. renderPassEvent = evt;
  23. }
  24.  
  25.  
  26. public void Setup(RenderTextureDescriptor baseDescriptor, RenderTargetHandle depthAttachmentHandle)
  27. {
  28. this.depthAttachmentHandle = depthAttachmentHandle;
  29. baseDescriptor.colorFormat = RenderTextureFormat.ARGB32;
  30. baseDescriptor.depthBufferBits = kDepthBufferBits;
  31. descriptor = baseDescriptor;
  32.  
  33. depthNormalsMaterial =
  34. CoreUtils.CreateEngineMaterial("Hidden/Internal-DepthNormalsTexture");
  35. }
  36.  
  37.  
  38. public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  39. {
  40. cmd.GetTemporaryRT(depthAttachmentHandle.id, descriptor, FilterMode.Point);
  41. ConfigureTarget(depthAttachmentHandle.Identifier());
  42. ConfigureClear(ClearFlag.All, Color.black);
  43. }
  44.  
  45.  
  46. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  47. {
  48. CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
  49.  
  50. using (new ProfilingSample(cmd, m_ProfilerTag))
  51. {
  52. context.ExecuteCommandBuffer(cmd);
  53. cmd.Clear();
  54.  
  55. var sortFlags = renderingData.cameraData.defaultOpaqueSortFlags;
  56. var drawSettings = CreateDrawingSettings(m_ShaderTagId, ref renderingData, sortFlags);
  57. drawSettings.perObjectData = PerObjectData.None;
  58.  
  59.  
  60. ref CameraData cameraData = ref renderingData.cameraData;
  61. Camera camera = cameraData.camera;
  62. if (cameraData.isStereoEnabled)
  63. context.StartMultiEye(camera);
  64.  
  65.  
  66. drawSettings.overrideMaterial = depthNormalsMaterial;
  67.  
  68. context.DrawRenderers(renderingData.cullResults, ref drawSettings,
  69. ref m_FilteringSettings);
  70.  
  71. cmd.SetGlobalTexture("_CameraDepthNormalsTexture", depthAttachmentHandle.id);
  72. }
  73.  
  74. context.ExecuteCommandBuffer(cmd);
  75. CommandBufferPool.Release(cmd);
  76. }
  77.  
  78. public void SetLayerMask(LayerMask mask)
  79. {
  80. m_FilteringSettings.layerMask = mask;
  81. }
  82.  
  83. public override void FrameCleanup(CommandBuffer cmd)
  84. {
  85. if (cmd == null)
  86. throw new ArgumentNullException("cmd");
  87.  
  88. if (depthAttachmentHandle != RenderTargetHandle.CameraTarget)
  89. {
  90. cmd.ReleaseTemporaryRT(depthAttachmentHandle.id);
  91. depthAttachmentHandle = RenderTargetHandle.CameraTarget;
  92. }
  93. }
  94. }
RAW Paste Data