Add distance falloff to a point light so illumination changes naturally over space.
Add distance attenuation to a point light so nearby fragments receive more energy than distant ones.
Overview
- Use a point light with a visible position.
- Fade the light naturally as distance increases.
Learning Objectives
- Understand the distance relationship between light position and fragment position
- Control illumination with an attenuation formula
- Observe how spatial distance changes the rendered result
Prerequisites
- point-light
- ambient-diffuse
Key Concepts
- A point light depends on position, not only direction.
- Distance attenuation often looks like
1.0 / (1.0 + k * d * d). - Diffuse lighting and attenuation should be multiplied together.
How To Implement (Step-by-step)
- Define a moving or fixed light position above the sphere.
- Compute the vector from the fragment to the light.
- Normalize that vector for diffuse shading.
- Use the original distance to build the attenuation term.
- Multiply diffuse by attenuation before outputting the color.
Self-check
- Is the sphere brightest near the light source?
- Does the brightness drop as the light moves away?
- Is the falloff smooth instead of clipped?
Common Mistakes
- Using the normalized light vector to measure distance.
- Applying attenuation before computing diffuse.
- Making the attenuation constant so strong that the sphere becomes black.
Answer Breakdown
Directional lights only need a direction. Point lights need a position as well.
Light Vector
First compute the vector from the current fragment position to the light position.
Distance
Store the distance before normalizing the vector, because attenuation needs the real length.
Attenuation
Use distance to reduce the light contribution smoothly. A squared distance often gives a clearer spatial effect.