From 2ff5cb1ca54c34097593d1adc8f48a0b7c3baec7 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 11 Dec 2020 18:22:54 -0800 Subject: [PATCH] decouple sprite mesh from textures --- Data/MeshSpriteDrawData.cs | 11 ++++- Effects/FXB/DiffuseLitSpriteEffect.fxb | 4 +- Effects/HLSL/DiffuseLitSpriteEffect.fx | 4 +- Geometry/{MeshSprite.cs => SpriteMesh.cs} | 59 +++++------------------ Renderer.cs | 6 +-- 5 files changed, 29 insertions(+), 55 deletions(-) rename Geometry/{MeshSprite.cs => SpriteMesh.cs} (60%) diff --git a/Data/MeshSpriteDrawData.cs b/Data/MeshSpriteDrawData.cs index 8d064bb..2280bab 100644 --- a/Data/MeshSpriteDrawData.cs +++ b/Data/MeshSpriteDrawData.cs @@ -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; diff --git a/Effects/FXB/DiffuseLitSpriteEffect.fxb b/Effects/FXB/DiffuseLitSpriteEffect.fxb index 9c25af2..6611fbc 100644 --- a/Effects/FXB/DiffuseLitSpriteEffect.fxb +++ b/Effects/FXB/DiffuseLitSpriteEffect.fxb @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:245ec4c40174cc29a8af797a30da477b6e2c9ddc95ed4999070bf1226d77ea7d -size 7692 +oid sha256:739429f89531ea53a1eebb8e19d43615af2b6d4ee80102f18dce969b8dfcc6af +size 7340 diff --git a/Effects/HLSL/DiffuseLitSpriteEffect.fx b/Effects/HLSL/DiffuseLitSpriteEffect.fx index 7114824..bdc2504 100644 --- a/Effects/HLSL/DiffuseLitSpriteEffect.fx +++ b/Effects/HLSL/DiffuseLitSpriteEffect.fx @@ -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; diff --git a/Geometry/MeshSprite.cs b/Geometry/SpriteMesh.cs similarity index 60% rename from Geometry/MeshSprite.cs rename to Geometry/SpriteMesh.cs index c7fd105..7870333 100644 --- a/Geometry/MeshSprite.cs +++ b/Geometry/SpriteMesh.cs @@ -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); diff --git a/Renderer.cs b/Renderer.cs index 2b4f8ae..1ac47b0 100644 --- a/Renderer.cs +++ b/Renderer.cs @@ -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;