Kav/Geometry/MeshSprite.cs

106 lines
3.3 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Kav
{
public class MeshSprite
{
private static readonly int PixelScale = 40;
public IndexBuffer IndexBuffer { get; }
public VertexBuffer VertexBuffer { get; }
public Texture2D Texture { get; }
public Texture2D Normal { get; }
public SpriteBillboardConstraint BillboardConstraint { get; }
public MeshSprite(
GraphicsDevice graphicsDevice,
Texture2D texture,
SpriteBillboardConstraint billboardConstraint
) {
Texture = texture;
Normal = null;
BillboardConstraint = billboardConstraint;
IndexBuffer = new IndexBuffer(
graphicsDevice,
IndexElementSize.SixteenBits,
6,
BufferUsage.WriteOnly
);
IndexBuffer.SetData(GenerateIndexArray());
VertexBuffer = new VertexBuffer(
graphicsDevice,
typeof(VertexPositionNormalTexture),
4,
BufferUsage.WriteOnly
);
VertexBuffer.SetData(GenerateVertexArray(Texture));
}
public MeshSprite(
GraphicsDevice graphicsDevice,
Texture2D texture,
Texture2D normal,
SpriteBillboardConstraint billboardConstraint
) {
Texture = texture;
Normal = normal;
BillboardConstraint = billboardConstraint;
IndexBuffer = new IndexBuffer(
graphicsDevice,
IndexElementSize.SixteenBits,
6,
BufferUsage.WriteOnly
);
IndexBuffer.SetData(GenerateIndexArray());
VertexBuffer = new VertexBuffer(
graphicsDevice,
typeof(VertexPositionNormalTexture),
4,
BufferUsage.WriteOnly
);
VertexBuffer.SetData(GenerateVertexArray(Texture));
}
private static short[] GenerateIndexArray()
{
short[] result = new short[6];
result[0] = 0;
result[1] = 1;
result[2] = 2;
result[3] = 1;
result[4] = 3;
result[5] = 2;
return result;
}
private static VertexPositionNormalTexture[] GenerateVertexArray(Texture2D texture)
{
VertexPositionNormalTexture[] result = new VertexPositionNormalTexture[4];
result[0].Position = new Vector3(-texture.Width / 2, texture.Height / 2, 0) / PixelScale;
result[0].Normal = new Vector3(0, 0, -1);
result[0].TextureCoordinate = new Vector2(0, 0);
result[1].Position = new Vector3(texture.Width / 2, texture.Height / 2, 0) / PixelScale;
result[1].Normal = new Vector3(0, 0, -1);
result[1].TextureCoordinate = new Vector2(1, 0);
result[2].Position = new Vector3(-texture.Width / 2, -texture.Height / 2, 0) / PixelScale;
result[2].Normal = new Vector3(0, 0, -1);
result[2].TextureCoordinate = new Vector2(0, 1);
result[3].Position = new Vector3(texture.Width / 2, -texture.Height / 2, 0) / PixelScale;
result[3].Normal = new Vector3(0, 0, -1);
result[3].TextureCoordinate = new Vector2(1, 1);
return result;
}
}
}