Normal Visualizer
Turning normals directly into color comes down to this one line:
Let's break it down.
What is a normal
A normal is a 3D vector that points away from a surface — think of it as an arrow stuck perpendicularly into the surface, indicating which direction that point "faces." On a sphere, the center normal points straight toward the camera, while edge normals tilt sideways.
Each component (X, Y, Z) ranges from -1.0 to 1.0. Positive X points right, positive Y points up, positive Z points toward the camera.
Why multiply by 0.5 and add 0.5
Normals live in the range [-1, 1], but colors can only display [0, 1] (0% to 100%).
* 0.5 + 0.5 remaps the range from [-1, 1] to [0, 1]:
- Pointing left (-1) → 0% (black)
- Neutral (0) → 50% (mid gray)
- Pointing right (+1) → 100% (bright)
The three axes then map to red (X), green (Y), and blue (Z), producing the colorful gradient you see on the sphere.
Try changing it
| Change | Effect |
|---|---|
Replace n * 0.5 + 0.5 with abs(n) | Only positive values remain — symmetric color distribution |
Use vec4(vec3(n.z * 0.5 + 0.5), 1.0) | Show only the Z axis — center bright, edges dark |
Use vec4(vec3(n.x * 0.5 + 0.5), 1.0) | Show only the X axis — a left-to-right grayscale gradient |
Exercise
The sphere in the exercise is completely black. Fill in the TODO to display the normal as color.
Answer Breakdown
Starting state: c is hardcoded as vec3(0.0), making the sphere black.
The fix: multiply the normal n by 0.5 and add 0.5 to shift its range from [-1, 1] into [0, 1] so it can be used as a color. Each axis maps to one color channel (X→red, Y→green, Z→blue).
Try changing * 0.5 + 0.5 to * 0.5 + 0.3 and see whether the overall sphere gets darker or lighter.