thief-mission-viewer/project/code/TMV/Model.cs

115 lines
3.7 KiB
C#
Raw Normal View History

2024-08-17 19:53:16 +00:00
using System.Collections.Generic;
using System.IO;
2024-08-17 19:53:16 +00:00
using Godot;
using KeepersCompound.LGS;
using KeepersCompound.TMV.UI;
namespace KeepersCompound.TMV;
public partial class Model : Node3D
{
public override void _Ready()
{
var modelSelector = GetNode<Control>("%ModelSelector") as ModelSelector;
modelSelector.LoadModel += BuildModel;
}
public void BuildModel(string rootPath, string modelPath)
2024-08-17 19:53:16 +00:00
{
foreach (var node in GetChildren())
{
node.QueueFree();
}
var modelFile = new ModelFile(modelPath);
if (modelFile == null)
{
GD.Print($"Failed to load model file: {modelPath}");
return;
2024-08-17 19:53:16 +00:00
}
// TODO: Remove this disgusting hack. Not only is it a hack, it doesn't support custom models
var baseDir = ProjectSettings.GlobalizePath($"user://objects/tmp");
var options = new EnumerationOptions
{
MatchCasing = MatchCasing.CaseInsensitive,
RecurseSubdirectories = true,
};
var materials = new List<StandardMaterial3D>();
foreach (var material in modelFile.Materials)
{
if (material.Type == 0)
{
var paths = Directory.GetFiles(baseDir, material.Name, options);
if (paths.IsEmpty()) continue;
materials.Add(new StandardMaterial3D
{
AlbedoTexture = TextureLoader.LoadTexture(paths[0])
});
}
else
{
var b = (material.Handle) & 0xff;
var g = (material.Handle >> 8) & 0xff;
var r = (material.Handle >> 16) & 0xff;
var colour = new Color(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f);
materials.Add(new StandardMaterial3D
{
AlbedoColor = colour
});
}
}
var surfaceDataMap = new Dictionary<int, MeshSurfaceData>();
2024-08-17 19:53:16 +00:00
foreach (var poly in modelFile.Polygons)
{
2024-08-18 10:14:30 +00:00
var vertices = new List<Vector3>();
var normal = modelFile.Normals[poly.Normal].ToGodotVec3();
var uvs = new List<Vector2>();
for (var i = 0; i < poly.VertexCount; i++)
2024-08-17 19:53:16 +00:00
{
2024-08-18 10:14:30 +00:00
var vertex = modelFile.Vertices[poly.VertexIndices[i]];
vertices.Add(vertex.ToGodotVec3());
if (i < poly.UvIndices.Length)
{
var uv = modelFile.Uvs[poly.UvIndices[i]];
uvs.Add(new Vector2(uv.X, uv.Y));
}
else
{
uvs.Add(Vector2.Zero);
2024-08-18 10:14:30 +00:00
}
2024-08-17 19:53:16 +00:00
}
if (!surfaceDataMap.ContainsKey(poly.Data))
{
surfaceDataMap.Add(poly.Data, new MeshSurfaceData());
}
surfaceDataMap[poly.Data].AddPolygon(vertices, normal, uvs, uvs);
}
var mesh = new ArrayMesh();
foreach (var (materialId, surfaceData) in surfaceDataMap)
{
var array = surfaceData.BuildSurfaceArray();
var surfaceIdx = mesh.GetSurfaceCount();
mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, array);
for (var i = 0; i < materials.Count; i++)
{
var m = modelFile.Materials[i];
if (m.Slot == materialId)
{
mesh.SurfaceSetMaterial(surfaceIdx, materials[i]);
break;
}
}
}
2024-08-17 19:53:16 +00:00
2024-08-25 10:22:13 +00:00
var pos = -modelFile.Header.Center.ToGodotVec3();
var meshInstance = new MeshInstance3D { Mesh = mesh, Position = pos };
2024-08-17 19:53:16 +00:00
AddChild(meshInstance);
}
}