Render2DLightingPass.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System.Collections.Generic;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Profiling;
  4. using UnityEngine.Rendering.Universal;
  5. namespace UnityEngine.Experimental.Rendering.Universal
  6. {
  7. internal class Render2DLightingPass : ScriptableRenderPass
  8. {
  9. static SortingLayer[] s_SortingLayers;
  10. Renderer2DData m_Renderer2DData;
  11. static readonly ShaderTagId k_CombinedRenderingPassNameOld = new ShaderTagId("Lightweight2D");
  12. static readonly ShaderTagId k_CombinedRenderingPassName = new ShaderTagId("Universal2D");
  13. static readonly ShaderTagId k_NormalsRenderingPassName = new ShaderTagId("NormalsRendering");
  14. static readonly ShaderTagId k_LegacyPassName = new ShaderTagId("SRPDefaultUnlit");
  15. static readonly List<ShaderTagId> k_ShaderTags = new List<ShaderTagId>() { k_LegacyPassName, k_CombinedRenderingPassName, k_CombinedRenderingPassNameOld };
  16. public Render2DLightingPass(Renderer2DData rendererData)
  17. {
  18. if (s_SortingLayers == null)
  19. s_SortingLayers = SortingLayer.layers;
  20. m_Renderer2DData = rendererData;
  21. }
  22. public void GetTransparencySortingMode(Camera camera, ref SortingSettings sortingSettings)
  23. {
  24. TransparencySortMode mode = camera.transparencySortMode;
  25. if (mode == TransparencySortMode.Default)
  26. {
  27. mode = m_Renderer2DData.transparencySortMode;
  28. if (mode == TransparencySortMode.Default)
  29. mode = camera.orthographic ? TransparencySortMode.Orthographic : TransparencySortMode.Perspective;
  30. }
  31. if (mode == TransparencySortMode.Perspective)
  32. {
  33. sortingSettings.distanceMetric = DistanceMetric.Perspective;
  34. }
  35. else if (mode == TransparencySortMode.Orthographic)
  36. {
  37. sortingSettings.distanceMetric = DistanceMetric.Orthographic;
  38. }
  39. else
  40. {
  41. sortingSettings.distanceMetric = DistanceMetric.CustomAxis;
  42. sortingSettings.customAxis = m_Renderer2DData.transparencySortAxis;
  43. }
  44. }
  45. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  46. {
  47. bool isLitView = true;
  48. #if UNITY_EDITOR
  49. if (renderingData.cameraData.isSceneViewCamera)
  50. isLitView = UnityEditor.SceneView.currentDrawingSceneView.sceneLighting;
  51. if (renderingData.cameraData.camera.cameraType == CameraType.Preview)
  52. isLitView = false;
  53. if (!Application.isPlaying)
  54. s_SortingLayers = SortingLayer.layers;
  55. #endif
  56. Camera camera = renderingData.cameraData.camera;
  57. FilteringSettings filterSettings = new FilteringSettings();
  58. filterSettings.renderQueueRange = RenderQueueRange.all;
  59. filterSettings.layerMask = -1;
  60. filterSettings.renderingLayerMask = 0xFFFFFFFF;
  61. filterSettings.sortingLayerRange = SortingLayerRange.all;
  62. bool isSceneLit = Light2D.IsSceneLit(camera);
  63. if (isSceneLit)
  64. {
  65. RendererLighting.Setup(renderingData, m_Renderer2DData);
  66. CommandBuffer cmd = CommandBufferPool.Get("Render 2D Lighting");
  67. cmd.Clear();
  68. RendererLighting.CreateNormalMapRenderTexture(cmd);
  69. cmd.SetGlobalFloat("_HDREmulationScale", m_Renderer2DData.hdrEmulationScale);
  70. cmd.SetGlobalFloat("_InverseHDREmulationScale", 1.0f / m_Renderer2DData.hdrEmulationScale);
  71. cmd.SetGlobalFloat("_UseSceneLighting", isLitView ? 1.0f : 0.0f);
  72. RendererLighting.SetShapeLightShaderGlobals(cmd);
  73. context.ExecuteCommandBuffer(cmd);
  74. DrawingSettings combinedDrawSettings = CreateDrawingSettings(k_ShaderTags, ref renderingData, SortingCriteria.CommonTransparent);
  75. DrawingSettings normalsDrawSettings = CreateDrawingSettings(k_NormalsRenderingPassName, ref renderingData, SortingCriteria.CommonTransparent);
  76. SortingSettings sortSettings = combinedDrawSettings.sortingSettings;
  77. GetTransparencySortingMode(camera, ref sortSettings);
  78. combinedDrawSettings.sortingSettings = sortSettings;
  79. combinedDrawSettings.sortingSettings = sortSettings;
  80. const int blendStylesCount = 4;
  81. bool[] hasBeenInitialized = new bool[blendStylesCount];
  82. for (int i = 0; i < s_SortingLayers.Length; i++)
  83. {
  84. // Some renderers override their sorting layer value with short.MinValue or short.MaxValue.
  85. // When drawing the first sorting layer, we should include the range from short.MinValue to layerValue.
  86. // Similarly, when drawing the last sorting layer, include the range from layerValue to short.MaxValue.
  87. short layerValue = (short)s_SortingLayers[i].value;
  88. var lowerBound = (i == 0) ? short.MinValue : layerValue;
  89. var upperBound = (i == s_SortingLayers.Length - 1) ? short.MaxValue : layerValue;
  90. filterSettings.sortingLayerRange = new SortingLayerRange(lowerBound, upperBound);
  91. int layerToRender = s_SortingLayers[i].id;
  92. Light2D.LightStats lightStats;
  93. lightStats = Light2D.GetLightStatsByLayer(layerToRender);
  94. // Allocate our blend style textures
  95. cmd.Clear();
  96. for (int blendStyleIndex = 0; blendStyleIndex < blendStylesCount; blendStyleIndex++)
  97. {
  98. uint blendStyleMask = (uint)(1 << blendStyleIndex);
  99. if ((lightStats.blendStylesUsed & blendStyleMask) > 0 && !hasBeenInitialized[blendStyleIndex])
  100. {
  101. RendererLighting.CreateBlendStyleRenderTexture(cmd, blendStyleIndex);
  102. hasBeenInitialized[blendStyleIndex] = true;
  103. }
  104. }
  105. context.ExecuteCommandBuffer(cmd);
  106. // Start Rendering
  107. if (lightStats.totalNormalMapUsage > 0)
  108. RendererLighting.RenderNormals(context, renderingData.cullResults, normalsDrawSettings, filterSettings);
  109. cmd.Clear();
  110. if (lightStats.totalLights > 0)
  111. {
  112. RendererLighting.RenderLights(camera, cmd, layerToRender, lightStats.blendStylesUsed);
  113. }
  114. else
  115. {
  116. RendererLighting.ClearDirtyLighting(cmd, lightStats.blendStylesUsed);
  117. }
  118. CoreUtils.SetRenderTarget(cmd, colorAttachment, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store, ClearFlag.None, Color.white);
  119. context.ExecuteCommandBuffer(cmd);
  120. Profiler.BeginSample("RenderSpritesWithLighting - Draw Transparent Renderers");
  121. context.DrawRenderers(renderingData.cullResults, ref combinedDrawSettings, ref filterSettings);
  122. Profiler.EndSample();
  123. if (lightStats.totalVolumetricUsage > 0)
  124. {
  125. cmd.Clear();
  126. RendererLighting.RenderLightVolumes(camera, cmd, layerToRender, colorAttachment, lightStats.blendStylesUsed);
  127. context.ExecuteCommandBuffer(cmd);
  128. cmd.Clear();
  129. }
  130. }
  131. cmd.Clear();
  132. Profiler.BeginSample("RenderSpritesWithLighting - Release RenderTextures");
  133. RendererLighting.ReleaseRenderTextures(cmd);
  134. Profiler.EndSample();
  135. context.ExecuteCommandBuffer(cmd);
  136. CommandBufferPool.Release(cmd);
  137. filterSettings.sortingLayerRange = SortingLayerRange.all;
  138. RenderingUtils.RenderObjectsWithError(context, ref renderingData.cullResults, camera, filterSettings, SortingCriteria.None);
  139. }
  140. else
  141. {
  142. CommandBuffer cmd = CommandBufferPool.Get("Render Unlit");
  143. DrawingSettings unlitDrawSettings = CreateDrawingSettings(k_ShaderTags, ref renderingData, SortingCriteria.CommonTransparent);
  144. CoreUtils.SetRenderTarget(cmd, colorAttachment, RenderBufferLoadAction.Load, RenderBufferStoreAction.Store, ClearFlag.None, Color.white);
  145. cmd.SetGlobalTexture("_ShapeLightTexture0", Texture2D.blackTexture);
  146. cmd.SetGlobalTexture("_ShapeLightTexture1", Texture2D.blackTexture);
  147. cmd.SetGlobalTexture("_ShapeLightTexture2", Texture2D.blackTexture);
  148. cmd.SetGlobalTexture("_ShapeLightTexture3", Texture2D.blackTexture);
  149. cmd.SetGlobalFloat("_UseSceneLighting", isLitView ? 1.0f : 0.0f);
  150. cmd.EnableShaderKeyword("USE_SHAPE_LIGHT_TYPE_0");
  151. context.ExecuteCommandBuffer(cmd);
  152. CommandBufferPool.Release(cmd);
  153. Profiler.BeginSample("Render Sprites Unlit");
  154. context.DrawRenderers(renderingData.cullResults, ref unlitDrawSettings, ref filterSettings);
  155. Profiler.EndSample();
  156. RenderingUtils.RenderObjectsWithError(context, ref renderingData.cullResults, camera, filterSettings, SortingCriteria.None);
  157. }
  158. }
  159. }
  160. }