DrawObjectsPass.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections.Generic;
  2. namespace UnityEngine.Rendering.Universal.Internal
  3. {
  4. /// <summary>
  5. /// Draw objects into the given color and depth target
  6. ///
  7. /// You can use this pass to render objects that have a material and/or shader
  8. /// with the pass names UniversalForward or SRPDefaultUnlit.
  9. /// </summary>
  10. public class DrawObjectsPass : ScriptableRenderPass
  11. {
  12. FilteringSettings m_FilteringSettings;
  13. RenderStateBlock m_RenderStateBlock;
  14. List<ShaderTagId> m_ShaderTagIdList = new List<ShaderTagId>();
  15. string m_ProfilerTag;
  16. ProfilingSampler m_ProfilingSampler;
  17. bool m_IsOpaque;
  18. public DrawObjectsPass(string profilerTag, bool opaque, RenderPassEvent evt, RenderQueueRange renderQueueRange, LayerMask layerMask, StencilState stencilState, int stencilReference)
  19. {
  20. m_ProfilerTag = profilerTag;
  21. m_ProfilingSampler = new ProfilingSampler(profilerTag);
  22. m_ShaderTagIdList.Add(new ShaderTagId("UniversalForward"));
  23. m_ShaderTagIdList.Add(new ShaderTagId("LightweightForward"));
  24. m_ShaderTagIdList.Add(new ShaderTagId("SRPDefaultUnlit"));
  25. renderPassEvent = evt;
  26. m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
  27. m_RenderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
  28. m_IsOpaque = opaque;
  29. if (stencilState.enabled)
  30. {
  31. m_RenderStateBlock.stencilReference = stencilReference;
  32. m_RenderStateBlock.mask = RenderStateMask.Stencil;
  33. m_RenderStateBlock.stencilState = stencilState;
  34. }
  35. }
  36. /// <inheritdoc/>
  37. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  38. {
  39. CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
  40. using (new ProfilingScope(cmd, m_ProfilingSampler))
  41. {
  42. context.ExecuteCommandBuffer(cmd);
  43. cmd.Clear();
  44. Camera camera = renderingData.cameraData.camera;
  45. var sortFlags = (m_IsOpaque) ? renderingData.cameraData.defaultOpaqueSortFlags : SortingCriteria.CommonTransparent;
  46. var drawSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortFlags);
  47. context.DrawRenderers(renderingData.cullResults, ref drawSettings, ref m_FilteringSettings, ref m_RenderStateBlock);
  48. // Render objects that did not match any shader pass with error shader
  49. RenderingUtils.RenderObjectsWithError(context, ref renderingData.cullResults, camera, m_FilteringSettings, SortingCriteria.None);
  50. }
  51. context.ExecuteCommandBuffer(cmd);
  52. CommandBufferPool.Release(cmd);
  53. }
  54. }
  55. }