- Home /
Unity 5 lightmapped scene assetbundle problem
Hey there,
i am having troubles loading an scene-assetbundle with lightmaps. Everything else works (loading bundles), but i can't find a solution to see lightmaps on my objects. I am building one assetbundle per scene, and load it with Scenemanager.loadAsync.
This is what it should look like (and how it looks if i load the scene the normal way, without assetbundles):
and this is what i get if i load my assetbundle:
the lightmaps somehow seem to be bundled in the assetbundle, if i load the bundle-scene in the editor and look at the lightmap-settings, i see the lightmaps are there - Unity just does not apply them to the objects.
Is this a bug or am i doing something wrong/do i need to do anything "special" to apply the lightmaps? Thanks in advance!
Here is a screenshot of the lighting-inspector with the assetbundle loaded - the lightmaps are loaded, but not applied :/
I had the same problem a long time ago and then I included a "blind" lightmapped scene into the build and that helped. So it looked the hightmapping got stripped in some way. So I guess you could try the same or try to add some lightmaps into your starting scene.
thanks for your advice, unfortunately this did not change anything for me :/ When you had the error, was this already with the new U5 Beast Lighting System?
I had this error in Enlighten, i.e. the new U5 lighting system. I just have several projects like one below: http://virtualplayground.d2.pl/WebGL/ptg2015/galleries/galleries.php and it works in all of them.
Answer by misterPantoni · Dec 02, 2016 at 11:04 AM
After a lot of tests and googeling around i found a working solution, although i am not totally happy with it:
If you store the lightmaps and the LightmapSettings for each renderer in a custom script, you can load the lightmaps on runtime. Look at this forum post and the sample project provided there, this is how i got it to work:
https://forum.unity3d.com/threads/problems-with-instantiating-baked-prefabs.324514/
The "bad" thing is, that now i need to have a duplicate of the lightmaps stored in my custom script - whenever i try to assign the lightmaps which are "in the scene" (and also after loading the assetbundle, at least in the editor i can see them), they somehow are not beeing loaded correcty by the renderers and i get wrong lightmap positions. So i have to store the lightmaps as extra texture and add them to the lightmap array to use them.
EDIT: The problems with the wrong index were just some indices i messed up - now i got it working the way i want it to, without duplicate Lightmaps. Here is the working script, just place it on the parent object of whatever is lightmapped in your scene you want to use as an assetbundle:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SceneAssetBundleLightmaps : MonoBehaviour{
[System.Serializable]
struct RendererInfo{
public Renderer renderer;
public int lightmapIndex;
public Vector4 lightmapOffsetScale;
}
[SerializeField]
RendererInfo[] m_RendererInfo;
void Awake (){
if (m_RendererInfo == null || m_RendererInfo.Length == 0)
return;
ApplyRendererInfo(m_RendererInfo);
}
static void ApplyRendererInfo (RendererInfo[] infos){
for (int i=0;i<infos.Length;i++){
var info = infos[i];
info.renderer.lightmapIndex = info.lightmapIndex;
info.renderer.lightmapScaleOffset = info.lightmapOffsetScale;
}
}
#if UNITY_EDITOR
[UnityEditor.MenuItem("Assets/Assign Scene Assetbundle Lightmaps")]
static void GenerateLightmapInfo (){
if (UnityEditor.Lightmapping.giWorkflowMode != UnityEditor.Lightmapping.GIWorkflowMode.OnDemand){
Debug.LogError("ExtractLightmapData requires that you have baked you lightmaps and Auto mode is disabled.");
return;
}
//UnityEditor.Lightmapping.Bake();
SceneAssetBundleLightmaps[] prefabs = FindObjectsOfType<SceneAssetBundleLightmaps>();
foreach (var instance in prefabs){
var gameObject = instance.gameObject;
var rendererInfos = new List<RendererInfo>();
var lightmaps = new List<Texture2D>();
GenerateLightmapInfo(gameObject, rendererInfos, lightmaps);
instance.m_RendererInfo = rendererInfos.ToArray();
}
}
static void GenerateLightmapInfo (GameObject root, List<RendererInfo> rendererInfos, List<Texture2D> lightmaps){
var renderers = root.GetComponentsInChildren<MeshRenderer>();
foreach (MeshRenderer renderer in renderers){
if (renderer.lightmapIndex != -1)
{
RendererInfo info = new RendererInfo();
info.renderer = renderer;
info.lightmapOffsetScale = renderer.lightmapScaleOffset;
info.lightmapIndex = renderer.lightmapIndex;
rendererInfos.Add(info);
}
}
}
#endif
}
Answer by mr_zog · Apr 26, 2018 at 07:53 AM
Using Unity 5.6.5p4 (64-bit)
Edit -> Project Settings -> Graphics -> Shader Stripping -> Lightmap Modes -> Set them to MANUAL and select the modes you use. Worked for us!
Credits go to: https://www.leegoonz.com/single-post/2016/07/10/Asset-bundle-with-Lightmap-packing-problem-How-to-solve https://docs.unity3d.com/540/Documentation/Manual/BuildingAssetBundles.html