Search Unity

How to correctly set lightmap parameters for Renderer in runtime?

Discussion in 'General Graphics' started by Kichang-Kim, Sep 21, 2018.

  1. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,011
    Hi. I'm trying to restore(change) lightmap parameters in runtime for my in-house LOD system.

    Here is my experimental steps and result:
    1. Bake lightmap using source static capsule object then store its renderer's lightmap parameters.
    2. In runtime, copy the renderer's parameters into new, dynamically loaded renderer.
    Code (CSharp):
    1. Renderer renderer = this.GetComponent<Renderer>();
    2.  
    3. if (this.LightmapSource != null)
    4. {
    5.     renderer.lightmapIndex = this.LightmapSource.lightmapIndex;
    6.     renderer.lightmapScaleOffset = this.LightmapSource.lightmapScaleOffset;
    7.     renderer.realtimeLightmapIndex = this.LightmapSource.realtimeLightmapIndex;
    8.     renderer.realtimeLightmapScaleOffset = this.LightmapSource.realtimeLightmapScaleOffset;
    9. }
    3. But result image is weird. Copied renderer seems incorrect. See attached image.
    Two capsule has exactly same mesh (unity's default capsule mesh) and lightmap parameters, but image is different.

    So my questions is, how to correctly save and restore lightmap parameters for Renderer?

    Thanks.

    Edit 1:
    I found that this problem occurs only with realtime light mode. When I change the light mode to mixed or baked, restored renderer shows exactly same lightmapped image.
     

    Attached Files:

    Last edited: Sep 21, 2018