[펌] Unity 5 lightmapped scene assetbundle problem
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
}
[출처] http://answers.unity3d.com/questions/1278569/unity-5-lightmapped-scene-assetbundle-problem.html
'Unity3D > LightMap' 카테고리의 다른 글
유니티5 라이트 맵 팁 (0) | 2015.06.19 |
---|---|
LightMapSize 조절 (0) | 2014.03.18 |
Unity Lightmapping Tips, Tricks and Thoughts (0) | 2014.03.18 |