Extract lightmap generation

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

View File

@ -112,6 +112,26 @@ public partial class Mission : Node3D
var lightmapUvs = new List<Vector2>(); var lightmapUvs = new List<Vector2>();
GenerateMeshContent(cell, maxPolyIdx, packingRects, rectIdToUvIdxMap, vertices, normals, indices, lightmapUvs); 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<int[]> rectIdToUvIdxMap, List<Vector2> lightmapUvs)
{
RectanglePacker.Pack(packingRects, out var bounds); RectanglePacker.Pack(packingRects, out var bounds);
var image = Image.Create((int)bounds.Width, (int)bounds.Height, false, Image.Format.Rgba8); var image = Image.Create((int)bounds.Width, (int)bounds.Height, false, Image.Format.Rgba8);
foreach (var rect in packingRects) foreach (var rect in packingRects)
@ -131,11 +151,8 @@ public partial class Mission : Node3D
{ {
rawColour += lightmap.GetPixel(l, x, y); 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 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));
// var texIdx = cell.RenderPolys[rect.Id].TextureId;
// colour = _textures[texIdx].GetPixel((int)x, (int)y);
image.SetPixel((int)(rect.X + x), (int)(rect.Y + y), colour); image.SetPixel((int)(rect.X + x), (int)(rect.Y + y), colour);
} }
} }
@ -161,20 +178,7 @@ public partial class Mission : Node3D
} }
} }
var material = new StandardMaterial3D return image;
{
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);
} }
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) 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)