decouple sprite mesh from textures

sprite_light_experimental
cosmonaut 2020-12-11 18:22:54 -08:00
parent 40ca0402d6
commit 2ff5cb1ca5
5 changed files with 29 additions and 55 deletions

View File

@ -1,21 +1,28 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Kav.Data
{
public struct MeshSpriteDrawData
{
public MeshSprite MeshSprite { get; }
public SpriteMesh MeshSprite { get; }
public Texture2D Texture { get; }
public Texture2D Normal { get; }
public SpriteBillboardConstraint BillboardConstraint { get; }
public Matrix TransformMatrix { get; }
public UVData UVOffset { get; }
public MeshSpriteDrawData(
MeshSprite meshSprite,
SpriteMesh meshSprite,
Texture2D texture,
Texture2D normal,
SpriteBillboardConstraint billboardConstraint,
Matrix transformMatrix,
UVData offset
) {
MeshSprite = meshSprite;
Texture = texture;
Normal = normal;
BillboardConstraint = billboardConstraint;
TransformMatrix = transformMatrix;
UVOffset = offset;

BIN
Effects/FXB/DiffuseLitSpriteEffect.fxb (Stored with Git LFS)

Binary file not shown.

View File

@ -43,8 +43,8 @@ PixelShaderInput main_vs(VertexShaderInput input)
output.PositionWS = mul(input.Position, World).xyz;
float2 texCoord;
texCoord.x = (input.TexCoord.x / UVOffsetAndDimensions.z) + UVOffsetAndDimensions.x;
texCoord.y = (input.TexCoord.y / UVOffsetAndDimensions.w) + UVOffsetAndDimensions.y;
texCoord.x = (input.TexCoord.x * UVOffsetAndDimensions.z) + UVOffsetAndDimensions.x;
texCoord.y = (input.TexCoord.y * UVOffsetAndDimensions.w) + UVOffsetAndDimensions.y;
output.TexCoord = texCoord;
return output;

View File

@ -4,7 +4,7 @@ using Microsoft.Xna.Framework.Graphics;
namespace Kav
{
public class MeshSprite : ICullable, IIndexDrawable
public class SpriteMesh : ICullable, IIndexDrawable
{
public enum FlipOptions
{
@ -14,7 +14,6 @@ namespace Kav
Both
}
private static readonly int PixelScale = 40;
private static readonly short[] Indices = new short[]
{
0,
@ -25,21 +24,16 @@ namespace Kav
2
};
public Texture2D Texture { get; }
public Texture2D Normal { get; }
public IndexBuffer IndexBuffer { get; }
public VertexBuffer VertexBuffer { get; }
public BoundingBox BoundingBox { get; }
public MeshSprite(
public SpriteMesh(
GraphicsDevice graphicsDevice,
Texture2D texture,
int width,
int height,
FlipOptions flipOptions
) {
Texture = texture;
Normal = null;
IndexBuffer = new IndexBuffer(
graphicsDevice,
IndexElementSize.SixteenBits,
@ -48,7 +42,7 @@ namespace Kav
);
IndexBuffer.SetData(Indices);
var vertexArray = GenerateVertexArray(Texture, flipOptions);
var vertexArray = GenerateVertexArray(width, height, flipOptions);
VertexBuffer = new VertexBuffer(
graphicsDevice,
@ -61,38 +55,11 @@ namespace Kav
BoundingBox = BoundingBox.CreateFromPoints(Positions(vertexArray));
}
public MeshSprite(
GraphicsDevice graphicsDevice,
Texture2D texture,
Texture2D normal,
FlipOptions flipOptions
) {
Texture = texture;
Normal = normal;
IndexBuffer = new IndexBuffer(
graphicsDevice,
IndexElementSize.SixteenBits,
6,
BufferUsage.WriteOnly
);
IndexBuffer.SetData(Indices);
var vertexArray = GenerateVertexArray(Texture, flipOptions);
VertexBuffer = new VertexBuffer(
graphicsDevice,
typeof(VertexPositionNormalTexture),
4,
BufferUsage.WriteOnly
);
VertexBuffer.SetData(vertexArray);
BoundingBox = BoundingBox.CreateFromPoints(Positions(vertexArray));
}
private static VertexPositionNormalTexture[] GenerateVertexArray(Texture2D texture, FlipOptions flipOptions)
private static VertexPositionNormalTexture[] GenerateVertexArray(int pixelWidth, int pixelHeight, FlipOptions flipOptions)
{
var width = pixelWidth / (float)40;
var height = pixelHeight / (float)40;
VertexPositionNormalTexture[] result = new VertexPositionNormalTexture[4];
var xLeft = 0;
@ -112,19 +79,19 @@ namespace Kav
yBottom = 0;
}
result[0].Position = new Vector3(-texture.Width / 2, texture.Height / 2, 0) / PixelScale;
result[0].Position = new Vector3(-width / 2, height / 2, 0);
result[0].Normal = new Vector3(0, 0, 1);
result[0].TextureCoordinate = new Vector2(xLeft, yTop);
result[1].Position = new Vector3(texture.Width / 2, texture.Height / 2, 0) / PixelScale;
result[1].Position = new Vector3(width / 2, height / 2, 0);
result[1].Normal = new Vector3(0, 0, 1);
result[1].TextureCoordinate = new Vector2(xRight, yTop);
result[2].Position = new Vector3(-texture.Width / 2, -texture.Height / 2, 0) / PixelScale;
result[2].Position = new Vector3(-width / 2, -height / 2, 0);
result[2].Normal = new Vector3(0, 0, 1);
result[2].TextureCoordinate = new Vector2(xLeft, yBottom);
result[3].Position = new Vector3(texture.Width / 2, -texture.Height / 2, 0) / PixelScale;
result[3].Position = new Vector3(width / 2, -height / 2, 0);
result[3].Normal = new Vector3(0, 0, 1);
result[3].TextureCoordinate = new Vector2(xRight, yBottom);

View File

@ -168,14 +168,14 @@ namespace Kav
continue;
}
DiffuseLitSpriteEffect.NormalMapEnabled = data.MeshSprite.Normal != null;
DiffuseLitSpriteEffect.NormalMapEnabled = data.Normal != null;
DiffuseLitSpriteEffect.World = matrix;
DiffuseLitSpriteEffect.UVOffset = data.UVOffset.Offset;
DiffuseLitSpriteEffect.SubTextureDimensions = data.UVOffset.Percentage;
GraphicsDevice.Textures[0] = data.MeshSprite.Texture;
GraphicsDevice.Textures[1] = data.MeshSprite.Normal;
GraphicsDevice.Textures[0] = data.Texture;
GraphicsDevice.Textures[1] = data.Normal;
GraphicsDevice.SetVertexBuffer(data.MeshSprite.VertexBuffer);
GraphicsDevice.Indices = data.MeshSprite.IndexBuffer;