2024-07-21 21:13:31 +00:00
|
|
|
using Godot;
|
|
|
|
using KeepersCompound.LGS;
|
|
|
|
using KeepersCompound.LGS.Database;
|
|
|
|
using KeepersCompound.LGS.Database.Chunks;
|
2024-07-30 20:50:34 +00:00
|
|
|
using RectpackSharp;
|
2024-07-21 21:13:31 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace KeepersCompound;
|
|
|
|
|
|
|
|
[Tool]
|
|
|
|
public partial class Mission : Node3D
|
|
|
|
{
|
|
|
|
[Export(PropertyHint.GlobalFile, "*.mis")]
|
|
|
|
public string FileName { get; set; }
|
|
|
|
[Export]
|
|
|
|
public bool Build = false;
|
2024-07-22 18:37:27 +00:00
|
|
|
[Export]
|
|
|
|
public bool Clear = false;
|
2024-07-21 21:13:31 +00:00
|
|
|
|
|
|
|
DbFile _file;
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
2024-07-22 20:09:27 +00:00
|
|
|
var fileNameLineEdit = GetNode<LineEdit>("%FileNameLineEdit");
|
|
|
|
fileNameLineEdit.Text = FileName;
|
|
|
|
fileNameLineEdit.TextSubmitted += (string text) => FileName = text;
|
|
|
|
GetNode<Button>("%BuildButton").Pressed += () => RebuildMap();
|
|
|
|
GetNode<Button>("%ClearButton").Pressed += () => ClearMap();
|
2024-07-21 21:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void _Process(double delta)
|
|
|
|
{
|
|
|
|
if (Build)
|
|
|
|
{
|
|
|
|
RebuildMap();
|
|
|
|
Build = false;
|
|
|
|
}
|
2024-07-22 18:37:27 +00:00
|
|
|
if (Clear)
|
|
|
|
{
|
|
|
|
ClearMap();
|
|
|
|
Clear = false;
|
|
|
|
}
|
2024-07-21 21:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void _Input(InputEvent @event)
|
|
|
|
{
|
|
|
|
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
|
|
|
|
{
|
|
|
|
if (keyEvent.Keycode == Key.R)
|
|
|
|
{
|
|
|
|
Build = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-22 18:37:27 +00:00
|
|
|
public void ClearMap()
|
2024-07-21 21:13:31 +00:00
|
|
|
{
|
|
|
|
foreach (var node in GetChildren())
|
|
|
|
{
|
|
|
|
node.QueueFree();
|
|
|
|
}
|
2024-07-22 18:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void RebuildMap()
|
|
|
|
{
|
|
|
|
ClearMap();
|
2024-07-21 21:13:31 +00:00
|
|
|
|
|
|
|
_file = new(FileName);
|
|
|
|
var wr = (WorldRep)_file.Chunks["WREXT"];
|
|
|
|
|
|
|
|
foreach (var cell in wr.Cells)
|
|
|
|
{
|
|
|
|
BuildCellMesh(cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void BuildCellMesh(WorldRep.Cell cell)
|
|
|
|
{
|
|
|
|
var numPolys = cell.PolyCount;
|
|
|
|
var numRenderPolys = cell.RenderPolyCount;
|
|
|
|
var numPortalPolys = cell.PortalPolyCount;
|
|
|
|
|
2024-07-30 20:50:34 +00:00
|
|
|
// There's nothing to render
|
2024-07-22 18:37:27 +00:00
|
|
|
if (numRenderPolys == 0 || numPortalPolys >= numPolys)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-21 21:13:31 +00:00
|
|
|
var vertices = new List<Vector3>();
|
|
|
|
var normals = new List<Vector3>();
|
2024-07-22 18:37:56 +00:00
|
|
|
var indices = new List<int>();
|
2024-07-30 20:50:34 +00:00
|
|
|
var lightmapUvs = new List<Vector2>();
|
2024-07-22 19:34:12 +00:00
|
|
|
var cellIdxOffset = 0;
|
|
|
|
|
2024-07-30 20:50:34 +00:00
|
|
|
// You'd think these would be the same number, but apparently not
|
|
|
|
// I think it's because water counts as a render poly and a portal poly
|
2024-07-22 19:34:12 +00:00
|
|
|
var maxPolyIdx = Math.Min(numRenderPolys, numPolys - numPortalPolys);
|
2024-07-30 20:50:34 +00:00
|
|
|
var packingRects = new PackingRectangle[maxPolyIdx];
|
|
|
|
var rectIdToUvIdxMap = new List<int[]>();
|
2024-07-22 19:34:12 +00:00
|
|
|
for (int i = 0; i < maxPolyIdx; i++)
|
2024-07-21 21:13:31 +00:00
|
|
|
{
|
|
|
|
var poly = cell.Polys[i];
|
2024-07-30 20:50:34 +00:00
|
|
|
var meshIdxOffset = vertices.Count;
|
2024-07-21 21:13:31 +00:00
|
|
|
|
2024-08-01 17:29:10 +00:00
|
|
|
var normal = cell.Planes[poly.PlaneId].Normal.ToGodotVec3();
|
2024-07-21 21:13:31 +00:00
|
|
|
var numPolyVertices = poly.VertexCount;
|
2024-07-22 18:37:56 +00:00
|
|
|
for (var j = 0; j < numPolyVertices; j++)
|
|
|
|
{
|
2024-07-22 19:34:12 +00:00
|
|
|
var vertex = cell.Vertices[cell.Indices[cellIdxOffset + j]];
|
2024-08-01 17:29:10 +00:00
|
|
|
vertices.Add(vertex.ToGodotVec3());
|
2024-07-30 20:50:34 +00:00
|
|
|
normals.Add(normal);
|
2024-07-22 18:37:56 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 20:50:34 +00:00
|
|
|
// Simple triangulation. Polys are always convex so we can just do a fan
|
2024-07-21 21:13:31 +00:00
|
|
|
for (int j = 1; j < numPolyVertices - 1; j++)
|
|
|
|
{
|
2024-07-22 19:34:12 +00:00
|
|
|
indices.Add(meshIdxOffset);
|
|
|
|
indices.Add(meshIdxOffset + j);
|
|
|
|
indices.Add(meshIdxOffset + j + 1);
|
2024-07-21 21:13:31 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 20:50:34 +00:00
|
|
|
// UVs
|
|
|
|
var renderPoly = cell.RenderPolys[i];
|
|
|
|
var light = cell.LightList[i];
|
|
|
|
packingRects[i] = new PackingRectangle(0, 0, light.Width, light.Height, i);
|
|
|
|
var uvIdxs = CalcBaseUV(cell, poly, renderPoly, light, lightmapUvs, cellIdxOffset);
|
|
|
|
rectIdToUvIdxMap.Add(uvIdxs);
|
|
|
|
|
2024-07-22 19:34:12 +00:00
|
|
|
cellIdxOffset += poly.VertexCount;
|
2024-07-21 21:13:31 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 20:50:34 +00:00
|
|
|
RectanglePacker.Pack(packingRects, out var bounds);
|
|
|
|
var image = Image.Create((int)bounds.Width, (int)bounds.Height, false, Image.Format.Rgba8);
|
|
|
|
foreach (var rect in packingRects)
|
|
|
|
{
|
|
|
|
// Build lightmap
|
|
|
|
var lightmap = cell.Lightmaps[rect.Id];
|
|
|
|
// TODO: Handle animlight layers
|
2024-08-01 17:23:17 +00:00
|
|
|
var layers = (uint)lightmap.Pixels.GetLength(0);
|
|
|
|
var height = (uint)lightmap.Pixels.GetLength(1);
|
|
|
|
var width = (uint)lightmap.Pixels.GetLength(2);
|
|
|
|
for (uint y = 0; y < height; y++)
|
2024-07-30 20:50:34 +00:00
|
|
|
{
|
2024-08-01 17:23:17 +00:00
|
|
|
for (uint x = 0; x < width; x++)
|
2024-07-30 20:50:34 +00:00
|
|
|
{
|
2024-08-01 17:23:17 +00:00
|
|
|
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);
|
2024-07-30 20:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform UVs
|
|
|
|
var lmUvIdxs = rectIdToUvIdxMap[rect.Id];
|
|
|
|
foreach (var idx in lmUvIdxs)
|
|
|
|
{
|
|
|
|
var uv = lightmapUvs[idx];
|
|
|
|
var u = uv.X;
|
|
|
|
var v = uv.Y;
|
|
|
|
|
|
|
|
// Clamp uv range to [0..1]
|
|
|
|
u %= 1;
|
|
|
|
v %= 1;
|
|
|
|
if (u < 0) u = Math.Abs(u);
|
|
|
|
if (v < 0) v = Math.Abs(v);
|
|
|
|
|
|
|
|
// Transform!
|
|
|
|
u = (rect.X + rect.Width * u) / (int)bounds.Width;
|
|
|
|
v = (rect.Y + rect.Height * v) / (int)bounds.Height;
|
|
|
|
lightmapUvs[idx] = new Vector2(u, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-22 18:52:40 +00:00
|
|
|
var cellNode = new Node3D();
|
|
|
|
|
2024-07-30 20:50:34 +00:00
|
|
|
var material = new StandardMaterial3D
|
|
|
|
{
|
|
|
|
AlbedoTexture = ImageTexture.CreateFromImage(image),
|
|
|
|
TextureFilter = BaseMaterial3D.TextureFilterEnum.Nearest,
|
|
|
|
};
|
|
|
|
|
2024-07-21 21:13:31 +00:00
|
|
|
var arrMesh = new ArrayMesh();
|
|
|
|
var arrays = new Godot.Collections.Array();
|
|
|
|
arrays.Resize((int)Mesh.ArrayType.Max);
|
|
|
|
arrays[(int)Mesh.ArrayType.Vertex] = vertices.ToArray();
|
|
|
|
arrays[(int)Mesh.ArrayType.Normal] = normals.ToArray();
|
2024-07-22 18:37:56 +00:00
|
|
|
arrays[(int)Mesh.ArrayType.Index] = indices.ToArray();
|
2024-07-30 20:50:34 +00:00
|
|
|
arrays[(int)Mesh.ArrayType.TexUV] = lightmapUvs.ToArray();
|
|
|
|
|
2024-07-21 21:13:31 +00:00
|
|
|
arrMesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, arrays);
|
2024-07-30 20:50:34 +00:00
|
|
|
arrMesh.SurfaceSetMaterial(0, material);
|
2024-07-22 19:34:12 +00:00
|
|
|
|
2024-07-21 21:13:31 +00:00
|
|
|
var meshInstance = new MeshInstance3D
|
|
|
|
{
|
|
|
|
Mesh = arrMesh,
|
|
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.On
|
|
|
|
};
|
2024-07-22 18:52:40 +00:00
|
|
|
cellNode.AddChild(meshInstance);
|
|
|
|
|
|
|
|
var occluder = new ArrayOccluder3D();
|
|
|
|
occluder.SetArrays(vertices.ToArray(), indices.ToArray());
|
2024-07-22 19:34:12 +00:00
|
|
|
var occluderInstance = new OccluderInstance3D
|
|
|
|
{
|
|
|
|
Occluder = occluder,
|
|
|
|
BakeSimplificationDistance = 0.0f
|
|
|
|
};
|
2024-07-22 18:52:40 +00:00
|
|
|
cellNode.AddChild(occluderInstance);
|
|
|
|
|
2024-07-22 18:37:27 +00:00
|
|
|
var r = new Random();
|
|
|
|
if (r.NextSingle() > 0.9 && cell.SphereRadius > 5.0)
|
|
|
|
{
|
|
|
|
var light = new OmniLight3D
|
|
|
|
{
|
2024-08-01 17:29:10 +00:00
|
|
|
Position = cell.SphereCenter.ToGodotVec3(),
|
2024-07-22 18:37:27 +00:00
|
|
|
OmniRange = cell.SphereRadius * (r.NextSingle() + 1.0f) * 0.5f,
|
|
|
|
};
|
2024-07-30 20:50:34 +00:00
|
|
|
// cellNode.AddChild(light);
|
2024-07-22 18:37:27 +00:00
|
|
|
}
|
2024-07-22 18:52:40 +00:00
|
|
|
|
|
|
|
AddChild(cellNode);
|
2024-07-21 21:13:31 +00:00
|
|
|
}
|
2024-07-30 20:50:34 +00:00
|
|
|
|
|
|
|
private static int[] CalcBaseUV(
|
|
|
|
WorldRep.Cell cell,
|
|
|
|
WorldRep.Cell.Poly poly,
|
|
|
|
WorldRep.Cell.RenderPoly renderPoly,
|
|
|
|
WorldRep.Cell.LightmapInfo light,
|
|
|
|
List<Vector2> lightmapUvs,
|
|
|
|
int cellIdxOffset)
|
|
|
|
{
|
|
|
|
// TODO: This is slightly hardcoded for ND. Check other stuff at some point. Should be handled in LG side imo
|
|
|
|
// TODO: This is a mess lol
|
2024-08-01 17:29:10 +00:00
|
|
|
var texU = renderPoly.TextureVectors.Item1.ToGodotVec3();
|
|
|
|
var texV = renderPoly.TextureVectors.Item2.ToGodotVec3();
|
2024-07-30 20:50:34 +00:00
|
|
|
|
|
|
|
var uu = texU.Dot(texU);
|
|
|
|
var vv = texV.Dot(texV);
|
|
|
|
var uv = texU.Dot(texV);
|
|
|
|
var lmUScale = 4.0f / light.Width;
|
|
|
|
var lmVScale = 4.0f / light.Height;
|
|
|
|
|
|
|
|
var baseU = renderPoly.TextureBases.Item1;
|
|
|
|
var baseV = renderPoly.TextureBases.Item2;
|
|
|
|
var lmUBase = lmUScale * (baseU + (0.5f - light.Bases.Item1) / 4.0f);
|
|
|
|
var lmVBase = lmVScale * (baseV + (0.5f - light.Bases.Item2) / 4.0f);
|
2024-08-01 17:29:10 +00:00
|
|
|
var anchor = cell.Vertices[cell.Indices[cellIdxOffset + 0]].ToGodotVec3(); // TODO: This probably shouldn't be hardcoded idx 0
|
2024-07-30 20:50:34 +00:00
|
|
|
|
|
|
|
var uvIdxs = new int[poly.VertexCount];
|
|
|
|
if (uv == 0.0)
|
|
|
|
{
|
|
|
|
var lmUVec = texU * lmUScale / uu;
|
|
|
|
var lmVVec = texV * lmVScale / vv;
|
|
|
|
for (var i = 0; i < poly.VertexCount; i++)
|
|
|
|
{
|
|
|
|
uvIdxs[i] = lightmapUvs.Count;
|
|
|
|
|
2024-08-01 17:29:10 +00:00
|
|
|
var v = cell.Vertices[cell.Indices[cellIdxOffset + i]].ToGodotVec3();
|
2024-07-30 20:50:34 +00:00
|
|
|
var delta = new Vector3(v.X - anchor.X, v.Y - anchor.Y, v.Z - anchor.Z);
|
|
|
|
var lmUV = new Vector2(delta.Dot(lmUVec) + lmUBase, delta.Dot(lmVVec) + lmVBase);
|
|
|
|
lightmapUvs.Add(lmUV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var denom = 1.0f / (uu * vv - uv * uv);
|
|
|
|
var lmUu = uu * lmVScale * denom;
|
|
|
|
var lmVv = vv * lmUScale * denom;
|
|
|
|
var lmUvu = lmUScale * denom * uv;
|
|
|
|
var lmUvv = lmVScale * denom * uv;
|
|
|
|
for (var i = 0; i < poly.VertexCount; i++)
|
|
|
|
{
|
|
|
|
uvIdxs[i] = lightmapUvs.Count;
|
|
|
|
|
2024-08-01 17:29:10 +00:00
|
|
|
var v = cell.Vertices[cell.Indices[cellIdxOffset + i]].ToGodotVec3();
|
2024-07-30 20:50:34 +00:00
|
|
|
var delta = new Vector3(v.X - anchor.X, v.Y - anchor.Y, v.Z - anchor.Z);
|
|
|
|
var du = delta.Dot(texU);
|
|
|
|
var dv = delta.Dot(texV);
|
|
|
|
var lmUV = new Vector2(lmUBase + lmVv * du - lmUvu * dv, lmVBase + lmUu * dv - lmUvv * du);
|
|
|
|
lightmapUvs.Add(lmUV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return uvIdxs;
|
|
|
|
}
|
2024-07-21 21:13:31 +00:00
|
|
|
}
|