Search Unity

Baked Lighting on Prefabs (Dynamically generated level)

Discussion in 'General Graphics' started by Sommer_, Jul 13, 2020.

  1. Sommer_

    Sommer_

    Joined:
    Apr 23, 2020
    Posts:
    18
    Hi Everyone!
    I am creating my level at runtime by instantiating prefabs. I am trying to find the best way to use lighting for them. I am familiar with light mapping, but since the level is not static, I am looking for the best way to approach this.

    I found this unity article :
    https://unity3d.com/how-to/light-baked-prefabs-on-mobile

    And am wondering if this is still the "best" way (as the article is from 2019, maybe things have changed).

    any input is great!
    thanks!
     
    AntonioModer likes this.
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Depends on your use case. "Best" is either to pre-bake the lightmaps in an external application or extracting them from Unity (like the above example you linked to), or to just rely on real time lighting.
     
  3. Sommer_

    Sommer_

    Joined:
    Apr 23, 2020
    Posts:
    18
    Hey bgolus,

    I came across this solution in another post and it works fantastically
    https://github.com/Ayfel/PrefabLightmapping

    VERY easy to use and i can load my prefab rooms at runtime with lightmaps.

    However, im now running into the issue that there are "seams" between my rooms. Similar to Elder Scrolls in this video

    They explain how they did lightmap blending (4:25-9:30 in the video) but i have no idea where to start on that haha
     
  4. Sommer_

    Sommer_

    Joined:
    Apr 23, 2020
    Posts:
    18
    For anyone looking for info on this..i figured it out and comment to shed some light..
    I followed a similar technique as in the video posted above. My level (as of now) is based on grid tileset of premade rooms that randomly generate. For each door, i created matching size floor planes, one inside the door, one outside. Using the prefab lightmap code linked above, im able to bake the lightmap per prefabs. The prefab lightmaping gives texture coordinates for each part of your prefab, which i would then read those pixels from the lightmap for each prefabs doorway, and blend them with each adjacent doorway.
    There are some trick and it took awhile to figure out, but it works. I used Color.Lerp to blend and adjusted the blend based on distance so it smoothly faded. Also you must copy each texture to a new texture else when you apply the blended pixels it will modify the main texture, which will effect every prefab instance. And the new lightmap textures have to be RGBAHalf.. this took awhile to figure out why my colors where just slightly off..
    Few more tricks but since each implementation is different, just know it can work and isnt too extremely difficult.. just a lot of playing around.. but in short.. lightmap prefab.. read pixels... blend... reapply new textures.. and call init again on the prefab script
     
  5. fendercodes

    fendercodes

    Joined:
    Feb 4, 2019
    Posts:
    191
    Would you be willing to post your code for this? I'm having a similar issue.
     
  6. FoleyX90

    FoleyX90

    Joined:
    Dec 7, 2012
    Posts:
    3
    This worked f***ing great! Thank you so much for sharing that github
     
  7. Ema-DF

    Ema-DF

    Joined:
    Apr 2, 2020
    Posts:
    3
    Hi
    The script works great if I build on pc, instead if I try for android the lightmap don’t work properly.
    Does anyone know how I can solve this?
    Thanks in advance
     
    DungDajHjep and James_Arndt like this.
  8. James_Arndt

    James_Arndt

    Unity Technologies

    Joined:
    Jan 4, 2021
    Posts:
    22
    When you say it doesn't work, what does that entail? Does it mean the lightmaps appear, but appear misplaced or garbled? Or does the lightmapped geometry simply show no lightmaps at all? I'm wondering if some mesh setting that is build specific to Android is shifting or messing with the lightmap UVs or even the offset data for each mesh object.
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    I'd wonder if the lightmaps aren't in DXT1/5 format since they're being copied into the prefab from the editor.
     
  10. RyanKeable

    RyanKeable

    Joined:
    Oct 16, 2013
    Posts:
    62
    I did a similar process to the first link to bake AO into dynamic objects for a VR game on the Oculus Quest.

    The lightmaps need to be converted to textures correctly and the UV's coords need to line up but thats about it. We did have to make sure we weren't compressing the texcoord02 or optimizing mesh data on import build so that could be an issue?

    Futhermore, we were only using a grayscale texture so we compressed everything into .tga Alpha8 files too to save on more space.

    annoyingly, that github resource would have been great 2 years ago! hahaha
     
  11. Ema-DF

    Ema-DF

    Joined:
    Apr 2, 2020
    Posts:
    3
    Hi james
    Im working on augmented reality project for mobile, the lightmaps appear but misplaced and garbled
     
  12. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    115
  13. megaverse6

    megaverse6

    Joined:
    Jul 11, 2022
    Posts:
    1
    Hello we were previously using the script provided by sir @JoachimAnte, and it did the job pretty neatly, for my workflow we are targetting webGL, windows, and Android but it was great until we bumped into a problem where the lightmaps are too bright in the Android build specifically, we used LightPrefabData script to bundle all lightmaps and then removed the static Flag from the root of prefab and asset bundled them... it works neatly in windows and webGL[even with compression], but only in android build when asset bundled prefabs are instantiated they become to Bright.. previously we had static batching enabled and static flag was active on prefab which caused the same issue but the once static flag on prefab turned off it was ok, compared now we wonder how could it be fixed, any help is appreciated
     
    DungDajHjep likes this.