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
{
public class MeshSprite
public class MeshSprite : ICullable
{
private static readonly int PixelScale = 40;
private static readonly short[] Indices = new short[]

View File

@ -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)

View File

@ -836,32 +836,17 @@ namespace Kav
}
}
private static IEnumerable<(Model, Matrix)> FrustumCull(
private static IEnumerable<(T, Matrix)> FrustumCull<T>(
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);
}
}
}