Skip to content

Instantly share code, notes, and snippets.

@keijiro
Last active February 25, 2020 05:45
Show Gist options
  • Save keijiro/8279233 to your computer and use it in GitHub Desktop.
Save keijiro/8279233 to your computer and use it in GitHub Desktop.
Extracts SH coefficients from light probes, and rebuilds light probes from given SH coefficients.
using UnityEngine;
using UnityEditor;
using System.Collections;
public class LightProbeRebuilderWindow : EditorWindow
{
string text = "Paste extracted coefficients here.";
float multiplier = 1.0f;
[MenuItem("Window/Light Probe Rebuilder")]
static void OpenNewWindow ()
{
var window = EditorWindow.GetWindow (typeof(LightProbeRebuilderWindow));
window.title = "Light Probe Rebuilder";
}
string ExtractCoefficients ()
{
var probes = LightmapSettings.lightProbes;
if (probes && probes.count > 0)
{
var text = "";
for (var i = 0; i < 26; i++)
text += probes.coefficients [i] + ",";
return text + probes.coefficients [26];
}
else
{
return "There is no light probe.";
}
}
void RebuildFromInput ()
{
var probeGroup = FindObjectOfType (typeof(LightProbeGroup)) as LightProbeGroup;
if (probeGroup == null)
{
var go = new GameObject ("Probes");
probeGroup = go.AddComponent<LightProbeGroup> ();
}
probeGroup.probePositions = new Vector3[]{
new Vector3 (0, 100, 100),
new Vector3 (100, 100, 0),
new Vector3 (-100, 100, 0),
new Vector3 (0, 200, 0)
};
var coeffs = new float[27 * 4];
var textLines = text.Split (',');
for (var i = 0; i < 27; i++)
{
var coeff = float.Parse (textLines [i]) * multiplier;
for (var i2 = 0; i2 < 4; i2++)
coeffs [i + i2 * 27] = coeff;
}
Lightmapping.BakeLightProbesOnly ();
LightmapSettings.lightProbes.coefficients = coeffs;
}
void OnGUI ()
{
var style = EditorStyles.textField;
style.wordWrap = true;
text = EditorGUILayout.TextField ("Coefficients", text, style, GUILayout.ExpandHeight (true));
multiplier = EditorGUILayout.Slider ("Multiplier", multiplier, 0.1f, 10.0f);
EditorGUILayout.BeginHorizontal ();
if (GUILayout.Button ("Rebuild Light Probes"))
RebuildFromInput ();
if (GUILayout.Button ("Extract Coefficients"))
text = ExtractCoefficients ();
EditorGUILayout.EndHorizontal ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment