Extract mesh content generation

This commit is contained in:
Jarrod Doyle 2024-08-03 17:22:38 +01:00
parent 5dceedeaf1
commit 114997f720
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 44 additions and 38 deletions

View File

@ -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<int[]>();
var vertices = new List<Vector3>();
var normals = new List<Vector3>();
var indices = new List<int>();
var lightmapUvs = new List<Vector2>();
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<int[]>();
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<int[]> rectIdToUvIdxMap, List<Vector3> vertices, List<Vector3> normals, List<int> indices, List<Vector2> 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<Vector3> vertices, List<int> indices)
{
var occluder = new ArrayOccluder3D();