WebGL Lambertian shading
Move your cursor around the square above. Your cursor moves a light which illuminates a decorative relief. This is implemented with WebGL, using the fragment shader to the right. The fragment shader illuminates each pixel by consulting the following “normal map” image:
The normal map tells us, for each pixel, the “normal” at that point on the surface.
The normal is a vector which points perpendicular to the surface.
This vector has XYZ components which are encoded as RGB values.
The X component is encoded by the amount of red, and so on.
The red is in the range
and we convert this to the X component in the range
The surface of the tile is completely matte.
It’s like wood, not like metal.
This is because the shader uses “Lambertian shading”,
which is a model of matte objects.
In Lambertian shading,
the intensity is proportional to
We can interpret this formula as answering the question,
“is the surface facing the light?”.
Note in particular that this formula does not depend on
The illumination of shiny objects depends on
because light is reflected towards the camera.
The illumination of matte objects does not depend on
because the matte surface scatters light equally in all directions.
Thanks to Sophie Lantreibecq, Ben Ellison, Andy Jones, Andreas Frisch, Luís Fonseca, and Callum Oakley for helping me debug this over many beers.
More by Jim
- Your syntax highlighter is wrong
- Granddad died today
- The Three Ts of Time, Thought and Typing: measuring cost on the web
- I hate telephones
- The sorry state of OpenSSL usability
- The dots do matter: how to scam a Gmail user
- My parents are Flat-Earthers
- How Hacker News stays interesting
- Project C-43: the lost origins of asymmetric crypto
- The hacker hype cycle
- The inception bar: a new phishing method
- Time is running out to catch COVID-19
- A probabilistic pub quiz for nerds
- Smear phishing: a new Android vulnerability
Tagged . All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.