Compare commits
No commits in common. "cbf309aaa410a9c55891522acf7c4f37ccfde31b" and "d125fb2cd3ff50d6b1705b5eeb3eaba68f034d0d" have entirely different histories.
cbf309aaa4
...
d125fb2cd3
|
@ -1,4 +1,3 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Numerics;
|
||||
|
||||
|
@ -29,16 +28,6 @@ public class WorldRep : IChunk
|
|||
DataSize = reader.ReadUInt32();
|
||||
CellCount = reader.ReadUInt32();
|
||||
}
|
||||
|
||||
public readonly float LightmapScaleMultiplier()
|
||||
{
|
||||
return Math.Sign(LightmapScale) switch
|
||||
{
|
||||
1 => LightmapScale,
|
||||
-1 => 1.0f / LightmapScale,
|
||||
_ => 1.0f,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public struct Cell
|
||||
|
@ -148,63 +137,6 @@ public class WorldRep : IChunk
|
|||
return Vector4.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly byte[] AsBytesRgba()
|
||||
{
|
||||
var length = Width * Height * 4;
|
||||
var bytes = new byte[length];
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
bytes[i] = 0;
|
||||
}
|
||||
|
||||
// TODO: might be faster to do these fors in the switch?
|
||||
for (var z = 0; z < Layers; z++)
|
||||
{
|
||||
for (var y = 0; y < Height; y++)
|
||||
{
|
||||
for (var x = 0; x < Width; x++)
|
||||
{
|
||||
var pIdx = 0 + x * Bpp + y * Bpp * Width + z * Bpp * Width * Height;
|
||||
var r = 0;
|
||||
var g = 0;
|
||||
var b = 0;
|
||||
var a = 0;
|
||||
switch (Bpp)
|
||||
{
|
||||
case 1:
|
||||
var raw1 = Pixels[pIdx];
|
||||
r = raw1;
|
||||
g = raw1;
|
||||
b = raw1;
|
||||
a = 255;
|
||||
break;
|
||||
case 2:
|
||||
var raw2 = Pixels[pIdx] + (Pixels[pIdx + 1] << 8);
|
||||
r = (int)(255 * (raw2 & 31) / 31.0f);
|
||||
g = (int)(255 * ((raw2 >> 5) & 31) / 31.0f);
|
||||
b = (int)(255 * ((raw2 >> 10) & 31) / 31.0f);
|
||||
a = 255;
|
||||
break;
|
||||
case 4:
|
||||
r = Pixels[pIdx + 2];
|
||||
g = Pixels[pIdx + 1];
|
||||
b = Pixels[pIdx];
|
||||
a = Pixels[pIdx + 3];
|
||||
break;
|
||||
}
|
||||
|
||||
var bIdx = x * 4 + y * 4 * Width;
|
||||
bytes[bIdx] = (byte)Math.Min(255, bytes[bIdx] + r);
|
||||
bytes[bIdx + 1] = (byte)Math.Min(255, bytes[bIdx + 1] + g);
|
||||
bytes[bIdx + 2] = (byte)Math.Min(255, bytes[bIdx + 2] + b);
|
||||
bytes[bIdx + 3] = (byte)Math.Min(255, bytes[bIdx + 3] + a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
|
||||
public byte VertexCount { get; set; }
|
||||
|
|
|
@ -267,7 +267,7 @@ public partial class Mission : Node3D
|
|||
|
||||
var renderPoly = cell.RenderPolys[polyIdx];
|
||||
var light = cell.LightList[polyIdx];
|
||||
var lightmapScale = worldRep.DataHeader.LightmapScaleMultiplier();
|
||||
var lightmapScale = worldRep.DataHeader.LightmapScale;
|
||||
var textureId = CalcBaseUV(cell, poly, renderPoly, light, textureUvs, lightmapUvs, lightmapScale, indicesOffset);
|
||||
|
||||
if (!surfaceDataMap.ContainsKey(textureId))
|
||||
|
@ -299,8 +299,23 @@ public partial class Mission : Node3D
|
|||
if (info.cellIndex >= cells.Length) GD.Print($"CellIndex too big: {info.cellIndex}/{cells.Length}");
|
||||
if (info.lightmapIndex >= cells[info.cellIndex].Lightmaps.Length) GD.Print($"LightmapIndex too big: {info.lightmapIndex}/{cells[info.cellIndex].Lightmaps.Length}");
|
||||
var lightmap = cells[info.cellIndex].Lightmaps[info.lightmapIndex];
|
||||
var cellLm = Image.CreateFromData(lightmap.Width, lightmap.Height, false, Image.Format.Rgba8, lightmap.AsBytesRgba());
|
||||
image.BlitRect(cellLm, new Rect2I(0, 0, lightmap.Width, lightmap.Height), new Vector2I((int)rect.X, (int)rect.Y));
|
||||
var layers = (uint)lightmap.Layers;
|
||||
var height = (uint)lightmap.Height;
|
||||
var width = (uint)lightmap.Width;
|
||||
for (uint y = 0; y < height; y++)
|
||||
{
|
||||
for (uint x = 0; x < width; x++)
|
||||
{
|
||||
var rawColour = System.Numerics.Vector4.Zero;
|
||||
for (uint l = 0; l < layers; l++)
|
||||
{
|
||||
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));
|
||||
image.SetPixel((int)(rect.X + x), (int)(rect.Y + y), colour);
|
||||
}
|
||||
}
|
||||
|
||||
if (!surfaceDataMap.ContainsKey(info.textureId)) GD.Print("Invalid SurfaceDataMap key");
|
||||
surfaceDataMap[info.textureId].TransformUv2s(info.uvStart, info.uvEnd, (uv) =>
|
||||
|
|
Loading…
Reference in New Issue