public void BackgroundThreadTask(object temp) { fastCameraBoundingFrustum = new Engine.Utilities.FastFrustum(ref viewProj); DoCulling(ref fastCameraBoundingFrustum); sun.CalcCascades(ref view, ref projection, nearClip, farClip, clipRange); }
// run from another thread during update to calc the view projection matrix and do culling public void CalcCascades(ref Matrix view, ref Matrix projection, float nearClip, float farClip, float clipRange) { // Get the corners of the frustum BoundingFrustum cameraFrustum = new BoundingFrustum(view * projection); cameraFrustum.GetCorners(frustumCornersWS); Matrix eyeTransform = view; Vector3.Transform(frustumCornersWS, ref eyeTransform, frustumCornersVS); // Get camera near and far values float near = nearClip; float far = MathHelper.Min(farClip, ShadowDistance); splitDepthsTmp[0] = near; splitDepthsTmp[NUM_CASCADES] = far; // Compute cascade split depths for (int i = 1; i < splitDepthsTmp.Length - 1; i++) splitDepthsTmp[i] = near + (far - near) * (float)Math.Pow((i / (float)NUM_CASCADES), 2); //splitDepthsTmp[1] = 35.0f; for (int i = 0; i < NUM_CASCADES; i++) { // Convert the split depths to percent of view range, so we can pass them to shader later LightClipPlanes[i].X = (splitDepthsTmp[i] - nearClip) / clipRange; LightClipPlanes[i].Y = (splitDepthsTmp[i + 1] - nearClip) / clipRange; // Create the view projection matrix for this cascade LightViewProjectionMatrices[i] = CreateLightViewProjectionMatrix(directionToSun, far, splitDepthsTmp[i], splitDepthsTmp[i + 1], i); LightFrustums[i] = new Engine.Utilities.FastFrustum(ref LightViewProjectionMatrices[i]); } Renderer.Instance.DoShadowsCulling(LightFrustums); }