RenderObjectsPass.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. [MovedFrom("UnityEngine.Experimental.Rendering.LWRP")] 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. m_ProfilerTag = profilerTag;
  38. m_ProfilingSampler = new ProfilingSampler(profilerTag);
  39. this.renderPassEvent = renderPassEvent;
  40. this.renderQueueType = renderQueueType;
  41. this.overrideMaterial = null;
  42. this.overrideMaterialPassIndex = 0;
  43. RenderQueueRange renderQueueRange = (renderQueueType == RenderQueueType.Transparent)
  44. ? RenderQueueRange.transparent
  45. : RenderQueueRange.opaque;
  46. m_FilteringSettings = new FilteringSettings(renderQueueRange, layerMask);
  47. if (shaderTags != null && shaderTags.Length > 0)
  48. {
  49. foreach (var passName in shaderTags)
  50. m_ShaderTagIdList.Add(new ShaderTagId(passName));
  51. }
  52. else
  53. {
  54. m_ShaderTagIdList.Add(new ShaderTagId("UniversalForward"));
  55. m_ShaderTagIdList.Add(new ShaderTagId("LightweightForward"));
  56. m_ShaderTagIdList.Add(new ShaderTagId("SRPDefaultUnlit"));
  57. }
  58. m_RenderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
  59. m_CameraSettings = cameraSettings;
  60. }
  61. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  62. {
  63. SortingCriteria sortingCriteria = (renderQueueType == RenderQueueType.Transparent)
  64. ? SortingCriteria.CommonTransparent
  65. : renderingData.cameraData.defaultOpaqueSortFlags;
  66. DrawingSettings drawingSettings = CreateDrawingSettings(m_ShaderTagIdList, ref renderingData, sortingCriteria);
  67. drawingSettings.overrideMaterial = overrideMaterial;
  68. drawingSettings.overrideMaterialPassIndex = overrideMaterialPassIndex;
  69. ref CameraData cameraData = ref renderingData.cameraData;
  70. Camera camera = cameraData.camera;
  71. // In case of camera stacking we need to take the viewport rect from base camera
  72. Rect pixelRect = renderingData.cameraData.pixelRect;
  73. float cameraAspect = (float) pixelRect.width / (float) pixelRect.height;
  74. CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
  75. using (new ProfilingScope(cmd, m_ProfilingSampler))
  76. {
  77. context.ExecuteCommandBuffer(cmd);
  78. cmd.Clear();
  79. if (m_CameraSettings.overrideCamera)
  80. {
  81. Matrix4x4 projectionMatrix = Matrix4x4.Perspective(m_CameraSettings.cameraFieldOfView, cameraAspect,
  82. camera.nearClipPlane, camera.farClipPlane);
  83. Matrix4x4 viewMatrix = camera.worldToCameraMatrix;
  84. Vector4 cameraTranslation = viewMatrix.GetColumn(3);
  85. viewMatrix.SetColumn(3, cameraTranslation + m_CameraSettings.offset);
  86. cmd.SetViewProjectionMatrices(viewMatrix, projectionMatrix);
  87. context.ExecuteCommandBuffer(cmd);
  88. }
  89. context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref m_FilteringSettings,
  90. ref m_RenderStateBlock);
  91. if (m_CameraSettings.overrideCamera && m_CameraSettings.restoreCamera)
  92. {
  93. cmd.Clear();
  94. cmd.SetViewProjectionMatrices(cameraData.viewMatrix, cameraData.projectionMatrix);
  95. }
  96. }
  97. context.ExecuteCommandBuffer(cmd);
  98. CommandBufferPool.Release(cmd);
  99. }
  100. }
  101. }