protected void RenderWithShadows(CascadedShadowMap shadowMap, Matrix view, Matrix projection, Light light, RendererParameters instance) { DrawGeometry(view, projection, new object[] { shadowMap, light }, mWithShadowsConfigurer, instance); }
protected void RenderWithoutShadows(Matrix view, Matrix projection, Light light, RendererParameters instance) { DrawGeometry(view, projection, new object[] { light }, mWithoutShadowsConfigurer, instance); }
public void GenerateCascades(ICamera camera, Light light, BoundingBox sceneAABB) { mLightView = Matrix.CreateLookAt(camera.Position, camera.Position + light.Direction, Vector3.Up); Vector3[] sceneCorners = sceneAABB.GetCorners(); Vector3.Transform(sceneCorners, ref mLightView, sceneCorners); BoundingBox lightSpaceSceneAABB = BoundingBox.CreateFromPoints(sceneCorners); float maxCameraDistance = camera.FarPlaneDistance - camera.NearPlaneDistance; Vector3 cameraPosition = Vector3.Transform(camera.Position, mLightView); for (int iCascadeCount = 0; iCascadeCount < mCascadeCount; iCascadeCount++) { Cascade cascade = mCascadeContainer[iCascadeCount]; if (iCascadeCount == mCascadeCount - 1) { cascade.ProjectionTransform = Matrix.CreateOrthographicOffCenter( lightSpaceSceneAABB.Min.X, lightSpaceSceneAABB.Max.X, lightSpaceSceneAABB.Min.Y, lightSpaceSceneAABB.Max.Y, -lightSpaceSceneAABB.Max.Z, -lightSpaceSceneAABB.Min.Z ); } else { float farCascadeDistance = maxCameraDistance * cascade.FarPlanePercentage; Matrix frustumProjection = Matrix.CreatePerspectiveFieldOfView( camera.FieldOfView, camera.AspectRatio, camera.NearPlaneDistance, camera.NearPlaneDistance + farCascadeDistance ); Vector3[] frustumCorners = new BoundingFrustum(camera.View * frustumProjection).GetCorners(); Vector3.Transform(frustumCorners, ref mLightView, frustumCorners); BoundingBox frustumBounds = BoundingBox.CreateFromPoints(frustumCorners); cascade.ProjectionTransform = Matrix.CreateOrthographicOffCenter( frustumBounds.Min.X, frustumBounds.Max.X, frustumBounds.Min.Y, frustumBounds.Max.Y, -lightSpaceSceneAABB.Max.Z, -lightSpaceSceneAABB.Min.Z ); } int tileX = iCascadeCount % 2; int tileY = iCascadeCount / 2; float bufferBorder = 3.0f / (float)mCascadeResolution; cascade.BufferBounds = new Vector4( 0.5f * tileX + bufferBorder, 0.5f * tileX + 0.5f - bufferBorder, 0.5f * tileY + bufferBorder, 0.5f * tileY + 0.5f - bufferBorder ); Matrix tileTransform = Matrix.Identity; tileTransform.M11 = 0.25f; tileTransform.M22 = -0.25f; tileTransform.Translation = new Vector3(0.25f + tileX * 0.5f, 0.25f + tileY * 0.5f, 0); cascade.TileTransform = tileTransform; } }
public void RenderAllInstancesWithShadows(CascadedShadowMap shadowMap, Matrix view, Matrix projection, Light light) { if (mWithShadowsConfigurer != null) { foreach (RendererParameters instance in mInstances) { RenderWithShadows(shadowMap, view, projection, light, instance); } } }
/// <summary> /// Instantiates scene light sources and shadow manager. /// </summary> private static void CreateLightsAndShadows() { mDirectionalLight = new Light( Vector3.Normalize(new Vector3(0.4233333f, 1.0f, -0.5533332f)), // Direction Vector3.Zero, // Gaze new Vector3(0.05333332f, 0.09882354f, 0.1819608f), // Ambient Color new Vector3(1, 0.9607844f, 0.8078432f), // Diffuse Color new Vector3(1, 0.9607844f, 0.8078432f) // Specular Color ); mShadowMap = new CascadedShadowMap(mDevice, 2048); }