Quantcast
Channel: Answers for "How the heck can I include Lightmapping data in unity 5 asset bundles???"
Viewing all articles
Browse latest Browse all 6

Answer by frevd

$
0
0
This is the component I use to store the lightmap settings (they must be setup correctly in the editor, but need to be preserved in order to be exported to a bundle). See StoreAll static method (which I call from an editor action in a separate script). It references the lightmaps directly so you won't have trouble later setting the right import settings for the textures. At runtime when I load the bundle I have this script in the main project as well (since scripts don't get exported due to Apple's policies of having code in the main executable). At runtime I call RestoreAll and pass the desired Lightmap mode. I only tested this for static single nondirectional lightmaps though (I migrated unity4 Beast with that). In order to migrate Beast lightmaps, I made an utility to export the lightmap parameters to a textfile and load them again up in unity5, since unity5 would simply clear every setting from Beast (same as what happens at bundle export time). The lightmaps contain bugs and aren't perfect, but a little adjustment is better than rebaking, at least in my indoor scenes. Of course when you restore the lightmap data, the lightmaps must also be present in the scene. Regarding the lightmaps I had an intensity issue when simply importing the EXR assets from Beast in unity5, but I found it can be solved rather easily by changing something on the textures and pressing the apply button (and then changing that something back, it's only about the re-encoding step done by unity5, since lightmapping in unity5 is not using the 2x factor any more). So, I hope this helps anybody running into the same problems. using UnityEngine; using System.Collections.Generic; /// /// Use the provided editor action to put this component on all objects that use static light maps, in order to have their lightmap parameters exported to an asset bundle and later restored properly. /// The corresponding lightmaps will automatically be referenced and included in the bundle. At loading time you need to have this script in your hosting project and call the static method RestoreAll. /// public class LightmapParams : MonoBehaviour { public Texture2D lightmapNear, lightmapFar; public int lightmapIndex; public Vector4 lightmapScaleOffset; /// This function is called from an editor action and creates or updates this component on all objects with renderers and static lightmap in the current scene. public static void StoreAll() { foreach (Renderer r in FindObjectsOfType()) if (r.lightmapIndex != -1) { LightmapParams lmp = r.gameObject.GetComponent(); if (!lmp) lmp = r.gameObject.AddComponent(); lmp.lightmapIndex = r.lightmapIndex; lmp.lightmapScaleOffset = r.lightmapScaleOffset; lmp.lightmapNear = LightmapSettings.lightmaps[lmp.lightmapIndex].lightmapNear; lmp.lightmapFar = LightmapSettings.lightmaps[lmp.lightmapIndex].lightmapFar; } } /// This function needs to be called after importing an assetbundle with exported lightmap data (see ), in order to restore it to the object. The collection it lightmaps will be set from the set of all lightmaps of all instances. public static void RestoreAll(LightmapsMode mode, bool removeComponentsAfterwards = false, bool setStatic = false) { List newLightmaps = new List(); foreach (LightmapParams p in FindObjectsOfType()) { Renderer r = p.gameObject.GetComponent(); if (!r) continue; r.lightmapIndex = p.lightmapIndex; r.lightmapScaleOffset = p.lightmapScaleOffset; // collect lightmaps from references: while (newLightmaps.Count <= p.lightmapIndex) newLightmaps.Add(new LightmapData()); newLightmaps[p.lightmapIndex].lightmapNear = p.lightmapNear; newLightmaps[p.lightmapIndex].lightmapFar = p.lightmapFar; if (setStatic) r.gameObject.isStatic = true; if (removeComponentsAfterwards) Destroy(p); } // activate: LightmapSettings.lightmaps = newLightmaps.ToArray(); LightmapSettings.lightmapsMode = mode; } /// This function is called from an editor action to remove all components in the scene. public static void RemoveAll() { foreach (LightmapParams p in FindObjectsOfType()) DestroyImmediate(p); } } Best, Matt

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>