From 7f986c546a3b508410b2daabb5deded55bb213ba Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Sun, 6 Dec 2020 19:36:00 -0800 Subject: [PATCH] abstract frustum culling --- Geometry/Interfaces/ICullable.cs | 9 +++++++++ Geometry/MeshSprite.cs | 2 +- Geometry/Model.cs | 14 +++++++------- Renderer.cs | 27 ++++++--------------------- 4 files changed, 23 insertions(+), 29 deletions(-) create mode 100644 Geometry/Interfaces/ICullable.cs diff --git a/Geometry/Interfaces/ICullable.cs b/Geometry/Interfaces/ICullable.cs new file mode 100644 index 0000000..831fc83 --- /dev/null +++ b/Geometry/Interfaces/ICullable.cs @@ -0,0 +1,9 @@ +using Microsoft.Xna.Framework; + +namespace Kav +{ + public interface ICullable + { + BoundingBox BoundingBox { get; } + } +} diff --git a/Geometry/MeshSprite.cs b/Geometry/MeshSprite.cs index a7cca6c..e53d5dc 100644 --- a/Geometry/MeshSprite.cs +++ b/Geometry/MeshSprite.cs @@ -4,7 +4,7 @@ using Microsoft.Xna.Framework.Graphics; namespace Kav { - public class MeshSprite + public class MeshSprite : ICullable { private static readonly int PixelScale = 40; private static readonly short[] Indices = new short[] diff --git a/Geometry/Model.cs b/Geometry/Model.cs index 6ffe912..3acbb44 100644 --- a/Geometry/Model.cs +++ b/Geometry/Model.cs @@ -2,14 +2,14 @@ using Microsoft.Xna.Framework; namespace Kav { - public class Model + public class Model : ICullable { public Mesh[] Meshes { get; } public BoundingBox BoundingBox { get; } - public Color Albedo - { - set + public Color Albedo + { + set { foreach (var mesh in Meshes) { @@ -32,7 +32,7 @@ namespace Kav meshPart.Metallic = value; } } - } + } } public float Roughness @@ -46,7 +46,7 @@ namespace Kav meshPart.Roughness = value; } } - } + } } public Model(Mesh[] meshes) @@ -83,7 +83,7 @@ namespace Kav } } } - + public void DisableMetallicRoughnessMaps() { foreach (var mesh in Meshes) diff --git a/Renderer.cs b/Renderer.cs index b45ac9a..08c673e 100644 --- a/Renderer.cs +++ b/Renderer.cs @@ -836,32 +836,17 @@ namespace Kav } } - private static IEnumerable<(Model, Matrix)> FrustumCull( + private static IEnumerable<(T, Matrix)> FrustumCull( BoundingFrustum boundingFrustum, - IEnumerable<(Model, Matrix)> modelTransforms - ) { - foreach (var modelTransform in modelTransforms) + IEnumerable<(T, Matrix)> cullableTransforms + ) where T : ICullable { + foreach (var (cullable, transform) in cullableTransforms) { - var boundingBox = TransformedBoundingBox(modelTransform.Item1.BoundingBox, modelTransform.Item2); + var boundingBox = TransformedBoundingBox(cullable.BoundingBox, transform); var containment = boundingFrustum.Contains(boundingBox); if (containment != ContainmentType.Disjoint) { - yield return modelTransform; - } - } - } - - private static IEnumerable<(MeshSprite, Matrix)> FrustumCull( - BoundingFrustum boundingFrustum, - IEnumerable<(MeshSprite, Matrix)> meshSpriteTransforms - ) { - foreach (var (meshSprite, transform) in meshSpriteTransforms) - { - var boundingBox = TransformedBoundingBox(meshSprite.BoundingBox, transform); - var containment = boundingFrustum.Contains(boundingBox); - if (containment != ContainmentType.Disjoint) - { - yield return (meshSprite, transform); + yield return (cullable, transform); } } }