RenderObjectsPass.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System.Collections.Generic;
  2. using UnityEngine.Rendering.Universal;
  3. using UnityEngine.Rendering;
  4. using UnityEngine.Scripting.APIUpdating;
  5. namespace UnityEngine.Experimental.Rendering.Universal
  6. {
  7. public class RenderObjectsPass : ScriptableRenderPass
  8. {
  9. RenderQueueType renderQueueType;
  10. FilteringSettings m_FilteringSettings;
  11. RenderObjects.CustomCameraSettings m_CameraSettings;
  12. string m_ProfilerTag;
  13. ProfilingSampler m_ProfilingSampler;
  14. public Material overrideMaterial { get; set; }
  15. public int overrideMaterialPassIndex { get; set; }
  16. List<ShaderTagId> m_ShaderTagIdList = new List<ShaderTagId>();
  17. public void SetDetphState(bool writeEnabled, CompareFunction function = CompareFunction.Less)
  18. {
  19. m_RenderStateBlock.mask |= RenderStateMask.Depth;
  20. m_RenderStateBlock.depthState = new DepthState(writeEnabled, function);
  21. }
  22. public void SetStencilState(int reference, CompareFunction compareFunction, StencilOp passOp, StencilOp failOp, StencilOp zFailOp)
  23. {
  24. StencilState stencilState = StencilState.defaultValue;
  25. stencilState.enabled = true;
  26. stencilState.SetCompareFunction(compareFunction);
  27. stencilState.SetPassOperation(passOp);
  28. stencilState.SetFailOperation(failOp);
  29. stencilState.SetZFailOperation(zFailOp);
  30. m_RenderStateBlock.mask |= RenderStateMask.Stencil;
  31. m_RenderStateBlock.stencilReference = reference;
  32. m_RenderStateBlock.stencilState = stencilState;
  33. }
  34. RenderStateBlock m_RenderStateBlock;
  35. public RenderObjectsPass(string profilerTag, RenderPassEvent renderPassEvent, string[] shaderTags, RenderQueueType renderQueueType, int layerMask, RenderObjects.CustomCameraSettings cameraSettings)
  36. {
  37. base.profilingSampler = new ProfilingSampler(nameof(RenderObjectsPass));
  38. m_ProfilerTag = profilerTag;
  39. m_ProfilingSampler = new ProfilingSampler(profilerTag);
  40. this.renderPassEvent = renderPassEvent;
  41. this.renderQueueType = renderQueueType;
  42. this.overrideMaterial = null;
  43. this.overrideMaterialPassIndex = 0;
  44. RenderQueueRange renderQueueRange = (renderQueueType == RenderQueueType.Transparent)
  45. ? RenderQueueRange.transparent
  46. : RenderQueueRange.opaque;
  47. m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
  48. if (shaderTags != null && shaderTags.Length > 0)
  49. {
  50. foreach (var passName in shaderTags)
  51. m_ShaderTagIdList.Add(new ShaderTagId(passName));
  52. }
  53. else
  54. {
  55. m_ShaderTagIdList.Add(new ShaderTagId("SRPDefaultUnlit"));
  56. m_ShaderTagIdList.Add(new ShaderTagId("UniversalForward"));
  57. m_ShaderTagIdList.Add(new ShaderTagId("UniversalForwardOnly"));
  58. }
  59. m_RenderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
  60. m_CameraSettings = cameraSettings;
  61. }
  62. internal RenderObjectsPass(URPProfileId profileId, RenderPassEvent renderPassEvent, string[] shaderTags, RenderQueueType renderQueueType, int layerMask, RenderObjects.CustomCameraSettings cameraSettings)
  63. : this(profileId.GetType().Name, renderPassEvent, shaderTags, renderQueueType, layerMask, cameraSettings)
  64. {
  65. m_ProfilingSampler = ProfilingSampler.Get(profileId);
  66. }
  67. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  68. {
  69. SortingCriteria sortingCriteria = (renderQueueType == RenderQueueType.Transparent)
  70. ? SortingCriteria.CommonTransparent
  71. : renderingData.cameraData.defaultOpaqueSortFlags;
  72. DrawingSettings drawingSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortingCriteria);
  73. drawingSettings.overrideMaterial = overrideMaterial;
  74. drawingSettings.overrideMaterialPassIndex = overrideMaterialPassIndex;
  75. ref CameraData cameraData = ref renderingData.cameraData;
  76. Camera camera = cameraData.camera;
  77. // In case of camera stacking we need to take the viewport rect from base camera
  78. Rect pixelRect = renderingData.cameraData.pixelRect;
  79. float cameraAspect = (float)pixelRect.width / (float)pixelRect.height;
  80. // NOTE: Do NOT mix ProfilingScope with named CommandBuffers i.e. CommandBufferPool.Get("name").
  81. // Currently there's an issue which results in mismatched markers.
  82. CommandBuffer cmd = CommandBufferPool.Get();
  83. using (new ProfilingScope(cmd, m_ProfilingSampler))
  84. {
  85. if (m_CameraSettings.overrideCamera)
  86. {
  87. if (cameraData.xr.enabled)
  88. {
  89. Debug.LogWarning("RenderObjects pass is configured to override camera matrices. While rendering in stereo camera matrices cannot be overridden.");
  90. }
  91. else
  92. {
  93. Matrix4x4 projectionMatrix = Matrix4x4.Perspective(m_CameraSettings.cameraFieldOfView, cameraAspect,
  94. camera.nearClipPlane, camera.farClipPlane);
  95. projectionMatrix = GL.GetGPUProjectionMatrix(projectionMatrix, cameraData.IsCameraProjectionMatrixFlipped());
  96. Matrix4x4 viewMatrix = cameraData.GetViewMatrix();
  97. Vector4 cameraTranslation = viewMatrix.GetColumn(3);
  98. viewMatrix.SetColumn(3, cameraTranslation + m_CameraSettings.offset);
  99. RenderingUtils.SetViewAndProjectionMatrices(cmd, viewMatrix, projectionMatrix, false);
  100. }
  101. }
  102. var activeDebugHandler = GetActiveDebugHandler(renderingData);
  103. if (activeDebugHandler != null)
  104. {
  105. activeDebugHandler.DrawWithDebugRenderState(context, cmd, ref renderingData, ref drawingSettings, ref m_FilteringSettings, ref m_RenderStateBlock,
  106. (ScriptableRenderContext ctx, ref RenderingData data, ref DrawingSettings ds, ref FilteringSettings fs, ref RenderStateBlock rsb) =>
  107. {
  108. ctx.DrawRenderers(data.cullResults, ref ds, ref fs, ref rsb);
  109. });
  110. }
  111. else
  112. {
  113. // Ensure we flush our command-buffer before we render...
  114. context.ExecuteCommandBuffer(cmd);
  115. cmd.Clear();
  116. // Render the objects...
  117. context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref m_FilteringSettings, ref m_RenderStateBlock);
  118. }
  119. if (m_CameraSettings.overrideCamera && m_CameraSettings.restoreCamera && !cameraData.xr.enabled)
  120. {
  121. RenderingUtils.SetViewAndProjectionMatrices(cmd, cameraData.GetViewMatrix(), cameraData.GetGPUProjectionMatrix(), false);
  122. }
  123. }
  124. context.ExecuteCommandBuffer(cmd);
  125. CommandBufferPool.Release(cmd);
  126. }
  127. }
  128. }