-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build Texture Pages during compilation (#203)
- Loading branch information
Showing
10 changed files
with
180 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
</PropertyGroup> | ||
<ItemGroup> | ||
<AssemblyAttribute Include="GenerateTexturePage.ProjectCompileDirectoryAttribute"> | ||
<_Parameter1 Condition=" '$(ProjectCompileDirectory)' == '' ">$(ProjectDir)../YAMS-LIB</_Parameter1> | ||
<_Parameter1 Condition=" '$(ProjectCompileDirectory)' != '' ">$(ProjectCompileDirectory)</_Parameter1> | ||
</AssemblyAttribute> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4"/> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
|
||
|
||
namespace GenerateTexturePage; | ||
|
||
// Needs to be synced with YAMS-LIB | ||
public class PageItem | ||
{ | ||
[JsonInclude] | ||
public string Name = ""; | ||
[JsonInclude] | ||
public ushort X; | ||
[JsonInclude] | ||
public ushort Y; | ||
[JsonInclude] | ||
public ushort Width; | ||
[JsonInclude] | ||
public ushort Height; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
|
||
using System.Reflection; | ||
using System.Text.Json; | ||
using GenerateTexturePage; | ||
using SixLabors.ImageSharp; | ||
using SixLabors.ImageSharp.PixelFormats; | ||
using SixLabors.ImageSharp.Processing; | ||
|
||
var yamsLibDirectory = Assembly.GetExecutingAssembly() | ||
.GetCustomAttribute<ProjectCompileDirectoryAttribute>() | ||
.ProjectCompileDirectory; | ||
Console.WriteLine($"Identified YAMS-LIB directory as: {yamsLibDirectory}"); | ||
|
||
const int pageDimension = 1024; | ||
var newTexturePage = new Image<Rgba32>(pageDimension, pageDimension); | ||
var textureInfo = new List<PageItem>(); | ||
int lastUsedX = 0, lastUsedY = 0, currentShelfHeight = 0; | ||
|
||
// TODO: Now that we have a seperate project for generating the pages, we should think about making this more optimized. | ||
void AddAllSpritesFromDir(string dirPath) | ||
{ | ||
// Recursively add sprites from subdirs | ||
foreach (string subDir in Directory.GetDirectories(dirPath)) | ||
{ | ||
AddAllSpritesFromDir(subDir); | ||
} | ||
|
||
foreach (string filePath in Directory.GetFiles(dirPath)) | ||
{ | ||
string extension = new FileInfo(filePath).Extension; | ||
if (String.IsNullOrWhiteSpace(extension) || extension == ".md" || extension == ".txt" || extension == ".gitignore" || extension == ".json") | ||
continue; | ||
if (filePath.EndsWith("texturepage.png")) | ||
continue; | ||
|
||
Image sprite = Image.Load(filePath); | ||
currentShelfHeight = Math.Max(currentShelfHeight, sprite.Height); | ||
if (lastUsedX + sprite.Width > pageDimension) | ||
{ | ||
lastUsedX = 0; | ||
lastUsedY += currentShelfHeight; | ||
currentShelfHeight = sprite.Height + 1; // One pixel padding | ||
|
||
if (sprite.Width > pageDimension) | ||
{ | ||
throw new NotSupportedException($"Currently a sprite ({filePath}) is bigger than the max size of a {pageDimension} texture page!"); | ||
} | ||
} | ||
|
||
if (lastUsedY + sprite.Height > pageDimension) throw new NotSupportedException($"Currently all the sprites would be above a {pageDimension} texture page!"); | ||
|
||
int xCoord = lastUsedX; | ||
int yCoord = lastUsedY; | ||
newTexturePage.Mutate(i => i.DrawImage(sprite, new Point(xCoord, yCoord), 1)); | ||
PageItem pageItem = new PageItem(); | ||
pageItem.X = (ushort)xCoord; | ||
pageItem.Y = (ushort)yCoord; | ||
pageItem.Width = (ushort)sprite.Width; | ||
pageItem.Height = (ushort)sprite.Height; | ||
pageItem.Name = Path.GetFileNameWithoutExtension(filePath); | ||
textureInfo.Add(pageItem); | ||
lastUsedX += sprite.Width + 1; //One pixel padding | ||
} | ||
} | ||
|
||
AddAllSpritesFromDir(yamsLibDirectory + "/sprites"); | ||
newTexturePage.SaveAsPng(yamsLibDirectory + "/sprites/texturepage.png"); | ||
File.WriteAllText(yamsLibDirectory + "/sprites/texturepageiteminfo.json", JsonSerializer.Serialize(textureInfo)); | ||
|
||
Console.WriteLine("Written texture page and texture page info."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace GenerateTexturePage; | ||
|
||
[System.AttributeUsage(System.AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)] | ||
sealed class ProjectCompileDirectoryAttribute : System.Attribute | ||
{ | ||
public string ProjectCompileDirectory { get; } | ||
public ProjectCompileDirectoryAttribute(string configurationLocation) | ||
{ | ||
this.ProjectCompileDirectory = configurationLocation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Text.Json.Serialization; | ||
namespace YAMS_LIB; | ||
|
||
// Needs to be synced with GenerateTexturePage | ||
public class PageItem | ||
{ | ||
[JsonInclude] | ||
public string Name = ""; | ||
[JsonInclude] | ||
public ushort X; | ||
[JsonInclude] | ||
public ushort Y; | ||
[JsonInclude] | ||
public ushort Width; | ||
[JsonInclude] | ||
public ushort Height; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
texturepage.png | ||
texturepageiteminfo.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters