No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GbufferVisualizationRendererFeature.cs 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4. using UnityEngine.Rendering.RenderGraphModule;
  5. // This example uses the gBuffer components in a RenderPass when they are not global.
  6. // The RenderPass will by default show the contents of the Specular Metallic Texture (_GBuffer2) on geometry in the scene,
  7. // but you can change the sampled gBuffer component by modifying the example shader.
  8. // Make sure to (1) set the rendering path to Deferred and (2) add a 3D object in your scene to see a result.
  9. public class GbufferVisualizationRendererFeature : ScriptableRendererFeature
  10. {
  11. class GBufferVisualizationRenderPass : ScriptableRenderPass
  12. {
  13. Material m_Material;
  14. string m_PassName = "Visualize GBuffer Components (and make gBuffer global)";
  15. private static readonly int GbufferLightingIndex = 3;
  16. // Other gBuffer components indices
  17. // private static readonly int GBufferNormalSmoothnessIndex = 2;
  18. // private static readonly int GbufferDepthIndex = 4;
  19. // private static readonly int GBufferRenderingLayersIndex = 5;
  20. // Components marked as optional are only present when the pipeline requests it.
  21. // If for example there is no rendering layers texture, _GBuffer5 will contain the ShadowMask texture
  22. private static readonly int[] s_GBufferShaderPropertyIDs = new int[]
  23. {
  24. // Contains Albedo Texture
  25. Shader.PropertyToID("_GBuffer0"),
  26. // Contains Specular Metallic Texture
  27. Shader.PropertyToID("_GBuffer1"),
  28. // Contains Normals and Smoothness, referenced as _CameraNormalsTexture in other shaders
  29. Shader.PropertyToID("_GBuffer2"),
  30. // Contains Lighting texture
  31. Shader.PropertyToID("_GBuffer3"),
  32. // Contains Depth texture, referenced as _CameraDepthTexture in other shaders (optional)
  33. Shader.PropertyToID("_GBuffer4"),
  34. // Contains Rendering Layers Texture, referenced as _CameraRenderingLayersTexture in other shaders (optional)
  35. Shader.PropertyToID("_GBuffer5"),
  36. // Contains ShadowMask texture (optional)
  37. Shader.PropertyToID("_GBuffer6")
  38. };
  39. private class PassData
  40. {
  41. // In this example, we want to use the gBuffer components in our pass.
  42. public TextureHandle[] gBuffer;
  43. public Material material;
  44. }
  45. public void Setup(Material material)
  46. {
  47. m_Material = material;
  48. }
  49. // This method will draw the contents of the gBuffer component requested in the shader
  50. static void ExecutePass(PassData data, RasterGraphContext context)
  51. {
  52. // Here, we read all the gBuffer components as an example even though the shader only needs one.
  53. // We still need to set it explicitly since it is not accessible globally (so the
  54. // shader won't have access to it by default).
  55. for (int i = 0; i < data.gBuffer.Length; i++)
  56. {
  57. data.material.SetTexture(s_GBufferShaderPropertyIDs[i], data.gBuffer[i]);
  58. }
  59. // Draw the gBuffer component requested by the shader over the geometry
  60. context.cmd.DrawProcedural(Matrix4x4.identity, data.material, 0, MeshTopology.Triangles, 3, 1);
  61. }
  62. // RecordRenderGraph is where the RenderGraph handle can be accessed, through which render passes can be added to the graph.
  63. // FrameData is a context container through which URP resources can be accessed and managed.
  64. public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
  65. {
  66. UniversalRenderingData universalRenderingData = frameData.Get<UniversalRenderingData>();
  67. // The gBuffer components are only used in deferred mode
  68. if (m_Material == null || universalRenderingData.renderingMode != RenderingMode.Deferred)
  69. return;
  70. // Get the gBuffer texture handles stored in the resourceData
  71. UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
  72. TextureHandle[] gBuffer = resourceData.gBuffer;
  73. using (var builder = renderGraph.AddRasterRenderPass<PassData>(m_PassName, out var passData))
  74. {
  75. passData.material = m_Material;
  76. // For this pass, we want to write to the activeColorTexture, which is the gBuffer Lighting component (_GBuffer3) in the deferred path.
  77. builder.SetRenderAttachment(resourceData.activeColorTexture, 0, AccessFlags.Write);
  78. // We are reading the gBuffer components in our pass, so we need call UseTexture on them.
  79. // When they are global, they can be all read with builder.UseAllGlobalTexture(true), but
  80. // in this pass they are not global.
  81. for (int i = 0; i < resourceData.gBuffer.Length; i++)
  82. {
  83. if (i == GbufferLightingIndex)
  84. {
  85. // We already specify we are writing to it above (SetRenderAttachment)
  86. continue;
  87. }
  88. builder.UseTexture(resourceData.gBuffer[i]);
  89. }
  90. // We need to set the gBuffer in the pass' data, otherwise the pass won't have access to it when it is executed.
  91. passData.gBuffer = gBuffer;
  92. // Assigns the ExecutePass function to the render pass delegate. This will be called by the render graph when executing the pass.
  93. builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context));
  94. }
  95. }
  96. }
  97. GBufferVisualizationRenderPass m_GBufferRenderPass;
  98. public Material m_Material;
  99. /// <inheritdoc/>
  100. public override void Create()
  101. {
  102. m_GBufferRenderPass = new GBufferVisualizationRenderPass
  103. {
  104. // This pass must be injected after rendering the deferred lights or later.
  105. renderPassEvent = RenderPassEvent.AfterRenderingDeferredLights
  106. };
  107. }
  108. public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
  109. {
  110. // The gBuffers are only used in the Deferred rendering path
  111. if (m_Material != null)
  112. {
  113. m_GBufferRenderPass.Setup(m_Material);
  114. renderer.EnqueuePass(m_GBufferRenderPass);
  115. }
  116. }
  117. }