#if UNITY_EDITOR
using UnityEditor;
usingSystem.IO;
#endif
usingUnityEngine;
using System.Collections.Generic;
[DisallowMultipleComponent,ExecuteInEditMode]
public classPrefabLightmapData : MonoBehaviour
{
[System.Serializable]
structRendererInfo
{
publicRenderer renderer;
publicint lightmapIndex;
publicVector4 lightmapOffsetScale;
}
[SerializeField]
RendererInfo[]m_RendererInfo;
[SerializeField]
Texture2D[]m_Lightmaps;
[SerializeField]
Texture2D[]m_Lightmaps2;
conststring LIGHTMAP_RESOURCE_PATH= "Assets/Resources/Lightmaps/";
[System.Serializable]
structTexture2D_Remap
{
publicint originalLightmapIndex;
publicTexture2D originalLightmap;
publicTexture2D lightmap0;
publicTexture2D lightmap1;
}
staticList<Texture2D_Remap>sceneLightmaps = newList<Texture2D_Remap>();
voidAwake()
{
ApplyLightmaps(m_RendererInfo,m_Lightmaps,m_Lightmaps2);
}
staticvoid ApplyLightmaps(RendererInfo[]rendererInfo,Texture2D[]lightmaps,Texture2D[]lightmaps2)
{
boolexistsAlready = false;
intcounter =0;
int[]lightmapArrayOffsetIndex;
if(rendererInfo== null|| rendererInfo.Length== 0)
return;
varsettingslightmaps = LightmapSettings.lightmaps;
varcombinedLightmaps = newList<LightmapData>();
lightmapArrayOffsetIndex= newint[lightmaps.Length];
for(inti =0;i <lightmaps.Length;i++)
{
existsAlready= false;
for(intj =0;j <settingslightmaps.Length;j++)
{
if(lightmaps[i]== settingslightmaps[j].lightmapFar)
{
lightmapArrayOffsetIndex[i]= j;
existsAlready= true;
}
}
if(!existsAlready)
{
lightmapArrayOffsetIndex[i]= counter+ settingslightmaps.Length;
varnewLightmapData = newLightmapData();
newLightmapData.lightmapFar= lightmaps[i];
newLightmapData.lightmapNear= lightmaps2[i];
combinedLightmaps.Add(newLightmapData);
++counter;
}
}
varcombinedLightmaps2 = newLightmapData[settingslightmaps.Length+ counter];
settingslightmaps.CopyTo(combinedLightmaps2,0);
if(counter> 0)
{
for(inti =0;i <combinedLightmaps.Count;i++)
{
combinedLightmaps2[i+ settingslightmaps.Length]= newLightmapData();
combinedLightmaps2[i+ settingslightmaps.Length].lightmapFar= combinedLightmaps[i].lightmapFar;
combinedLightmaps2[i+ settingslightmaps.Length].lightmapNear= combinedLightmaps[i].lightmapNear;
}
}
ApplyRendererInfo(rendererInfo,lightmapArrayOffsetIndex);
LightmapSettings.lightmaps= combinedLightmaps2;
}
staticvoid ApplyRendererInfo(RendererInfo[]infos,int[]arrayOffsetIndex)
{
for(inti =0;i <infos.Length;i++)
{
varinfo =infos[i];
info.renderer.lightmapIndex= arrayOffsetIndex[info.lightmapIndex];
info.renderer.lightmapScaleOffset= info.lightmapOffsetScale;
}
}
#if UNITY_EDITOR
[MenuItem("Assets/Update Scene with Prefab Lightmaps")]
staticvoid UpdateLightmaps()
{
PrefabLightmapData[]prefabs =FindObjectsOfType<PrefabLightmapData>();
foreach(varinstance inprefabs)
{
ApplyLightmaps(instance.m_RendererInfo,instance.m_Lightmaps,instance.m_Lightmaps2);
}
Debug.Log("Prefab lightmaps updated");
}
[MenuItem("Assets/Bake Prefab Lightmaps")]
staticvoid GenerateLightmapInfo()
{
Debug.ClearDeveloperConsole();
if(Lightmapping.giWorkflowMode!= Lightmapping.GIWorkflowMode.OnDemand)
{
Debug.LogError("ExtractLightmapData requires that you have baked you lightmaps and Auto mode is disabled.");
return;
}
Lightmapping.Bake();
stringlightMapPath =System.IO.Path.Combine(Directory.GetCurrentDirectory(),LIGHTMAP_RESOURCE_PATH);
if(!Directory.Exists(lightMapPath))
Directory.CreateDirectory(lightMapPath);
sceneLightmaps= newList<Texture2D_Remap>();
//var scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
varsceneName =Path.GetFileNameWithoutExtension(EditorApplication.currentScene);
varresourcePath =LIGHTMAP_RESOURCE_PATH+ sceneName;
varscenePath =System.IO.Path.GetDirectoryName(EditorApplication.currentScene)+ "/"+ sceneName+ "/";
PrefabLightmapData[]prefabs =FindObjectsOfType<PrefabLightmapData>();
foreach(varinstance inprefabs)
{
vargameObject =instance.gameObject;
varrendererInfos = newList<RendererInfo>();
varlightmaps =new List<Texture2D>();
varlightmaps2 =new List<Texture2D>();
GenerateLightmapInfo(scenePath,resourcePath,gameObject,rendererInfos,lightmaps,lightmaps2);
instance.m_RendererInfo= rendererInfos.ToArray();
instance.m_Lightmaps= lightmaps.ToArray();
instance.m_Lightmaps2= lightmaps2.ToArray();
vartargetPrefab =PrefabUtility.GetPrefabParent(gameObject)as GameObject;
if(targetPrefab!= null)
{
//Prefab
PrefabUtility.ReplacePrefab(gameObject,targetPrefab);
}
ApplyLightmaps(instance.m_RendererInfo,instance.m_Lightmaps,instance.m_Lightmaps2);
}
Debug.Log("Update to prefab lightmaps finished");
}
staticvoid GenerateLightmapInfo(stringscenePath,string resourcePath,GameObject root,List<RendererInfo>rendererInfos,List<Texture2D>lightmaps,List<Texture2D>lightmaps2)
{
varrenderers =root.GetComponentsInChildren<MeshRenderer>();
foreach(MeshRenderer renderer inrenderers)
{
if(renderer.lightmapIndex!= -1)
{
RendererInfoinfo =new RendererInfo();
info.renderer= renderer;
info.lightmapOffsetScale= renderer.lightmapScaleOffset;
Texture2Dlightmap =LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapFar;
Texture2D lightmap2 =LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapNear;
intsceneLightmapIndex = AddLightmap(scenePath,resourcePath,renderer.lightmapIndex,lightmap,lightmap2);
info.lightmapIndex= lightmaps.IndexOf(sceneLightmaps[sceneLightmapIndex].lightmap0);
if(info.lightmapIndex== -1)
{
info.lightmapIndex= lightmaps.Count;
lightmaps.Add(sceneLightmaps[sceneLightmapIndex].lightmap0);
lightmaps2.Add(sceneLightmaps[sceneLightmapIndex].lightmap1);
}
rendererInfos.Add(info);
}
}
}
staticint AddLightmap(stringscenePath,string resourcePath,int originalLightmapIndex,Texture2D lightmap,Texture2D lightmap2)
{
intnewIndex =-1;
for(inti =0;i <sceneLightmaps.Count;i++)
{
if(sceneLightmaps[i].originalLightmapIndex== originalLightmapIndex)
{
returni;
}
}
if(newIndex== -1)
{
varlightmap_Remap = newTexture2D_Remap();
lightmap_Remap.originalLightmapIndex= originalLightmapIndex;
lightmap_Remap.originalLightmap= lightmap;
varfilename =scenePath +"Lightmap-" +originalLightmapIndex;
lightmap_Remap.lightmap0= GetLightmapAsset(filename+ "_comp_light.exr",resourcePath +"_light",originalLightmapIndex,lightmap);
if(lightmap2!= null)
{
lightmap_Remap.lightmap1= GetLightmapAsset(filename+ "_comp_dir.exr",resourcePath +"_dir",originalLightmapIndex,lightmap2);
}
sceneLightmaps.Add(lightmap_Remap);
newIndex= sceneLightmaps.Count- 1;
}
returnnewIndex;
}
staticTexture2D GetLightmapAsset(stringfilename,string resourcePath,int originalLightmapIndex,Texture2D lightmap)
{
AssetDatabase.ImportAsset(filename,ImportAssetOptions.ForceUpdate);
varimporter =AssetImporter.GetAtPath(filename)as TextureImporter;
importer.isReadable= true;
AssetDatabase.ImportAsset(filename,ImportAssetOptions.ForceUpdate);
varassetLightmap = AssetDatabase.LoadAssetAtPath<Texture2D>(filename);
varassetPath =resourcePath +"-" +originalLightmapIndex+ ".asset";
varnewLightmap =Instantiate<Texture2D>(assetLightmap);
AssetDatabase.CreateAsset(newLightmap,assetPath);
newLightmap= AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath);
importer.isReadable= false;
AssetDatabase.ImportAsset(filename,ImportAssetOptions.ForceUpdate);
returnnewLightmap;
}
#endif
}