diffuse sprite effect takes uv data

tiled_draw_refactor
cosmonaut 2020-12-10 13:53:55 -08:00
parent 8ff6e26887
commit 0ebad486c5
8 changed files with 93 additions and 55 deletions

View File

@ -7,18 +7,18 @@ namespace Kav.Data
public MeshSprite MeshSprite { get; }
public SpriteBillboardConstraint BillboardConstraint { get; }
public Matrix TransformMatrix { get; }
public UVOffsets UVOffsets { get; }
public UVData UVOffset { get; }
public MeshSpriteDrawData(
MeshSprite meshSprite,
SpriteBillboardConstraint billboardConstraint,
Matrix transformMatrix,
UVOffsets uVOffsets
UVData offset
) {
MeshSprite = meshSprite;
BillboardConstraint = billboardConstraint;
TransformMatrix = transformMatrix;
UVOffsets = uVOffsets;
UVOffset = offset;
}
}
}

19
Data/UVData.cs Normal file
View File

@ -0,0 +1,19 @@
using Microsoft.Xna.Framework;
namespace Kav.Data
{
public struct UVData
{
public Vector2 Offset { get; }
public Vector2 Percentage { get; }
public UVData(
Vector2 positionInAtlas,
Vector2 subTextureDimensions,
Vector2 atlasDimensions
) {
Percentage = subTextureDimensions / atlasDimensions;
Offset = positionInAtlas / atlasDimensions;
}
}
}

View File

@ -1,22 +0,0 @@
using Microsoft.Xna.Framework;
namespace Kav.Data
{
public struct UVOffsets
{
public static UVOffsets Default { get; } = new UVOffsets(
new Vector2(0, 0), new Vector2(1, 1)
);
// (start / width), (end / width)
public Vector2 StartUV { get; }
// (start / height), (end / height)
public Vector2 EndUV { get; }
public UVOffsets(Vector2 startUV, Vector2 endUV)
{
StartUV = startUV;
EndUV = endUV;
}
}
}

View File

@ -1,3 +1,4 @@
using Kav.Data;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
@ -10,6 +11,8 @@ namespace Kav
EffectParameter directionalLightDirectionParam;
EffectParameter directionalLightColorParam;
EffectParameter uvOffsetAndDimensionsParam;
EffectParameter worldParam;
EffectParameter worldViewProjectionParam;
EffectParameter worldInverseTransposeParam;
@ -30,6 +33,9 @@ namespace Kav
Matrix view = Matrix.Identity;
Matrix projection = Matrix.Identity;
Vector2 uvOffset;
Vector2 subTextureDimensions;
EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All;
public bool NormalMapEnabled
@ -109,6 +115,26 @@ namespace Kav
}
}
public Vector2 UVOffset
{
get { return uvOffset; }
set
{
uvOffset = value;
dirtyFlags |= EffectDirtyFlags.UVOrDimensions;
}
}
public Vector2 SubTextureDimensions
{
get { return subTextureDimensions; }
set
{
subTextureDimensions = value;
dirtyFlags |= EffectDirtyFlags.UVOrDimensions;
}
}
public DiffuseLitSpriteEffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.DiffuseLitSpriteEffect)
{
CacheEffectParameters();
@ -142,6 +168,16 @@ namespace Kav
dirtyFlags &= ~EffectDirtyFlags.WorldViewProj;
}
if ((dirtyFlags & EffectDirtyFlags.UVOrDimensions) != 0)
{
uvOffsetAndDimensionsParam.SetValue(new Vector4(
UVOffset.X, UVOffset.Y,
SubTextureDimensions.X, SubTextureDimensions.Y
));
dirtyFlags &= ~EffectDirtyFlags.UVOrDimensions;
}
if ((dirtyFlags & EffectDirtyFlags.PixelShaderIndex) != 0)
{
int shaderIndex = 0;
@ -166,6 +202,8 @@ namespace Kav
directionalLightDirectionParam = Parameters["DirectionalLightDirection"];
directionalLightColorParam = Parameters["DirectionalLightColor"];
uvOffsetAndDimensionsParam = Parameters["UVOffsetAndDimensions"];
shaderIndexParam = Parameters["ShaderIndex"];
}
}

View File

@ -11,6 +11,7 @@ namespace Kav
VertexShaderIndex = 8,
PixelShaderIndex = 16,
ViewProj = 32,
UVOrDimensions = 64,
All = -1
}
}

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

Binary file not shown.

View File

@ -5,23 +5,19 @@
DECLARE_TEXTURE(Texture, 0);
DECLARE_TEXTURE(Normal, 1);
BEGIN_CONSTANTS
float3 AmbientColor;
float3 AmbientColor _ps(c0) _cb(c0);
float3 PointLightPositions[8];
float3 PointLightColors[8];
float3 PointLightPositions[8] _ps(c1) _cb(c1);
float3 PointLightColors[8] _ps(c9) _cb(c9);
float3 DirectionalLightDirection;
float3 DirectionalLightColor;
float3 DirectionalLightDirection _ps(c17) _cb(c17);
float3 DirectionalLightColor _ps(c18) _cb(c18);
float4 UVOffsetAndDimensions;
MATRIX_CONSTANTS
float4x4 WorldInverseTranspose _ps(c19) _cb(c19);
float4x4 World _vs(c0) _cb(c23);
float4x4 WorldViewProjection _vs(c4) _cb(c27);
END_CONSTANTS
float4x4 WorldInverseTranspose;
float4x4 World;
float4x4 WorldViewProjection;
struct VertexShaderInput
{
@ -43,10 +39,14 @@ PixelShaderInput main_vs(VertexShaderInput input)
PixelShaderInput output;
output.Position = mul(input.Position, WorldViewProjection);
output.TexCoord = input.TexCoord;
output.NormalWS = normalize(mul(input.Normal, (float3x3)WorldInverseTranspose));
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;
output.TexCoord = texCoord;
return output;
}

View File

@ -171,6 +171,8 @@ namespace Kav
DiffuseLitSpriteEffect.NormalMapEnabled = data.MeshSprite.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;