Hello!
I've read MJP's great post on CSM and jiggling/shimmering and it seem to be the exact problem i'm having (I realise it's from 7 years ago or so but it seems very relevant)
I was hoping someone you had time to help me solve it or point me in the right direction. I feel like i've learned the concepts and read everything I can find on the subject but I just can't seem to solve it. I'm more than happy to pay someone for their time, I just want this solved so I can forget about it because my game is at a fun stage were I can really start adding all the good bits (combat and spell effects, dungeons, swords etc).
Anyway..
Here is a youtube video of the problem. I turn on the render target view about halfway through so you can see the shadow map top right. I've turned off all but the closest cascade for now and stretched it rather far so the quality isn't amazing but it shows the jiggling problem nicely.
Please help
My method for making the projection is pretty short and is very similar to MJPs post:
public void GenerateCSMOrthoSliceTS(float pNearClip, float pfarClip)
{
Vector3[] frustumCorners = new Vector3[8];
Matrix mCameraViewProj = _Camera.CameraView;
mCameraViewProj *= Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, _Camera._aspectRatio, pNearClip, pfarClip);
BoundingFrustum oCameraViewProjFrustum = new BoundingFrustum(mCameraViewProj);
frustumCorners = oCameraViewProjFrustum.GetCorners();
Vector3 frustumCenter = new Vector3(0, 0, 0);
for (int i = 0; i < 8; i++)
frustumCenter += frustumCorners[i];
frustumCenter /= 8;
// don't bother recaculating the radius if we've already done it
if (radius == 0)
radius = (frustumCorners[0] - frustumCorners[6]).Length() / 2.0f;
Vector3 eye = frustumCenter + (SunlightDirection * radius);
ShadowLightPos = eye;
Vector3 ShadowLookAt = frustumCenter;
ShadowLightView = Matrix.CreateLookAt(eye, ShadowLookAt, new Vector3(0, 1, 0));
ShadowLightProjection = Matrix.CreateOrthographicOffCenter(-radius, radius, -radius, radius, -radius * 8.0f, radius * 8.0f);
ShadowLightViewProjectionMatrix = ShadowLightView * ShadowLightProjection;
if (_nojiggle)
{
float ShadowMapSize = 4096.0f; // Set this to the size of your shadow map
Vector3 shadowOrigin = Vector3.Transform(Vector3.Zero, ShadowLightViewProjectionMatrix);
shadowOrigin *= (ShadowMapSize / 2.0f);
Vector2 roundedOrigin = new Vector2((float)Math.Round(shadowOrigin.X), (float)Math.Round(shadowOrigin.Y));
Vector2 rounding = roundedOrigin - new Vector2(shadowOrigin.X, shadowOrigin.Y);
rounding /= (ShadowMapSize / 2.0f);
Matrix roundMatrix = Matrix.CreateTranslation(rounding.X, rounding.Y, 0.0f);
ShadowLightViewProjectionMatrix *= roundMatrix;
}
}
↧