Compare commits

...

3 Commits

Author SHA1 Message Date
Jarrod Doyle d1d6b310ab
Move material creation back to Mission 2024-08-11 15:43:44 +01:00
Jarrod Doyle 59edc0744c
Add texture manager/loader 2024-08-11 15:23:56 +01:00
Jarrod Doyle 51136b9526
Remove old code/comments 2024-08-11 14:31:27 +01:00
3 changed files with 123 additions and 81 deletions

View File

@ -5,23 +5,18 @@ 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(Texture texture) public MeshSurfaceData()
{ {
_texture = texture;
Empty = true; Empty = true;
} }
@ -79,12 +74,4 @@ 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,12 +41,10 @@ public partial class Mission : Node3D
public bool Dump = false; public bool Dump = false;
DbFile _file; DbFile _file;
List<ImageTexture> _textures; TextureLoader _textureLoader;
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) =>
{ {
@ -82,8 +80,6 @@ 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();
@ -94,6 +90,8 @@ 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);
@ -102,11 +100,6 @@ 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)
@ -146,19 +139,19 @@ public partial class Mission : Node3D
} }
var lightmapTexture = BuildLightmapTexture(cells, packingRects.ToArray(), rectDataMap, surfaceDataMap); var lightmapTexture = BuildLightmapTexture(cells, packingRects.ToArray(), rectDataMap, surfaceDataMap);
foreach (var surface in surfaceDataMap.Values) foreach (var (textureId, surface) in surfaceDataMap)
{ {
if (surface.Empty) if (surface.Empty)
{ {
continue; continue;
} }
var array = surface.BuildSurfaceArray(); var albedoTexture = _textureLoader.Get(textureId);
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);
} }
@ -185,7 +178,7 @@ public partial class Mission : Node3D
if (!surfaceDataMap.ContainsKey(textureId)) if (!surfaceDataMap.ContainsKey(textureId))
{ {
surfaceDataMap.Add(textureId, new MeshSurfaceData(_textures[textureId])); surfaceDataMap.Add(textureId, new MeshSurfaceData());
} }
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);
@ -259,13 +252,8 @@ 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 = _textures[textureId]; var texture = _textureLoader.Get(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;
@ -332,36 +320,6 @@ 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++)
@ -380,22 +338,10 @@ public partial class Mission : Node3D
} }
path += item.Name; path += item.Name;
if (texturePaths.TryGetValue(path.ToLower(), out var newPath)) if (!_textureLoader.Load(i, path))
{ {
path = newPath; GD.Print($"Failed to load texture: {path}");
} }
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)));
} }
} }
@ -413,4 +359,13 @@ 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

@ -0,0 +1,100 @@
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]];
}
}