Use indices for meshes

This commit is contained in:
Jarrod Doyle 2024-07-22 19:37:56 +01:00
parent 1ba0c6ac27
commit 76b95bcf67
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 11 additions and 3 deletions

View File

@ -85,6 +85,7 @@ public partial class Mission : Node3D
var vertices = new List<Vector3>(); var vertices = new List<Vector3>();
var normals = new List<Vector3>(); var normals = new List<Vector3>();
var indices = new List<int>();
var idxOffset = 0; var idxOffset = 0;
for (int i = 0; i < numPolys; i++) for (int i = 0; i < numPolys; i++)
{ {
@ -100,13 +101,19 @@ public partial class Mission : Node3D
var normal = plane.Normal; var normal = plane.Normal;
var numPolyVertices = poly.VertexCount; var numPolyVertices = poly.VertexCount;
var baseIdx = vertices.Count;
for (var j = 0; j < numPolyVertices; j++)
{
var vertex = cell.Vertices[cell.Indices[idxOffset + j]];
vertices.Add(vertex.ToGodotVec3(4.0f));
normals.Add(normal.ToGodotVec3(4.0f));
}
for (int j = 1; j < numPolyVertices - 1; j++) for (int j = 1; j < numPolyVertices - 1; j++)
{ {
foreach (var offset in new int[] { 0, j, j + 1 }) foreach (var offset in new int[] { 0, j, j + 1 })
{ {
var vertex = cell.Vertices[cell.Indices[idxOffset + offset]]; indices.Add(baseIdx + offset);
vertices.Add(vertex.ToGodotVec3(4.0f));
normals.Add(normal.ToGodotVec3(4.0f));
} }
} }
@ -118,6 +125,7 @@ public partial class Mission : Node3D
arrays.Resize((int)Mesh.ArrayType.Max); arrays.Resize((int)Mesh.ArrayType.Max);
arrays[(int)Mesh.ArrayType.Vertex] = vertices.ToArray(); arrays[(int)Mesh.ArrayType.Vertex] = vertices.ToArray();
arrays[(int)Mesh.ArrayType.Normal] = normals.ToArray(); arrays[(int)Mesh.ArrayType.Normal] = normals.ToArray();
arrays[(int)Mesh.ArrayType.Index] = indices.ToArray();
arrMesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, arrays); arrMesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, arrays);
var meshInstance = new MeshInstance3D var meshInstance = new MeshInstance3D