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

@ -73,6 +73,8 @@ public sealed class VehicleVisualInstance
private readonly VehicleAssetCache _cache;
private readonly VehicleAssetCache.VehicleTemplate _template;
private readonly Dictionary<string, BoundHardpoint> _hardpoints = new(StringComparer.Ordinal);
private readonly List<BoundHardpoint> _hardpointList = new();
private readonly List<(int X, int Y)> _activeCells = new();
private readonly Dictionary<string, BoundJoint> _joints = new(StringComparer.Ordinal);
private readonly Dictionary<string, Node3D> _namedNodes = new(StringComparer.Ordinal);
private readonly Dictionary<string, Transform3D> _restLocalByName = new(StringComparer.Ordinal);
@ -83,23 +85,30 @@ public sealed class VehicleVisualInstance
private readonly HashSet<string> _shearedHardpoints = new(StringComparer.Ordinal);
private readonly VehicleRigAnimator.RigState _rigState = new();
private readonly VehicleVisualRole _role;
private readonly string _chassisId;
private MeshInstance3D? _lodStaticMesh;
private bool _isStaticLod;
private bool _castShadows = true;
private bool _hasAnyStrippedCell;
public static double PerfDamageHeatMs;
public static int PerfMaterialDupCount;
private VehicleVisualInstance(
VehicleAssetCache cache,
VehicleAssetCache.VehicleTemplate template,
Node3D root,
Node3D modelRoot,
string chassisId)
string chassisId,
VehicleVisualRole role)
{
_cache = cache;
_template = template;
Root = root;
ModelRoot = modelRoot;
_chassisId = chassisId;
_role = role;
int w = template.Package.GridWidth;
int h = template.Package.GridHeight;
@ -124,6 +133,7 @@ public sealed class VehicleVisualInstance
public IReadOnlyDictionary<string, BoundJoint> Joints => _joints;
public int BoundHardpointCount => _hardpoints.Count;
public bool IsStaticLod => _isStaticLod;
public bool HasAnyStrippedCell => _hasAnyStrippedCell;
public void SetLodState(bool useStaticLod, bool castShadows)
{
@ -146,6 +156,10 @@ public sealed class VehicleVisualInstance
}
return;
}
if (_hasAnyStrippedCell)
{
useStaticLod = false;
}
if (_isStaticLod != useStaticLod)
{
_isStaticLod = useStaticLod;
@ -197,7 +211,7 @@ public sealed class VehicleVisualInstance
};
ApplyRoleTint(dropMesh, role);
root.AddChild(dropMesh);
return new VehicleVisualInstance(cache, template, root, dropMesh, chassisId);
return new VehicleVisualInstance(cache, template, root, dropMesh, chassisId, role);
}
if (template.SceneTemplate.Duplicate() is not Node3D modelRoot)
@ -223,7 +237,7 @@ public sealed class VehicleVisualInstance
}
root.AddChild(lodMesh);
var instance = new VehicleVisualInstance(cache, template, root, modelRoot, chassisId)
var instance = new VehicleVisualInstance(cache, template, root, modelRoot, chassisId, role)
{
_lodStaticMesh = lodMesh
};
@ -315,12 +329,14 @@ public sealed class VehicleVisualInstance
throw new InvalidOperationException($"{pkg.Id}: duplicated scene missing anchor node '{rec.AnchorId}'.");
}
_hardpoints[hp.Id] = new BoundHardpoint
var bh = new BoundHardpoint
{
Spec = hp,
AnchorInfo = anchorInfo,
AnchorNode = anchorNode
};
_hardpoints[hp.Id] = bh;
_hardpointList.Add(bh);
}
// Apply role palette tinting to painted hull meshes before binding per-cell damage clones
@ -332,6 +348,16 @@ public sealed class VehicleVisualInstance
// Bind detachable exterior armor/shroud parts to their (CellX, CellY)
int w = pkg.GridWidth;
int h = pkg.GridHeight;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
if (spec.IsActive(x, y))
{
_activeCells.Add((x, y));
}
}
}
foreach (var dp in _template.DetachableParts)
{
if (dp.CellX < 0 || dp.CellX >= w || dp.CellY < 0 || dp.CellY >= h)
@ -415,81 +441,84 @@ public sealed class VehicleVisualInstance
/// </summary>
public void SyncDamageAndHeat(ChassisGrid grid, DebrisSystem debris, Vector3 vehicleVelocity)
{
int w = Math.Min(grid.Spec.Width, _template.Package.GridWidth);
int h = Math.Min(grid.Spec.Height, _template.Package.GridHeight);
long t0 = RustToDust.Ymir.ArenaSession.PerfEnabled ? System.Diagnostics.Stopwatch.GetTimestamp() : 0;
Vector3 blastOrigin = Root.IsInsideTree() ? Root.GlobalPosition : Root.Position;
// 1. Part-by-cell detachment & cell scorch/heat tinting
for (int y = 0; y < h; y++)
for (int c = 0; c < _activeCells.Count; c++)
{
for (int x = 0; x < w; x++)
var (x, y) = _activeCells[c];
float armor = grid.Armor[x, y];
var parts = _cellParts[x, y];
if (!_strippedCells[x, y] && armor <= 0.01f)
{
if (!grid.Spec.IsActive(x, y))
{
continue;
}
float armor = grid.Armor[x, y];
var parts = _cellParts[x, y];
if (!_strippedCells[x, y] && armor <= 0.01f)
{
_strippedCells[x, y] = true;
for (int i = 0; i < parts.Count; i++)
{
var pb = parts[i];
if (!pb.Detached && GodotObject.IsInstanceValid(pb.Node))
{
pb.Detached = true;
debris.SpawnDetachedPart(pb.Node, blastOrigin, vehicleVelocity, impulseScale: 1.0f, applyScorch: true);
}
}
continue;
}
if (parts.Count == 0)
{
continue;
}
float maxArmor = MathF.Max(1f, grid.MaxArmor[x, y]);
float dmgFrac = 1f - Math.Clamp(armor / maxArmor, 0f, 1f);
float heatGlow = Math.Clamp((grid.Temperature[x, y] - 65f) / 110f, 0f, 1f);
float tintScore = MathF.Round(dmgFrac * 20f) + MathF.Round(heatGlow * 20f) * 32f;
if (dmgFrac < 0.25f && heatGlow < 0.05f)
{
continue;
}
_strippedCells[x, y] = true;
_hasAnyStrippedCell = true;
for (int i = 0; i < parts.Count; i++)
{
var pb = parts[i];
if (pb.Detached || !GodotObject.IsInstanceValid(pb.Node))
if (!pb.Detached && GodotObject.IsInstanceValid(pb.Node))
{
continue;
pb.Detached = true;
pb.Node.Visible = true;
debris.SpawnDetachedPart(pb.Node, blastOrigin, vehicleVelocity, impulseScale: 1.0f, applyScorch: true);
}
if (MathF.Abs(pb.LastTintScore - tintScore) < 0.5f)
{
continue;
}
pb.LastTintScore = tintScore;
ApplyCellDamageAndHeat(pb, dmgFrac, heatGlow);
}
continue;
}
if (parts.Count == 0)
{
continue;
}
float maxArmor = MathF.Max(1f, grid.MaxArmor[x, y]);
float temp = grid.Temperature[x, y];
if (armor > maxArmor * 0.75f && temp < 70.5f)
{
continue;
}
float dmgFrac = 1f - Math.Clamp(armor / maxArmor, 0f, 1f);
float heatGlow = Math.Clamp((temp - 65f) / 110f, 0f, 1f);
float tintScore = MathF.Round(dmgFrac * 20f) + MathF.Round(heatGlow * 20f) * 32f;
if (dmgFrac < 0.25f && heatGlow < 0.05f)
{
continue;
}
for (int i = 0; i < parts.Count; i++)
{
var pb = parts[i];
if (pb.Detached || !GodotObject.IsInstanceValid(pb.Node))
{
continue;
}
if (MathF.Abs(pb.LastTintScore - tintScore) < 0.5f)
{
continue;
}
pb.LastTintScore = tintScore;
ApplyCellDamageAndHeat(pb, dmgFrac, heatGlow);
}
}
// 2. Hardpoint shear & mounted gear heat glow
foreach (var bh in _hardpoints.Values)
for (int i = 0; i < _hardpointList.Count; i++)
{
var bh = _hardpointList[i];
if (!_shearedHardpoints.Contains(bh.Spec.Id) && !grid.IsHardpointOperational(bh.Spec))
{
_shearedHardpoints.Add(bh.Spec.Id);
_hasAnyStrippedCell = true;
if (bh.GearNode is not null && GodotObject.IsInstanceValid(bh.GearNode))
{
Node3D sheared = bh.GearNode;
bh.GearNode = null;
bh.MountedModuleId = null;
bh.GearHeatMaterials.Clear();
sheared.Visible = true;
debris.SpawnDetachedPart(sheared, blastOrigin, vehicleVelocity, impulseScale: 1.15f, applyScorch: true);
}
continue;
@ -510,11 +539,22 @@ public sealed class VehicleVisualInstance
for (int m = 0; m < bh.GearHeatMaterials.Count; m++)
{
StandardMaterial3D gm = bh.GearHeatMaterials[m];
gm.EmissionEnabled = heatFrac > 0.02f;
gm.Emission = new Color(1.0f, 0.34f, 0.08f);
gm.EmissionEnergyMultiplier = heatFrac * 2.2f;
bool wantEmit = heatFrac > 0.02f;
if (gm.EmissionEnabled != wantEmit || wantEmit)
{
gm.EmissionEnabled = wantEmit;
if (wantEmit)
{
gm.Emission = new Color(1.0f, 0.34f, 0.08f);
gm.EmissionEnergyMultiplier = heatFrac * 2.2f;
}
}
}
}
if (t0 != 0)
{
PerfDamageHeatMs += (System.Diagnostics.Stopwatch.GetTimestamp() - t0) * 1000.0 / System.Diagnostics.Stopwatch.Frequency;
}
}
public static Node3D CreateStandaloneGearVisual(VehicleAssetCache cache, ModuleSpec mod)
@ -522,13 +562,13 @@ public sealed class VehicleVisualInstance
var list = new List<StandardMaterial3D>();
GearVisualRecipe recipe = ResolveGearRecipe(mod);
var template = cache.GetOrLoadGear(recipe.GearGlbId);
if (template.SceneTemplate.Duplicate() is not Node3D node)
var node = new MeshInstance3D
{
return new Node3D { Name = $"Gear_{mod.Id}" };
}
node.Name = $"GearDrop_{mod.Id}";
node.Scale = recipe.VariantScale;
node.Position = new Vector3(0f, -template.MinY * recipe.VariantScale.Y, 0f);
Name = $"GearDrop_{mod.Id}",
Mesh = template.BakedDropMesh,
Scale = recipe.VariantScale,
Position = new Vector3(0f, -template.MinY * recipe.VariantScale.Y, 0f)
};
if (recipe.PaintTint.HasValue || recipe.AccentTint.HasValue)
{
ApplyGearVariantTint(node, recipe.PaintTint, recipe.AccentTint, list);
@ -544,9 +584,18 @@ public sealed class VehicleVisualInstance
{
GearVisualRecipe recipe = ResolveGearRecipe(mod);
VehicleAssetCache.GearTemplate gearTemplate = _cache.GetOrLoadGear(recipe.GearGlbId);
if (gearTemplate.SceneTemplate.Duplicate() is not Node3D gearNode)
Node3D gearNode;
if (_role == VehicleVisualRole.Player)
{
throw new InvalidOperationException($"Failed to duplicate gear GLB '{recipe.GearGlbId}'.");
if (gearTemplate.SceneTemplate.Duplicate() is not Node3D dup)
{
throw new InvalidOperationException($"Failed to duplicate gear GLB '{recipe.GearGlbId}'.");
}
gearNode = dup;
}
else
{
gearNode = new MeshInstance3D { Mesh = gearTemplate.BakedDropMesh };
}
gearNode.Name = $"Mounted_{mod.Id}_{hp.Id}";
@ -789,6 +838,7 @@ public sealed class VehicleVisualInstance
StandardMaterial3D copy = src is StandardMaterial3D sm
? (StandardMaterial3D)sm.Duplicate()
: new StandardMaterial3D { AlbedoColor = new Color(0.32f, 0.34f, 0.30f), Roughness = 0.6f, Metallic = 0.35f };
PerfMaterialDupCount++;
binding.Node.SetSurfaceOverrideMaterial(s, copy);
binding.Materials.Add((copy, copy.AlbedoColor, copy.Roughness));
}