diff --git a/project/code/Mission.cs b/project/code/Mission.cs index 804cc56..62c39b2 100644 --- a/project/code/Mission.cs +++ b/project/code/Mission.cs @@ -112,6 +112,26 @@ public partial class Mission : Node3D var lightmapUvs = new List(); GenerateMeshContent(cell, maxPolyIdx, packingRects, rectIdToUvIdxMap, vertices, normals, indices, lightmapUvs); + Image lightmap = BuildLightmap(cell, packingRects, rectIdToUvIdxMap, lightmapUvs); + + var material = new StandardMaterial3D + { + AlbedoTexture = ImageTexture.CreateFromImage(lightmap), + TextureFilter = BaseMaterial3D.TextureFilterEnum.Nearest, + }; + + MeshInstance3D mesh = GenerateMesh(vertices, normals, indices, lightmapUvs, material); + OccluderInstance3D occluderInstance = GenerateOccluder(vertices, indices); + + var cellNode = new Node3D(); + cellNode.AddChild(mesh); + cellNode.AddChild(occluderInstance); + + AddChild(cellNode); + } + + private static Image BuildLightmap(WorldRep.Cell cell, PackingRectangle[] packingRects, List rectIdToUvIdxMap, List lightmapUvs) + { RectanglePacker.Pack(packingRects, out var bounds); var image = Image.Create((int)bounds.Width, (int)bounds.Height, false, Image.Format.Rgba8); foreach (var rect in packingRects) @@ -131,11 +151,8 @@ public partial class Mission : Node3D { rawColour += lightmap.GetPixel(l, x, y); } - var colour = new Color(MathF.Min(rawColour.X, 1.0f), MathF.Min(rawColour.Y, 1.0f), MathF.Min(rawColour.Z, 1.0f), MathF.Min(rawColour.W, 1.0f)); - // !HACK: lol just overwriting the lightmap :xdd: - // var texIdx = cell.RenderPolys[rect.Id].TextureId; - // colour = _textures[texIdx].GetPixel((int)x, (int)y); + var colour = new Color(MathF.Min(rawColour.X, 1.0f), MathF.Min(rawColour.Y, 1.0f), MathF.Min(rawColour.Z, 1.0f), MathF.Min(rawColour.W, 1.0f)); image.SetPixel((int)(rect.X + x), (int)(rect.Y + y), colour); } } @@ -161,20 +178,7 @@ public partial class Mission : Node3D } } - var material = new StandardMaterial3D - { - AlbedoTexture = ImageTexture.CreateFromImage(image), - TextureFilter = BaseMaterial3D.TextureFilterEnum.Nearest, - }; - - MeshInstance3D mesh = GenerateMesh(vertices, normals, indices, lightmapUvs, material); - OccluderInstance3D occluderInstance = GenerateOccluder(vertices, indices); - - var cellNode = new Node3D(); - cellNode.AddChild(mesh); - cellNode.AddChild(occluderInstance); - - AddChild(cellNode); + return image; } private static void GenerateMeshContent(WorldRep.Cell cell, int maxPolyIdx, PackingRectangle[] packingRects, List rectIdToUvIdxMap, List vertices, List normals, List indices, List lightmapUvs)