I have an 8192x8192 texture atlas that contains 1024x1024 shadow maps aligned side by side. I'm trying to calculate the uv coordinates so that I can sample each shadow map appropriately based on the 'shadowIndex' of my light. With a single non-atlas shadowmap I can easily calculate the uv coordinates like this:
tc.x = lightViewPosition.x / lightViewPosition.w / 2.0f + 0.5f;
tc.y = -lightViewPosition.y / lightViewPosition.w / 2.0f + 0.5f;
shadowFactor += shadowMap.SampleCmpLevelZero(SampleComp, tc, lightDepthValue);
How can I replicate the same thing while also factoring in an offset for sampling the correct shadow map from the atlas? I tried this:
tc.x = lightViewPosition.x / lightViewPosition.w / 2.0f + 0.5f;
tc.y = -lightViewPosition.y / lightViewPosition.w / 2.0f + 0.5f;
float tileSize = 1024.0f / 8.0f;
float tileOffset = tileSize * lights[i].shadowIndex;
tc += frac((tc - tileOffset) / tileSize) * tileSize + tileOffset;
But this does not work correctly. Additionally I have to consider that the y (V) coordinate should not be affected while the shadow index isn't a multiple of 8 because there are 8 shadow maps per row. Any ideas? Thanks.