Search Unity

Updating old shader to newer lightmapping

Discussion in 'Shaders' started by lmbarns, Apr 5, 2022.

  1. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,628
    I have some art content that was made back for Unity 5 and from 2017+ the lightmapping bakes shadows to the wrong faces, I believe the uv's for the lightmap are either reversed from how it used to be, or the uv channel for lightmapping changed between Unity 5 and modern versions?

    Example:
    The shadows are on the top rather than around the base. The shadow that rock is in is created from a cookie on the directional light.

    upload_2022-4-5_11-23-44.png
    upload_2022-4-5_11-24-54.png


    The shader was using the following:
    Code (CSharp):
    1. v2f vert (appdata v)
    2.             {
    3.                 v2f o;
    4.                 o.vertex = UnityObjectToClipPos(v.vertex);
    5.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    6.                 UNITY_TRANSFER_FOG(o,o.vertex);
    7.                 o.uv2 = v.uv2.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    8.                 o.uv3 = TRANSFORM_TEX(v.uv, _Tex1);
    9.                 o.uv4 = TRANSFORM_TEX(v.uv, _Tex2);
    10.                 o.uv5 = TRANSFORM_TEX(v.uv, _Tex3);
    11.                 o.color = v.color;
    12.                 return o;
    13.             }
    14.          
    15.             fixed4 frag (v2f i) : SV_Target
    16.             {
    17.                 // sample the texture
    18.                 fixed4 col = tex2D(_MainTex, i.uv);
    19.                 col.rgb *= 1.0-saturate(i.color.r + i.color.g + i.color.b); // the inverse of all other info
    20.  
    21.                 fixed4 splat1 = tex2D(_Tex1,i.uv3) * i.color.r;
    22.                 fixed4 splat2 = tex2D(_Tex2,i.uv4) * i.color.g;
    23.                 fixed4 splat3 = tex2D(_Tex3,i.uv5) * i.color.b;
    24.                 col.rgb += splat1.rgb + splat2.rgb + splat3.rgb;
    25.                 col.rgb = saturate(col.rgb);
    26.              
    27.                 #ifdef LIGHTMAP_ON
    28.                     fixed3 lm = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap,i.uv2)).rgb;
    29.                     col.rgb *= lm;
    30.                 #endif
    31.              
    32.                 // apply fog
    33.                 UNITY_APPLY_FOG(i.fogCoord, col);
    34.  
    35.                 col.a = 1.0;
    36.                 return col;
    37.             }