ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# How to convert an orientation to a vector? (for a more realistic lens flare)

FAQ   •   VIEW ALL
 How to convert an orientation to a vector? (for a more realistic lens flare) on Dec 5, 2016 at 8:54:12 am

short version:

How can I convert a layer/light's orientation into a vector, so I can use vector math on it?

long version:

I've got a spotlight in a comp, and as it moves around, sometimes it's pointing toward the camera, sometimes it turns away. Trapcode Lux lets us see the beam, and I'm using VC Optical Flares to show us the "light source".

In real life, a lens flare will be brighter if a light is pointing at the camera, and less bright if it's pointing away. So I'm trying to find a way to calculate the difference in angles between the spotlight orientations and an imaginary line joining the camera and the light. Then, depending on the size of the light cone, I can attenuate the flare brightness.

So far I've worked out:
- vector maths seems to be the way to do this, rather than trig
- angle between two vectors can be done with Dan's helpful snippet of code below
- my first vector is simply (light.position - camera.position)
- the other vector needs to come from the light orientation. But my light is animated with Orientation values rather than a point of interest, as the animation is actually generated in Blender. I could do a hacky thing where I just parent a Null to the light as a sort of reverse-engineered-point-of-interest, but it's ... hacky.

So I need to convert the light's orientation into a (normalisable) vector. How do I do this? Thanks!

`angle = radiansToDegrees( Math.acos( dot( normalize(vector1), normalize(vector2) ) ) );`

 Re: How to convert an orientation to a vector? (for a more realistic lens flare)on Dec 5, 2016 at 5:04:33 pm

This should give you 1.0 when the light is pointed at the layer and -1.0 when it's point away from the layer.\:

L = thisComp.layer("Light 1");
v1 = normalize(toWorld(anchorPoint) - L.toWorld([0,0,0]));
v2 = L.toWorldVec([0,0,1]);
dot(v1,v2)

Dan

 Re: How to convert an orientation to a vector? (for a more realistic lens flare)on Dec 6, 2016 at 11:17:57 am

Thanks Dan!

The finished expression is below. With a spotlight and a camera in a comp, if you apply this expression to a Lens Flare brightness parameter, then the flare will adjust its brightness automatically depending on whether the light is glaring into the camera or away. Just like a light house. And it respects the spotlight's cone angle and feather parameters.

Many, many thanks :)

```// 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 &lt;= 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```

 Re: How to convert an orientation to a vector? (for a more realistic lens flare)on Dec 6, 2016 at 11:21:04 am

Gah... the line toward the end reading
"if (relativeAngle & lt;= coneFeatherAngle) {"