Ambient + Diffuse

1 / 17
Learn core lighting terms (diffuse/specular/rim) on a simple sphere.

Adding a brightness floor to prevent completely black shadows comes down to this line:

Let's break it down.


Why ambient light is needed

Pure diffuse shading leaves back-facing surfaces totally black. In reality, light bounces around a scene and areas with no direct illumination still receive some indirect light. Ambient lighting is the simplest approximation of that — a fixed, direction-independent brightness layer applied to everything.

In code:


How amb and diff work together

They are simply added together:

amb is a constant (0.18 = 18%) that never changes regardless of surface direction. diff is the diffuse term — close to 1.0 where the surface faces the light, and 0.0 on the shadow side.

Combined: the bright side approaches 1.18 (slightly clipped), while the shadow side holds at 18% instead of going black.


Try changing it

ChangeEffect
amb = 0.18 to 0.05Darker shadows, higher contrast
amb = 0.18 to 0.5Shadow side gets very bright, the sphere looks flat
(amb + diff) to mix(amb, 1.0, diff)Smooth blend between ambient and diffuse, avoids overexposure

Exercise

In the exercise, amb = 0.0 so the shadow side is completely black. Fix the TODO line by setting amb to an appropriate value (like 0.18) to give dark areas some base brightness.

Answer Breakdown

Starting state: amb = 0.0 means no ambient contribution — the shadow side looks just like standard Lambert (pure black).

The fix: change amb from 0.0 to 0.18, adding an 18% brightness floor to every pixel regardless of its orientation to the light.

Try setting amb to 0.35, then 0.05, and compare how the overall mood of the sphere changes.

GLSL Code Editor

Correct Code Preview

Current Code Preview