From 114997f720e1c23e632c3d16b9d7f7dd744fafc1 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sat, 3 Aug 2024 17:22:38 +0100 Subject: [PATCH] Extract mesh content generation --- project/code/Mission.cs | 82 ++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/project/code/Mission.cs b/project/code/Mission.cs index edb132c..804cc56 100644 --- a/project/code/Mission.cs +++ b/project/code/Mission.cs @@ -99,48 +99,18 @@ public partial class Mission : Node3D return; } + // You'd think these would be the same number, but apparently not + // I think it's because water counts as a render poly and a portal poly + var maxPolyIdx = Math.Min(numRenderPolys, numPolys - numPortalPolys); + + var packingRects = new PackingRectangle[maxPolyIdx]; + var rectIdToUvIdxMap = new List(); + var vertices = new List(); var normals = new List(); var indices = new List(); var lightmapUvs = new List(); - var cellIdxOffset = 0; - - // You'd think these would be the same number, but apparently not - // I think it's because water counts as a render poly and a portal poly - var maxPolyIdx = Math.Min(numRenderPolys, numPolys - numPortalPolys); - var packingRects = new PackingRectangle[maxPolyIdx]; - var rectIdToUvIdxMap = new List(); - for (int i = 0; i < maxPolyIdx; i++) - { - var poly = cell.Polys[i]; - var meshIdxOffset = vertices.Count; - - var normal = cell.Planes[poly.PlaneId].Normal.ToGodotVec3(); - var numPolyVertices = poly.VertexCount; - for (var j = 0; j < numPolyVertices; j++) - { - var vertex = cell.Vertices[cell.Indices[cellIdxOffset + j]]; - vertices.Add(vertex.ToGodotVec3()); - normals.Add(normal); - } - - // Simple triangulation. Polys are always convex so we can just do a fan - for (int j = 1; j < numPolyVertices - 1; j++) - { - indices.Add(meshIdxOffset); - indices.Add(meshIdxOffset + j); - indices.Add(meshIdxOffset + j + 1); - } - - // UVs - var renderPoly = cell.RenderPolys[i]; - var light = cell.LightList[i]; - packingRects[i] = new PackingRectangle(0, 0, light.Width, light.Height, i); - var uvIdxs = CalcBaseUV(cell, poly, renderPoly, light, lightmapUvs, cellIdxOffset); - rectIdToUvIdxMap.Add(uvIdxs); - - cellIdxOffset += poly.VertexCount; - } + GenerateMeshContent(cell, maxPolyIdx, packingRects, rectIdToUvIdxMap, vertices, normals, indices, lightmapUvs); RectanglePacker.Pack(packingRects, out var bounds); var image = Image.Create((int)bounds.Width, (int)bounds.Height, false, Image.Format.Rgba8); @@ -207,6 +177,42 @@ public partial class Mission : Node3D AddChild(cellNode); } + private static void GenerateMeshContent(WorldRep.Cell cell, int maxPolyIdx, PackingRectangle[] packingRects, List rectIdToUvIdxMap, List vertices, List normals, List indices, List lightmapUvs) + { + var cellIdxOffset = 0; + for (int i = 0; i < maxPolyIdx; i++) + { + var poly = cell.Polys[i]; + var meshIdxOffset = vertices.Count; + + var normal = cell.Planes[poly.PlaneId].Normal.ToGodotVec3(); + var numPolyVertices = poly.VertexCount; + for (var j = 0; j < numPolyVertices; j++) + { + var vertex = cell.Vertices[cell.Indices[cellIdxOffset + j]]; + vertices.Add(vertex.ToGodotVec3()); + normals.Add(normal); + } + + // Simple triangulation. Polys are always convex so we can just do a fan + for (int j = 1; j < numPolyVertices - 1; j++) + { + indices.Add(meshIdxOffset); + indices.Add(meshIdxOffset + j); + indices.Add(meshIdxOffset + j + 1); + } + + // UVs + var renderPoly = cell.RenderPolys[i]; + var light = cell.LightList[i]; + packingRects[i] = new PackingRectangle(0, 0, light.Width, light.Height, i); + var uvIdxs = CalcBaseUV(cell, poly, renderPoly, light, lightmapUvs, cellIdxOffset); + rectIdToUvIdxMap.Add(uvIdxs); + + cellIdxOffset += poly.VertexCount; + } + } + private static OccluderInstance3D GenerateOccluder(List vertices, List indices) { var occluder = new ArrayOccluder3D();