Kav/Geometry/SpriteMesh.cs

110 lines
3.2 KiB
C#
Raw Normal View History

2020-12-07 03:27:46 +00:00
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Kav
{
2020-12-12 02:22:54 +00:00
public class SpriteMesh : ICullable, IIndexDrawable
{
2020-12-10 06:28:51 +00:00
public enum FlipOptions
{
None,
Horizontal,
Vertical,
Both
}
2020-12-07 03:01:23 +00:00
private static readonly short[] Indices = new short[]
{
0,
1,
2,
1,
3,
2
};
2020-12-07 03:27:46 +00:00
public IndexBuffer IndexBuffer { get; }
public VertexBuffer VertexBuffer { get; }
public BoundingBox BoundingBox { get; }
2020-12-12 02:22:54 +00:00
public SpriteMesh(
GraphicsDevice graphicsDevice,
2020-12-12 02:22:54 +00:00
int width,
int height,
2020-12-10 06:28:51 +00:00
FlipOptions flipOptions
) {
IndexBuffer = new IndexBuffer(
graphicsDevice,
IndexElementSize.SixteenBits,
6,
BufferUsage.WriteOnly
);
2020-12-07 03:01:23 +00:00
IndexBuffer.SetData(Indices);
2020-12-12 02:22:54 +00:00
var vertexArray = GenerateVertexArray(width, height, flipOptions);
2020-12-07 03:27:46 +00:00
VertexBuffer = new VertexBuffer(
graphicsDevice,
typeof(VertexPositionNormalTexture),
4,
BufferUsage.WriteOnly
);
2020-12-07 03:27:46 +00:00
VertexBuffer.SetData(vertexArray);
BoundingBox = BoundingBox.CreateFromPoints(Positions(vertexArray));
}
2020-12-12 02:22:54 +00:00
private static VertexPositionNormalTexture[] GenerateVertexArray(int pixelWidth, int pixelHeight, FlipOptions flipOptions)
{
2020-12-12 02:22:54 +00:00
var width = pixelWidth / (float)40;
var height = pixelHeight / (float)40;
VertexPositionNormalTexture[] result = new VertexPositionNormalTexture[4];
2020-12-10 06:28:51 +00:00
var xLeft = 0;
var xRight = 1;
var yTop = 0;
var yBottom = 1;
2020-12-10 06:28:51 +00:00
if (flipOptions == FlipOptions.Horizontal || flipOptions == FlipOptions.Both)
{
xLeft = 1;
xRight = 0;
}
if (flipOptions == FlipOptions.Vertical || flipOptions == FlipOptions.Both)
{
yTop = 1;
yBottom = 0;
}
2020-12-10 06:28:51 +00:00
2020-12-12 02:22:54 +00:00
result[0].Position = new Vector3(-width / 2, height / 2, 0);
2020-12-12 02:52:05 +00:00
result[0].Normal = new Vector3(0, 0, -1);
result[0].TextureCoordinate = new Vector2(xLeft, yTop);
2020-12-12 02:22:54 +00:00
result[1].Position = new Vector3(width / 2, height / 2, 0);
2020-12-12 02:52:05 +00:00
result[1].Normal = new Vector3(0, 0, -1);
result[1].TextureCoordinate = new Vector2(xRight, yTop);
2020-12-12 02:22:54 +00:00
result[2].Position = new Vector3(-width / 2, -height / 2, 0);
2020-12-12 02:52:05 +00:00
result[2].Normal = new Vector3(0, 0, -1);
result[2].TextureCoordinate = new Vector2(xLeft, yBottom);
2020-12-12 02:22:54 +00:00
result[3].Position = new Vector3(width / 2, -height / 2, 0);
2020-12-12 02:52:05 +00:00
result[3].Normal = new Vector3(0, 0, -1);
result[3].TextureCoordinate = new Vector2(xRight, yBottom);
return result;
}
2020-12-07 03:27:46 +00:00
private static IEnumerable<Vector3> Positions(IEnumerable<VertexPositionNormalTexture> vertices)
{
foreach (var vertex in vertices)
{
yield return vertex.Position;
}
}
}
}