Post

Replies

Boosts

Views

Activity

Reply to Shadows: Using a shadow map
After over a week of wandering around in the dark, I discovered how convert the coordinates. The input shadowSpacePosition is in Metal Normalized Coordinates: x and y -1 to 1, z 0 to 1 with the origin at the center of the drawable. When this is stored in the depth texture it is converted to texture coordinates: x,y and z 0 to 1 with the origin on the bottom-left. So to read the proper depth in the depth texture the code is: &#9;&#9; constexpr sampler shadowSampler(coord::normalized, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; filter::linear, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; address::clamp_to_zero, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; compare_func:: less); &#9;&#9;shadowPosition = in.shadowSpacePosition.xyz; &#9;&#9;shadowPosition.y *= -1; &#9;&#9;shadowPosition.xy += 1.0; &#9;&#9;shadowPosition.xy /= 2; &#9;&#9;cmp = shadowTexture.sample_compare(shadowSampler, shadowPosition.xy , shadowPosition.z); &#9; if(cmp < 0.1) &#9;&#9;&#9; isShadow = true; &#9;&#9;else &#9;&#9;&#9;&#9;isShadow = false;
Dec ’20