84 lines
3.3 KiB
C#
84 lines
3.3 KiB
C#
using AC2RE.Definitions;
|
|
using ImageMagick;
|
|
using LandblockExtraction.DatEngine;
|
|
using System.Numerics;
|
|
|
|
namespace LandblockExtraction.AtlasMaker;
|
|
public class TerrainAtlasManager {
|
|
private readonly string PATHTERRAINIMG = @".\terrains";
|
|
private PortalEngine portalEngine;
|
|
|
|
public Dictionary<int, Vector2> textureCoord;
|
|
public Dictionary<int, MagickImage> terrainTexture;
|
|
public Dictionary<int, Terrain> terrains;
|
|
public TerrainAtlasManager(PortalEngine portalEngine) {
|
|
this.portalEngine = portalEngine;
|
|
textureCoord = new Dictionary<int, Vector2>();
|
|
terrainTexture = new Dictionary<int, MagickImage>();
|
|
terrains = new Dictionary<int, Terrain>();
|
|
|
|
testGenerateList();
|
|
}
|
|
public void ExtractTexture() {
|
|
using (var atlasBuilder = new AtlasBuilder(portalEngine)) {
|
|
foreach (var terrain in portalEngine.cTerrainDesc.terrains) {
|
|
foreach (var surface in portalEngine.cSurfaceDesc.surfaces) {
|
|
if (surface.surfIndex == terrain.surfaceInfo) {
|
|
atlasBuilder.AddTexture((int)terrain.terrainIndex, surface.terrainMaterials.First().baseMaterials.First().materialDid);
|
|
//terrains.Add((int)terrain.terrainIndex, GenerateSubTerrain((int)terrain.terrainIndex, surface.terrainMaterials));
|
|
}
|
|
}
|
|
textureCoord.Add((int)terrain.terrainIndex, Vector2.Zero);
|
|
}
|
|
|
|
atlasBuilder.GenerateAtlas();
|
|
foreach (var img in atlasBuilder.textures) {
|
|
terrainTexture.Add(img.Key, img.Value);
|
|
}
|
|
}
|
|
}
|
|
/*private Terrain GenerateSubTerrain(int index, List<CSurfaceDesc.MaterialGroup> materialGroups) {
|
|
Terrain tmpTerrain = new Terrain(index);
|
|
foreach(var material in materialGroups) {
|
|
int indexTer = GetIndexBySurface(material.s)
|
|
SubTerrain subTerrain = new(ma)
|
|
}
|
|
}*/
|
|
private void testGenerateList() {
|
|
foreach(var surfaces in portalEngine.cSurfaceDesc.surfaces) {
|
|
Console.WriteLine($"{surfaces.surfIndex} :");
|
|
foreach( var surface in surfaces.terrainMaterials) {
|
|
Console.WriteLine($"\t[{surface.minPitch} < {surface.maxPitch}]({surface.baseMaterials.First().materialDid}) - Vec3: {surface.vertexColor.First().vertexColor}");
|
|
}
|
|
}
|
|
}
|
|
public void SaveAllTerrain() {
|
|
if (!Directory.Exists(PATHTERRAINIMG)) {
|
|
Directory.CreateDirectory(PATHTERRAINIMG);
|
|
}
|
|
|
|
foreach(var img in terrainTexture) {
|
|
var path = Path.Combine(PATHTERRAINIMG, img.Key.ToString());
|
|
|
|
img.Value.Write(path + ".jpg");
|
|
}
|
|
}
|
|
private int? GetIndexBySurface(uint surfaceIndex) {
|
|
foreach(var terrain in portalEngine.cTerrainDesc.terrains) {
|
|
if (terrain.surfaceInfo == surfaceIndex)
|
|
return (int)terrain.terrainIndex;
|
|
}
|
|
return null;
|
|
}
|
|
public void GenerateUV() {
|
|
int count = (int)Math.Ceiling(Math.Sqrt(textureCoord.Count));
|
|
int atlasSize = (int)64 * count;
|
|
int index = 0;
|
|
foreach (var terrain in textureCoord) {
|
|
int x = (index % count) * (int)64;
|
|
int y = (index / count) * (int)64;
|
|
textureCoord[index] = new Vector2(x, y);
|
|
index++;
|
|
}
|
|
}
|
|
}
|