frustum cull meshsprites
parent
46f2cad81a
commit
fc09082f1b
|
@ -1,3 +1,4 @@
|
|||
using System.Collections.Generic;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
|
@ -16,13 +17,14 @@ namespace Kav
|
|||
2
|
||||
};
|
||||
|
||||
public IndexBuffer IndexBuffer { get; }
|
||||
public VertexBuffer VertexBuffer { get; }
|
||||
|
||||
public Texture2D Texture { get; }
|
||||
public Texture2D Normal { get; }
|
||||
public SpriteBillboardConstraint BillboardConstraint { get; }
|
||||
|
||||
public IndexBuffer IndexBuffer { get; }
|
||||
public VertexBuffer VertexBuffer { get; }
|
||||
public BoundingBox BoundingBox { get; }
|
||||
|
||||
public MeshSprite(
|
||||
GraphicsDevice graphicsDevice,
|
||||
Texture2D texture,
|
||||
|
@ -40,13 +42,17 @@ namespace Kav
|
|||
);
|
||||
IndexBuffer.SetData(Indices);
|
||||
|
||||
var vertexArray = GenerateVertexArray(Texture);
|
||||
|
||||
VertexBuffer = new VertexBuffer(
|
||||
graphicsDevice,
|
||||
typeof(VertexPositionNormalTexture),
|
||||
4,
|
||||
BufferUsage.WriteOnly
|
||||
);
|
||||
VertexBuffer.SetData(GenerateVertexArray(Texture));
|
||||
VertexBuffer.SetData(vertexArray);
|
||||
|
||||
BoundingBox = BoundingBox.CreateFromPoints(Positions(vertexArray));
|
||||
}
|
||||
|
||||
public MeshSprite(
|
||||
|
@ -67,13 +73,17 @@ namespace Kav
|
|||
);
|
||||
IndexBuffer.SetData(Indices);
|
||||
|
||||
var vertexArray = GenerateVertexArray(Texture);
|
||||
|
||||
VertexBuffer = new VertexBuffer(
|
||||
graphicsDevice,
|
||||
typeof(VertexPositionNormalTexture),
|
||||
4,
|
||||
BufferUsage.WriteOnly
|
||||
);
|
||||
VertexBuffer.SetData(GenerateVertexArray(Texture));
|
||||
VertexBuffer.SetData(vertexArray);
|
||||
|
||||
BoundingBox = BoundingBox.CreateFromPoints(Positions(vertexArray));
|
||||
}
|
||||
|
||||
private static VertexPositionNormalTexture[] GenerateVertexArray(Texture2D texture)
|
||||
|
@ -98,5 +108,13 @@ namespace Kav
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static IEnumerable<Vector3> Positions(IEnumerable<VertexPositionNormalTexture> vertices)
|
||||
{
|
||||
foreach (var vertex in vertices)
|
||||
{
|
||||
yield return vertex.Position;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
19
Renderer.cs
19
Renderer.cs
|
@ -297,7 +297,9 @@ namespace Kav
|
|||
i += 1;
|
||||
}
|
||||
|
||||
foreach (var (sprite, transform) in meshSpriteTransforms)
|
||||
var boundingFrustum = new BoundingFrustum(camera.View * camera.Projection);
|
||||
|
||||
foreach (var (sprite, transform) in FrustumCull(boundingFrustum, meshSpriteTransforms))
|
||||
{
|
||||
DiffuseLitSpriteEffect.NormalMapEnabled = sprite.Normal != null;
|
||||
|
||||
|
@ -849,6 +851,21 @@ namespace Kav
|
|||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static BoundingBox TransformedBoundingBox(BoundingBox boundingBox, Matrix matrix)
|
||||
{
|
||||
var center = (boundingBox.Min + boundingBox.Max) / 2f;
|
||||
|
|
Loading…
Reference in New Issue