Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Static batching breaks my shader.

Discussion in 'Shaders' started by quinng, Oct 28, 2019.

  1. quinng

    quinng

    Joined:
    Jul 2, 2015
    Posts:
    22
    I have a shader that takes a static lightmap as an input paramter. I use this shader to copy the lighting off of an original mesh object and onto other instances of the mesh. In my case I'm not actually creating mesh objects, but passing draw requests for mesh instances directly to the renderer.

    This works fine, as long as I don't check static batching on two or more of the original scene meshes. If I do, the meshes render as all white, or black with some improperly placed light splotches (i.e. improperly mapped light coordinates). I think I understand what the problem is. The meshes are being grouped into larger meshes and this is messing up the lightmap coordinates when I try to render individual mesh instance at runtime. I'm wondering if there is a way write my shader so that it properly supports static batching.

    Code (CSharp):
    1. Shader "Custom/FarsidePrelitShader"
    2. {
    3.     Properties
    4.     {
    5.         //Though I don't need to declare it here, I pass in a main texture map and a lightmap.
    6.         //_MainTex("Texture", 2D)
    7.         //_LightMap("LightMap", 2D)
    8.         // Context number is for the stencil check. It shouldn't have anything to do with the lightmap issue.
    9.         _ContextNum("Context Number", Int) = 0
    10.     }
    11.     SubShader
    12.     {
    13.         Tags { "RenderType" = "Opaque" "Queue" = "Geometry+2"}
    14.         LOD 100
    15.         Stencil {
    16.             Ref[_ContextNum]
    17.             Comp equal
    18.         }
    19.         Pass
    20.         {
    21.             CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             // make fog work
    25.             #pragma multi_compile_fog
    26.             #include "UnityCG.cginc"
    27.             struct appdata
    28.             {
    29.                 float4 vertex : POSITION;
    30.                 float2 uv : TEXCOORD0;
    31.                 float2 light_uv : TEXCOORD1;
    32.             };
    33.             struct v2f
    34.             {
    35.                 float2 uv : TEXCOORD0;
    36.                 float2 light_uv : TEXCOORD1;
    37.                 UNITY_FOG_COORDS(1)
    38.                 float4 vertex : SV_POSITION;
    39.             };
    40.             sampler2D _MainTex;
    41.             float4 _MainTex_ST;
    42.             sampler2D _LightMap;
    43.             float4 _LightMap_ST;
    44.             v2f vert (appdata v)
    45.             {
    46.                 v2f o;
    47.                 o.vertex = UnityObjectToClipPos(v.vertex);
    48.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    49.                 o.light_uv = TRANSFORM_TEX(v.light_uv, _LightMap);
    50.                 UNITY_TRANSFER_FOG(o,o.vertex);
    51.                 return o;
    52.             }
    53.             fixed4 frag (v2f i) : SV_Target
    54.             {
    55.                 // sample the texture
    56.                 fixed4 col = tex2D(_MainTex, i.uv);
    57.                 col*= tex2D(_LightMap, i.light_uv);
    58.                 // apply fog
    59.                 UNITY_APPLY_FOG(i.fogCoord, col);
    60.                 return col;
    61.             }
    62.             ENDCG
    63.         }
    64.     }
    65. }
    Some of my research I did in writing this shader suggested that I should use DecodeLightmap like this:
    //col.rgb *= DecodeLightmap(tex2D(_LightMap, i.light_uv));
    I think I had issues with this or didn't notice a difference. Is this function something that can help me solve my problem?