Fix incorrect anim light map layering

This commit is contained in:
Jarrod Doyle 2024-10-27 09:21:19 +00:00
parent 0a41d78cac
commit c162a5028d
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 22 additions and 27 deletions

View File

@ -152,6 +152,7 @@ public class WorldRep : IChunk
public class Lightmap
{
private readonly bool[] _litLayers;
public List<byte[]> 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<byte[]>();
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]);
}
}
}
}

View File

@ -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);