sampleImage() is a powerful new addition to AE's expressions arsenal, and it opens the doors to a lot of new possibilities. One is idea is to use an "obscuration" layer to modulate the intensity of the light source on another layer. As a specific example, let's look at how we can use sampleImage() to cause a cloud layer to affect the intensity of a lens flare.

First we need to create a cloud layer. We can use AE's Fractal Noise effect and then apply the Knoll Unmult effect (free at Red Giant Software) to create an alpha channel (so any underlying layers will be visible). Then we can apply a lens flare to a layer below the cloud layer. Then we can add a sampleImage() expression to the lens flare's Flare Brightness parameter. As a sample point, we'll use the lens flare's Flare Center parameter (but applied to the cloud layer). We'll specify a 7 pixel by 7 pixel sample area (which seems to strike a good balance between a representative sample size and render time).

Note that sampleImage() will take up to four arguments, but in this case we only need to specify the first two because the default values for the third and fourth arguments are already what we need. If we needed the sampling to occur before masks or effects are applied, we could set the third argument to false, but since we need the sampling to occur after the effects (because the clouds and alpha channel are generated by effects), we'll just accept the default value of true.

Once we've retrieved the sample data from the cloud layer, we can isolate the alpha value from the returned [r,g,b,a] array by using an index of three. Since the color and alpha values returned are numbers between 0.0 and 1.0, we'll use our old friend linear() to translate the alpha value to an appropriate corresponding value of Flare Brightness.

Here's the expression:


target = thisComp.layer("clouds");
alpha = target.sampleImage(effect("Lens Flare")("Flare Center"),[3.5, 3.5])[3];
linear(alpha, 0, 0.36, 80, 10)

Using sampleImage(), we can cause the opacity of the clouds to affect the brightness of the lens flare.

Kudos to Trent Armstrong for the idea. Check out Trent's Vimeo page here.