From da3f9b92034892c9e106af88150d74f755d0deac Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Mon, 5 Aug 2024 19:25:44 +0100 Subject: [PATCH] Load and apply textures --- project/code/Mission.cs | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/project/code/Mission.cs b/project/code/Mission.cs index 8b694eb..78771e5 100644 --- a/project/code/Mission.cs +++ b/project/code/Mission.cs @@ -24,11 +24,11 @@ public partial class Mission : Node3D public bool Dump = false; DbFile _file; - List _textures; // TODO: Make these textures :) + List _textures; public override void _Ready() { - _textures = new List(); + _textures = new List(); var missionSelector = GetNode("%MissionSelector") as MissionSelector; missionSelector.LoadMission += (string path) => @@ -77,7 +77,7 @@ public partial class Mission : Node3D _file = new(FileName); var textureList = (TxList)_file.Chunks["TXLIST"]; - // LoadTextures(textureList); + LoadTextures(textureList); if (Dump) DumpTextureList(textureList); var wr = (WorldRep)_file.Chunks["WREXT"]; @@ -106,13 +106,27 @@ public partial class Mission : Node3D var surfaceArrays = BuildSurfaceArrays(cell, maxPolyIdx, out var packingRects); Image lightmap = BuildLightmap(cell, packingRects, surfaceArrays); - var material = new StandardMaterial3D - { - AlbedoTexture = ImageTexture.CreateFromImage(lightmap), - TextureFilter = BaseMaterial3D.TextureFilterEnum.Nearest, - }; - MeshInstance3D mesh = GenerateMesh(surfaceArrays, material); + // !Hack: This should be somewhere else? + var materials = new List(); + for (var i = 0; i < maxPolyIdx; i++) + { + var textureId = cell.RenderPolys[i].TextureId; + // !HACK: Sky textures :) + if (textureId >= _textures.Count) + { + textureId = 0; + } + + var material = new StandardMaterial3D + { + AlbedoTexture = _textures[textureId], + TextureFilter = BaseMaterial3D.TextureFilterEnum.Nearest, + }; + materials.Add(material); + } + + MeshInstance3D mesh = GenerateMesh(surfaceArrays, materials); OccluderInstance3D occluderInstance = GenerateOccluder(surfaceArrays); var cellNode = new Node3D(); @@ -253,13 +267,13 @@ public partial class Mission : Node3D return occluderInstance; } - private static MeshInstance3D GenerateMesh(List surfaceArrays, StandardMaterial3D material) + private static MeshInstance3D GenerateMesh(List surfaceArrays, List materials) { var arrMesh = new ArrayMesh(); for (var i = 0; i < surfaceArrays.Count; i++) { arrMesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, surfaceArrays[i]); - arrMesh.SurfaceSetMaterial(i, material); + arrMesh.SurfaceSetMaterial(i, materials[i]); } var meshInstance = new MeshInstance3D @@ -360,8 +374,7 @@ public partial class Mission : Node3D } if (Dump) GD.Print($"Loading texture: {path}"); - // _textures.Add(ImageTexture.CreateFromImage(Image.LoadFromFile(path))); - _textures.Add(Image.LoadFromFile(path)); + _textures.Add(ImageTexture.CreateFromImage(Image.LoadFromFile(path))); } }