Skip to content

Instantly share code, notes, and snippets.

@tylorr
Last active August 29, 2015 14:09
Show Gist options
  • Save tylorr/a8f07dee9b8890ae9fa8 to your computer and use it in GitHub Desktop.
Save tylorr/a8f07dee9b8890ae9fa8 to your computer and use it in GitHub Desktop.
Decode light probe coefficients
using UnityEngine;
using System.Collections;
public class LightprobDecode : MonoBehaviour {
public Renderer ReferenceRenderer;
public Transform DirLightTransform;
void Awake () {
const float kInv2SqrtPI = 0.28209479177F;
const float kNormalization = 2.95679308573F;
const float kSqrt3Div2SqrtPI = 0.4886025119F;
float[] coefficients = new float[27];
LightmapSettings.lightProbes.GetInterpolatedLightProbe(ReferenceRenderer.transform.position, ReferenceRenderer, coefficients);
float rScale = coefficients[0] / kInv2SqrtPI;
float gScale = coefficients[1] / kInv2SqrtPI;
float bScale = coefficients[2] / kInv2SqrtPI;
float rColor = rScale / (kNormalization * 2.0f);
float gColor = gScale / (kNormalization * 2.0f);
float bColor = bScale / (kNormalization * 2.0f);
float rDirY = 0;
if (rScale > 0) rDirY = -coefficients[3] / (kSqrt3Div2SqrtPI * rScale);
float gDirY = 0;
if (gScale > 0) gDirY = -coefficients[4] / (kSqrt3Div2SqrtPI * gScale);
float bDirY = 0;
if (bScale > 0) bDirY = -coefficients[5] / (kSqrt3Div2SqrtPI * bScale);
float rDirZ = 0;
if (rScale > 0) rDirZ = coefficients[6] / (kSqrt3Div2SqrtPI * rScale);
float gDirZ = 0;
if (gScale > 0) gDirZ = coefficients[7] / (kSqrt3Div2SqrtPI * gScale);
float bDirZ = 0;
if (bScale > 0) bDirZ = coefficients[8] / (kSqrt3Div2SqrtPI * bScale);
float rDirX = 0;
if (rScale > 0) rDirX = -coefficients[9] / (kSqrt3Div2SqrtPI * rScale);
float gDirX = 0;
if (gScale > 0) gDirX = -coefficients[10] / (kSqrt3Div2SqrtPI * gScale);
float bDirX = 0;
if (bScale > 0) bDirX = -coefficients[11] / (kSqrt3Div2SqrtPI * bScale);
var rDir = new Vector3(rDirX, rDirY, rDirZ);
var gDir = new Vector3(gDirX, gDirY, gDirZ);
var bDir = new Vector3(bDirX, bDirY, bDirZ);
Debug.Log (-DirLightTransform.forward);
var colorTimesIntensity = new Color(rColor, gColor, bColor);
Debug.Log(colorTimesIntensity);
Debug.Log (rDir);
Debug.Log (gDir);
Debug.Log (bDir);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment