Search Unity

Different light interaction in edit mode and play mode

Discussion in 'Shaders' started by theforgot3n1, Nov 2, 2021.

  1. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    205
    I have a shader that is applied to SpriteRenderers in Built-in pipeline Unity that renders shadows + normals for sprites. The light's interaction with the sprites in edit mode works perfectly but it has a very strange interaction in playmode. In playmode, the light only minimally affects the tiles at a lower y value. (entire level is rotated 90 degrees, a bit confusing I know). This weird interaction only happens for tiles with normals.

    Note the environment is built out of tilemaps (only for the purpose of generating GameObjects to put SpriteRenderers on, the tilemap itself does no rendering), so the whole thing is re-generated at runtime. But that doesn't matter as the same happens when I pull the tiles out of the tilemap.

    Here's a direct comparison:



    There is something with the rotation of the tile relative to the light that produces this effect, but I don't know why. Here is what I mean with a 90 degree rotation.



    And here's the shader used for this:


    Code (CSharp):
    1. Shader "Custom/SpriteShadowNormal" {
    2.     Properties{
    3.         _Color("Color", Color) = (1,1,1,1)
    4.         [PerRendererData]_MainTex("Sprite Texture", 2D) = "white" {}
    5.         _Cutoff("Shadow alpha cutoff", Range(0,1)) = 0.5
    6.         _NormalMap ("NormalMap", 2D) = "bump" {}
    7.     }
    8.         SubShader{
    9.             Tags
    10.             {
    11.                 "Queue" = "Geometry"
    12.                 "RenderType" = "TransparentCutout"
    13.             }
    14.             LOD 200
    15.  
    16.             Cull Off
    17.  
    18.             CGPROGRAM
    19.             // Lambert lighting model, and enable shadows on all light types
    20.             #pragma surface surf Lambert vertex:vert addshadow
    21.  
    22.             // Use shader model 3.0 target, to get nicer looking lighting
    23.             #pragma target 3.0
    24.             #include "UnityCG.cginc"
    25.  
    26.             sampler2D _MainTex;
    27.             sampler2D _NormalMap;
    28.             fixed4 _Color;
    29.             fixed _Cutoff;
    30.  
    31.             struct appdata_t
    32.             {
    33.                 float4 vertex : POSITION;
    34.                 float4 tangent : TANGENT;
    35.                 float3 normal : NORMAL;
    36.                 float2 texcoord : TEXCOORD0;
    37.                 float2 texcoord1 : TEXCOORD1;
    38.                 float2 texcoord2 : TEXCOORD2;
    39.                 float2 texcoord3 : TEXCOORD3;
    40.                 float4 color : COLOR;
    41.             };
    42.  
    43.             struct Input
    44.             {
    45.                 float2 uv_MainTex;
    46.                 //float2 uv_NormalMap;
    47.                 fixed4 color;
    48.             };
    49.  
    50.             Input vert(inout appdata_t IN, out Input OUT)
    51.             {
    52.                 OUT.uv_MainTex = IN.texcoord;
    53.                 OUT.color = IN.color;
    54.  
    55.                 return OUT;
    56.             }
    57.  
    58.             void surf(Input IN, inout SurfaceOutput o) {
    59.                 fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color * IN.color;
    60.                 o.Albedo = c.rgb;
    61.                 o.Alpha = c.a;
    62.                 o.Normal = UnpackNormal(tex2D (_NormalMap, IN.uv_MainTex));
    63.                 clip(o.Alpha - _Cutoff);
    64.             }
    65.             ENDCG
    66.         }
    67.             FallBack "Diffuse"
    68. }
    Any ideas why this is happening? I just don't get it.
     
    Last edited: Nov 2, 2021
  2. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    205
    By testing settings and such, I have concluded the issue is NOT produced by static/dynamic batching or the structure of the normal map (using Unity's "From Grayscale" option in the normal map has the same issue).
     
    Last edited: Nov 2, 2021
  3. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    205
    Figured it out. Nearly pulled my hair out in the process. I tested different shaders, rotations, settings on shaders etc. but the problem was apparently due to some Sprite Atlases not working properly with the normal maps assigned as secondary textures. This makes sense, but was incredibly hard to figure out.

    I solved the issue for now by removing the atlases, but that is only temporary while I figure out how to include the normals in the atlases.
     
  4. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    205
    I confirmed that this lighting issue is caused by the Sprite Atlas packing of the secondary normal maps into the secondary texture atlas.

    And I have nailed this down to being a bug pretty much 100%.

    I made a repro project using the same shaders + textures and settings of the grass tile (one custom shader, one from this git link). The lighting bug only occurs when the sprites are atlased and specifically when in linear color space. In gamma space everything works fine and dandy.

    Gamma color space:



    Linear color space:



    The point light when in linear color space also brightens the tile up way more in general, which I don't know why. Might make a post about that separately. Regardless, the lighting is messed up in linear color space. Removing the sprite atlas fixes this issue, making the lighting evenly balanced on the tile.

    I have attached a repro project too in case it is helpful for anyone. @Venkify I saw you posting in a related thread about secondary textures and normal maps, might this be interesting?

    I use Unity 2020.3.2.1f1, Built-in renderer.
     

    Attached Files:

  5. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
    Thank you for taking the time to investigate this on your own. It does not seem to be related to baked or realtime GI. Rather, it appears to be a realtime rendering issue. Please submit a bug report, and someone from the customer QA team will take a look. You could also point them to this thread in your report. Thanks!
     
    theforgot3n1 likes this.
  6. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    205
    I have sent a bug report. Thanks for your attention! You are also correct in that it is not related to baked lighting or GI but just regular lighting. Also, interestingly enough, it happens to point lights but NOT spot lights (nor I think directional lights).
     
  7. theforgot3n1

    theforgot3n1

    Joined:
    Sep 26, 2018
    Posts:
    205
    I forgot to include the link in my bug report! Could you edit the report with it? Case number is 1378322. @kristijonas_unity
     
  8. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
    Thanks for submitting a bug report! I have included a link to this thread in the report for you. The case is currently waiting to be picked up.