Fix all 6 60 FPS Soul Pass findings and add permanent regression tests

This commit is contained in:
Meta Vens 2026-12-04 23:22:18 +01:00
commit f5d23ce49e
12 changed files with 1031 additions and 305 deletions

View file

@ -62,6 +62,7 @@ public sealed class VehicleAssetCache : IDisposable
public required string Id { get; init; }
public required string GlbPath { get; init; }
public required Node3D SceneTemplate { get; init; }
public required ArrayMesh BakedDropMesh { get; init; }
public required Vector3 Size { get; init; }
public required float MinY { get; init; }
public required Vector3 LocalModelForward { get; init; }
@ -444,7 +445,7 @@ public sealed class VehicleAssetCache : IDisposable
// C: Build single-node BakedStaticMesh + decimated BakedLodFarMesh (>130m) per template
ArrayMesh bakedStaticMesh = BuildBakedStaticMesh(sceneRoot, coarseMaterials: false);
ArrayMesh bakedCoarseStatic = BuildBakedStaticMesh(sceneRoot, coarseMaterials: true);
ArrayMesh bakedLodFarMesh = DecimateMeshGrid(bakedCoarseStatic, cellSize: 0.45f);
ArrayMesh bakedLodFarMesh = DecimateMeshGrid(bakedCoarseStatic, cellSize: 0.45f, targetMinY: minY);
return new VehicleTemplate
{
@ -489,6 +490,7 @@ public sealed class VehicleAssetCache : IDisposable
ComputeSceneMeshBounds(sceneRoot, out Vector3 bMin, out Vector3 bMax);
MergeGearSubMeshes(sceneRoot, gearId);
ArrayMesh bakedDropMesh = BuildBakedStaticMesh(sceneRoot, coarseMaterials: false, bakeVertexColor: false);
Vector3 localFwd = new Vector3(0f, 0f, 1f);
return new GearTemplate
@ -496,6 +498,7 @@ public sealed class VehicleAssetCache : IDisposable
Id = gearId,
GlbPath = glbPath,
SceneTemplate = sceneRoot,
BakedDropMesh = bakedDropMesh,
Size = bMax - bMin,
MinY = bMin.Y,
LocalModelForward = localFwd
@ -733,7 +736,8 @@ public sealed class VehicleAssetCache : IDisposable
}
}
// A. Merge static non-detachable sub-meshes per rigid articulated parent
// A. Merge static non-detachable sub-meshes per rigid articulated parent, and collapse PaintTag==0 surfaces on protected structural meshes
var mergedProtectedMeshes = new HashSet<MeshInstance3D>();
foreach (var (rigidParent, items) in staticGroups)
{
if (rigidParent is MeshInstance3D parentMi && CanMergeMeshInstance(parentMi))
@ -748,6 +752,7 @@ public sealed class VehicleAssetCache : IDisposable
{
parentMi.MaterialOverride = null;
parentMi.Mesh = merged;
mergedProtectedMeshes.Add(parentMi);
for (int i = 0; i < items.Count; i++)
{
ReparentChildrenAndRemove(items[i].Mi);
@ -773,6 +778,26 @@ public sealed class VehicleAssetCache : IDisposable
}
}
for (int i = 0; i < allMeshes.Count; i++)
{
MeshInstance3D mi = allMeshes[i];
if (GodotObject.IsInstanceValid(mi) &&
protectedNodes.Contains(mi) &&
!mergedProtectedMeshes.Contains(mi) &&
CanMergeMeshInstance(mi) &&
mi.Mesh is not null &&
mi.Mesh.GetSurfaceCount() > 1)
{
var single = new List<(MeshInstance3D, Transform3D)>(1) { (mi, Transform3D.Identity) };
ArrayMesh? collapsed = BuildMergedArrayMesh(single);
if (collapsed is not null && collapsed.GetSurfaceCount() < mi.Mesh.GetSurfaceCount())
{
mi.MaterialOverride = null;
mi.Mesh = collapsed;
}
}
}
// Prune empty non-protected grouping nodes left behind
PruneEmptyNonProtectedNodes(sceneRoot, sceneRoot, protectedNodes);
@ -844,7 +869,7 @@ public sealed class VehicleAssetCache : IDisposable
PruneEmptyNonProtectedNodes(sceneRoot, sceneRoot, keptNodes);
}
private static ArrayMesh BuildBakedStaticMesh(Node3D sceneRoot, bool coarseMaterials = false)
private static ArrayMesh BuildBakedStaticMesh(Node3D sceneRoot, bool coarseMaterials = false, bool bakeVertexColor = true)
{
var allMeshes = new List<MeshInstance3D>();
CollectAllMeshInstances(sceneRoot, allMeshes);
@ -859,7 +884,7 @@ public sealed class VehicleAssetCache : IDisposable
}
}
return BuildMergedArrayMesh(items, coarseMaterials) ?? new ArrayMesh();
return BuildMergedArrayMesh(items, coarseMaterials, bakeVertexColor) ?? new ArrayMesh();
}
private static void PruneEmptyNonProtectedNodes(Node cur, Node sceneRoot, HashSet<Node> protectedNodes)
@ -895,7 +920,12 @@ public sealed class VehicleAssetCache : IDisposable
int Transparency,
int CullMode);
private static bool TryGetMaterialValueKey(Material mat, bool coarse, out StandardMatKey key)
private static bool TryGetMaterialValueKey(
Material mat,
bool coarse,
bool bakeVertexColor,
out StandardMatKey key,
out Color vertexTint)
{
if (mat is StandardMaterial3D sm &&
sm.AlbedoTexture is null &&
@ -908,14 +938,16 @@ public sealed class VehicleAssetCache : IDisposable
int paintTag = (mName.Contains("olive") || mName.Contains("khaki")) ? 1
: (mName.Contains("signal") || mName.Contains("orange")) ? 2
: 0;
float colorScale = coarse ? 6f : 255f;
float pbrScale = coarse ? 2f : 100f;
bool useVCol = bakeVertexColor && paintTag == 0;
vertexTint = useVCol ? sm.AlbedoColor : Colors.White;
float colorScale = coarse ? 2f : 32f;
float pbrScale = coarse ? 0f : 4f;
key = new StandardMatKey(
paintTag,
(int)MathF.Round(sm.AlbedoColor.R * colorScale),
(int)MathF.Round(sm.AlbedoColor.G * colorScale),
(int)MathF.Round(sm.AlbedoColor.B * colorScale),
(int)MathF.Round(sm.AlbedoColor.A * 10f),
useVCol ? 0 : (int)MathF.Round(sm.AlbedoColor.R * colorScale),
useVCol ? 0 : (int)MathF.Round(sm.AlbedoColor.G * colorScale),
useVCol ? 0 : (int)MathF.Round(sm.AlbedoColor.B * colorScale),
useVCol ? 10 : (int)MathF.Round(sm.AlbedoColor.A * 10f),
(int)MathF.Round(sm.Metallic * pbrScale),
(int)MathF.Round(sm.Roughness * pbrScale),
sm.EmissionEnabled,
@ -928,16 +960,21 @@ public sealed class VehicleAssetCache : IDisposable
return true;
}
key = default;
vertexTint = Colors.White;
return false;
}
private sealed class MaterialSurfaceBucket
{
public required Material? Material;
public readonly List<(Mesh Mesh, int SurfaceIdx, Transform3D RelXf)> Sources = new();
public bool UsesVertexColor;
public readonly List<(Mesh Mesh, int SurfaceIdx, Transform3D RelXf, Color VertexTint)> Sources = new();
}
private static ArrayMesh? BuildMergedArrayMesh(List<(MeshInstance3D Mi, Transform3D RelXf)> items, bool coarseMaterials = false)
private static ArrayMesh? BuildMergedArrayMesh(
List<(MeshInstance3D Mi, Transform3D RelXf)> items,
bool coarseMaterials = false,
bool bakeVertexColor = true)
{
if (items.Count == 0)
{
@ -946,7 +983,7 @@ public sealed class VehicleAssetCache : IDisposable
var buckets = new List<MaterialSurfaceBucket>();
var bucketByMatId = new Dictionary<ulong, MaterialSurfaceBucket>();
var bucketByMatVal = new Dictionary<StandardMatKey, MaterialSurfaceBucket>();
var bucketByMatVal = new Dictionary<(StandardMatKey Key, bool UseVCol), MaterialSurfaceBucket>();
MaterialSurfaceBucket? nullMatBucket = null;
for (int i = 0; i < items.Count; i++)
@ -966,6 +1003,7 @@ public sealed class VehicleAssetCache : IDisposable
}
Material? mat = mi.MaterialOverride ?? mi.GetActiveMaterial(s);
MaterialSurfaceBucket bucket;
Color vTint = Colors.White;
if (mat is null)
{
nullMatBucket ??= new MaterialSurfaceBucket { Material = null };
@ -975,12 +1013,21 @@ public sealed class VehicleAssetCache : IDisposable
}
bucket = nullMatBucket;
}
else if (TryGetMaterialValueKey(mat, coarseMaterials, out var matKey))
else if (TryGetMaterialValueKey(mat, coarseMaterials, bakeVertexColor, out var matKey, out vTint))
{
if (!bucketByMatVal.TryGetValue(matKey, out bucket!))
bool useVCol = bakeVertexColor && matKey.PaintTag == 0;
if (!bucketByMatVal.TryGetValue((matKey, useVCol), out bucket!))
{
bucket = new MaterialSurfaceBucket { Material = mat };
bucketByMatVal[matKey] = bucket;
Material bucketMat = mat;
if (useVCol && mat is StandardMaterial3D sm)
{
var vcMat = (StandardMaterial3D)sm.Duplicate();
vcMat.AlbedoColor = Colors.White;
vcMat.VertexColorUseAsAlbedo = true;
bucketMat = vcMat;
}
bucket = new MaterialSurfaceBucket { Material = bucketMat, UsesVertexColor = useVCol };
bucketByMatVal[(matKey, useVCol)] = bucket;
buckets.Add(bucket);
}
}
@ -994,7 +1041,7 @@ public sealed class VehicleAssetCache : IDisposable
buckets.Add(bucket);
}
}
bucket.Sources.Add((mesh, s, relXf));
bucket.Sources.Add((mesh, s, relXf, vTint));
}
}
@ -1011,14 +1058,14 @@ public sealed class VehicleAssetCache : IDisposable
bool anyTangent = false;
bool anyUv = false;
bool anyUv2 = false;
bool anyColor = false;
bool anyColor = bucket.UsesVertexColor;
int totalVerts = 0;
int totalIndices = 0;
var cachedArrays = new List<(Godot.Collections.Array Arrays, Transform3D RelXf, int VertCount, int IndexCount)>(bucket.Sources.Count);
var cachedArrays = new List<(Godot.Collections.Array Arrays, Transform3D RelXf, Color VTint, int VertCount, int IndexCount)>(bucket.Sources.Count);
for (int s = 0; s < bucket.Sources.Count; s++)
{
var (mesh, surfIdx, relXf) = bucket.Sources[s];
var (mesh, surfIdx, relXf, vTint) = bucket.Sources[s];
var arr = mesh.SurfaceGetArrays(surfIdx);
if (arr.Count <= (int)Mesh.ArrayType.Vertex ||
arr[(int)Mesh.ArrayType.Vertex].VariantType != Variant.Type.PackedVector3Array)
@ -1045,7 +1092,7 @@ public sealed class VehicleAssetCache : IDisposable
totalVerts += verts.Length;
totalIndices += idxCount;
cachedArrays.Add((arr, relXf, verts.Length, idxCount));
cachedArrays.Add((arr, relXf, vTint, verts.Length, idxCount));
}
if (totalVerts == 0 || totalIndices == 0)
@ -1065,7 +1112,7 @@ public sealed class VehicleAssetCache : IDisposable
int iOffset = 0;
for (int c = 0; c < cachedArrays.Count; c++)
{
var (arr, relXf, vCount, idxCount) = cachedArrays[c];
var (arr, relXf, vTint, vCount, idxCount) = cachedArrays[c];
Vector3[] srcVerts = arr[(int)Mesh.ArrayType.Vertex].AsVector3Array();
bool flipWinding = relXf.Basis.Determinant() < 0f;
Basis normBasis = relXf.Basis.Inverse().Transposed();
@ -1140,11 +1187,20 @@ public sealed class VehicleAssetCache : IDisposable
{
if (arr.Count > (int)Mesh.ArrayType.Color && arr[(int)Mesh.ArrayType.Color].VariantType == Variant.Type.PackedColorArray)
{
Array.Copy(arr[(int)Mesh.ArrayType.Color].AsColorArray(), 0, outColors, vOffset, vCount);
Color[] srcC = arr[(int)Mesh.ArrayType.Color].AsColorArray();
if (bucket.UsesVertexColor)
{
for (int v = 0; v < vCount; v++) outColors[vOffset + v] = srcC[v] * vTint;
}
else
{
Array.Copy(srcC, 0, outColors, vOffset, vCount);
}
}
else
{
for (int v = 0; v < vCount; v++) outColors[vOffset + v] = Colors.White;
Color fill = bucket.UsesVertexColor ? vTint : Colors.White;
for (int v = 0; v < vCount; v++) outColors[vOffset + v] = fill;
}
}
@ -1199,7 +1255,7 @@ public sealed class VehicleAssetCache : IDisposable
return outMesh.GetSurfaceCount() > 0 ? outMesh : null;
}
private static ArrayMesh DecimateMeshGrid(ArrayMesh src, float cellSize)
private static ArrayMesh DecimateMeshGrid(ArrayMesh src, float cellSize, float targetMinY)
{
var outMesh = new ArrayMesh();
float invCell = 1f / MathF.Max(0.02f, cellSize);
@ -1223,6 +1279,9 @@ public sealed class VehicleAssetCache : IDisposable
bool hasNorm = arr.Count > (int)Mesh.ArrayType.Normal && arr[(int)Mesh.ArrayType.Normal].VariantType == Variant.Type.PackedVector3Array;
Vector3[]? norms = hasNorm ? arr[(int)Mesh.ArrayType.Normal].AsVector3Array() : null;
bool hasColor = arr.Count > (int)Mesh.ArrayType.Color && arr[(int)Mesh.ArrayType.Color].VariantType == Variant.Type.PackedColorArray;
Color[]? colors = hasColor ? arr[(int)Mesh.ArrayType.Color].AsColorArray() : null;
bool hasIdx = arr.Count > (int)Mesh.ArrayType.Index && arr[(int)Mesh.ArrayType.Index].VariantType == Variant.Type.PackedInt32Array;
int[]? indices = hasIdx ? arr[(int)Mesh.ArrayType.Index].AsInt32Array() : null;
int triVertCount = indices is not null ? indices.Length : verts.Length;
@ -1231,7 +1290,9 @@ public sealed class VehicleAssetCache : IDisposable
var vertToCellIdx = new int[verts.Length];
var accumPos = new List<Vector3>(verts.Length / 2 + 16);
var accumNorm = norms is not null ? new List<Vector3>(verts.Length / 2 + 16) : null;
var accumColor = colors is not null ? new List<Color>(verts.Length / 2 + 16) : null;
var counts = new List<int>(verts.Length / 2 + 16);
var cellMinY = new List<float>(verts.Length / 2 + 16);
for (int v = 0; v < verts.Length; v++)
{
@ -1247,13 +1308,17 @@ public sealed class VehicleAssetCache : IDisposable
cellToNewIdx[key] = ci;
accumPos.Add(p);
if (accumNorm is not null) accumNorm.Add(norms![v]);
if (accumColor is not null) accumColor.Add(colors![v]);
counts.Add(1);
cellMinY.Add(p.Y);
}
else
{
accumPos[ci] += p;
if (accumNorm is not null) accumNorm[ci] += norms![v];
if (accumColor is not null) accumColor[ci] += colors![v];
counts[ci]++;
if (p.Y < cellMinY[ci]) cellMinY[ci] = p.Y;
}
vertToCellIdx[v] = ci;
}
@ -1287,20 +1352,30 @@ public sealed class VehicleAssetCache : IDisposable
var outVerts = new Vector3[accumPos.Count];
Vector3[]? outNorms = accumNorm is not null ? new Vector3[accumPos.Count] : null;
Color[]? outColors = accumColor is not null ? new Color[accumPos.Count] : null;
for (int i = 0; i < accumPos.Count; i++)
{
outVerts[i] = accumPos[i] / counts[i];
Vector3 avg = accumPos[i] / counts[i];
// Preserve exact ground-contacting vertex minY so far LOD ChassisDrops never float or sink
float y = cellMinY[i] <= targetMinY + 0.08f ? cellMinY[i] : avg.Y;
if (y < targetMinY) y = targetMinY;
outVerts[i] = new Vector3(avg.X, y, avg.Z);
if (outNorms is not null)
{
Vector3 n = accumNorm![i];
outNorms[i] = n.LengthSquared() > 1e-8f ? n.Normalized() : Vector3.Up;
}
if (outColors is not null)
{
outColors[i] = accumColor![i] / counts[i];
}
}
var newArr = new Godot.Collections.Array();
newArr.Resize((int)Mesh.ArrayType.Max);
newArr[(int)Mesh.ArrayType.Vertex] = outVerts;
if (outNorms is not null) newArr[(int)Mesh.ArrayType.Normal] = outNorms;
if (outColors is not null) newArr[(int)Mesh.ArrayType.Color] = outColors;
newArr[(int)Mesh.ArrayType.Index] = outIndices.ToArray();
int newSurf = outMesh.GetSurfaceCount();