Created
September 4, 2024 15:34
-
-
Save JLChnToZ/0ac1542d483788e022dba99d474fdd5d to your computer and use it in GitHub Desktop.
One-Click migrates all supported baked lights on scene to use Bakery.
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 System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Rendering; | |
using UnityEngine.SceneManagement; | |
using UnityEditor; | |
public static class BakeryAutoMigrate { | |
static Texture2D spotCookie; | |
[MenuItem("Bakery/Utilities/Migrate and Adjust Lights", priority = 60)] | |
public static void AuoMigrate() { | |
var queue = new Queue<Transform>(); | |
var temp = new List<GameObject>(); | |
for (int i = 0, count = SceneManager.loadedSceneCount; i < count; i++) { | |
var scene = SceneManager.GetSceneAt(i); | |
scene.GetRootGameObjects(temp); | |
foreach (var go in temp) queue.Enqueue(go.transform); | |
} | |
int counter = 0; | |
while (queue.TryDequeue(out var transform)) { | |
for (int i = 0, count = transform.childCount; i < count; i++) { | |
var child = transform.GetChild(i); | |
queue.Enqueue(child); | |
} | |
if (!transform.TryGetComponent(out Light light) || | |
light.lightmapBakeType == LightmapBakeType.Realtime) continue; | |
BakeryPointLight pointLight; | |
BakeryDirectLight directLight; | |
BakeryLightMesh lightMesh; | |
switch (light.type) { | |
case LightType.Point: | |
case LightType.Spot: | |
if (light.TryGetComponent(out directLight)) Undo.DestroyObjectImmediate(directLight); | |
if (light.TryGetComponent(out lightMesh)) Undo.DestroyObjectImmediate(lightMesh); | |
if (!light.TryGetComponent(out pointLight)) | |
pointLight = Undo.AddComponent<BakeryPointLight>(light.gameObject); | |
else | |
Undo.RecordObject(pointLight, "Bakery Migrator"); | |
GetLinearLightParameters(light, out pointLight.color, out pointLight.intensity); | |
pointLight.indirectIntensity = light.bounceIntensity; | |
pointLight.cutoff = light.range; | |
pointLight.angle = light.spotAngle; | |
if (light.type == LightType.Spot) { | |
pointLight.cookie = light.cookie as Texture2D; | |
if (!pointLight.cookie) { | |
if (!spotCookie) spotCookie = AssetDatabase.LoadAssetAtPath<Texture2D>($"{ftLightmaps.GetRuntimePath()}ftUnitySpotTexture.bmp"); | |
pointLight.cookie = spotCookie; | |
} | |
pointLight.projMode = BakeryPointLight.ftLightProjectionMode.Cookie; | |
} else { | |
pointLight.cookie = light.cookie as Texture2D; | |
pointLight.projMode = pointLight.cookie ? | |
BakeryPointLight.ftLightProjectionMode.Cubemap : | |
BakeryPointLight.ftLightProjectionMode.Omni; | |
} | |
counter++; | |
break; | |
case LightType.Directional: | |
if (light.TryGetComponent(out lightMesh)) Undo.DestroyObjectImmediate(lightMesh); | |
if (light.TryGetComponent(out pointLight)) Undo.DestroyObjectImmediate(pointLight); | |
if (!light.TryGetComponent(out directLight)) | |
directLight = Undo.AddComponent<BakeryDirectLight>(light.gameObject); | |
else | |
Undo.RecordObject(directLight, "Bakery Migrator"); | |
GetLinearLightParameters(light, out directLight.color, out directLight.intensity); | |
directLight.indirectIntensity = light.bounceIntensity; | |
counter++; | |
break; | |
case LightType.Rectangle: | |
if (light.TryGetComponent(out directLight)) Undo.DestroyObjectImmediate(directLight); | |
if (light.TryGetComponent(out pointLight)) Undo.DestroyObjectImmediate(pointLight); | |
if (!light.TryGetComponent(out lightMesh)) | |
lightMesh = Undo.AddComponent<BakeryLightMesh>(light.gameObject); | |
else | |
Undo.RecordObject(lightMesh, "Bakery Migrator"); | |
GetLinearLightParameters(light, out lightMesh.color, out lightMesh.intensity); | |
lightMesh.indirectIntensity = light.bounceIntensity; | |
lightMesh.cutoff = light.range * 1.5F; | |
lightMesh.selfShadow = false; | |
counter++; | |
break; | |
default: | |
Debug.LogWarning($"Unsupported light type: {light.type}", light); | |
break; | |
} | |
} | |
if (counter > 0) { | |
Undo.CollapseUndoOperations(Undo.GetCurrentGroup()); | |
EditorUtility.DisplayDialog("Bakery Migrator", $"Migrated and adjusted {counter} lights.", "OK"); | |
} | |
} | |
static void GetLinearLightParameters(Light light, out Color lightColor, out float intensity) { | |
intensity = light.intensity; | |
lightColor = light.color; | |
if (PlayerSettings.colorSpace != ColorSpace.Linear) return; | |
if (!GraphicsSettings.lightsUseLinearIntensity) { | |
lightColor.r = Mathf.Pow(lightColor.r * intensity, 2.2f); | |
lightColor.g = Mathf.Pow(lightColor.g * intensity, 2.2f); | |
lightColor.b = Mathf.Pow(lightColor.b * intensity, 2.2f); | |
intensity = Mathf.Max(Mathf.Max(lightColor.r, lightColor.g), lightColor.b); | |
lightColor /= intensity; | |
} else if (GraphicsSettings.lightsUseColorTemperature) { | |
#if UNITY_2019_3_OR_NEWER | |
if (!light.useColorTemperature) return; | |
#endif | |
lightColor *= Mathf.CorrelatedColorTemperatureToRGB(light.colorTemperature).gamma; | |
} | |
lightColor.a = 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment