home/ atoms/ phong-reflection-model

The Phong model sums ambient, diffuse, and specular components to shade 3D surfaces

The Phong reflection model approximates surface lighting by adding three terms. Ambient is a constant base color simulating indirect light. Diffuse (Lambertian) scales with the dot product of the surface normal and light direction. Specular produces the glossy highlight: it depends on the reflected light direction and view direction dot product raised to a shininess exponent. High shininess gives a tight bright spot; low gives a broad dull sheen. The final color is ambient + diffuse + specular. Each term is scaled by a material constant and a light color. The model is physically approximate but cheap and widely used in real-time shader code.

Examples

vec3 phong(vec3 lightDir, vec3 normal, vec3 rd, Material mat) {
  vec3 ambient  = mat.ambientColor;
  vec3 diffuse  = mat.diffuseColor * clamp(dot(lightDir, normal), 0., 1.);
  vec3 R        = reflect(lightDir, normal);
  vec3 specular = mat.specularColor * pow(clamp(dot(R, -rd), 0., 1.), mat.alpha);
  return ambient + diffuse + specular;
}

Assessment

Add a Phong shader to a raymarched sphere. Separately zero out each term and describe the visual effect of each in isolation.

“materials such as metals and polished surfaces have specular reflection that look brighter depending on the camera angle”