Use Texture2dArray for lightmap texture

This commit is contained in:
Jarrod Doyle 2024-08-27 21:18:31 +01:00
parent cbf309aaa4
commit 092896aa52
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
3 changed files with 68 additions and 51 deletions

View File

@ -149,56 +149,44 @@ public class WorldRep : IChunk
} }
} }
public readonly byte[] AsBytesRgba() public readonly byte[] AsBytesRgba(int layer)
{ {
var length = Width * Height * 4; if (layer >= Layers)
var bytes = new byte[length];
for (var i = 0; i < length; i++)
{ {
bytes[i] = 0; throw new ArgumentOutOfRangeException(nameof(layer));
} }
// TODO: might be faster to do these fors in the switch? var length = Width * Height * 4;
for (var z = 0; z < Layers; z++) var layerOffset = layer * Bpp * Width * Height;
var bytes = new byte[length];
for (var y = 0; y < Height; y++)
{ {
for (var y = 0; y < Height; y++) for (var x = 0; x < Width; x++)
{ {
for (var x = 0; x < Width; x++) var bIdx = x * 4 + y * 4 * Width;
var pIdx = x * Bpp + y * Bpp * Width + layerOffset;
switch (Bpp)
{ {
var pIdx = 0 + x * Bpp + y * Bpp * Width + z * Bpp * Width * Height; case 1:
var r = 0; var raw1 = Pixels[pIdx];
var g = 0; bytes[bIdx] = raw1;
var b = 0; bytes[bIdx + 1] = raw1;
var a = 0; bytes[bIdx + 2] = raw1;
switch (Bpp) bytes[bIdx + 3] = 255;
{ break;
case 1: case 2:
var raw1 = Pixels[pIdx]; var raw2 = Pixels[pIdx] + (Pixels[pIdx + 1] << 8);
r = raw1; bytes[bIdx] = (byte)(255 * (raw2 & 31) / 31.0f);
g = raw1; bytes[bIdx + 1] = (byte)(255 * ((raw2 >> 5) & 31) / 31.0f);
b = raw1; bytes[bIdx + 2] = (byte)(255 * ((raw2 >> 10) & 31) / 31.0f);
a = 255; bytes[bIdx + 3] = 255;
break; break;
case 2: case 4:
var raw2 = Pixels[pIdx] + (Pixels[pIdx + 1] << 8); bytes[bIdx] = Pixels[pIdx + 2];
r = (int)(255 * (raw2 & 31) / 31.0f); bytes[bIdx + 1] = Pixels[pIdx + 1];
g = (int)(255 * ((raw2 >> 5) & 31) / 31.0f); bytes[bIdx + 2] = Pixels[pIdx];
b = (int)(255 * ((raw2 >> 10) & 31) / 31.0f); bytes[bIdx + 3] = Pixels[pIdx + 3];
a = 255; break;
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);
} }
} }
} }

View File

@ -282,7 +282,11 @@ public partial class Mission : Node3D
return new LightmapRectData(cellIdx, polyIdx, textureId, start, end); return new LightmapRectData(cellIdx, polyIdx, textureId, start, end);
} }
private static Texture BuildLightmapTexture(WorldRep.Cell[] cells, PackingRectangle[] packingRects, Dictionary<int, LightmapRectData> rectDataMap, Dictionary<int, MeshSurfaceData> surfaceDataMap) private static Texture2DArray BuildLightmapTexture(
WorldRep.Cell[] cells,
PackingRectangle[] packingRects,
Dictionary<int, LightmapRectData> rectDataMap,
Dictionary<int, MeshSurfaceData> surfaceDataMap)
{ {
var bounds = Timing.TimeStage("RectPack", () => var bounds = Timing.TimeStage("RectPack", () =>
{ {
@ -290,7 +294,16 @@ public partial class Mission : Node3D
return bounds; return bounds;
}); });
GD.Print($"Creating lightmap with bounds: ({bounds.Width}, {bounds.Height})"); GD.Print($"Creating lightmap with bounds: ({bounds.Width}, {bounds.Height})");
var image = Image.CreateEmpty((int)bounds.Width, (int)bounds.Height, false, Image.Format.Rgba8);
var lightmapFormat = Image.Format.Rgba8;
var lmLayerCount = 33;
var lmImages = new Godot.Collections.Array<Image>();
lmImages.Resize(lmLayerCount);
for (var i = 0; i < lmLayerCount; i++)
{
lmImages[i] = Image.CreateEmpty((int)bounds.Width, (int)bounds.Height, false, lightmapFormat);
}
foreach (var rect in packingRects) foreach (var rect in packingRects)
{ {
if (!rectDataMap.ContainsKey(rect.Id)) GD.Print("Invalid rectDataMap key"); if (!rectDataMap.ContainsKey(rect.Id)) GD.Print("Invalid rectDataMap key");
@ -298,9 +311,18 @@ public partial class Mission : Node3D
if (info.cellIndex >= cells.Length) GD.Print($"CellIndex too big: {info.cellIndex}/{cells.Length}"); 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}"); 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 lightmap = cells[info.cellIndex].Lightmaps[info.lightmapIndex];
var cellLm = Image.CreateFromData(lightmap.Width, lightmap.Height, false, Image.Format.Rgba8, lightmap.AsBytesRgba()); var width = lightmap.Width;
image.BlitRect(cellLm, new Rect2I(0, 0, lightmap.Width, lightmap.Height), new Vector2I((int)rect.X, (int)rect.Y)); var height = lightmap.Height;
var layerCount = lightmap.Layers;
var srcRect = new Rect2I(0, 0, width, height);
var dst = new Vector2I((int)rect.X, (int)rect.Y);
for (var i = 0; i < layerCount; i++)
{
var cellLm = Image.CreateFromData(width, height, false, lightmapFormat, lightmap.AsBytesRgba(i));
lmImages[i].BlitRect(cellLm, srcRect, dst);
}
if (!surfaceDataMap.ContainsKey(info.textureId)) GD.Print("Invalid SurfaceDataMap key"); if (!surfaceDataMap.ContainsKey(info.textureId)) GD.Print("Invalid SurfaceDataMap key");
surfaceDataMap[info.textureId].TransformUv2s(info.uvStart, info.uvEnd, (uv) => surfaceDataMap[info.textureId].TransformUv2s(info.uvStart, info.uvEnd, (uv) =>
@ -321,7 +343,9 @@ public partial class Mission : Node3D
}); });
} }
return ImageTexture.CreateFromImage(image); var lightmapTexture = new Texture2DArray();
lightmapTexture.CreateFromImages(lmImages);
return lightmapTexture;
} }
private int CalcBaseUV( private int CalcBaseUV(
@ -448,7 +472,7 @@ public partial class Mission : Node3D
} }
const string MATERIAL_PATH = "res://project/materials/base.tres"; const string MATERIAL_PATH = "res://project/materials/base.tres";
private static Material BuildMaterial(Texture albedoTexture, Texture lightmapTexture, bool lmHdr) private static Material BuildMaterial(Texture2D albedoTexture, Texture2DArray lightmapTexture, bool lmHdr)
{ {
var material = ResourceLoader.Load<ShaderMaterial>(MATERIAL_PATH).Duplicate() as ShaderMaterial; var material = ResourceLoader.Load<ShaderMaterial>(MATERIAL_PATH).Duplicate() as ShaderMaterial;
material.SetShaderParameter("texture_albedo", albedoTexture); material.SetShaderParameter("texture_albedo", albedoTexture);

View File

@ -4,7 +4,7 @@
code = "shader_type spatial; code = "shader_type spatial;
render_mode blend_mix,depth_draw_opaque,cull_back,unshaded; render_mode blend_mix,depth_draw_opaque,cull_back,unshaded;
uniform sampler2D texture_albedo : filter_linear_mipmap_anisotropic,repeat_enable; uniform sampler2D texture_albedo : filter_linear_mipmap_anisotropic,repeat_enable;
uniform sampler2D lightmap_albedo : filter_linear_mipmap_anisotropic,repeat_enable; uniform sampler2DArray lightmap_albedo : filter_linear_mipmap_anisotropic,repeat_enable;
uniform bool lightmap_2x; uniform bool lightmap_2x;
float srgb_to_linear_e(float input) { float srgb_to_linear_e(float input) {
@ -28,7 +28,12 @@ vec3 srgb_to_linear(vec3 input) {
void fragment() { void fragment() {
vec4 albedo_tex = texture(texture_albedo,UV); vec4 albedo_tex = texture(texture_albedo,UV);
vec4 lightmap_tex = texture(lightmap_albedo,UV2); vec4 lightmap_tex = vec4(0.0);
for (int i = 0; i < 33; i++) {
lightmap_tex += texture(lightmap_albedo, vec3(UV2, float(i)));
}
lightmap_tex = min(vec4(1.0), lightmap_tex);
if (lightmap_2x) { if (lightmap_2x) {
lightmap_tex *= 2.0f; lightmap_tex *= 2.0f;
} }