texture atlas stuff
parent
d476f254e4
commit
c468d6276d
|
@ -0,0 +1,14 @@
|
|||
namespace Kav
|
||||
{
|
||||
public struct AtlasAnimation
|
||||
{
|
||||
public UVData[] Frames { get; }
|
||||
public int Framerate { get; }
|
||||
|
||||
public AtlasAnimation(UVData[] frames, int framerate)
|
||||
{
|
||||
Frames = frames;
|
||||
Framerate = framerate;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
using Kav.Data;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace Kav
|
||||
{
|
||||
public struct TextureAtlasSlice
|
||||
{
|
||||
public int X { get; }
|
||||
public int Y { get; }
|
||||
public int W { get; }
|
||||
public int H { get; }
|
||||
|
||||
public UVData UVData { get; }
|
||||
|
||||
// for use with tiling
|
||||
public Vector2 TiledUVOffset { get; }
|
||||
|
||||
public TextureAtlasSlice(int x, int y, int w, int h, int totalW, int totalH)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
W = w;
|
||||
H = h;
|
||||
|
||||
UVData = new UVData(new Vector2(x, y), new Vector2(w, h), new Vector2(totalW, totalH));
|
||||
|
||||
TiledUVOffset = new Vector2(x / (float)totalW, y / (float)totalH);
|
||||
}
|
||||
}
|
||||
|
||||
public class TextureAtlas
|
||||
{
|
||||
public Texture2D Texture { get; }
|
||||
protected List<string> Names { get; } = new List<string>();
|
||||
protected Dictionary<string, TextureAtlasSlice> Slices { get; } = new Dictionary<string, TextureAtlasSlice>();
|
||||
|
||||
public TextureAtlas(GraphicsDevice graphicsDevice, FileInfo atlasMetadataFile)
|
||||
{
|
||||
var atlasData = CrunchAtlasReader.ReadTextureAtlas(atlasMetadataFile);
|
||||
|
||||
var textureData = atlasData.Textures[0];
|
||||
|
||||
using var stream = File.OpenRead(Path.Combine(atlasMetadataFile.DirectoryName, textureData.Name + ".png"));
|
||||
Texture = Texture2D.FromStream(graphicsDevice, stream);
|
||||
|
||||
foreach (var slice in textureData.Images)
|
||||
{
|
||||
Names.Add(slice.N);
|
||||
|
||||
Slices.Add(
|
||||
slice.N,
|
||||
new TextureAtlasSlice(
|
||||
slice.X,
|
||||
slice.Y,
|
||||
slice.W,
|
||||
slice.H,
|
||||
Texture.Width,
|
||||
Texture.Height
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public TextureAtlasSlice Lookup(string name)
|
||||
{
|
||||
return Slices[name];
|
||||
}
|
||||
|
||||
public string Name(int index)
|
||||
{
|
||||
return Names[index];
|
||||
}
|
||||
|
||||
public int Count()
|
||||
{
|
||||
return Names.Count;
|
||||
}
|
||||
}
|
||||
|
||||
// Assumes all subimages are the same size
|
||||
public class TiledTextureAtlas : TextureAtlas
|
||||
{
|
||||
public int NumRows { get; }
|
||||
public int NumColumns { get; }
|
||||
|
||||
public TiledTextureAtlas(GraphicsDevice graphicsDevice, FileInfo atlasMetadataFile) : base(graphicsDevice, atlasMetadataFile)
|
||||
{
|
||||
var subImageSlice = Slices[Names[0]];
|
||||
NumRows = Texture.Height / subImageSlice.H;
|
||||
NumColumns = Texture.Width / subImageSlice.W;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Kav.Data
|
||||
namespace Kav
|
||||
{
|
||||
public struct UVData
|
||||
{
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<RootNamespace>Kav</RootNamespace>
|
||||
<Authors>Evan Hemsley</Authors>
|
||||
<Copyright>Evan Hemsley 2020</Copyright>
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<RootNamespace>Kav</RootNamespace>
|
||||
<Authors>Evan Hemsley</Authors>
|
||||
<Copyright>Evan Hemsley 2020</Copyright>
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Kav
|
||||
{
|
||||
public static class CrunchAtlasReader
|
||||
{
|
||||
static JsonSerializerOptions options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
public static CrunchTextureAtlasData ReadTextureAtlas(FileInfo file)
|
||||
{
|
||||
return JsonSerializer.Deserialize<CrunchTextureAtlasData>(File.ReadAllText(file.FullName), options);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
namespace Kav
|
||||
{
|
||||
public struct CrunchTextureAtlasData
|
||||
{
|
||||
public CrunchTextureAtlasTextureData[] Textures { get; set; }
|
||||
}
|
||||
|
||||
public struct CrunchTextureAtlasTextureData
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public CrunchTextureAtlasImageData[] Images { get; set; }
|
||||
}
|
||||
|
||||
public struct CrunchTextureAtlasImageData
|
||||
{
|
||||
public string N { get; set; }
|
||||
public int X { get; set; }
|
||||
public int Y { get; set; }
|
||||
public int W { get; set; }
|
||||
public int H { get; set; }
|
||||
}
|
||||
}
|
|
@ -125,7 +125,7 @@ namespace Kav
|
|||
) {
|
||||
GraphicsDevice.SetRenderTarget(renderTarget);
|
||||
|
||||
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
|
||||
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
||||
GraphicsDevice.RasterizerState = RasterizerState.CullNone;
|
||||
GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
|
||||
GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
|
||||
|
|
Loading…
Reference in New Issue