Load and apply textures

This commit is contained in:
Jarrod Doyle 2024-08-05 19:25:44 +01:00
parent 79f7ebd7eb
commit da3f9b9203
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 26 additions and 13 deletions

View File

@ -24,11 +24,11 @@ public partial class Mission : Node3D
public bool Dump = false;
DbFile _file;
List<Image> _textures; // TODO: Make these textures :)
List<ImageTexture> _textures;
public override void _Ready()
{
_textures = new List<Image>();
_textures = new List<ImageTexture>();
var missionSelector = GetNode<Control>("%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);
// !Hack: This should be somewhere else?
var materials = new List<StandardMaterial3D>();
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 = ImageTexture.CreateFromImage(lightmap),
AlbedoTexture = _textures[textureId],
TextureFilter = BaseMaterial3D.TextureFilterEnum.Nearest,
};
materials.Add(material);
}
MeshInstance3D mesh = GenerateMesh(surfaceArrays, 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<Godot.Collections.Array> surfaceArrays, StandardMaterial3D material)
private static MeshInstance3D GenerateMesh(List<Godot.Collections.Array> surfaceArrays, List<StandardMaterial3D> 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)));
}
}