Search Unity

Lightmap working in Editor but not in Game mode.

Discussion in 'Shaders' started by Thial, Jan 15, 2021.

  1. Thial

    Thial

    Joined:
    Jan 20, 2017
    Posts:
    4
    Hello. I am working on a tesselated grass shader. I have 2 passes, 1 for rendering the base mesh with a texture and another pass for the actual tesselated grass. I am casting the lightmap in both passes by using the following equation:


    o.uv = uv.xy * unity_LightmapST.xy + unity_LightmapST.zw

    The lightmap is being displayed properly on both the terrain and the grass in the Editor:


    But as soon as I enter the game mode the uv mapping on the grass blades is incorrect and behaves as if instead of using the equation above I was only using:


    o.uv = uv.xy

    so it sort of behaves like the unity_LightmapST variable doesn't exist and results in the following (lightmap fully scaled to my actual texture uv):


    Does anyone know why this might be happening ?
    Any help would be highly appreciated.
     
  2. Thial

    Thial

    Joined:
    Jan 20, 2017
    Posts:
    4
    Found the solution myself. The problem was the UVs that I was passing in for the calculations. Using the UVs of the main texture worked correctly in the editor but not in the game.
    I have fixed it by providing another set of UVs to the input struct (TEXCOORD1) and using the following formula in vert:

    o.lmuv = v.lmuv.xy * unity_LightmapST.xy + unity_LightmapST.zw;

    After that I have passed down the "lmuv" all the way down to the geometry call and used that for the lightmap texture instead of trying to use the regular uv from TEXCOORD0 and the unity_LightmapST value.