Shadow maps and projected textures
Both look at the scene from the light. A shadow map renders it from the light into a framebuffer, keeping depth; a fragment is in shadow if something nearer to the light was stored where it falls. A projected texture maps a slide through the light's projection instead. This page combines shadowMap and projectiveTexture, and can draw the left and right halves with one setting changed.
What the samples do, and what this page changes
- Depth in 8 bits. shadowMap.js writes
gl_FragCoord.zinto an RGBA8 colour, so only 256 depths can be told apart. With its light 5.9 units away (near 3, far 10), neighbouring levels are about 0.03 world units apart at the cube, so its bias of 0.005 in depth is about 0.04 units. WebGL2 can render depth directly into aDEPTH_COMPONENT24texture, and compare it in hardware through asampler2DShadow. - A new framebuffer every frame. shadowMap.js creates a framebuffer and a 1024 × 1024 depth renderbuffer inside
render()and never deletes them: 2 MB more per frame until the garbage collector catches up. Here each shadow map is made once. - shadowMap.js's cube has vertices with w = 1.2, so after the divide it is 0.83 wide, not 1. It sets the sampler
"texture"rather thanuTextureMap(harmless: unit 0 is the default). - No lighting. shadowMap.js colours lit fragments with the surface colour and shadowed ones black, so a face turned away from the light still looks lit. Here shading is diffuse, and only faces towards the light can be lit.
- projectiveTexture.js's projection. It computes
0.5*clip.xyz + 0.5and samples withtextureProj, which divides by that third component,0.5*z + 0.5, not by w. At the cube's corners the result is up to 0.9 texture widths from the right place. Its projector also sits atnormalize(lightPosition), 1 unit from the origin, while its facing test uses the light's real position. - rotateY. Your
MV.mjsdefinesrotateYbut does not export it, unlikerotateXandrotateZ; an ES-module import of it fails. These pages userotate(angle, vec3(0, 1, 0)).