using System; using System.Collections.Generic; using System.IO; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace Kav { public class Renderer { private const int MAX_INSTANCE_VERTEX_COUNT = 1000000; private const int MAX_SHADOW_CASCADES = 4; private int ShadowMapSize { get; } private GraphicsDevice GraphicsDevice { get; } private VertexBuffer FullscreenTriangle { get; } private int NumShadowCascades { get; } private RenderTarget2D ColorRenderTarget { get; } private RenderTarget2D[] ShadowRenderTargets { get; } private DeferredPBREffect DeferredPBREffect { get; } /* FIXME: these next two dont actually have anything to do with PBR */ private DeferredPBR_GBufferEffect Deferred_GBufferEffect { get; } private DeferredPBR_AmbientLightEffect DeferredAmbientLightEffect { get; } private DeferredPBR_PointLightEffect DeferredPointLightEffect { get; } private DeferredPBR_DirectionalLightEffect DeferredDirectionalLightEffect { get; } private Deferred_ToonEffect Deferred_ToonEffect { get; } private SimpleDepthEffect SimpleDepthEffect { get; } private LinearDepthEffect LinearDepthEffect { get; } private Effect ToneMapEffect { get; } private SkyboxEffect SkyboxEffect { get; } private DiffuseLitSpriteEffect DiffuseLitSpriteEffect { get; } private RenderTargetCube PointShadowCubeMap { get; } private Kav.Model UnitCube { get; } private SpriteBatch SpriteBatch { get; } private DynamicVertexBuffer GBufferInstanceVertexBuffer { get; } private readonly GBufferInstanceVertex[] GBufferInstanceVertices = new GBufferInstanceVertex[MAX_INSTANCE_VERTEX_COUNT]; public Renderer( GraphicsDevice graphicsDevice, int renderDimensionsX, int renderDimensionsY, int numShadowCascades, int shadowMapSize ) { GraphicsDevice = graphicsDevice; ShadowMapSize = shadowMapSize; NumShadowCascades = (int)MathHelper.Clamp(numShadowCascades, 1, MAX_SHADOW_CASCADES); ShadowRenderTargets = new RenderTarget2D[numShadowCascades]; for (var i = 0; i < numShadowCascades; i++) { ShadowRenderTargets[i] = new RenderTarget2D( GraphicsDevice, ShadowMapSize, ShadowMapSize, false, SurfaceFormat.Single, DepthFormat.Depth24 ); } ColorRenderTarget = new RenderTarget2D( graphicsDevice, renderDimensionsX, renderDimensionsY, false, SurfaceFormat.Color, DepthFormat.Depth24, 0, RenderTargetUsage.PreserveContents ); PointShadowCubeMap = new RenderTargetCube( GraphicsDevice, shadowMapSize, false, SurfaceFormat.Single, DepthFormat.Depth24 ); SimpleDepthEffect = new SimpleDepthEffect(GraphicsDevice); LinearDepthEffect = new LinearDepthEffect(GraphicsDevice); DeferredPBREffect = new DeferredPBREffect(GraphicsDevice); Deferred_GBufferEffect = new DeferredPBR_GBufferEffect(GraphicsDevice); DeferredAmbientLightEffect = new DeferredPBR_AmbientLightEffect(GraphicsDevice); DeferredPointLightEffect = new DeferredPBR_PointLightEffect(GraphicsDevice); DeferredDirectionalLightEffect = new DeferredPBR_DirectionalLightEffect(GraphicsDevice); DeferredDirectionalLightEffect.ShadowMapSize = ShadowMapSize; ToneMapEffect = new Effect(graphicsDevice, Resources.ToneMapEffect); Deferred_ToonEffect = new Deferred_ToonEffect(GraphicsDevice); SkyboxEffect = new SkyboxEffect(GraphicsDevice); DiffuseLitSpriteEffect = new DiffuseLitSpriteEffect(GraphicsDevice); FullscreenTriangle = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), 3, BufferUsage.WriteOnly); FullscreenTriangle.SetData(new VertexPositionTexture[3] { new VertexPositionTexture(new Vector3(-1, -3, 0), new Vector2(0, 2)), new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)), new VertexPositionTexture(new Vector3(3, 1, 0), new Vector2(2, 0)) }); UnitCube = Kav.ModelLoader.Load( GraphicsDevice, Smuggler.Importer.ImportGLB(GraphicsDevice, new MemoryStream(Resources.UnitCubeModel)) ); SpriteBatch = new SpriteBatch(graphicsDevice); GBufferInstanceVertexBuffer = new DynamicVertexBuffer( GraphicsDevice, VertexDeclarations.GBufferInstanceDeclaration, MAX_INSTANCE_VERTEX_COUNT, BufferUsage.WriteOnly ); } // TODO: we could make this a lot more efficient probably // draws mesh sprites with a forward rendered diffuse lighting technique public void MeshSpriteRender( RenderTarget2D renderTarget, PerspectiveCamera camera, IEnumerable<(Model, Matrix)> modelTransforms, IEnumerable<(MeshSprite, Matrix)> meshSpriteTransforms, AmbientLight ambientLight, IEnumerable pointLights, DirectionalLight? directionalLight ) { GraphicsDevice.SetRenderTarget(ColorRenderTarget); GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1f, 0); GraphicsDevice.DepthStencilState = DepthStencilState.Default; DepthRender(ColorRenderTarget, camera, modelTransforms); GraphicsDevice.Clear(ClearOptions.Target, new Color(0, 0, 0, 0), 1f, 0); GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead; GraphicsDevice.RasterizerState = RasterizerState.CullNone; GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp; GraphicsDevice.BlendState = BlendState.AlphaBlend; DiffuseLitSpriteEffect.View = camera.View; DiffuseLitSpriteEffect.Projection = camera.Projection; DiffuseLitSpriteEffect.AmbientColor = ambientLight.Color.ToVector3(); if (directionalLight.HasValue) { DiffuseLitSpriteEffect.DirectionalLightDirection = directionalLight.Value.Direction; DiffuseLitSpriteEffect.DirectionalLightColor = directionalLight.Value.Color.ToVector3() * directionalLight.Value.Intensity; } else { DiffuseLitSpriteEffect.DirectionalLightColor = Vector3.Zero; } var i = 0; foreach (var pointLight in pointLights) { if (i > DiffuseLitSpriteEffect.MaxPointLights) { break; } DiffuseLitSpriteEffect.PointLights[i] = pointLight; i += 1; } var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection); foreach (var (sprite, transform) in FrustumCull(boundingFrustum, meshSpriteTransforms)) { DiffuseLitSpriteEffect.NormalMapEnabled = sprite.Normal != null; if (sprite.BillboardConstraint == SpriteBillboardConstraint.None) { DiffuseLitSpriteEffect.World = transform; } else if (sprite.BillboardConstraint == SpriteBillboardConstraint.Horizontal) { DiffuseLitSpriteEffect.World = Matrix.CreateConstrainedBillboard( transform.Translation, camera.Position, Vector3.Up, camera.Forward, camera.Position - transform.Translation ); } else { DiffuseLitSpriteEffect.World = Matrix.CreateConstrainedBillboard( transform.Translation, camera.Position, Vector3.Up, null, null ); } GraphicsDevice.Textures[0] = sprite.Texture; GraphicsDevice.Textures[1] = sprite.Normal; GraphicsDevice.SetVertexBuffer(sprite.VertexBuffer); GraphicsDevice.Indices = sprite.IndexBuffer; foreach (var pass in DiffuseLitSpriteEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, sprite.VertexBuffer.VertexCount, 0, 2 ); } } GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.Clear(new Color(0, 0, 0, 0)); SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null); SpriteBatch.Draw(ColorRenderTarget, Vector2.Zero, Color.White); SpriteBatch.End(); } public void DepthRender( RenderTarget2D renderTarget, PerspectiveCamera camera, IEnumerable<(Model, Matrix)> modelTransforms ) { GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.DepthStencilState = DepthStencilState.Default; var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection); foreach (var (model, transform) in FrustumCull(boundingFrustum, modelTransforms)) { foreach (var modelMesh in model.Meshes) { foreach (var meshPart in modelMesh.MeshParts) { SimpleDepthEffect.Model = transform; SimpleDepthEffect.View = camera.View; SimpleDepthEffect.Projection = camera.Projection; GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer); GraphicsDevice.Indices = meshPart.IndexBuffer; foreach (var pass in SimpleDepthEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, meshPart.VertexBuffer.VertexCount, 0, meshPart.Triangles.Length ); } } } } } public void SkyboxRender( RenderTarget2D renderTarget, PerspectiveCamera camera, TextureCube skybox ) { GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.RasterizerState.CullMode = CullMode.CullClockwiseFace; SkyboxEffect.Skybox = skybox; var view = camera.View; view.Translation = Vector3.Zero; SkyboxEffect.View = view; SkyboxEffect.Projection = camera.Projection; GraphicsDevice.SetVertexBuffer(UnitCube.Meshes[0].MeshParts[0].VertexBuffer); GraphicsDevice.Indices = UnitCube.Meshes[0].MeshParts[0].IndexBuffer; foreach (var pass in SkyboxEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, UnitCube.Meshes[0].MeshParts[0].VertexBuffer.VertexCount, 0, UnitCube.Meshes[0].MeshParts[0].Triangles.Length ); } GraphicsDevice.RasterizerState.CullMode = CullMode.CullCounterClockwiseFace; } /// /// GBuffer binding must have 4 render targets. /// public void InstancedGBufferRender( RenderTargetBinding[] gBuffer, PerspectiveCamera camera, T drawable, IEnumerable transforms ) where T : ICullable, IIndexDrawable, IGBufferDrawable { int numInstances = 0; GraphicsDevice.SetRenderTargets(gBuffer); GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.BlendState = BlendState.Opaque; Deferred_GBufferEffect.HardwareInstancingEnabled = true; Deferred_GBufferEffect.Albedo = drawable.Albedo; Deferred_GBufferEffect.Metallic = drawable.Metallic; Deferred_GBufferEffect.Roughness = drawable.Roughness; Deferred_GBufferEffect.AlbedoTexture = drawable.AlbedoTexture; Deferred_GBufferEffect.NormalTexture = drawable.NormalTexture; Deferred_GBufferEffect.MetallicRoughnessTexture = drawable.MetallicRoughnessTexture; Deferred_GBufferEffect.View = camera.View; Deferred_GBufferEffect.Projection = camera.Projection; var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection); foreach (var transform in FrustumCull(boundingFrustum, drawable, transforms)) { GBufferInstanceVertices[numInstances].World = transform; numInstances += 1; } GBufferInstanceVertexBuffer.SetData( GBufferInstanceVertices, 0, numInstances, SetDataOptions.Discard ); GraphicsDevice.SetVertexBuffers( drawable.VertexBuffer, new VertexBufferBinding(GBufferInstanceVertexBuffer, 0, 1) ); GraphicsDevice.Indices = drawable.IndexBuffer; foreach (var pass in Deferred_GBufferEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawInstancedPrimitives( PrimitiveType.TriangleList, 0, 0, drawable.VertexBuffer.VertexCount, 0, drawable.IndexBuffer.IndexCount / 3, numInstances ); } } public void GBufferRender( RenderTargetBinding[] gBuffer, PerspectiveCamera camera, IEnumerable<(Model, Matrix)> modelTransforms ) { GraphicsDevice.SetRenderTargets(gBuffer); GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.BlendState = BlendState.Opaque; var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection); foreach (var (model, transform) in FrustumCull(boundingFrustum, modelTransforms)) { foreach (var modelMesh in model.Meshes) { foreach (var meshPart in modelMesh.MeshParts) { Deferred_GBufferEffect.World = transform; Deferred_GBufferEffect.View = camera.View; Deferred_GBufferEffect.Projection = camera.Projection; Deferred_GBufferEffect.HardwareInstancingEnabled = false; Deferred_GBufferEffect.Albedo = meshPart.Albedo; Deferred_GBufferEffect.Metallic = meshPart.Metallic; Deferred_GBufferEffect.Roughness = meshPart.Roughness; Deferred_GBufferEffect.AlbedoTexture = meshPart.AlbedoTexture; Deferred_GBufferEffect.NormalTexture = meshPart.NormalTexture; Deferred_GBufferEffect.MetallicRoughnessTexture = meshPart.MetallicRoughnessTexture; GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer); GraphicsDevice.Indices = meshPart.IndexBuffer; foreach (var pass in Deferred_GBufferEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, meshPart.VertexBuffer.VertexCount, 0, meshPart.Triangles.Length ); } } } } } public void AmbientLightRender( RenderTarget2D renderTarget, Texture2D gPosition, Texture2D gAlbedo, AmbientLight ambientLight ) { GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.BlendState = BlendState.Opaque; GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead; DeferredAmbientLightEffect.GPosition = gPosition; DeferredAmbientLightEffect.GAlbedo = gAlbedo; DeferredAmbientLightEffect.AmbientColor = ambientLight.Color.ToVector3(); foreach (var pass in DeferredAmbientLightEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.SetVertexBuffer(FullscreenTriangle); GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); } } public void PointLightRender( RenderTarget2D renderTarget, Texture2D gPosition, Texture2D gAlbedo, Texture2D gNormal, Texture2D gMetallicRoughness, PerspectiveCamera camera, IEnumerable<(Model, Matrix)> modelTransforms, PointLight pointLight ) { RenderPointShadows(camera, modelTransforms, pointLight); GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead; GraphicsDevice.BlendState = BlendState.Additive; DeferredPointLightEffect.GPosition = gPosition; DeferredPointLightEffect.GAlbedo = gAlbedo; DeferredPointLightEffect.GNormal = gNormal; DeferredPointLightEffect.GMetallicRoughness = gMetallicRoughness; DeferredPointLightEffect.ShadowMap = PointShadowCubeMap; DeferredPointLightEffect.EyePosition = camera.Position; DeferredPointLightEffect.PointLightPosition = pointLight.Position; DeferredPointLightEffect.PointLightColor = pointLight.Color.ToVector3() * pointLight.Intensity; DeferredPointLightEffect.FarPlane = 25f; // FIXME: magic value foreach (var pass in DeferredPointLightEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.SetVertexBuffer(FullscreenTriangle); GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); } } public void DirectionalLightRender( RenderTarget2D renderTarget, Texture2D gPosition, Texture2D gAlbedo, Texture2D gNormal, Texture2D gMetallicRoughness, PerspectiveCamera camera, IEnumerable<(Model, Matrix)> modelTransforms, DirectionalLight directionalLight, int numShadowCascades ) { RenderDirectionalShadows(camera, modelTransforms, directionalLight, DeferredDirectionalLightEffect); GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead; GraphicsDevice.BlendState = BlendState.Additive; DeferredDirectionalLightEffect.GPosition = gPosition; DeferredDirectionalLightEffect.GAlbedo = gAlbedo; DeferredDirectionalLightEffect.GNormal = gNormal; DeferredDirectionalLightEffect.GMetallicRoughness = gMetallicRoughness; DeferredDirectionalLightEffect.ShadowMapOne = ShadowRenderTargets[0]; if (numShadowCascades > 1) { DeferredDirectionalLightEffect.ShadowMapTwo = ShadowRenderTargets[1]; } if (numShadowCascades > 2) { DeferredDirectionalLightEffect.ShadowMapThree = ShadowRenderTargets[2]; } if (numShadowCascades > 3) { DeferredDirectionalLightEffect.ShadowMapFour = ShadowRenderTargets[3]; } DeferredDirectionalLightEffect.DirectionalLightDirection = directionalLight.Direction; DeferredDirectionalLightEffect.DirectionalLightColor = directionalLight.Color.ToVector3() * directionalLight.Intensity; DeferredDirectionalLightEffect.ViewMatrix = camera.View; DeferredDirectionalLightEffect.EyePosition = camera.Position; foreach (EffectPass pass in DeferredDirectionalLightEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.SetVertexBuffer(FullscreenTriangle); GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); } } public void DirectionalLightToonRender( RenderTarget2D renderTarget, Texture2D gPosition, Texture2D gAlbedo, Texture2D gNormal, Texture2D gMetallicRoughness, PerspectiveCamera camera, IEnumerable<(Model, Matrix)> modelTransforms, DirectionalLight directionalLight, int numShadowCascades, bool ditheredShadows ) { RenderDirectionalShadows(camera, modelTransforms, directionalLight, Deferred_ToonEffect); GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead; GraphicsDevice.BlendState = BlendState.Additive; Deferred_ToonEffect.GPosition = gPosition; Deferred_ToonEffect.GAlbedo = gAlbedo; Deferred_ToonEffect.GNormal = gNormal; Deferred_ToonEffect.GMetallicRoughness = gMetallicRoughness; Deferred_ToonEffect.DitheredShadows = ditheredShadows; Deferred_ToonEffect.EyePosition = camera.Position; Deferred_ToonEffect.DirectionalLightDirection = directionalLight.Direction; Deferred_ToonEffect.DirectionalLightColor = directionalLight.Color.ToVector3() * directionalLight.Intensity; Deferred_ToonEffect.ShadowMapOne = ShadowRenderTargets[0]; if (numShadowCascades > 1) { Deferred_ToonEffect.ShadowMapTwo = ShadowRenderTargets[1]; } if (numShadowCascades > 2) { Deferred_ToonEffect.ShadowMapThree = ShadowRenderTargets[2]; } if (numShadowCascades > 3) { Deferred_ToonEffect.ShadowMapFour = ShadowRenderTargets[3]; } Deferred_ToonEffect.ViewMatrix = camera.View; foreach (EffectPass pass in Deferred_ToonEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.SetVertexBuffer(FullscreenTriangle); GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); } } private void RenderDirectionalShadows( PerspectiveCamera camera, IEnumerable<(Model, Matrix)> modelTransforms, DirectionalLight directionalLight, ShadowCascadeEffect effect ) { // render the individual shadow cascades var previousFarPlane = camera.NearPlane; for (var i = 0; i < NumShadowCascades; i++) { var farPlane = camera.FarPlane / (MathHelper.Max((NumShadowCascades - i - 1) * 2f, 1f)); // divide the view frustum var shadowCamera = new PerspectiveCamera( camera.Position, camera.Forward, camera.Up, camera.FieldOfView, camera.AspectRatio, previousFarPlane, farPlane ); // TODO: This is tightly coupled to the effect and it sucks RenderDirectionalShadowMap(shadowCamera, modelTransforms, directionalLight, effect, i); effect.CascadeFarPlanes[i] = farPlane; previousFarPlane = farPlane; } } private void RenderDirectionalShadowMap( PerspectiveCamera camera, IEnumerable<(Model, Matrix)> modelTransforms, DirectionalLight directionalLight, ShadowCascadeEffect effect, int shadowCascadeIndex ) { GraphicsDevice.SetRenderTarget(ShadowRenderTargets[shadowCascadeIndex]); GraphicsDevice.Clear(Color.White); GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.BlendState = BlendState.Opaque; var cameraBoundingFrustum = new BoundingFrustum(camera.View * camera.Projection); Vector3[] frustumCorners = cameraBoundingFrustum.GetCorners(); Vector3 frustumCenter = Vector3.Zero; for (var i = 0; i < frustumCorners.Length; i++) { frustumCenter += frustumCorners[i]; } frustumCenter /= 8f; var lightView = Matrix.CreateLookAt(frustumCenter + directionalLight.Direction, frustumCenter, Vector3.Backward); for (var i = 0; i < frustumCorners.Length; i++) { frustumCorners[i] = Vector3.Transform(frustumCorners[i], lightView); } BoundingBox lightBox = BoundingBox.CreateFromPoints(frustumCorners); SimpleDepthEffect.View = lightView; SimpleDepthEffect.Projection = Matrix.CreateOrthographicOffCenter( lightBox.Min.X, lightBox.Max.X, lightBox.Min.Y, lightBox.Max.Y, -lightBox.Max.Z - 10f, // TODO: near clip plane needs scene AABB info to get rid of this magic value -lightBox.Min.Z ); var lightSpaceMatrix = SimpleDepthEffect.View * SimpleDepthEffect.Projection; if (shadowCascadeIndex == 0) { effect.LightSpaceMatrixOne = lightSpaceMatrix; } else if (shadowCascadeIndex == 1) { effect.LightSpaceMatrixTwo = lightSpaceMatrix; } else if (shadowCascadeIndex == 2) { effect.LightSpaceMatrixThree = lightSpaceMatrix; } else if (shadowCascadeIndex == 3) { effect.LightSpaceMatrixFour = lightSpaceMatrix; } var boundingFrustum = new BoundingFrustum(lightSpaceMatrix); foreach (var (model, transform) in FrustumCull(boundingFrustum, modelTransforms)) { foreach (var modelMesh in model.Meshes) { foreach (var meshPart in modelMesh.MeshParts) { GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer); GraphicsDevice.Indices = meshPart.IndexBuffer; SimpleDepthEffect.Model = transform; foreach (var pass in SimpleDepthEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, meshPart.VertexBuffer.VertexCount, 0, meshPart.Triangles.Length ); } } } } } private void RenderPointShadows( PerspectiveCamera camera, IEnumerable<(Model, Matrix)> modelTransforms, PointLight pointLight ) { GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.BlendState = BlendState.Opaque; LinearDepthEffect.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.PiOver2, 1, 0.1f, 25f // FIXME: magic value ); LinearDepthEffect.FarPlane = 25f; LinearDepthEffect.LightPosition = pointLight.Position; foreach (CubeMapFace face in Enum.GetValues(typeof(CubeMapFace))) { GraphicsDevice.SetRenderTarget(PointShadowCubeMap, face); Vector3 targetDirection; Vector3 targetUpDirection; switch(face) { case CubeMapFace.PositiveX: targetDirection = Vector3.Right; targetUpDirection = Vector3.Up; break; case CubeMapFace.NegativeX: targetDirection = Vector3.Left; targetUpDirection = Vector3.Up; break; case CubeMapFace.PositiveY: targetDirection = Vector3.Up; targetUpDirection = Vector3.Forward; break; case CubeMapFace.NegativeY: targetDirection = Vector3.Down; targetUpDirection = Vector3.Backward; break; case CubeMapFace.PositiveZ: targetDirection = Vector3.Backward; targetUpDirection = Vector3.Up; break; case CubeMapFace.NegativeZ: targetDirection = Vector3.Forward; targetUpDirection = Vector3.Up; break; default: targetDirection = Vector3.Right; targetUpDirection = Vector3.Up; break; } LinearDepthEffect.View = Matrix.CreateLookAt( pointLight.Position, pointLight.Position + targetDirection, targetUpDirection ); var boundingFrustum = new BoundingFrustum(LinearDepthEffect.View * LinearDepthEffect.Projection); foreach (var (model, transform) in FrustumCull(boundingFrustum, modelTransforms)) { foreach (var modelMesh in model.Meshes) { foreach (var meshPart in modelMesh.MeshParts) { GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer); GraphicsDevice.Indices = meshPart.IndexBuffer; LinearDepthEffect.Model = transform; foreach (var pass in LinearDepthEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, meshPart.VertexBuffer.VertexCount, 0, meshPart.Triangles.Length ); } } } } } } private static IEnumerable<(T, Matrix)> FrustumCull( BoundingFrustum boundingFrustum, IEnumerable<(T, Matrix)> cullableTransforms ) where T : ICullable { foreach (var (cullable, transform) in cullableTransforms) { var boundingBox = TransformedBoundingBox(cullable.BoundingBox, transform); var containment = boundingFrustum.Contains(boundingBox); if (containment != ContainmentType.Disjoint) { yield return (cullable, transform); } } } private static IEnumerable FrustumCull( BoundingFrustum boundingFrustum, T cullable, IEnumerable transforms ) where T : ICullable { foreach (var transform in transforms) { var boundingBox = TransformedBoundingBox(cullable.BoundingBox, transform); var containment = boundingFrustum.Contains(boundingBox); if (containment != ContainmentType.Disjoint) { yield return transform; } } } private static BoundingBox TransformedBoundingBox(BoundingBox boundingBox, Matrix matrix) { var center = (boundingBox.Min + boundingBox.Max) / 2f; var extent = (boundingBox.Max - boundingBox.Min) / 2f; var newCenter = Vector3.Transform(center, matrix); var newExtent = Vector3.TransformNormal(extent, AbsoluteMatrix(matrix)); return new BoundingBox(newCenter - newExtent, newCenter + newExtent); } private static Matrix AbsoluteMatrix(Matrix matrix) { return new Matrix( Math.Abs(matrix.M11), Math.Abs(matrix.M12), Math.Abs(matrix.M13), Math.Abs(matrix.M14), Math.Abs(matrix.M21), Math.Abs(matrix.M22), Math.Abs(matrix.M23), Math.Abs(matrix.M24), Math.Abs(matrix.M31), Math.Abs(matrix.M32), Math.Abs(matrix.M33), Math.Abs(matrix.M34), Math.Abs(matrix.M41), Math.Abs(matrix.M42), Math.Abs(matrix.M43), Math.Abs(matrix.M44) ); } } }