Search Unity

Lightmapping for runtime instantiated objects

Discussion in 'Global Illumination' started by freddyw, Mar 18, 2015.

  1. freddyw

    freddyw

    Joined:
    Oct 15, 2014
    Posts:
    5
    Is there a way to use the lightmaps for runtime instantiated objects?

    Before Unity5, baked lightmaps will also apply to the duplicated object, but in Unity 5, I was told that the lightmapping works completely differently. Once you click build lightmap is baked for entire scene, not separate objects, therefore if you duplicate an object in already baked scene, the lightmap will not be applied.

    So something like endless runner type of games cannot use lightmapping anymore?
     
  2. KEngelstoft

    KEngelstoft

    Unity Technologies

    Joined:
    Aug 13, 2013
    Posts:
    1,366
    You should use light probes and reflection probes for dynamic objects.
     
  3. freddyw

    freddyw

    Joined:
    Oct 15, 2014
    Posts:
    5
    Thank you KEngelstoft, but I don't think light probes and reflection probes will work in this case.

    The objects I want to instantiate aren't dynamic, they're static, that's why I was able to use the lightmapping.
    I want to instantiate a piece of `ground` for a infinite runner type of game. There are many different pieces randomly instantiated as player runs through, and there are also curves and turns.
    Because the ground is randomly selected at runtime, I can't just setup a scene in editor and bake for an entire scene, and for an infinite runner type of game, you won't know how far the player will get.

    I received a suggestion to put every single piece of ground in a scene on its own and bake for each of them separately, and at runtime load them additively to the current scene and move it to the position I want. I'm not sure whether its going to work, I'll test and try it out. With more than 100 pieces of ground it's going to take a while...
     
  4. KEngelstoft

    KEngelstoft

    Unity Technologies

    Joined:
    Aug 13, 2013
    Posts:
    1,366
    That is a good suggestion, for static objects that should work.
     
  5. jmjd

    jmjd

    Joined:
    Nov 14, 2012
    Posts:
    50
    Hello @KEngelstoft. We are having this same problem on our project. This was possible in Unity 4 and we were using this technique in our endless runner. But we've just upgraded to 5 and it is now broken. It looks like there is already a bug for this (issue 676606). But there isn't really any info on that issue so I am not sure.

    Is there anyway to get this issue escalated, as we are planning to release in a couple months. The problem seems clear, the lightmapIndex and lightmapScaleOffset values don't seem to be saved to prefabs, or copied to a cloned object.

    Thanks!
     
  6. KEngelstoft

    KEngelstoft

    Unity Technologies

    Joined:
    Aug 13, 2013
    Posts:
    1,366
    Since issue 676606 is already in our system and has the highest priority, it isn't possible to escalate it further.
     
  7. mikamikem

    mikamikem

    Joined:
    Mar 3, 2013
    Posts:
    10
    If it helps at all while you're waiting for an official fix, you can just cache the lightmapIndex and lightmapScaleOffset on a component next to your renderer and set them on the renderer during Awake or Enable. We were using this to swap lightmaps in Unity 4 and that portion seems to still work in Unity 5 (although if you're using any of the new GI stuff that may not transfer properly).
     
  8. jmjd

    jmjd

    Joined:
    Nov 14, 2012
    Posts:
    50
    @KEngelstoft, Thanks, if there's any more info or progress updates about that issue, I'd be happy to hear them.

    @mikamikem Yeah, I've thought about doing this, and if I need to I will. Just don't want to work on a temp fix, if an official fix is imminent.
     
  9. yvrez

    yvrez

    Joined:
    Mar 7, 2010
    Posts:
    19
    Hello Guys any news on this one? We have exactly the same problem since our migration to Unity 5 all the cloned at runtime objects are losing their lightmaps. Since we are almost in July and the last message was in March i'm wondering if we should implements that fix or wait for a proper solution from Unity. Thx !
     
  10. Boris-S

    Boris-S

    Joined:
    Jan 23, 2013
    Posts:
    23
    Any news? I have the same problem... Going to duplicate lightmap parameters when instantiating as a temporary solution, but it looks like a dirty hack...
     
  11. dactilardesign

    dactilardesign

    Joined:
    Jun 9, 2009
    Posts:
    68
    Hello all. I would like to keep lightmaps when cloning objects (not even in runtime) baceuse I have a scene of +2000 objetcs that will be cloned 4 times to save memory (4 times less lightmap memory, and rendering time which is huge).
    Looking forward to a sollution too.
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  13. RajGamer

    RajGamer

    Joined:
    Apr 25, 2014
    Posts:
    1
    Hello @Joachim_Ante , @KEngelstoft This is not a proper solution as I cannot fill all my 200 prefabs with lightmap offsets and indexes. Even this solution also not working properly.
    Please let me know when can I expect good solution for this problem?
     
  14. agilelensalex

    agilelensalex

    Joined:
    Apr 7, 2014
    Posts:
    24
    Anyone know if there's been progress on this? Still something that would be great to have.
     
  15. jjaine

    jjaine

    Joined:
    Oct 27, 2016
    Posts:
    3
    Still waiting for this...
     
  16. Samantha_K

    Samantha_K

    Joined:
    Sep 29, 2016
    Posts:
    36
    Still no with 5.6?
     
  17. tswierkot

    tswierkot

    Joined:
    Feb 15, 2017
    Posts:
    25
    I am having the same problem described here. I need to create a road from instantiated chunks containing baked lightmaps, however the instantiated objects do not use the lightmaps of the prefab. Currently I am resolving it using the following code snipper:
    Code (CSharp):
    1. private void SetInstantiatedLightmap(GameObject obj, GameObject prefab)
    2.     {
    3.         var imr = obj.GetComponentsInChildren<MeshRenderer>().ToList();
    4.         imr.Add(obj.GetComponent<MeshRenderer>());
    5.         var pmr = prefab.GetComponentsInChildren<MeshRenderer>().ToList();
    6.         pmr.Add(prefab.GetComponent<MeshRenderer>());
    7.         if (imr != null && pmr != null && imr.Count == pmr.Count)
    8.         {
    9.             for (int i = 0; i < imr.Count; ++i)
    10.             {
    11.                 var renderer = imr[i].GetComponent<MeshRenderer>();
    12.                 var prefabRenderer = pmr[i].GetComponent<MeshRenderer>();
    13.                 if (renderer != null && prefabRenderer != null)
    14.                 {
    15.                     renderer.lightmapIndex = prefabRenderer.lightmapIndex;
    16.                     renderer.lightmapScaleOffset = prefabRenderer.lightmapScaleOffset;
    17.                     renderer.realtimeLightmapIndex = prefabRenderer.realtimeLightmapIndex;
    18.                     renderer.realtimeLightmapScaleOffset = prefabRenderer.realtimeLightmapScaleOffset;
    19.                 }
    20.             }
    21.         }
    22.     }
    Does anyone know if there is a better way?
     
  18. Deleted User

    Deleted User

    Guest

    Wow...
    How about baking lightmap at runtime ? ( loading time ? )

     
  19. tatisgordon

    tatisgordon

    Joined:
    Apr 4, 2017
    Posts:
    1
    Any news on this subject? i would like to set up lightmap on my infinite runner.
     
  20. leppryt

    leppryt

    Joined:
    Jul 28, 2017
    Posts:
    3
    "
    highest priority
     
    Gasimo and C-T-Y like this.
  21. Bentoon

    Bentoon

    Joined:
    Apr 26, 2013
    Posts:
    98
    Last edited: Jul 5, 2020
  22. cineconcerts

    cineconcerts

    Joined:
    Dec 18, 2019
    Posts:
    10
    Thanks for the script - I can't seem to get it to work unfortunately. The lightmap just won't apply when I run it on the phone, or when I drop the prefab into the main scene either. I tried messing with the lightmap modes too but I just can't get it to work. I'm placing the prefab in AR. I also tried it with Auto Generate turned both off and on. Any help would be greatly appreciated. Thank you.