shadowCoord.xy += shadowCoord.w;
?
What. Is this some kind of hack to add 1 to x and y to go from [-1, 1] to [0, 1]?
Short version for theagent:
It's not a "hack". But that its just for that.
Long version with explanation:
Basically we want to shift from unit cube coordinates to texture coordinates.
[-1:1] -> [0:1] So: (uv +1) / 2 is what we want to do.
But that can be done only after we are back from homogeneous coordinates. And this can be done by dividing coordinate with w. But that is not linear operation and can't be done at vertex shader becouse of interpolation. So it has to be done at fragment level. This can be done by using textureProj2d wich is just simply texture2d with cheap w divide by hardware.
So instead of doing this at fragment level:
1 2
| shadowCoord.xy /= shadowCoord.w; shadowCoord.xy = shadowCoord.w * 0.5 + 0.5; |
So It can be done:
1 2 3 4 5 6 7
| shadowCoord.xy += shadowCoord.w; shadowCoord.xy *= 0.5;
shadowCoord.xy /= shadowCoord.w;
|
Edit: Just thinked it trougth and noticed that simple method work allways when w is 1. But can that be guaranteed for shadow mapping? But this method work also for deferred renderer where you want to map world position to screen coordinates without additional fragment load.