PrefabLightmapData.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. public class PrefabLightmapData : MonoBehaviour
  6. {
  7. //LightMap 信息
  8. [System.Serializable]
  9. public struct RendererInfo
  10. {
  11. public Renderer renderer;
  12. public int lightmapIndex;
  13. public Vector4 lightmapOffsetScale;
  14. public int realtimeLightmapIndex;
  15. public Vector4 realtimeLightmapScaleOffset;
  16. public string shaderName;
  17. public string shaderPath;
  18. }
  19. public List<RendererInfo> m_RendererInfo;
  20. public List<Texture2D> lightmapColor;
  21. public List<Texture2D> lightmapDir;
  22. public LightmapData[] lightmapDataList;
  23. public LightmapsMode lightmapsMode;
  24. /// <summary>
  25. /// 设置光照数据
  26. /// </summary>
  27. [ContextMenu("SetUpLightmap")]
  28. public void SetUpLightmap()
  29. {
  30. int count = lightmapColor.Count > lightmapDir.Count ? lightmapColor.Count : lightmapDir.Count;
  31. lightmapDataList = new LightmapData[count];
  32. for (int idx = 0; idx < count; idx++)
  33. {
  34. lightmapDataList[idx] = new LightmapData();
  35. lightmapDataList[idx].lightmapColor = idx < lightmapColor.Count ? lightmapColor[idx] : null;
  36. lightmapDataList[idx].lightmapDir = idx < lightmapDir.Count ? lightmapDir[idx] : null;
  37. }
  38. LightmapSettings.lightmapsMode = lightmapsMode;
  39. LightmapSettings.lightmaps = lightmapDataList;
  40. LoadLightmap();
  41. }
  42. /// <summary>
  43. /// 保存lightmap数据
  44. /// </summary>
  45. [ContextMenu("SaveLightmap")]
  46. public void SaveLightmap()
  47. {
  48. SaveLightmapData();
  49. }
  50. /// <summary>
  51. /// 保存lightmap信息
  52. /// </summary>
  53. private void SaveLightmapData()
  54. {
  55. lightmapColor = new List<Texture2D>();
  56. lightmapDir = new List<Texture2D>();
  57. LightmapData[] lightmaps = LightmapSettings.lightmaps;
  58. for (int idx = 0; idx < lightmaps.Length; idx++)
  59. {
  60. LightmapData data = lightmaps[idx];
  61. if (data.lightmapColor != null)
  62. {
  63. lightmapColor.Add(data.lightmapColor);
  64. }
  65. if (data.lightmapDir != null)
  66. {
  67. lightmapDir.Add(data.lightmapDir);
  68. }
  69. }
  70. m_RendererInfo = new List<RendererInfo>();
  71. MeshRenderer[] renderers = GetComponentsInChildren<MeshRenderer>();
  72. for (int idx = 0; idx < renderers.Length; idx++)
  73. {
  74. MeshRenderer renderer = renderers[idx];
  75. if (renderer.lightmapIndex != -1)
  76. {
  77. RendererInfo info = new RendererInfo();
  78. info.renderer = renderer;
  79. info.lightmapOffsetScale = renderer.lightmapScaleOffset;
  80. info.lightmapIndex = renderer.lightmapIndex;
  81. info.realtimeLightmapIndex = renderer.realtimeLightmapIndex;
  82. info.realtimeLightmapScaleOffset = renderer.realtimeLightmapScaleOffset;
  83. info.shaderName = renderer.sharedMaterial.shader.name;
  84. #if UNITY_EDITOR
  85. info.shaderPath = UnityEditor.AssetDatabase.GetAssetPath(renderer.sharedMaterial.shader);
  86. #endif
  87. m_RendererInfo.Add(info);
  88. }
  89. }
  90. lightmapsMode = LightmapSettings.lightmapsMode;
  91. }
  92. /// <summary>
  93. /// 加载lightmap信息
  94. /// </summary>
  95. private void LoadLightmap()
  96. {
  97. if (m_RendererInfo.Count <= 0) return;
  98. for (int idx = 0; idx < m_RendererInfo.Count; idx++)
  99. {
  100. RendererInfo item = m_RendererInfo[idx];
  101. item.renderer.lightmapIndex = item.lightmapIndex;
  102. item.renderer.lightmapScaleOffset = item.lightmapOffsetScale;
  103. item.renderer.realtimeLightmapIndex = item.realtimeLightmapIndex;
  104. item.renderer.realtimeLightmapScaleOffset = item.realtimeLightmapScaleOffset;
  105. Shader shader = ResourceMgr.Instance.FindShader(item.shaderName, item.shaderPath);
  106. if(shader!=null)
  107. item.renderer.material.shader = shader;
  108. }
  109. }
  110. }