Skip to content

Instantly share code, notes, and snippets.

@howiemnet
Created December 7, 2016 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howiemnet/49eab3f27a523559e4dfebebc9e71167 to your computer and use it in GitHub Desktop.
Save howiemnet/49eab3f27a523559e4dfebebc9e71167 to your computer and use it in GitHub Desktop.
After Effects Expression to control flare brightness from spotlight orientation
// AE expression to modulate a spotlight's flare brightness
// depending on whether the spot is facing into or away
// from the camera
// h 6/12/2016
// change these to reference your spotlight
// and camera as appropriate
L = thisComp.layer("Light 1");
C = thisComp.activeCamera;
// maximum and minimum brightness
// these are multiplied by the existing brightness
// parameter, so you can still tweak the overall
// flare brightness in the usual way
maxBrightness = 1;
minBrightness = 0.2;
// our calculations are based on 0 --> 180
// light pointing AT camera --> AWAY from cam
// so halve the light cone angle for simplicity
coneAngle = L.lightOption.coneAngle / 2;
coneFeatherAngle = coneAngle - (coneAngle * L.lightOption.coneFeather * 0.01);
// work out the angle between the light's direction
// and the vector between camera and light
// (all credit Dan Ebberts)
v1 = normalize(C.toWorld([0,0,0]) - L.toWorld([0,0,0]));
v2 = L.toWorldVec([0,0,1]);
relativeAngle = radiansToDegrees( Math.acos (dot(v1,v2)))
// map the angle to the brightness multipliers
if (relativeAngle <= coneFeatherAngle) {
output = maxBrightness;
} else {
if (relativeAngle >= coneAngle) {
output = minBrightness;
} else {
output = linear (relativeAngle, coneFeatherAngle, coneAngle, maxBrightness, minBrightness);
}
}
// multiply our final output multplier by
// the existing brightness parameter
output * value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment