Created
February 7, 2018 02:32
-
-
Save TouiSoraHe/c14a0c790a56f84489fd25695dbefd66 to your computer and use it in GitHub Desktop.
Unity5.x 2017动态加载烘焙贴图
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
[ExecuteInEditMode, DisallowMultipleComponent] | |
public class PrefabLightmapData : MonoBehaviour | |
{ | |
[System.Serializable] | |
struct RendererInfo | |
{ | |
public Renderer renderer; | |
public int lightmapIndex; | |
public Vector4 lightmapOffsetScale;//光照贴图偏移量 | |
} | |
[SerializeField] | |
RendererInfo[] m_RendererInfo; | |
[SerializeField] | |
Texture2D[] m_Lightmaps; | |
void Awake() | |
{ | |
if (m_RendererInfo == null || m_RendererInfo.Length == 0) | |
return; | |
var lightmaps = LightmapSettings.lightmaps; | |
var combinedLightmaps = new LightmapData[lightmaps.Length + m_Lightmaps.Length]; | |
lightmaps.CopyTo(combinedLightmaps, 0); | |
for (int i = 0; i < m_Lightmaps.Length; i++) | |
{ | |
combinedLightmaps[i + lightmaps.Length] = new LightmapData(); | |
combinedLightmaps[i + lightmaps.Length].lightmapColor = m_Lightmaps[i]; | |
} | |
ApplyRendererInfo(m_RendererInfo, lightmaps.Length); | |
LightmapSettings.lightmaps = combinedLightmaps; | |
} | |
static void ApplyRendererInfo(RendererInfo[] infos, int lightmapOffsetIndex) | |
{ | |
for (int i = 0; i < infos.Length; i++) | |
{ | |
var info = infos[i]; | |
info.renderer.lightmapIndex = info.lightmapIndex + lightmapOffsetIndex; | |
info.renderer.lightmapScaleOffset = info.lightmapOffsetScale; | |
} | |
} | |
#if UNITY_EDITOR | |
[UnityEditor.InitializeOnLoadMethod] | |
static void BackCompleted() | |
{ | |
UnityEditor.Lightmapping.completed = () => | |
{ | |
GenerateLightmapInfo(); | |
}; | |
} | |
static void GenerateLightmapInfo() | |
{ | |
PrefabLightmapData[] prefabs = FindObjectsOfType<PrefabLightmapData>(); | |
foreach (var instance in prefabs) | |
{ | |
var gameObject = instance.gameObject; | |
var rendererInfos = new List<RendererInfo>(); | |
var lightmaps = new List<Texture2D>(); | |
//这个方法的目的是将该gameObject以及该gameObject的子对象上的光照贴图信息记录下来 | |
GenerateLightmapInfo(gameObject, rendererInfos, lightmaps); | |
instance.m_RendererInfo = rendererInfos.ToArray(); | |
instance.m_Lightmaps = lightmaps.ToArray(); | |
Debug.Log(instance.m_RendererInfo.Length); | |
//将该gameObject上的设置(对该脚本的设置)应用到预制体上 | |
var targetPrefab = UnityEditor.PrefabUtility.GetPrefabParent(gameObject) as GameObject; | |
if (targetPrefab != null) | |
{ | |
//UnityEditor.Prefab | |
UnityEditor.PrefabUtility.ReplacePrefab(gameObject, targetPrefab); | |
} | |
instance.m_RendererInfo = rendererInfos.ToArray(); | |
instance.m_Lightmaps = lightmaps.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; | |
//lightmapColor:存储入射光颜色的光照贴图。 | |
Texture2D lightmap = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapColor; | |
info.lightmapIndex = lightmaps.IndexOf(lightmap); | |
if (info.lightmapIndex == -1) | |
{ | |
info.lightmapIndex = lightmaps.Count; | |
lightmaps.Add(lightmap); | |
} | |
rendererInfos.Add(info); | |
} | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment