diff --git a/KeepersCompound.LGS/Database/Chunks/WorldRep.cs b/KeepersCompound.LGS/Database/Chunks/WorldRep.cs index 4b797b7..2a7094a 100644 --- a/KeepersCompound.LGS/Database/Chunks/WorldRep.cs +++ b/KeepersCompound.LGS/Database/Chunks/WorldRep.cs @@ -152,6 +152,7 @@ public class WorldRep : IChunk public class Lightmap { + private readonly bool[] _litLayers; public List Pixels { get; set; } public int Layers; @@ -163,10 +164,12 @@ public class WorldRep : IChunk { var layers = 1 + BitOperations.PopCount(bitmask); var length = bytesPerPixel * width * height; + _litLayers = new bool[33]; Pixels = new List(); for (var i = 0; i < layers; i++) { Pixels.Add(reader.ReadBytes(length)); + _litLayers[i] = true; } Layers = layers; Width = width; @@ -246,6 +249,8 @@ public class WorldRep : IChunk pLayer[idx + 1] = (byte)Math.Clamp(pLayer[idx + 1] + g, 0, 255); pLayer[idx + 2] = (byte)Math.Clamp(pLayer[idx + 2] + b, 0, 255); pLayer[idx + 3] = 255; + + _litLayers[layer] = true; } public void AddLight(int layer, int x, int y, Vector3 color, float strength, bool hdr) @@ -271,9 +276,18 @@ public class WorldRep : IChunk public void Reset(Vector3 ambientLight, bool hdr) { - Layers = 0; + Layers = 33; Pixels.Clear(); - AddLayer(); + var bytesPerLayer = Width * Height * Bpp; + for (var i = 0; i < Layers; i++) + { + Pixels.Add(new byte[bytesPerLayer]); + for (var j = 0; j < bytesPerLayer; j++) + { + Pixels[i][j] = 0; + } + _litLayers[i] = false; + } for (var y = 0; y < Height; y++) { @@ -284,22 +298,14 @@ public class WorldRep : IChunk } } - public void AddLayer() - { - var bytesPerLayer = Width * Height * Bpp; - Pixels.Add(new byte[bytesPerLayer]); - for (var j = 0; j < bytesPerLayer; j++) - { - Pixels[Layers][j] = 0; - } - Layers++; - } - public void Write(BinaryWriter writer) { - foreach (var layer in Pixels) + for (var i = 0; i < Layers; i++) { - writer.Write(layer); + if (_litLayers[i]) + { + writer.Write(Pixels[i]); + } } } } diff --git a/KeepersCompound.Lightmapper/Program.cs b/KeepersCompound.Lightmapper/Program.cs index f3b967b..8e7bfb0 100644 --- a/KeepersCompound.Lightmapper/Program.cs +++ b/KeepersCompound.Lightmapper/Program.cs @@ -445,13 +445,6 @@ class Program foreach (var light in lights) { var layer = 0; - if (light.anim) - { - // Because we're building the AnimLightBitmask in this loop we - // know there aren't any layers set above us. So the target layer - // is just the number of set bits + 1. - layer = BitOperations.PopCount(info.AnimLightBitmask) + 1; - } // Check if plane normal is facing towards the light // If it's not then we're never going to be (directly) lit by this @@ -535,11 +528,7 @@ class Program cell.AnimLights.Add((ushort)light.lightTableIndex); } info.AnimLightBitmask |= 1u << paletteIdx; - - if (layer >= lightmap.Layers) - { - lightmap.AddLayer(); - } + layer = paletteIdx + 1; } var strength = CalculateLightStrengthAtPoint(light, pos, plane); lightmap.AddLight(layer, x, y, light.color, strength, hdr);