Search Unity

Switch between two lightmaps; is it doable and is it codeable?

Discussion in 'Scripting' started by Krodil, Feb 7, 2011.

  1. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    Hi,
    Is it possible to have two baked lightmaps (i.e one from a sunlit scene and one from a moonlit scene), and then shift between them in code?
     
  2. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    For terrains you can just do this:
    Code (csharp):
    1.  
    2. Terrain terrain;
    3. terrain.lightmapIndex = moonlitIndex; //tried this and it works
    4.  
    For meshes I guess this probably works:
    Code (csharp):
    1.  
    2. MeshRenderer re = GetComponent<MeshRenderer>();
    3. re.lightmapIndex = moonlitIndex;
    4.  
    You will need to know the indices of your lightmaps in the lightmap array. (Look under Maps in the Lightmapping window).

    You can also add lightmaps to the array:
    Code (csharp):
    1.  
    2. LightmapData lmapData = new LightmapData();
    3. lmapData.lightmapFar = lightmapTextureFar;
    4. lmapData.lightmapNear = lightmapTextureNear;
    5. int totalLightMaps = LightmapSettings.lightmaps.Length + 1;
    6. LightmapData[] lmaps = new LightmapData[totalLightMaps];
    7. for (int i = 0; i < totalLightMaps - 1; i++)
    8. {
    9.     lmaps[i] = LightmapSettings.lightmaps[i];
    10. }
    11. lmaps[totalLightMaps - 1] = lmapData;
    12. LightmapSettings.lightmaps = lmaps;
    13.  
     
    Last edited: Feb 7, 2011
  3. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    ohh, how would you combine this into a button? I cant see any variables, are they not needed? Im a newb ;)
     
  4. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    It works. Nice. Just copy the rendered lightmap out of the scene folder and rename it. Then do a new render with the other light active.
     
    Last edited: Mar 1, 2011
  5. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    rrh likes this.