ScreenSpaceShadowResolvePass.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. namespace UnityEngine.Rendering.Universal.Internal
  3. {
  4. /// <summary>
  5. /// Resolves shadows in a screen space texture.
  6. /// </summary>
  7. public class ScreenSpaceShadowResolvePass : ScriptableRenderPass
  8. {
  9. Material m_ScreenSpaceShadowsMaterial;
  10. RenderTargetHandle m_ScreenSpaceShadowmap;
  11. RenderTextureDescriptor m_RenderTextureDescriptor;
  12. const string m_ProfilerTag = "Resolve Shadows";
  13. public ScreenSpaceShadowResolvePass(RenderPassEvent evt, Material screenspaceShadowsMaterial)
  14. {
  15. m_ScreenSpaceShadowsMaterial = screenspaceShadowsMaterial;
  16. m_ScreenSpaceShadowmap.Init("_ScreenSpaceShadowmapTexture");
  17. renderPassEvent = evt;
  18. }
  19. public void Setup(RenderTextureDescriptor baseDescriptor)
  20. {
  21. m_RenderTextureDescriptor = baseDescriptor;
  22. m_RenderTextureDescriptor.depthBufferBits = 0;
  23. m_RenderTextureDescriptor.msaaSamples = 1;
  24. m_RenderTextureDescriptor.colorFormat = RenderingUtils.SupportsRenderTextureFormat(RenderTextureFormat.R8)
  25. ? RenderTextureFormat.R8
  26. : RenderTextureFormat.ARGB32;
  27. }
  28. public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
  29. {
  30. cmd.GetTemporaryRT(m_ScreenSpaceShadowmap.id, m_RenderTextureDescriptor, FilterMode.Bilinear);
  31. RenderTargetIdentifier screenSpaceOcclusionTexture = m_ScreenSpaceShadowmap.Identifier();
  32. ConfigureTarget(screenSpaceOcclusionTexture);
  33. ConfigureClear(ClearFlag.All, Color.white);
  34. }
  35. /// <inheritdoc/>
  36. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  37. {
  38. if (m_ScreenSpaceShadowsMaterial == null)
  39. {
  40. Debug.LogErrorFormat("Missing {0}. {1} render pass will not execute. Check for missing reference in the renderer resources.", m_ScreenSpaceShadowsMaterial, GetType().Name);
  41. return;
  42. }
  43. if (renderingData.lightData.mainLightIndex == -1)
  44. return;
  45. Camera camera = renderingData.cameraData.camera;
  46. bool stereo = renderingData.cameraData.isStereoEnabled;
  47. CommandBuffer cmd = CommandBufferPool.Get(m_ProfilerTag);
  48. if (!stereo)
  49. {
  50. cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
  51. cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, m_ScreenSpaceShadowsMaterial);
  52. cmd.SetViewProjectionMatrices(camera.worldToCameraMatrix, camera.projectionMatrix);
  53. }
  54. else
  55. {
  56. // Avoid setting and restoring camera view and projection matrices when in stereo.
  57. RenderTargetIdentifier screenSpaceOcclusionTexture = m_ScreenSpaceShadowmap.Identifier();
  58. Blit(cmd, screenSpaceOcclusionTexture, screenSpaceOcclusionTexture, m_ScreenSpaceShadowsMaterial);
  59. }
  60. context.ExecuteCommandBuffer(cmd);
  61. CommandBufferPool.Release(cmd);
  62. }
  63. /// <inheritdoc/>
  64. public override void FrameCleanup(CommandBuffer cmd)
  65. {
  66. if (cmd == null)
  67. throw new ArgumentNullException("cmd");
  68. cmd.ReleaseTemporaryRT(m_ScreenSpaceShadowmap.id);
  69. }
  70. }
  71. }