DeltaVR/Assets/Scripts/LightProbeGroupMerger.cs
2021-01-24 17:16:33 +02:00

45 lines
1.9 KiB
C#

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public static class LightProbeGroupMerger
{
const string MergedLightProbeGroupName = "MergedLightProbeGroup";
[MenuItem("Tools/Lights/Merge Light Probe Groups")]
static void MergeLightProbeGroups()
{
var lightProbeGroups = GameObject.FindObjectsOfType<LightProbeGroup>();
if (lightProbeGroups.Length > 1)
{
// Collect light probe positions from all light probe groups in the scene.
var mergedLightProbePositions = new List<Vector3>();
foreach (var lightProbeGroup in lightProbeGroups)
{
foreach (var lightProbePosition in lightProbeGroup.probePositions)
{
mergedLightProbePositions.Add(lightProbeGroup.transform.TransformPoint(lightProbePosition));
}
}
// Create a new light probe group that merges all the light probe positions.
var newGameObject = new GameObject(MergedLightProbeGroupName);
Undo.RegisterCreatedObjectUndo(newGameObject, $"Created {MergedLightProbeGroupName}");
var mergedLightProbeGroup = newGameObject.AddComponent<LightProbeGroup>();
mergedLightProbeGroup.probePositions = mergedLightProbePositions.ToArray();
// Destroy the old individual light probe groups.
foreach (var lightProbeGroup in lightProbeGroups)
{
if (lightProbeGroup != null && lightProbeGroup.gameObject != null)
{
Undo.DestroyObjectImmediate(lightProbeGroup.gameObject);
}
}
Debug.Log($"Merged {mergedLightProbePositions.Count} light probes from {lightProbeGroups.Length} light probe groups into '{MergedLightProbeGroupName}'.");
Selection.activeObject = newGameObject;
}
}
}