allow sprite flipping on meshsprite

mesh_sprite_draw
cosmonaut 2020-12-09 22:28:51 -08:00
parent 7964d8a171
commit 2f589e584d
4 changed files with 32 additions and 16 deletions

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

Binary file not shown.

View File

@ -44,7 +44,7 @@ PixelShaderInput main_vs(VertexShaderInput input)
output.Position = mul(input.Position, WorldViewProjection);
output.TexCoord = input.TexCoord;
output.NormalWS = mul(input.Normal, (float3x3)WorldInverseTranspose).xyz;
output.NormalWS = normalize(mul(input.Normal, (float3x3)WorldInverseTranspose));
output.PositionWS = mul(input.Position, World).xyz;
return output;

View File

@ -7,16 +7,14 @@ namespace Kav
{
private readonly Vector3[] positions;
private readonly Vector3[] colors;
private readonly float[] intensities;
readonly EffectParameter lightPositionsParam;
readonly EffectParameter lightColorsParam;
public PointLightCollection(EffectParameter lightPositionsParam, EffectParameter lightColorsParam, int maxLights)
{
this.positions = new Vector3[maxLights];
this.colors = new Vector3[maxLights];
this.intensities = new float[maxLights];
positions = new Vector3[maxLights];
colors = new Vector3[maxLights];
this.lightPositionsParam = lightPositionsParam;
this.lightColorsParam = lightColorsParam;
}

View File

@ -6,6 +6,14 @@ namespace Kav
{
public class MeshSprite : ICullable, IIndexDrawable
{
public enum FlipOptions
{
None,
Horizontal,
Vertical,
Both
}
private static readonly int PixelScale = 40;
private static readonly short[] Indices = new short[]
{
@ -26,7 +34,8 @@ namespace Kav
public MeshSprite(
GraphicsDevice graphicsDevice,
Texture2D texture
Texture2D texture,
FlipOptions flipOptions
) {
Texture = texture;
Normal = null;
@ -39,7 +48,7 @@ namespace Kav
);
IndexBuffer.SetData(Indices);
var vertexArray = GenerateVertexArray(Texture);
var vertexArray = GenerateVertexArray(Texture, flipOptions);
VertexBuffer = new VertexBuffer(
graphicsDevice,
@ -55,7 +64,8 @@ namespace Kav
public MeshSprite(
GraphicsDevice graphicsDevice,
Texture2D texture,
Texture2D normal
Texture2D normal,
FlipOptions flipOptions
) {
Texture = texture;
Normal = normal;
@ -68,7 +78,7 @@ namespace Kav
);
IndexBuffer.SetData(Indices);
var vertexArray = GenerateVertexArray(Texture);
var vertexArray = GenerateVertexArray(Texture, flipOptions);
VertexBuffer = new VertexBuffer(
graphicsDevice,
@ -81,25 +91,33 @@ namespace Kav
BoundingBox = BoundingBox.CreateFromPoints(Positions(vertexArray));
}
private static VertexPositionNormalTexture[] GenerateVertexArray(Texture2D texture)
private static VertexPositionNormalTexture[] GenerateVertexArray(Texture2D texture, FlipOptions flipOptions)
{
VertexPositionNormalTexture[] result = new VertexPositionNormalTexture[4];
var xLeft = 0;
var xRight = 1;
if (flipOptions == FlipOptions.Horizontal || flipOptions == FlipOptions.Both)
{
xLeft = 1;
xRight = 0;
}
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[0].TextureCoordinate = new Vector2(xLeft, 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[1].TextureCoordinate = new Vector2(xRight, 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[2].TextureCoordinate = new Vector2(xLeft, 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);
result[3].TextureCoordinate = new Vector2(xRight, 1);
return result;
}