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

60 lines
1.6 KiB
C#
Raw Normal View History

2024-08-17 19:53:16 +00:00
using System.Collections.Generic;
using Godot;
using Godot.Collections;
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;
}
private void BuildModel(string rootPath, string modelPath)
{
foreach (var node in GetChildren())
{
node.QueueFree();
}
var modelFile = new ModelFile(modelPath);
if (modelFile == null)
{
GD.Print("UH OH");
}
var vertices = new List<Vector3>();
var indices = new List<int>();
foreach (var v in modelFile.Vertices)
{
vertices.Add(v.ToGodotVec3());
}
foreach (var poly in modelFile.Polygons)
{
for (var i = 1; i < poly.VertexCount - 1; i++)
{
indices.Add(poly.VertexIndices[0]);
indices.Add(poly.VertexIndices[i]);
indices.Add(poly.VertexIndices[i + 1]);
}
}
var array = new Array();
array.Resize((int)Mesh.ArrayType.Max);
array[(int)Mesh.ArrayType.Vertex] = vertices.ToArray();
array[(int)Mesh.ArrayType.Index] = indices.ToArray();
var mesh = new ArrayMesh();
mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, array);
var meshInstance = new MeshInstance3D { Mesh = mesh };
AddChild(meshInstance);
}
}