Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Rendering.RenderGraphModule;
  4. using UnityEngine.Rendering;
  5. using UnityEngine.Rendering.RendererUtils;
  6. using UnityEngine.Rendering.Universal;
  7. // This example clears the current active color texture, then renders the scene geometry associated to the m_LayerMask layer.
  8. // Add scene geometry to your own custom layers and experiment switching the layer mask in the render feature UI.
  9. // You can use the frame debugger to inspect the pass output.
  10. public class RendererListRenderFeature : ScriptableRendererFeature
  11. {
  12. class RendererListPass : ScriptableRenderPass
  13. {
  14. // Layer mask used to filter objects to put in the renderer list
  15. private LayerMask m_LayerMask;
  16. // List of shader tags used to build the renderer list
  17. private List<ShaderTagId> m_ShaderTagIdList = new List<ShaderTagId>();
  18. public RendererListPass(LayerMask layerMask)
  19. {
  20. m_LayerMask = layerMask;
  21. }
  22. // This class stores the data needed by the pass, passed as parameter to the delegate function that executes the pass
  23. private class PassData
  24. {
  25. public RendererListHandle rendererListHandle;
  26. }
  27. // Sample utility method that showcases how to create a renderer list via the RenderGraph API
  28. private void InitRendererLists(ContextContainer frameData, ref PassData passData, RenderGraph renderGraph)
  29. {
  30. // Access the relevant frame data from the Universal Render Pipeline
  31. UniversalRenderingData universalRenderingData = frameData.Get<UniversalRenderingData>();
  32. UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
  33. UniversalLightData lightData = frameData.Get<UniversalLightData>();
  34. var sortFlags = cameraData.defaultOpaqueSortFlags;
  35. RenderQueueRange renderQueueRange = RenderQueueRange.opaque;
  36. FilteringSettings filterSettings = new FilteringSettings(renderQueueRange, m_LayerMask);
  37. ShaderTagId[] forwardOnlyShaderTagIds = new ShaderTagId[]
  38. {
  39. new ShaderTagId("UniversalForwardOnly"),
  40. new ShaderTagId("UniversalForward"),
  41. new ShaderTagId("SRPDefaultUnlit"), // Legacy shaders (do not have a gbuffer pass) are considered forward-only for backward compatibility
  42. new ShaderTagId("LightweightForward") // Legacy shaders (do not have a gbuffer pass) are considered forward-only for backward compatibility
  43. };
  44. m_ShaderTagIdList.Clear();
  45. foreach (ShaderTagId sid in forwardOnlyShaderTagIds)
  46. m_ShaderTagIdList.Add(sid);
  47. DrawingSettings drawSettings = RenderingUtils.CreateDrawingSettings(m_ShaderTagIdList, universalRenderingData, cameraData, lightData, sortFlags);
  48. var param = new RendererListParams(universalRenderingData.cullResults, drawSettings, filterSettings);
  49. passData.rendererListHandle = renderGraph.CreateRendererList(param);
  50. }
  51. // This static method is used to execute the pass and passed as the RenderFunc delegate to the RenderGraph render pass
  52. static void ExecutePass(PassData data, RasterGraphContext context)
  53. {
  54. context.cmd.ClearRenderTarget(RTClearFlags.Color, Color.green, 1,0);
  55. context.cmd.DrawRendererList(data.rendererListHandle);
  56. }
  57. // This is where the renderGraph handle can be accessed.
  58. // Each ScriptableRenderPass can use the RenderGraph handle to add multiple render passes to the render graph
  59. public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
  60. {
  61. string passName = "RenderList Render Pass";
  62. // This simple pass clears the current active color texture, then renders the scene geometry associated to the m_LayerMask layer.
  63. // Add scene geometry to your own custom layers and experiment switching the layer mask in the render feature UI.
  64. // You can use the frame debugger to inspect the pass output
  65. // add a raster render pass to the render graph, specifying the name and the data type that will be passed to the ExecutePass function
  66. using (var builder = renderGraph.AddRasterRenderPass<PassData>(passName, out var passData))
  67. {
  68. // UniversalResourceData contains all the texture handles used by the renderer, including the active color and depth textures
  69. // The active color and depth textures are the main color and depth buffers that the camera renders into
  70. UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
  71. // Fill up the passData with the data needed by the pass
  72. InitRendererLists(frameData, ref passData, renderGraph);
  73. // Make sure the renderer list is valid
  74. //if (!passData.rendererListHandle.IsValid())
  75. // return;
  76. // We declare the RendererList we just created as an input dependency to this pass, via UseRendererList()
  77. builder.UseRendererList(passData.rendererListHandle);
  78. // Setup as a render target via UseTextureFragment and UseTextureFragmentDepth, which are the equivalent of using the old cmd.SetRenderTarget(color,depth)
  79. builder.SetRenderAttachment(resourceData.activeColorTexture, 0);
  80. builder.SetRenderAttachmentDepth(resourceData.activeDepthTexture, AccessFlags.Write);
  81. // Assign the ExecutePass function to the render pass delegate, which will be called by the render graph when executing the pass
  82. builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context));
  83. builder.SetRenderFunc<PassData>(ExecutePass);
  84. }
  85. }
  86. }
  87. RendererListPass m_ScriptablePass;
  88. public LayerMask m_LayerMask;
  89. /// <inheritdoc/>
  90. public override void Create()
  91. {
  92. m_ScriptablePass = new RendererListPass(m_LayerMask);
  93. // Configures where the render pass should be injected.
  94. m_ScriptablePass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
  95. }
  96. // Here you can inject one or multiple render passes in the renderer.
  97. // This method is called when setting up the renderer once per-camera.
  98. public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
  99. {
  100. renderer.EnqueuePass(m_ScriptablePass);
  101. }
  102. }