Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

[Unity2021.2.6] 2D Shadow RenderBuffer Clear Problems

Discussion in '2D Experimental Preview' started by ergexh, Dec 24, 2021.

  1. ergexh


    Sep 10, 2020
    Not native speaker. Hope my expression would be clear. I just found a problem in 2D shadow rendering in Unity 2021.2.6. Here's a recording video.

    There are 2 light2D in this scene. They share the same sorting layer. The center one's light order is 1 and the upper-right one's order is 0. At start I just left the center light on and nothing wrong. When I enabled the upper-right light, something wrong happened. It seemed like the buffer rendering shadow can't be cleared correctly. If I drag the upper-right light closely to let the ShadowCaster2D's boundshpere meet lit condition, this bug vanished. Perhaps this bug only happens when there is a Light2D outside of the ShadowCaster2D's boundshpere. (Shadow is short because I modified the original code and reset the shadow radius.)

    I tried to fix this and it works. Additionally calling ClearRenderTarget in ReleaseShadowRenderTexture function in ShadowRendering.cs.

    Code (CSharp):
    1.         public static void ReleaseShadowRenderTexture(CommandBuffer cmdBuffer, int shadowIndex)
    2.         {
    3.             var colorChannel = shadowIndex % 4;
    4.             var textureIndex = shadowIndex / 4;
    6.             if (colorChannel == 0)
    7.             {
    8.                 //Custom modification
    9.                 cmdBuffer.SetRenderTarget(m_RenderTargets[shadowIndex].id);
    10.                 cmdBuffer.ClearRenderTarget(true, true, Color.clear);
    11.                 //Original Code
    12.                 cmdBuffer.ReleaseTemporaryRT(m_RenderTargets[textureIndex].id);
    13.             }
    14.         }
    Simple fix. I'm actually quite greenhand to unity. Don't know if this is efficient enough. Hope unity official will fix this.