Stable Cascaded Shadow Maps - Sphere Based Bounding Help

Started by
11 comments, last by skyemaidstone 6 years, 6 months ago

Hiya again guys,

I have a niggling problem with my CSM. The middle an far cascades noticeably "swim" as you move around. It's not really noticeable on the close one but i'm that's probably just due to the filtering and the factor it has such a high quality map to work from.

Anyway I have 3 cascades and i'm using a bounding box for each. They all overlap with they near clip and this works fairly nicely but I was looking at the csm sample that some nicely ported over to monogame. The problem is I can't get the sphere based bounding boxes working at all for me. My debug rendertargets for all the cascades are just blank. If I switch my camera to be the shadow light camera (the sun) it seems fine (and it works using my bounding box method so i think that's fine). I THINK i'm just calculating the bounding box (using a sphere) incorrectly some how.

Could someone help me make this method use sphere's instead please:


public void GenerateCSMOrthoSlice(float pfarClip)
        {
            Vector3[] frustumCornersWS = new Vector3[8];
            Vector3[] frustumCornersLS = new Vector3[8];
            BoundingFrustum viewFrustum = new BoundingFrustum(_Camera.CameraView * Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, _Camera._aspectRatio, 10, pfarClip));
            frustumCornersWS = viewFrustum.GetCorners();


            Vector3 frustumCentroid = new Vector3(0, 0, 0);
            for (int i = 0; i < 8; i++)
                frustumCentroid += frustumCornersWS;
            frustumCentroid /= 8;

            lightsView = Matrix.Identity;
            lightsViewProjectionMatrix = Matrix.Identity;

            ShadowLightPos = frustumCentroid + (SunlightDirection * 100);

            ShadowLookAt = frustumCentroid;

            ShadowLightView = Matrix.CreateLookAt(ShadowLightPos, ShadowLookAt, new Vector3(0, 1, 0));

            Vector3.Transform(frustumCornersWS, ref ShadowLightView, frustumCornersLS);

            Vector3 mins = frustumCornersLS[0];
            Vector3 maxes = frustumCornersLS[0];
            for (int i = 0; i < 8; i++)
            {
                if (frustumCornersLS.X > maxes.X)
                    maxes.X = frustumCornersLS.X;
                else if (frustumCornersLS.X < mins.X)
                    mins.X = frustumCornersLS.X;
                if (frustumCornersLS.Y > maxes.Y)
                    maxes.Y = frustumCornersLS.Y;
                else if (frustumCornersLS.Y < mins.Y)
                    mins.Y = frustumCornersLS.Y;
                if (frustumCornersLS.Z > maxes.Z)
                    maxes.Z = frustumCornersLS.Z;
                else if (frustumCornersLS.Z < mins.Z)
                    mins.Z = frustumCornersLS.Z;
            }

            float diagonalLength = (frustumCornersWS[0] - frustumCornersWS[6]).Length();
            diagonalLength += 2;    //Without this, the shadow map isn't big enough in the world.
            float worldsUnitsPerTexel = diagonalLength / (float)4096;

            Vector3 vBorderOffset = (new Vector3(diagonalLength, diagonalLength, diagonalLength) - (maxes - mins)) * 0.5f;
            maxes += vBorderOffset;
            mins -= vBorderOffset;

            mins /= worldsUnitsPerTexel;
            mins.X = (float)Math.Floor(mins.X);
            mins.Y = (float)Math.Floor(mins.Y);
            mins.Z = (float)Math.Floor(mins.Z);
            mins *= worldsUnitsPerTexel;

            maxes /= worldsUnitsPerTexel;
            maxes.X = (float)Math.Floor(maxes.X);
            maxes.Y = (float)Math.Floor(maxes.Y);
            maxes.Z = (float)Math.Floor(maxes.Z);
            maxes *= worldsUnitsPerTexel;

            ShadowLightProjection = Matrix.CreateOrthographicOffCenter(mins.X, maxes.X, mins.Y, maxes.Y, -maxes.Z - 500f, -mins.Z);
            lightsView = Matrix.CreateLookAt(ShadowLightPos, ShadowLookAt, new Vector3(0, 1, 0));
            lightsViewProjectionMatrix = lightsView * ShadowLightProjection;
        }

What I tried (which doesn't work at all) is:


public void GenerateCSMOrthoSlice(float pfarClip)
        {

            bool StabilizeCascades = true;
            Vector3 minExtents = Vector3.Zero;
            Vector3 maxExtents = Vector3.Zero;
            Vector3[] frustumCornersWS = new Vector3[8];
            Vector3[] frustumCornersLS = new Vector3[8];
            BoundingFrustum viewFrustum = new BoundingFrustum(_Camera.CameraView * Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, _Camera._aspectRatio, 10, pfarClip));
            frustumCornersWS = viewFrustum.GetCorners();


            Vector3 frustumCentroid = new Vector3(0, 0, 0);
            for (int i = 0; i < 8; i++)
                frustumCentroid += frustumCornersWS;
            frustumCentroid /= 8;



            // sphere based  cascade
            if (StabilizeCascades)
            {
            
            // This needs to be constant for it to be stable
            var upDir = Vector3.Up;

            // Calculate the radius of a bounding sphere surrounding the frustum corners
            var sphereRadius = 0.0f;
            for (var i = 0; i < 8; ++i)
            {
                var dist = (_frustumCorners - frustumCentroid).Length();
                sphereRadius = Math.Max(sphereRadius, dist);
            }

            sphereRadius = (float)Math.Ceiling(sphereRadius * 16.0f) / 16.0f;

            maxExtents = new Vector3(sphereRadius);
            minExtents = -maxExtents;
            }



            lightsView = Matrix.Identity;
            lightsViewProjectionMatrix = Matrix.Identity;

            //Vector3 sunlightdirection = new Vector3(0.21f, 0.11f, -0.5f);
            ShadowLightPos = frustumCentroid + (SunlightDirection * 100);


            //ShadowLookAt = _SLLookAt;
            ShadowLookAt = frustumCentroid;

            ShadowLightView = Matrix.CreateLookAt(ShadowLightPos, ShadowLookAt, new Vector3(0, 1, 0));

            Vector3.Transform(frustumCornersWS, ref ShadowLightView, frustumCornersLS);

            Vector3 mins = frustumCornersLS[0];
            Vector3 maxes = frustumCornersLS[0];
            for (int i = 0; i < 8; i++)
            {
                if (frustumCornersLS.X > maxes.X)
                    maxes.X = frustumCornersLS.X;
                else if (frustumCornersLS.X < mins.X)
                    mins.X = frustumCornersLS.X;
                if (frustumCornersLS.Y > maxes.Y)
                    maxes.Y = frustumCornersLS.Y;
                else if (frustumCornersLS.Y < mins.Y)
                    mins.Y = frustumCornersLS.Y;
                if (frustumCornersLS.Z > maxes.Z)
                    maxes.Z = frustumCornersLS.Z;
                else if (frustumCornersLS.Z < mins.Z)
                    mins.Z = frustumCornersLS.Z;
            }



            float diagonalLength = (frustumCornersWS[0] - frustumCornersWS[6]).Length();
            diagonalLength += 2;    //Without this, the shadow map isn't big enough in the world.
            float worldsUnitsPerTexel = diagonalLength / (float)4096;

            Vector3 vBorderOffset = (new Vector3(diagonalLength, diagonalLength, diagonalLength) - (maxes - mins)) * 0.5f;
            maxes += vBorderOffset;
            mins -= vBorderOffset;

            mins /= worldsUnitsPerTexel;
            mins.X = (float)Math.Floor(mins.X);
            mins.Y = (float)Math.Floor(mins.Y);
            mins.Z = (float)Math.Floor(mins.Z);
            mins *= worldsUnitsPerTexel;

            maxes /= worldsUnitsPerTexel;
            maxes.X = (float)Math.Floor(maxes.X);
            maxes.Y = (float)Math.Floor(maxes.Y);
            maxes.Z = (float)Math.Floor(maxes.Z);
            maxes *= worldsUnitsPerTexel;

            lightsView = Matrix.CreateLookAt(ShadowLightPos, ShadowLookAt, new Vector3(0, 1, 0));
            lightsViewProjectionMatrix = lightsView * ShadowLightProjection;
            ShadowLightProjection = Matrix.CreateOrthographicOffCenter(mins.X, maxes.X, mins.Y, maxes.Y, -maxes.Z - 500f, -mins.Z);

            if (StabilizeCascades)
            {
                ShadowLightProjection = Matrix.CreateOrthographicOffCenter(minExtents.X, minExtents.Y, maxExtents.X, maxExtents.Y, 0.0f, pfarClip);


                // Create the rounding matrix, by projecting the world-space origin and determining
                // the fractional offset in texel space
                var shadowMatrixTemp = lightsViewProjectionMatrix;
                var shadowOrigin = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);
                shadowOrigin = Vector4.Transform(shadowOrigin, shadowMatrixTemp);
                shadowOrigin = shadowOrigin * (4096 / 2.0f);

                var roundedOrigin = Round(shadowOrigin);
                var roundOffset = roundedOrigin - shadowOrigin;
                roundOffset = roundOffset * (2.0f / 4096);
                roundOffset.Z = 0.0f;
                roundOffset.W = 0.0f;

                ShadowLightProjection.M41 += roundOffset.X;
                ShadowLightProjection.M42 += roundOffset.Y;
                ShadowLightProjection.M43 += roundOffset.Z;
                ShadowLightProjection.M44 += roundOffset.W;
            }

            //
            //lightsView = Matrix.CreateLookAt(ShadowLightPos, ShadowLookAt, new Vector3(0, 1, 0));
            //lightsViewProjectionMatrix = lightsView * ShadowLightProjection;
        }

 

Advertisement

I'm no MonoGame expert, but looking at your code it looks like you mixed up the parameters to Matrix.CreateOrthographicOffCenter() in the stabilized version. 

Thanks, yes. Well spotted. Unfortunately that had no effect. 

Shimmery shadows have been bugging me for ages but I can't seem to figure out how to use the sphere method of making my cascades

If anyone could give  me some code/pseudo code for finding the bounding sphere of my shadow camera that would do. I was reading shaderx7 on this subject. Which was interedting but the math is over my head. I don't mind the wasted space. By the time my game is finished games will be able to support one giant texture lol

Thanks

Here is my code that is used in production (So I know it works ;-) )

 


	V4 vMin = vCorners[0];
	V4 vMax = vCorners[0];
	for(int j = 1; j<8; j++) {
		vMin = VMin(vMin, vCorners[j]);
		vMax = VMax(vMax, vCorners[j]);
	}
	V4 vSize = vMax - vMin;
	float fRadius = VLen3(vSize).x() * 0.5f;
	mShadowProj = MOrthoRH(-fRadius, fRadius, -fRadius, fRadius, -fMaxZ, -fMinZ);

	// Snap center to shadow texel
	// This is done by transforming center of CSM frustum into light post projection (texel space) and
	// perform snapping in this space.
	M44 mCameraToLight = MInverseAffine(mLightToCamera);
	V4 vCenterCamera = VLerp(vMin, vMax, VSplat(0.5f));
	V4 vCenterLight = VTransform43(mCameraToLight, vCenterCamera);
	V4 vCenterTexel = VTransform44(mShadowProj, vCenterLight);
	vCenterTexel = VProject(vCenterTexel);
	uint32 nCascadeSize = descShadowCSM.nWidth >> 1;
	float fShadowSize = nCascadeSize * 0.25f;
	V4 vShadowSize = VSplat(fShadowSize);
	V4 vShadowSizeInv = VSplat(1.0f / fShadowSize);
	V4 vSnap = VMul(vCenterTexel, vShadowSize);
	vSnap = VFloor(vSnap);
	vSnap = VMul(vSnap, vShadowSizeInv);
	V4 vSnapOffset = VLoadZero() - vSnap;
	M44 mSnapTrans = MLoadIdentity();
	mSnapTrans.vTrans = VXY01(vSnapOffset);
	mShadowProj = mSnapTrans * mShadowProj;
     

 

Hope it makes sense.

BTW: Looking at your code I think the issue is the way you apply the offset directly to the matrix instead of doing a multiplication with a translation matrix (Like I do in the last line)

Henning

Oh, and I now use the quantization trick instead of the code above to get more precision (http://dev.theomader.com/stable-csm/)

Thanks, your help is much appreciated. I'm not familiar with that language but i'll see if I can work it out (I'm just a c# xna/monogame guy)

This ia  video of the problem. I'm not if i'm barking up the wrong tree. But shadows are very "jiggly" on some objects. I wanted to try the sphere based approach. Which from my understanding is making a bounding sphere from the shadow camera view frustrum then creating my usual orthographic projection for my cascade from that. Am I undestanding it correctly?

Here's a video of the annoying problem I have. Shortly into the video i turn on the rendertarget display so you can see the 3 cascade render targets on the right. As you can see theres a lot of jiggle going on. Depth Bias doesn't seem to make any difference really.

 

 

 

I'm a complete noob here but i see that you rotate your shadow maps with camera orientation and this way it's impossible to avoid jittering. I think you have to keep the shadow map orientation constant in worldspace and only adapt it to the position of the camera ignoring player view direction. So when you rotate but don't move, you would see the corners of cascade transitions fixed to the floor.  Does this make sense?

I think you're probably correct/ That's what I was referring to with using a sphere from the camera frustrum to build the bounding box for my orthographic projection for the shadow like.

I think i'm missing something though. I've found some examples here and there but I'm just not getting it. Maybe some pseudo code would help me.

Semlers linked blog contains more articles and code, however...

Looking at code snippets often raises more questions than answers: What conventions are used here (is this a camera to world matrix or world to camera? what multiplication order do they use? etc. etc.) - mostly you have to care for the deatils yourself and after you undrestood the concept it's only the details that's left.

I recommend you simplyfy the problem instead so cou can solve it more easily in your own:

1. Ignore frustum and any bounds and make each cascade just centered to the texel closest to the camera. (visualizing texel grid on a simple ground plane should help to set this up. And if this works all math is known.)

2. If you get this to work robustly so the cascades move with the camera without jitter the main problem is solved and you can start to optimize it for the frustum, which shouldn't be that hard at this point.

 

 

 

 

This topic is closed to new replies.

Advertisement