Last active
July 31, 2018 18:26
-
-
Save zezba9000/e18f455298a6d29eb9208ba417ac0b42 to your computer and use it in GitHub Desktop.
Unity LightMapSwitcher
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; | |
using System.IO; | |
using System.Collections.Generic; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
[Serializable] | |
public class LightMapGroup | |
{ | |
public string sourceFolder; | |
public LightProbeGroup[] lightProbeGroups; | |
public Light[] lights; | |
public List<Texture2D> directionTextures, lightTextures, shadowMaskTextures; | |
internal LightmapData[] data; | |
internal bool isInit; | |
private Texture2D LoadAssetTexture(string file) | |
{ | |
string assetPath = Path.Combine("Assets", sourceFolder); | |
assetPath = Path.Combine(assetPath, Path.GetFileName(file)); | |
var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath); | |
if (texture == null) | |
{ | |
Debug.LogError("Unable to load texture: " + assetPath); | |
return null; | |
} | |
return texture; | |
} | |
public void GatherFiles() | |
{ | |
if (string.IsNullOrEmpty(sourceFolder)) | |
{ | |
isInit = true; | |
return; | |
} | |
// format source path | |
string sourceFolderFormated = sourceFolder.Replace('/', '\\'); | |
string sourceFolderFull = Path.Combine(Application.dataPath, sourceFolderFormated); | |
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN | |
sourceFolderFull = sourceFolderFull.Replace('/', '\\'); | |
#else | |
sourceFolderFull = sourceFolderFull.Replace('\\', '/'); | |
#endif | |
// validate source path | |
if (!Directory.Exists(sourceFolderFull)) | |
{ | |
Debug.LogError("LightMapGroup source folder does not exist: " + sourceFolderFull); | |
return; | |
} | |
// gather textures | |
directionTextures = new List<Texture2D>(); | |
lightTextures = new List<Texture2D>(); | |
shadowMaskTextures = new List<Texture2D>(); | |
foreach (string file in Directory.GetFiles(sourceFolderFull)) | |
{ | |
if (file.EndsWith("_dir.png")) | |
{ | |
var texture = LoadAssetTexture(file); | |
if (texture != null) directionTextures.Add(texture); | |
else return; | |
} | |
else if (file.EndsWith("_light.exr")) | |
{ | |
var texture = LoadAssetTexture(file); | |
if (texture != null) lightTextures.Add(texture); | |
else return; | |
} | |
else if (file.EndsWith("_shadowmask.png")) | |
{ | |
var texture = LoadAssetTexture(file); | |
if (texture != null) shadowMaskTextures.Add(texture); | |
else return; | |
} | |
} | |
// create data object | |
CreateDataObject(); | |
isInit = true; | |
} | |
public void CreateDataObject() | |
{ | |
data = new LightmapData[directionTextures.Count]; | |
for (int i = 0; i != data.Length; ++i) | |
{ | |
data[i] = new LightmapData(); | |
data[i].lightmapDir = directionTextures[i]; | |
if (i < lightTextures.Count) data[i].lightmapColor = lightTextures[i]; | |
if (i < shadowMaskTextures.Count) data[i].shadowMask = shadowMaskTextures[i]; | |
} | |
} | |
public void Disable() | |
{ | |
if (lightProbeGroups != null) | |
{ | |
foreach (var group in lightProbeGroups) group.enabled = false; | |
} | |
if (lights != null) | |
{ | |
foreach (var light in lights) light.enabled = false; | |
} | |
} | |
public void Enable() | |
{ | |
LightmapSettings.lightmaps = data; | |
if (lightProbeGroups != null) | |
{ | |
foreach (var group in lightProbeGroups) group.enabled = true; | |
} | |
if (lights != null) | |
{ | |
foreach (var light in lights) light.enabled = true; | |
} | |
} | |
} | |
public class LightMapSwitcher : MonoBehaviour | |
{ | |
public static LightMapSwitcher singleton; | |
public int defaultGroup; | |
public LightMapGroup[] lightMapGroups; | |
public LightMapSwitcher() | |
{ | |
singleton = this; | |
} | |
[InitializeOnLoadMethod] | |
private static void PostBuild() | |
{ | |
foreach (var group in singleton.lightMapGroups) group.CreateDataObject(); | |
singleton.ApplyDefaultGroup(); | |
} | |
[ContextMenu("LightMap: Gather Files")] | |
private void GatherFiles() | |
{ | |
foreach (var group in lightMapGroups) group.GatherFiles(); | |
Apply(defaultGroup); | |
} | |
[ContextMenu("LightMap: Apply Default Group")] | |
private void ApplyDefaultGroup() | |
{ | |
Apply(defaultGroup); | |
} | |
public void Apply(int groupIndex) | |
{ | |
if (lightMapGroups == null || groupIndex > lightMapGroups.Length-1) return; | |
foreach (var group in lightMapGroups) group.Disable(); | |
lightMapGroups[groupIndex].Enable(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment