Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DrawShadow2DPass.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Experimental.Rendering;
  4. using UnityEngine.Rendering.RenderGraphModule;
  5. using CommonResourceData = UnityEngine.Rendering.Universal.UniversalResourceData;
  6. namespace UnityEngine.Rendering.Universal
  7. {
  8. internal class DrawShadow2DPass : ScriptableRenderPass
  9. {
  10. static readonly string k_ShadowPass = "Shadow2D UnsafePass";
  11. static readonly string k_ShadowVolumetricPass = "Shadow2D Volumetric UnsafePass";
  12. private static readonly ProfilingSampler m_ProfilingSampler = new ProfilingSampler(k_ShadowPass);
  13. private static readonly ProfilingSampler m_ProfilingSamplerVolume = new ProfilingSampler(k_ShadowVolumetricPass);
  14. private static readonly ProfilingSampler m_ExecuteProfilingSampler = new ProfilingSampler("Draw Shadow");
  15. private static readonly ProfilingSampler m_ExecuteLightProfilingSampler = new ProfilingSampler("Draw Light");
  16. TextureHandle[] intermediateTexture = new TextureHandle[1];
  17. static List<Light2D> intermediateLight = new List<Light2D>(1);
  18. [Obsolete(DeprecationMessage.CompatibilityScriptingAPIObsolete, false)]
  19. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  20. {
  21. throw new NotImplementedException();
  22. }
  23. private static void ExecuteShadowPass(UnsafeCommandBuffer cmd, DrawLight2DPass.PassData passData, Light2D light)
  24. {
  25. using (new ProfilingScope(cmd, m_ExecuteProfilingSampler))
  26. {
  27. cmd.SetRenderTarget(passData.shadowMap, passData.shadowDepth);
  28. cmd.ClearRenderTarget(RTClearFlags.All, Color.clear, 1, 0);
  29. var projectedShadowMaterial = passData.rendererData.GetProjectedShadowMaterial();
  30. var projectedUnshadowMaterial = passData.rendererData.GetProjectedUnshadowMaterial();
  31. projectedShadowMaterial.SetTexture(DrawLight2DPass.k_FalloffLookupID, passData.fallOffLookUp);
  32. projectedUnshadowMaterial.SetTexture(DrawLight2DPass.k_FalloffLookupID, passData.fallOffLookUp);
  33. ShadowRendering.PrerenderShadows(cmd, passData.rendererData, ref passData.layerBatch, light, 0, light.shadowIntensity);
  34. }
  35. }
  36. public void Render(RenderGraph graph, ContextContainer frameData, Renderer2DData rendererData, ref LayerBatch layerBatch, int batchIndex, bool isVolumetric = false)
  37. {
  38. Universal2DResourceData universal2DResourceData = frameData.Get<Universal2DResourceData>();
  39. CommonResourceData commonResourceData = frameData.Get<CommonResourceData>();
  40. if (!layerBatch.lightStats.useShadows ||
  41. isVolumetric && !layerBatch.lightStats.useVolumetricShadowLights)
  42. return;
  43. var shadowTexture = universal2DResourceData.shadowsTexture;
  44. var depthTexture = universal2DResourceData.shadowsDepth;
  45. using (var builder = graph.AddUnsafePass<DrawLight2DPass.PassData>(!isVolumetric ? k_ShadowPass : k_ShadowVolumetricPass, out var passData, !isVolumetric ? m_ProfilingSampler : m_ProfilingSamplerVolume))
  46. {
  47. passData.layerBatch = layerBatch;
  48. passData.rendererData = rendererData;
  49. passData.isVolumetric = isVolumetric;
  50. passData.shadowMap = shadowTexture;
  51. passData.shadowDepth = depthTexture;
  52. passData.normalMap = layerBatch.lightStats.useNormalMap ? universal2DResourceData.normalsTexture[batchIndex] : TextureHandle.nullHandle;
  53. passData.fallOffLookUp = graph.ImportTexture(DrawLight2DPass.m_FallOffRTHandle);
  54. passData.lightLookUp = graph.ImportTexture(DrawLight2DPass.m_LightLookupRTHandle);
  55. if (!isVolumetric)
  56. {
  57. passData.lightTextures = universal2DResourceData.lightTextures[batchIndex];
  58. passData.depthTexture = universal2DResourceData.intermediateDepth;
  59. builder.UseTexture(passData.depthTexture, AccessFlags.Write);
  60. }
  61. else
  62. {
  63. intermediateTexture[0] = commonResourceData.activeColorTexture;
  64. passData.lightTextures = intermediateTexture;
  65. }
  66. if (passData.lightTexturesRT == null || passData.lightTexturesRT.Length != passData.lightTextures.Length)
  67. passData.lightTexturesRT = new RenderTargetIdentifier[passData.lightTextures.Length];
  68. for (int i = 0; i < passData.lightTextures.Length; ++i)
  69. builder.UseTexture(passData.lightTextures[i], AccessFlags.Write);
  70. if (layerBatch.lightStats.useNormalMap)
  71. builder.UseTexture(universal2DResourceData.normalsTexture[batchIndex]);
  72. builder.UseTexture(shadowTexture, AccessFlags.Write);
  73. builder.UseTexture(depthTexture, AccessFlags.Write);
  74. builder.UseTexture(passData.fallOffLookUp);
  75. builder.UseTexture(passData.lightLookUp);
  76. foreach (var light in layerBatch.shadowLights)
  77. {
  78. if (light == null || !light.m_CookieSpriteTextureHandle.IsValid())
  79. continue;
  80. if (!isVolumetric || (isVolumetric && light.volumetricEnabled))
  81. builder.UseTexture(light.m_CookieSpriteTextureHandle);
  82. }
  83. builder.AllowPassCulling(false);
  84. builder.AllowGlobalStateModification(true);
  85. builder.SetRenderFunc((DrawLight2DPass.PassData data, UnsafeGraphContext context) =>
  86. {
  87. for (int i = 0; i < data.layerBatch.shadowLights.Count; ++i)
  88. {
  89. intermediateLight.Clear();
  90. intermediateLight.Add(data.layerBatch.shadowLights[i]);
  91. var cmd = context.cmd;
  92. // Shadow Pass
  93. ExecuteShadowPass(cmd, data, intermediateLight[0]);
  94. // Set up MRT
  95. if (Renderer2D.supportsMRT && !data.isVolumetric)
  96. {
  97. for (int j = 0; j < data.lightTextures.Length; ++j)
  98. data.lightTexturesRT[j] = data.lightTextures[j];
  99. cmd.SetRenderTarget(data.lightTexturesRT, data.depthTexture);
  100. }
  101. else
  102. cmd.SetRenderTarget(data.lightTextures[0]);
  103. // Light Pass
  104. using (new ProfilingScope(cmd, DrawLight2DPass.m_ProfilingSamplerLowLevel))
  105. {
  106. DrawLight2DPass.ExecuteUnsafe(cmd, data, ref data.layerBatch, intermediateLight, true);
  107. }
  108. }
  109. });
  110. }
  111. }
  112. }
  113. }