4
\$\begingroup\$

I have tried to reduce the shadow shimmering/flickering using the technique described here: http://msdn.microsoft.com/en-us/library/windows/desktop/ee416324%28v=vs.85%29.aspx

It works as I want and shimmering is reduced but sometimes I have artifacts. It looks like my code tries to index space outside the shadow map. The article above writes about it but I didn't find a solution.

Screen

When I played with the code I also got black strips on the corners.

Code:

// reduce shadow shimmering/flickering
Vector2 vecWorldUnitsPerTexel = Vector2(D.x() / (float)D.shadowMapSize(), D.y() / (float)D.shadowMapSize());

// get only x and y dimensions
Vector2 min2D = min.vector2(),
                max2D = max.vector2();

min2D /= vecWorldUnitsPerTexel;
min2D = Round(min2D);
min2D *= vecWorldUnitsPerTexel;

max2D /= vecWorldUnitsPerTexel;
max2D = Round(max2D);
max2D *= vecWorldUnitsPerTexel;

min.set(min2D, min.z);
max.set(max2D, max.z);

// crop matrix based on this article https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch10.html
Vector scale;
Vector offset;
scale.x  = 2.0f / (max.x - min.x);
scale.y  = 2.0f / (max.y - min.y);
scale.z =  1.0f / (max.z - min.z);

offset.x = -0.5f * (max.x + min.x) * scale.x;
offset.y = -0.5f * (max.y + min.y) * scale.y;
offset.z = -min.z * scale.z;

Matrix4 m;
m.x = Vector4(scale.x, 0, 0, 0);
m.y = Vector4(0, scale.y, 0, 0);
m.z = Vector4(0, 0, scale.z, 0);
m.w = Vector4(offset.x, offset.y, offset.z, 1.0f);

I think that I should store in the depth map a slightly larger area but I'm not sure how to do this. I tried to change the scale of the crop matrix but it doesn't help.

EDIT: It seems I've found a solution. When I'm rounding (or flooring) min and max values I subtract one from the min value and add one to the max value. This makes the shadow map contain a slightly larger area and I don't see any artifacts.

\$\endgroup\$
  • \$\begingroup\$ If you've solved your problem, please post your solution as an Answer, not as an edit tucked inside the question. This doesn't prevent other users from also answering if you'd still like to gather alternative solutions. \$\endgroup\$ – DMGregory Nov 25 '19 at 14:56
  • \$\begingroup\$ Shall I do that now or this is just tip for the future? \$\endgroup\$ – Harry Nov 25 '19 at 21:33
  • \$\begingroup\$ I'd happily upvote the answer now if you can flesh it out in a bit more detail. :) \$\endgroup\$ – DMGregory Nov 25 '19 at 22:28
  • \$\begingroup\$ Sorry, I checked the code and unfortunately the implementation completely changed through the years. Now it is combined with other effects like CSM. I'm not sure what prevents from this issue right now as the part responsible for flickering reduction is the same. Probably there was something wrong with the final matrix in my sample code. \$\endgroup\$ – Harry Nov 26 '19 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.