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

77 lines
2.0 KiB
C#
Raw Permalink Normal View History

2024-08-11 11:28:24 +00:00
using System;
using System.Collections.Generic;
using Godot;
using GArray = Godot.Collections.Array;
namespace KeepersCompound.TMV;
public class MeshSurfaceData
{
public bool Empty { get; private set; }
private readonly List<Vector3> _vertices = new();
private readonly List<Vector3> _normals = new();
private readonly List<int> _indices = new();
2024-08-18 10:13:18 +00:00
private readonly List<Vector2> _uv1s = new();
private readonly List<Vector2> _uv2s = new();
2024-08-11 11:28:24 +00:00
2024-08-11 14:43:44 +00:00
public MeshSurfaceData()
2024-08-11 11:28:24 +00:00
{
Empty = true;
}
2024-08-18 10:13:18 +00:00
public void TransformUv2s(int start, int end, Func<Vector2, Vector2> f)
2024-08-11 11:28:24 +00:00
{
for (var i = start; i < end; i++)
{
2024-08-18 10:13:18 +00:00
_uv2s[i] = f(_uv2s[i]);
2024-08-11 11:28:24 +00:00
}
}
// TODO: Guard against empty polygons being added
public (int, int) AddPolygon(
List<Vector3> vertices,
Vector3 normal,
2024-08-18 10:13:18 +00:00
List<Vector2> uv1s,
List<Vector2> uv2s)
2024-08-11 11:28:24 +00:00
{
Empty = false;
var vertexCount = vertices.Count;
var indexOffset = _vertices.Count;
_vertices.AddRange(vertices);
for (var i = 0; i < vertexCount; i++)
{
_normals.Add(normal);
}
// Simple triangulation. Polys are always convex so we can just do a fan
for (int j = 1; j < vertexCount - 1; j++)
{
_indices.Add(indexOffset);
_indices.Add(indexOffset + j);
_indices.Add(indexOffset + j + 1);
}
2024-08-18 10:13:18 +00:00
_uv1s.AddRange(uv1s);
_uv2s.AddRange(uv2s);
2024-08-11 11:28:24 +00:00
2024-08-18 10:13:18 +00:00
var end = _uv2s.Count;
var start = end - uv2s.Count;
2024-08-11 11:28:24 +00:00
return (start, end);
}
public GArray BuildSurfaceArray()
{
var array = new GArray();
array.Resize((int)Mesh.ArrayType.Max);
array[(int)Mesh.ArrayType.Vertex] = _vertices.ToArray();
array[(int)Mesh.ArrayType.Normal] = _normals.ToArray();
array[(int)Mesh.ArrayType.Index] = _indices.ToArray();
2024-08-18 10:13:18 +00:00
array[(int)Mesh.ArrayType.TexUV] = _uv1s.ToArray();
array[(int)Mesh.ArrayType.TexUV2] = _uv2s.ToArray();
2024-08-11 11:28:24 +00:00
return array;
}
}