Compare commits

..

No commits in common. "d1d6b310ab320b9b9a54dcd54e9010fea74e461c" and "2ec121e056701a7a2055b5a8d875e8f5eb0e2693" have entirely different histories.

3 changed files with 80 additions and 122 deletions

View File

@ -5,18 +5,23 @@ using GArray = Godot.Collections.Array;
namespace KeepersCompound.TMV; namespace KeepersCompound.TMV;
// TODO: Add UV transform method. Should take a transform function and a range of UVs to apply it to
public class MeshSurfaceData public class MeshSurfaceData
{ {
const string MATERIAL_PATH = "res://project/materials/base.tres";
public bool Empty { get; private set; } public bool Empty { get; private set; }
private readonly Texture _texture;
private readonly List<Vector3> _vertices = new(); private readonly List<Vector3> _vertices = new();
private readonly List<Vector3> _normals = new(); private readonly List<Vector3> _normals = new();
private readonly List<int> _indices = new(); private readonly List<int> _indices = new();
private readonly List<Vector2> _textureUvs = new(); private readonly List<Vector2> _textureUvs = new();
private readonly List<Vector2> _lightmapUvs = new(); private readonly List<Vector2> _lightmapUvs = new();
public MeshSurfaceData() public MeshSurfaceData(Texture texture)
{ {
_texture = texture;
Empty = true; Empty = true;
} }
@ -74,4 +79,12 @@ public class MeshSurfaceData
return array; return array;
} }
public Material BuildMaterial(Texture lightmapTexture)
{
var material = ResourceLoader.Load<ShaderMaterial>(MATERIAL_PATH).Duplicate() as ShaderMaterial;
material.SetShaderParameter("texture_albedo", _texture);
material.SetShaderParameter("lightmap_albedo", lightmapTexture);
return material;
}
} }

View File

@ -41,10 +41,12 @@ public partial class Mission : Node3D
public bool Dump = false; public bool Dump = false;
DbFile _file; DbFile _file;
TextureLoader _textureLoader; List<ImageTexture> _textures;
public override void _Ready() public override void _Ready()
{ {
_textures = new List<ImageTexture>();
var missionSelector = GetNode<Control>("%MissionSelector") as MissionSelector; var missionSelector = GetNode<Control>("%MissionSelector") as MissionSelector;
missionSelector.LoadMission += (string path) => missionSelector.LoadMission += (string path) =>
{ {
@ -80,6 +82,8 @@ public partial class Mission : Node3D
public void ClearMap() public void ClearMap()
{ {
_textures.Clear();
foreach (var node in GetChildren()) foreach (var node in GetChildren())
{ {
node.QueueFree(); node.QueueFree();
@ -90,8 +94,6 @@ public partial class Mission : Node3D
{ {
ClearMap(); ClearMap();
_textureLoader = new TextureLoader("", FileName.GetBaseDir());
_file = new(FileName); _file = new(FileName);
var textureList = (TxList)_file.Chunks["TXLIST"]; var textureList = (TxList)_file.Chunks["TXLIST"];
LoadTextures(textureList); LoadTextures(textureList);
@ -100,6 +102,11 @@ public partial class Mission : Node3D
var wr = (WorldRep)_file.Chunks["WREXT"]; var wr = (WorldRep)_file.Chunks["WREXT"];
BuildMeshes(wr.Cells); BuildMeshes(wr.Cells);
// foreach (var cell in wr.Cells)
// {
// BuildCellMesh(cell);
// }
} }
private void BuildMeshes(WorldRep.Cell[] cells) private void BuildMeshes(WorldRep.Cell[] cells)
@ -139,19 +146,19 @@ public partial class Mission : Node3D
} }
var lightmapTexture = BuildLightmapTexture(cells, packingRects.ToArray(), rectDataMap, surfaceDataMap); var lightmapTexture = BuildLightmapTexture(cells, packingRects.ToArray(), rectDataMap, surfaceDataMap);
foreach (var (textureId, surface) in surfaceDataMap) foreach (var surface in surfaceDataMap.Values)
{ {
if (surface.Empty) if (surface.Empty)
{ {
continue; continue;
} }
var albedoTexture = _textureLoader.Get(textureId); var array = surface.BuildSurfaceArray();
var material = surface.BuildMaterial(lightmapTexture);
var mesh = new ArrayMesh(); var mesh = new ArrayMesh();
mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, surface.BuildSurfaceArray());
mesh.SurfaceSetMaterial(0, BuildMaterial(albedoTexture, lightmapTexture));
mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, array);
mesh.SurfaceSetMaterial(0, material);
var meshInstance = new MeshInstance3D { Mesh = mesh }; var meshInstance = new MeshInstance3D { Mesh = mesh };
AddChild(meshInstance); AddChild(meshInstance);
} }
@ -178,7 +185,7 @@ public partial class Mission : Node3D
if (!surfaceDataMap.ContainsKey(textureId)) if (!surfaceDataMap.ContainsKey(textureId))
{ {
surfaceDataMap.Add(textureId, new MeshSurfaceData()); surfaceDataMap.Add(textureId, new MeshSurfaceData(_textures[textureId]));
} }
var surfaceData = surfaceDataMap[textureId]; var surfaceData = surfaceDataMap[textureId];
var (start, end) = surfaceData.AddPolygon(vertices, normal, textureUvs, lightmapUvs); var (start, end) = surfaceData.AddPolygon(vertices, normal, textureUvs, lightmapUvs);
@ -252,8 +259,13 @@ public partial class Mission : Node3D
// TODO: This is slightly hardcoded for ND. Check other stuff at some point. Should be handled in LG side imo // TODO: This is slightly hardcoded for ND. Check other stuff at some point. Should be handled in LG side imo
// TODO: This is a mess lol // TODO: This is a mess lol
var textureId = renderPoly.TextureId; var textureId = renderPoly.TextureId;
// !HACK: Sky textures :)
if (textureId >= _textures.Count)
{
textureId = 0;
}
var texture = _textureLoader.Get(textureId); var texture = _textures[textureId];
var texU = renderPoly.TextureVectors.Item1.ToGodotVec3(); var texU = renderPoly.TextureVectors.Item1.ToGodotVec3();
var texV = renderPoly.TextureVectors.Item2.ToGodotVec3(); var texV = renderPoly.TextureVectors.Item2.ToGodotVec3();
var baseU = renderPoly.TextureBases.Item1; var baseU = renderPoly.TextureBases.Item1;
@ -320,6 +332,36 @@ public partial class Mission : Node3D
private void LoadTextures(TxList textureList) private void LoadTextures(TxList textureList)
{ {
static string PathToKey(string baseDir, string path)
{
return path.TrimPrefix(baseDir).GetBaseName().ToLower();
}
// TODO: This has hardcoded .png extension and relies on you placing extracted and converted images in godot user directory
// Collect all the fm textures here to help with case sensitivity :)
// TODO: Only do this on case sensitive systems?
var baseDir = FileName.GetBaseDir();
var options = new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive };
var dirPaths = Directory.GetDirectories(baseDir, "fam", options);
options.RecurseSubdirectories = true;
var texturePaths = new Dictionary<string, string>();
// Godot doesn't support runtime DDS :)
// TODO: Load DDS BMP PCX GIF CEL
string[] validExtensions = { "png", "tga" };
foreach (var dirPath in dirPaths)
{
foreach (var path in Directory.EnumerateFiles(dirPath, "*", options))
{
if (validExtensions.Contains(path.GetExtension().ToLower()))
{
// TODO: This only adds the first one found rather than the highest priority
texturePaths.TryAdd(PathToKey(baseDir, path), path);
}
}
}
// TODO: Use PathJoin // TODO: Use PathJoin
var count = textureList.ItemCount; var count = textureList.ItemCount;
for (var i = 0; i < count; i++) for (var i = 0; i < count; i++)
@ -338,10 +380,22 @@ public partial class Mission : Node3D
} }
path += item.Name; path += item.Name;
if (!_textureLoader.Load(i, path)) if (texturePaths.TryGetValue(path.ToLower(), out var newPath))
{ {
GD.Print($"Failed to load texture: {path}"); path = newPath;
} }
else if (File.Exists(ProjectSettings.GlobalizePath($"user://textures{path}.png")))
{
path = ProjectSettings.GlobalizePath($"user://textures{path}.png");
}
else
{
GD.Print($"Failed to find texture: {path}");
path = "user://textures/jorge.png";
}
if (Dump) GD.Print($"Loading texture: {path}");
_textures.Add(ImageTexture.CreateFromImage(Image.LoadFromFile(path)));
} }
} }
@ -359,13 +413,4 @@ public partial class Mission : Node3D
GD.Print($" {i}:\n Tokens: [{item.Tokens[0]}, {item.Tokens[1]}, {item.Tokens[2]}, {item.Tokens[3]}]\n Name: {item.Name}"); GD.Print($" {i}:\n Tokens: [{item.Tokens[0]}, {item.Tokens[1]}, {item.Tokens[2]}, {item.Tokens[3]}]\n Name: {item.Name}");
} }
} }
const string MATERIAL_PATH = "res://project/materials/base.tres";
private static Material BuildMaterial(Texture albedoTexture, Texture lightmapTexture)
{
var material = ResourceLoader.Load<ShaderMaterial>(MATERIAL_PATH).Duplicate() as ShaderMaterial;
material.SetShaderParameter("texture_albedo", albedoTexture);
material.SetShaderParameter("lightmap_albedo", lightmapTexture);
return material;
}
} }

View File

@ -1,100 +0,0 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Godot;
namespace KeepersCompound.TMV;
public class TextureLoader
{
private readonly string _rootPath; // TODO: Load from installation resources
private readonly string _fmPath;
private readonly Dictionary<string, string> _fmTexturePaths = new();
private readonly List<ImageTexture> _textureCache = new();
private readonly Dictionary<int, int> _idMap = new();
private readonly Dictionary<string, int> _pathMap = new();
public TextureLoader(string rootPath, string fmPath)
{
_rootPath = rootPath;
_fmPath = fmPath;
LoadDefaultTexture();
RegisterFmTexturePaths();
}
private void LoadDefaultTexture()
{
// TODO: This should be a resource loaded from RES
const string path = "user://textures/jorge.png";
var texture = ImageTexture.CreateFromImage(Image.LoadFromFile(path));
_textureCache.Add(texture);
}
private void RegisterFmTexturePaths()
{
// TODO: Load DDS BMP PCX GIF CEL
string[] validExtensions = { "png", "tga" };
var famOptions = new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive };
var textureOptions = new EnumerationOptions
{
MatchCasing = MatchCasing.CaseInsensitive,
RecurseSubdirectories = true,
};
foreach (var dirPath in Directory.EnumerateDirectories(_fmPath, "fam", famOptions))
{
foreach (var path in Directory.EnumerateFiles(dirPath, "*", textureOptions))
{
if (validExtensions.Contains(path.GetExtension().ToLower()))
{
// TODO: This only adds the first one found rather than the highest priority
var key = path.TrimPrefix(_fmPath).GetBaseName().ToLower();
_fmTexturePaths.TryAdd(key, path);
}
}
}
}
public bool Load(int id, string path)
{
var userTexturesPath = ProjectSettings.GlobalizePath($"user://textures{path}.png");
var loaded = false;
if (_fmTexturePaths.TryGetValue(path.ToLower(), out var filePath))
{
_textureCache.Add(ImageTexture.CreateFromImage(Image.LoadFromFile(filePath)));
loaded = true;
}
else if (File.Exists(userTexturesPath))
{
_textureCache.Add(ImageTexture.CreateFromImage(Image.LoadFromFile(userTexturesPath)));
loaded = true;
}
var index = loaded ? _textureCache.Count - 1 : 0;
_idMap.TryAdd(id, index);
_pathMap.TryAdd(path, index);
return loaded;
}
public ImageTexture Get(int id)
{
if (!_idMap.ContainsKey(id))
{
return _textureCache[0];
}
return _textureCache[_idMap[id]];
}
public ImageTexture Get(string path)
{
if (!_pathMap.ContainsKey(path))
{
return _textureCache[0];
}
return _textureCache[_pathMap[path]];
}
}