map3drendering/LandblockExtraction/AtlasMaker/AtlasBuilder.cs
Troispoils b9a2b87fe9 Update for AtlasBuilder ok
Need to improuve duplicate vertex for mapping texture.
2024-02-27 17:04:09 +01:00

49 lines
1.4 KiB
C#

using AC2RE.Definitions;
using ImageMagick;
using LandblockExtraction.DatEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace LandblockExtraction.AtlasMaker;
public class AtlasBuilder {
private readonly int TEXTURESIZE = 64;
private TexturesImage texturesImage;
private List<MagickImage> textures;
public AtlasBuilder(PortalEngine portalEngine) {
textures = new();
texturesImage = new TexturesImage(portalEngine);
}
public bool AddTexture(DataId matId) {
var img = texturesImage.GetImage(matId);
if(img == null) return false;
textures.Add(img);
return true;
}
public void GenerateAtlas() {
int count = (int)Math.Ceiling(Math.Sqrt(textures.Count));
int atlasSize = (int)TEXTURESIZE * count;
using (MagickImage atlas = new MagickImage(new MagickColor("#FFFFFF"), atlasSize, atlasSize)) {
int index = 0;
foreach (var kvp in textures) {
int x = (index % count) * (int)TEXTURESIZE;
int y = (index / count) * (int)TEXTURESIZE;
atlas.Composite(kvp, x, y);
index++;
if (index >= count * count) break;
}
atlas.Write("atlas.jpg");
}
}
}