Unity 4 lightmaps in Unity 5
Hey all,
I'm looking for help regarding Unity UV2 Generation for lightmapping. I have a Unity 4 project that has lightmaps generated and we have to migrate to Unity 5 but reuse the Unity 4 lightmaps.
As of right now, I have been able to inject the old maps in the lightmapping system, by forcing a lightmap array and reusing each object's tiling and offset values, but UV2s are all wrong. Everything looks ok, rendering is OK, but only the positioning of the maps are off.
I tried to manually export the values of UV2 for a mesh in Unity 4 to then inject them in the Unity 5 model (first disabled the generation of UVs in Unity 5 for models), but I found out that the same model in both versions of Unity does not have the same vertex count (with the same import settings). But this is another question.
So my question is: is there any way to export the UV2 values generated in Unity 4 to Unity 5.
Thank you,
Answer by olilamont159 · Feb 11, 2016 at 10:46 PM
Hello all,
Yes I successfully made it work! I have Unity 4 lightmaps in Unity 5. But be aware that this is not an easy and clean solution. I'll try to explain my process as best as I can:
Start in Unity 4 by exporting all lightmapped GameObjects Meshes (those with lightmapIndex != -1 on their Renderers) on the file system in a homemade format. I personnally made a text file with separator characters. You need to export everything you will need in Unity 5 to recreate a Mesh object, so triangles, normals, tangents, sub meshes, etc, etc. This can be done with some Editor scripts looping on all of your exported scenes, then all lightmapped objects. You have to make sure all exported models have unique name, or at least they are exported uniquely.
Still in Unity 4, you must store in a component added to all lightmapped object the data needed for the lightmapping system: that is offset, tiling and lightmap Index. You can also do that with Editor scripts that loops on the scenes, then add a component to the objects, then Save.
Then, in Unity 5, you can upgrade your project. Once this is done, there is two things to do: import the exportes meshes, create new Mesh class instances, then serialize these in the object's MeshFilters components. This way, your models are not links to the Model asset in the AssetDatabase, but a serialized version of the exact data from Unity 4, including lightmaps UVs. :)
Then, you must inject lightmapping data in the multiple objects Renderers, data we stored in the components.
And last but not least, there must be code that injects the actual lightmap textures into LightmapSettings, so you must match the lightmapArray from Unity 4, also mode, etc.
You may need to turn off Static Batching options in rendering, this option seems to do weird things to the models data.
Also, your dynamic lighting may not be aware that certain objects are lit, as we are injecting stuff into the lightmapping system. You may need to work on the layers bitfield of your lights.
And lastly, your lightmap textures may need to get tweaked in their import options, to have the actual pixel perfect colors from Unity 4.
So here you go, I hope I didn't miss a step. Let me know if it helps!
Answer by SilverStorm · Oct 19, 2015 at 01:15 PM
I am also looking for a way to do this, I wonder if you have made progress in doing this since your post?
Have you tried using the export package to export entire scenes and see if the original settings stay? I am looking to see if lightmaps can be used specifically in a shader.
Answer by stu_chineseroom · Jan 06, 2016 at 10:35 AM
Hello! Did you get any further with this? I've been trying to manually reference the lightmaps as a regular texture, but the lightmap importer in Unity5 is perhaps more involved and seems to mean I can't use the simple constant decoding as in 4. You mentioned injecting the old lightmaps into the lightmap array - that sounds interesting, do you have any more info on that? e.g. are you editing the LightmapSettings.lightmaps array directly?
Thanks, stu
I have stayed with Unity 4 and have not found a way to export them properly. However the latest Unity stream revealed they are working on a new baked Lightmap system which is much easier to use. It will be in 5.4 I believe.