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.
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.
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
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
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.
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
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