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 _vertices = new(); private readonly List _normals = new(); private readonly List _indices = new(); private readonly List _uv1s = new(); private readonly List _uv2s = new(); public MeshSurfaceData() { Empty = true; } public void TransformUv2s(int start, int end, Func f) { for (var i = start; i < end; i++) { _uv2s[i] = f(_uv2s[i]); } } // TODO: Guard against empty polygons being added public (int, int) AddPolygon( List vertices, Vector3 normal, List uv1s, List uv2s) { 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); } _uv1s.AddRange(uv1s); _uv2s.AddRange(uv2s); var end = _uv2s.Count; var start = end - uv2s.Count; 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(); array[(int)Mesh.ArrayType.TexUV] = _uv1s.ToArray(); array[(int)Mesh.ArrayType.TexUV2] = _uv2s.ToArray(); return array; } }