Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Swapping Lightmaps - UV problems

Discussion in 'Scripting' started by tattyswad, Sep 19, 2014.

  1. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    Hello,

    I've searched for an answer for this but not found exactly what I'm looking for. I have a script that loads lightmaps from 2 different folders so that I can switch between night and day. The problem is that the UV mapping for the lightmaps seems to get messed up every time I load from the folders.

    The original lightmaps are stored in the scene folder I.e. "Assets/Scenes/Chapel_Test_01/"

    I also have night and day versions of the lightmaps stored in the following folders "Assets/Resources/LightmapsNight" and "Assets/Resources/LightmapsDay" respectively.

    the script loads the night lightmaps when the "j" key is pressed and loads the day lightmaps when the "k" key is pressed. I.e. once the lightmaps have been changed the first time they are always reading form the resources folder and when I change back to the day lightmaps the UV is still messed up. There are the same amount of lightmaps for day and night so each array should be the same size.

    I have attached screenshot showing the problem and also the script in question too. Any suggestions would be greatly appreciated.

    thanks

    The code below is .js I don't know why it says its C Sharp

    lightmap issue.jpg
    Code (csharp):
    1.  
    2. function Update(){
    3.  
    4. if (Input.GetKeyDown("j")){
    5.     SetLightMapsNight();
    6.     }
    7.    
    8. if (Input.GetKeyDown("k")){
    9.     SetLightMapsDay();
    10.     }
    11. }
    12.  
    13. function SetLightMapsNight () {
    14. var pathString : String = "LightmapsNight/";
    15. var lightmaparray : LightmapData[] = LightmapSettings.lightmaps;
    16. var mapdata : LightmapData = new LightmapData();
    17.     for (var i=0; i<lightmaparray.length; i++)
    18.     {    
    19.     mapdata.lightmapFar = UnityEngine.Resources.Load(pathString + "LightmapFar-" + i.ToString(), Texture2D) as Texture2D;    
    20.     mapdata.lightmapNear = UnityEngine.Resources.Load(pathString + "LightmapNear-" + i.ToString(), Texture2D) as Texture2D;    
    21.     lightmaparray[i] = mapdata;
    22.     }
    23.  
    24. LightmapSettings.lightmaps = lightmaparray;
    25. }
    26.  
    27.  
    28. function SetLightMapsDay () {
    29. var pathString : String = "LightmapsDay/";
    30. var lightmaparray : LightmapData[] = LightmapSettings.lightmaps;
    31. var mapdata : LightmapData = new LightmapData();
    32.     for (var i=0; i<lightmaparray.length; i++)
    33.     {    
    34.     mapdata.lightmapFar = UnityEngine.Resources.Load(pathString + "LightmapFar-" + i.ToString(), Texture2D) as Texture2D;    
    35.     mapdata.lightmapNear = UnityEngine.Resources.Load(pathString + "LightmapNear-" + i.ToString(), Texture2D) as Texture2D;    
    36.     lightmaparray[i] = mapdata;
    37.     }
    38.  
    39. LightmapSettings.lightmaps = lightmaparray;
    40. }[CODE]
     

    Attached Files:

  2. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    I've just noticed it's loading the same light map into each slot so it looks like there's something wrong with the array code?
     
  3. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    Can anyone suggest why this would load the same map into every slot?

    Thanks