Created
June 9, 2016 09:25
-
-
Save starry-abyss/fe0e5aaa991906f67a4d6c1ae2f4f8b5 to your computer and use it in GitHub Desktop.
Storing ambient reflection in Unity 5 for runtime switching
This file contains 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.Collections; | |
using UnityEngine.Rendering; | |
public class ChangeLightScript : MonoBehaviour { | |
public Light sceneLight; | |
public Color fogColor; | |
public Cubemap nightSkyReflection; | |
SphericalHarmonicsL2 ambientProbeNight; | |
public Color[] ambientProbeNightColors = new Color[9]; | |
Material skyboxMaterial; | |
public Color ambientColorNight = new Color(0.2f, 0.2f, 0.2f); | |
Color originalAmbientColor; | |
Cubemap originalSkyReflection; | |
Color originalFogColor; | |
SphericalHarmonicsL2 originalAmbientProbe; | |
Material originalSkybox; | |
int skyNumber = 0; | |
// Use this for initialization | |
void Start () { | |
skyboxMaterial = GetComponent<Skybox>().material; | |
originalSkyReflection = RenderSettings.customReflection; | |
originalFogColor = RenderSettings.fogColor; | |
originalAmbientColor = RenderSettings.ambientSkyColor; | |
originalAmbientProbe = RenderSettings.ambientProbe; | |
originalSkybox = RenderSettings.skybox; | |
ambientProbeNight = new SphericalHarmonicsL2(); | |
string shString = "night SH {"; | |
for (int index = 0; index < 9; ++index) | |
{ | |
//Debug.Log(index); | |
Color shColor = ambientProbeNightColors[index]; | |
ambientProbeNight[0, index] = shColor.r; | |
ambientProbeNight[1, index] = shColor.g; | |
ambientProbeNight[2, index] = shColor.b; | |
shString += string.Format("[{0}, {1}, {2}] ", ambientProbeNight[0, index], ambientProbeNight[1, index], ambientProbeNight[2, index]); | |
} | |
shString += "}"; | |
//Debug.Log(shString); | |
} | |
// Update is called once per frame | |
void Update () { | |
/*if (Input.GetKeyDown(KeyCode.Space)) | |
{ | |
if (skyNumber == 0) | |
{ | |
SetNight(); | |
skyNumber = 1; | |
} | |
else | |
{ | |
SetDay(); | |
skyNumber = 0; | |
} | |
}*/ | |
} | |
void SetDay() | |
{ | |
if (originalAmbientProbe != null) | |
RenderSettings.ambientProbe = originalAmbientProbe; | |
if (sceneLight != null) | |
{ | |
sceneLight.enabled = true; | |
} | |
if (skyboxMaterial != null) | |
RenderSettings.skybox = originalSkybox; | |
if (originalSkyReflection != null) | |
RenderSettings.customReflection = originalSkyReflection; | |
RenderSettings.fogColor = originalFogColor; | |
RenderSettings.ambientSkyColor = originalAmbientColor; | |
} | |
void SetNight() | |
{ | |
if (ambientProbeNight != null) | |
RenderSettings.ambientProbe = ambientProbeNight; | |
if (nightSkyReflection != null) | |
RenderSettings.customReflection = nightSkyReflection; | |
if (sceneLight != null) | |
{ | |
sceneLight.enabled = false; | |
} | |
if (skyboxMaterial != null) | |
RenderSettings.skybox = skyboxMaterial; | |
RenderSettings.fogColor = fogColor; | |
RenderSettings.ambientSkyColor = ambientColorNight; | |
} | |
void OnPreCull() { | |
SetNight(); | |
} | |
void OnPostRender() { | |
SetDay(); | |
} | |
} |
This file contains 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 UnityEditor; | |
using UnityEngine; | |
using UnityEngine.Rendering; | |
[CustomEditor(typeof(ChangeLightScript))] | |
public class ChangeLightScriptEditor : Editor { | |
public override void OnInspectorGUI() { | |
DrawDefaultInspector(); | |
if(GUILayout.Button("Store ambient GI")) | |
{ | |
ChangeLightScript myScript = (ChangeLightScript) target; | |
//myScript.BuildObject(); | |
SphericalHarmonicsL2 sh = RenderSettings.ambientProbe; | |
Color[] shColors = new Color[9]; | |
string shString = "SH {"; | |
for (int index = 0; index < 9; ++index) | |
{ | |
shColors[index] = new Color(sh[0, index], sh[1, index], sh[2, index]); | |
//Debug.Log(index); | |
shString += string.Format("[{0}, {1}, {2}] ", sh[0, index], sh[1, index], sh[2, index]); | |
} | |
myScript.ambientProbeNightColors = shColors; | |
shString += "}"; | |
Debug.Log(shString); | |
//GUILayout.Label("OK!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment