🎨 Fragment Shader Demo

Pixel Coloring & Phong Lighting

← Home → See Vertex Shader Demo
What You're Seeing:
• Full Phong lighting (ambient + diffuse + specular)
• Shiny highlights that move with rotation
• Smooth shading across faces
• Fragment shader calculates each pixel

Fragment Shader Role

• Calculates final pixel color
• Runs ONCE per pixel (thousands/frame)
• Applies lighting model
• Creates shadows & highlights

Theory: Rasterization

After the vertex stage, each triangle is converted to pixels. Every covered pixel gets interpolated vNormal, vWorldPos and vColor (barycentric weights), and the fragment shader runs once for it.

Theory: Phong / Blinn-Phong

color = (ambient + diffuse + specular) · baseColor ambient = 0.3 N = normalize(vNormal) L = normalize(lightPos - worldPos) V = normalize(viewPos - worldPos) H = normalize(L + V) (half vector) diffuse = max(N·L, 0) (Lambert's cosine law) specular = 0.5 · max(N·H, 0)^32 (shininess = 32)

Theory: Reading the Terms

• Ambient: constant floor so shadowed faces are not black
• Diffuse: brightest when the surface faces the light (N·L = 1), zero at 90° or more
• Specular: sharp highlight when H aligns with N; higher exponent = smaller, tighter spot
• Because it runs per pixel, highlights move smoothly as the cube rotates

Theory: Vertex vs Fragment Cost

A cube has 24 vertices but covers thousands of pixels. Per-pixel lighting (this demo) is more accurate than per-vertex lighting (Gouraud) but costs more per frame.