abstract frustum culling

instancing
cosmonaut 2020-12-06 19:36:00 -08:00
parent fc09082f1b
commit 7f986c546a
4 changed files with 23 additions and 29 deletions

View File

@ -0,0 +1,9 @@
using Microsoft.Xna.Framework;
namespace Kav
{
public interface ICullable
{
BoundingBox BoundingBox { get; }
}
}

View File

@ -4,7 +4,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace Kav namespace Kav
{ {
public class MeshSprite public class MeshSprite : ICullable
{ {
private static readonly int PixelScale = 40; private static readonly int PixelScale = 40;
private static readonly short[] Indices = new short[] private static readonly short[] Indices = new short[]

View File

@ -2,14 +2,14 @@ using Microsoft.Xna.Framework;
namespace Kav namespace Kav
{ {
public class Model public class Model : ICullable
{ {
public Mesh[] Meshes { get; } public Mesh[] Meshes { get; }
public BoundingBox BoundingBox { get; } public BoundingBox BoundingBox { get; }
public Color Albedo public Color Albedo
{ {
set set
{ {
foreach (var mesh in Meshes) foreach (var mesh in Meshes)
{ {
@ -32,7 +32,7 @@ namespace Kav
meshPart.Metallic = value; meshPart.Metallic = value;
} }
} }
} }
} }
public float Roughness public float Roughness
@ -46,7 +46,7 @@ namespace Kav
meshPart.Roughness = value; meshPart.Roughness = value;
} }
} }
} }
} }
public Model(Mesh[] meshes) public Model(Mesh[] meshes)
@ -83,7 +83,7 @@ namespace Kav
} }
} }
} }
public void DisableMetallicRoughnessMaps() public void DisableMetallicRoughnessMaps()
{ {
foreach (var mesh in Meshes) foreach (var mesh in Meshes)

View File

@ -836,32 +836,17 @@ namespace Kav
} }
} }
private static IEnumerable<(Model, Matrix)> FrustumCull( private static IEnumerable<(T, Matrix)> FrustumCull<T>(
BoundingFrustum boundingFrustum, BoundingFrustum boundingFrustum,
IEnumerable<(Model, Matrix)> modelTransforms IEnumerable<(T, Matrix)> cullableTransforms
) { ) where T : ICullable {
foreach (var modelTransform in modelTransforms) 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); var containment = boundingFrustum.Contains(boundingBox);
if (containment != ContainmentType.Disjoint) if (containment != ContainmentType.Disjoint)
{ {
yield return modelTransform; yield return (cullable, transform);
}
}
}
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);
} }
} }
} }