Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

## rendering

A simulation of synthesis with a single point of light and no attenuation

The same synthetic and optical decay expression is applied to the specular and diffuse properties

Note that as the layers move away from the light, they become darker

## idea

One of the challenges of using 3D lighting in After Effects is that the intensity of the light doesn’t decrease with distance. In the real world, the intensity of light decays with the square of the distance to the light. What we want to do is come up with a convincing simulation for this natural phenomenon. There should be variables that allow us to specify how fast the light decays with distance, and how close the layer must be to the light so that it does not decay.

## design

When you convert a layer to 3D, it picks up new properties related to the layer’s response to lights in the scene. We’ll use two of these attributes, Diffuse and Specular, to create our simulation. We’re going to apply the expression to both of these properties. For simplicity, let’s assume there is only one light in the scene. We will first calculate how far away our layer is from the light. If we are within the “no decay” threshold, we do nothing. If the threshold is exceeded, we decrease the parameter value exponentially as the distance increases, using math.exp ().

## Expression code

``````decay = 005.; ///< the decay rate of light with increasing distance
noFalloff = 200; < There will be no attenuation within this distance (pixels) from the light source

L = thisComp.layer("Light 1");

// @note The distance between the light source and this layer
d = length(L.transform.position, transform.position);
if (d < noFalloff) { / / / < no attenuation
value;
} else { / / / < attenuation
value / Math.exp((d - noFalloff) * decay);
}
Copy the code``````