render: unified MultiMeshPartPool with MechPaint, PomDecalPool, and per-weapon VFX
This commit is contained in:
parent
3e499c31ee
commit
2529ba62d2
9 changed files with 2593 additions and 871 deletions
|
|
@ -7,7 +7,8 @@ namespace RustToDust.Client;
|
|||
|
||||
/// <summary>
|
||||
/// Manages physically detached armor plates and sheared hardpoint gear as tumbling,
|
||||
/// terrain-bouncing debris pieces that settle and fade out cleanly after 8 seconds.
|
||||
/// terrain-bouncing debris pieces submitted directly into <see cref="MultiMeshPartPool"/>
|
||||
/// (zero per-piece Node3D/MeshInstance3D/StandardMaterial3D allocations).
|
||||
/// </summary>
|
||||
public partial class DebrisSystem : Node3D
|
||||
{
|
||||
|
|
@ -18,11 +19,15 @@ public partial class DebrisSystem : Node3D
|
|||
|
||||
private sealed class DebrisPiece
|
||||
{
|
||||
public required Node3D Node;
|
||||
public required Vector3 Velocity;
|
||||
public required Vector3 AngularVelocity;
|
||||
public required List<StandardMaterial3D> FadeMaterials;
|
||||
public required float[] BaseAlphas;
|
||||
public required ArrayMesh Mesh;
|
||||
public Vector3 Position;
|
||||
public Basis Rotation;
|
||||
public Vector3 Scale;
|
||||
public Vector3 Velocity;
|
||||
public Vector3 AngularVelocity;
|
||||
public float RoleIndex;
|
||||
public float Scorch;
|
||||
public float HeatGlow;
|
||||
public float Age;
|
||||
public float Lifetime;
|
||||
public bool Settled;
|
||||
|
|
@ -33,60 +38,51 @@ public partial class DebrisSystem : Node3D
|
|||
|
||||
public int ActiveCount => _pieces.Count;
|
||||
|
||||
public bool TryGetPiecePosition(int index, out Vector3 position)
|
||||
{
|
||||
if (index >= 0 && index < _pieces.Count)
|
||||
{
|
||||
position = _pieces[index].Position;
|
||||
return true;
|
||||
}
|
||||
position = Vector3.Zero;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void ClearAll()
|
||||
{
|
||||
for (int i = 0; i < _pieces.Count; i++)
|
||||
{
|
||||
if (GodotObject.IsInstanceValid(_pieces[i].Node))
|
||||
{
|
||||
_pieces[i].Node.QueueFree();
|
||||
}
|
||||
}
|
||||
_pieces.Clear();
|
||||
|
||||
for (int i = GetChildCount() - 1; i >= 0; i--)
|
||||
{
|
||||
Node ch = GetChild(i);
|
||||
RemoveChild(ch);
|
||||
ch.QueueFree();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reparents <paramref name="part"/> from its vehicle hierarchy at its exact world transform
|
||||
/// into <see cref="DebrisSystem"/> and launches it with an outward blast impulse and tumble.
|
||||
/// Registers a detached rigid part or sheared gear mesh at its world transform
|
||||
/// and launches it with an outward blast impulse and tumble.
|
||||
/// </summary>
|
||||
public void SpawnDetachedPart(
|
||||
Node3D part,
|
||||
ArrayMesh mesh,
|
||||
Transform3D worldXform,
|
||||
Vector3 blastOriginWorld,
|
||||
Vector3 inheritedVelocity,
|
||||
float roleIndex,
|
||||
float impulseScale = 1.0f,
|
||||
bool applyScorch = true)
|
||||
{
|
||||
if (!GodotObject.IsInstanceValid(part))
|
||||
if (mesh is null || mesh.GetSurfaceCount() == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Evict oldest settled or oldest piece if we hit the cap so debris never leaks indefinitely
|
||||
while (_pieces.Count >= MaxActiveDebris)
|
||||
{
|
||||
RemoveAt(0);
|
||||
_pieces.RemoveAt(0);
|
||||
}
|
||||
|
||||
Transform3D worldXform = part.IsInsideTree() ? part.GlobalTransform : part.Transform;
|
||||
Node? oldParent = part.GetParent();
|
||||
oldParent?.RemoveChild(part);
|
||||
AddChild(part);
|
||||
|
||||
if (part.IsInsideTree() && IsInsideTree())
|
||||
Vector3 scale = worldXform.Basis.Scale;
|
||||
if (!scale.IsFinite() || scale.LengthSquared() < 1e-6f)
|
||||
{
|
||||
part.GlobalTransform = worldXform;
|
||||
}
|
||||
else
|
||||
{
|
||||
part.Transform = worldXform;
|
||||
scale = Vector3.One;
|
||||
}
|
||||
Basis rot = worldXform.Basis.Orthonormalized();
|
||||
|
||||
Vector3 outward = worldXform.Origin - blastOriginWorld;
|
||||
outward.Y = 0f;
|
||||
|
|
@ -109,21 +105,17 @@ public partial class DebrisSystem : Node3D
|
|||
(NextFloat() * 2f - 1f) * 7.5f,
|
||||
(NextFloat() * 2f - 1f) * 7.5f);
|
||||
|
||||
var fadeMats = new List<StandardMaterial3D>();
|
||||
PrepareInstanceMaterials(part, fadeMats, applyScorch);
|
||||
var baseAlphas = new float[fadeMats.Count];
|
||||
for (int i = 0; i < fadeMats.Count; i++)
|
||||
{
|
||||
baseAlphas[i] = fadeMats[i].AlbedoColor.A;
|
||||
}
|
||||
|
||||
_pieces.Add(new DebrisPiece
|
||||
{
|
||||
Node = part,
|
||||
Mesh = mesh,
|
||||
Position = worldXform.Origin,
|
||||
Rotation = rot,
|
||||
Scale = scale,
|
||||
Velocity = vel,
|
||||
AngularVelocity = angVel,
|
||||
FadeMaterials = fadeMats,
|
||||
BaseAlphas = baseAlphas,
|
||||
RoleIndex = roleIndex,
|
||||
Scorch = applyScorch ? 0.85f : 0.15f,
|
||||
HeatGlow = applyScorch ? 0.45f : 0f,
|
||||
Age = 0f,
|
||||
Lifetime = DefaultLifetime,
|
||||
Settled = false
|
||||
|
|
@ -141,30 +133,28 @@ public partial class DebrisSystem : Node3D
|
|||
for (int i = _pieces.Count - 1; i >= 0; i--)
|
||||
{
|
||||
DebrisPiece p = _pieces[i];
|
||||
if (!GodotObject.IsInstanceValid(p.Node))
|
||||
p.Age += stepDt;
|
||||
if (p.Age >= p.Lifetime)
|
||||
{
|
||||
_pieces.RemoveAt(i);
|
||||
continue;
|
||||
}
|
||||
|
||||
p.Age += stepDt;
|
||||
if (p.Age >= p.Lifetime)
|
||||
if (p.HeatGlow > 0f)
|
||||
{
|
||||
RemoveAt(i);
|
||||
continue;
|
||||
p.HeatGlow = MathF.Max(0f, p.HeatGlow - stepDt * 0.22f);
|
||||
}
|
||||
|
||||
if (!p.Settled)
|
||||
{
|
||||
p.Velocity += new Vector3(0f, Gravity * stepDt, 0f);
|
||||
Vector3 pos = p.Node.Position + p.Velocity * stepDt;
|
||||
Vector3 pos = p.Position + p.Velocity * stepDt;
|
||||
|
||||
Basis basis = p.Node.Basis;
|
||||
Basis rot = p.Rotation;
|
||||
float spinLen = p.AngularVelocity.Length();
|
||||
if (spinLen > 1e-4f)
|
||||
{
|
||||
basis = new Basis(p.AngularVelocity / spinLen, spinLen * stepDt) * basis;
|
||||
basis = basis.Orthonormalized();
|
||||
rot = (new Basis(p.AngularVelocity / spinLen, spinLen * stepDt) * rot).Orthonormalized();
|
||||
}
|
||||
|
||||
float floorY = terrain.SampleHeight(pos.X, pos.Z) + 0.10f;
|
||||
|
|
@ -187,94 +177,29 @@ public partial class DebrisSystem : Node3D
|
|||
}
|
||||
}
|
||||
|
||||
p.Node.Position = pos;
|
||||
p.Node.Basis = basis;
|
||||
p.Position = pos;
|
||||
p.Rotation = rot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Smooth alpha fade during final 2 seconds
|
||||
public void SubmitToPool(MultiMeshPartPool pool)
|
||||
{
|
||||
for (int i = 0; i < _pieces.Count; i++)
|
||||
{
|
||||
DebrisPiece p = _pieces[i];
|
||||
float remaining = p.Lifetime - p.Age;
|
||||
if (remaining < FadeDuration)
|
||||
{
|
||||
float alphaFrac = Math.Clamp(remaining / FadeDuration, 0f, 1f);
|
||||
for (int m = 0; m < p.FadeMaterials.Count; m++)
|
||||
{
|
||||
StandardMaterial3D sm = p.FadeMaterials[m];
|
||||
if (sm.Transparency == BaseMaterial3D.TransparencyEnum.Disabled)
|
||||
{
|
||||
sm.Transparency = BaseMaterial3D.TransparencyEnum.Alpha;
|
||||
}
|
||||
Color col = sm.AlbedoColor;
|
||||
col.A = p.BaseAlphas[m] * alphaFrac;
|
||||
sm.AlbedoColor = col;
|
||||
}
|
||||
}
|
||||
float alpha = remaining < FadeDuration
|
||||
? Math.Clamp(remaining / FadeDuration, 0.02f, 1f)
|
||||
: 1f;
|
||||
float sinkY = remaining < FadeDuration ? (1f - alpha) * 0.65f : 0f;
|
||||
Vector3 scale = p.Scale * (remaining < FadeDuration ? (0.35f + 0.65f * alpha) : 1f);
|
||||
Transform3D xf = new(p.Rotation * Basis.FromScale(scale), p.Position - new Vector3(0f, sinkY, 0f));
|
||||
Color custom = new(p.Scorch, p.HeatGlow, p.RoleIndex, alpha);
|
||||
pool.Submit(p.Mesh, in xf, in custom, wantsShadow: false);
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveAt(int index)
|
||||
{
|
||||
DebrisPiece p = _pieces[index];
|
||||
_pieces.RemoveAt(index);
|
||||
if (GodotObject.IsInstanceValid(p.Node))
|
||||
{
|
||||
if (p.Node.GetParent() == this)
|
||||
{
|
||||
RemoveChild(p.Node);
|
||||
}
|
||||
p.Node.QueueFree();
|
||||
}
|
||||
}
|
||||
|
||||
private static void PrepareInstanceMaterials(Node cur, List<StandardMaterial3D> outMats, bool applyScorch)
|
||||
{
|
||||
if (cur is MeshInstance3D mi && mi.Mesh is not null)
|
||||
{
|
||||
if (mi.MaterialOverride is StandardMaterial3D ov)
|
||||
{
|
||||
var copy = (StandardMaterial3D)ov.Duplicate();
|
||||
if (applyScorch)
|
||||
{
|
||||
ApplyScorchTint(copy);
|
||||
}
|
||||
mi.MaterialOverride = copy;
|
||||
outMats.Add(copy);
|
||||
}
|
||||
else
|
||||
{
|
||||
int surfCount = mi.Mesh.GetSurfaceCount();
|
||||
for (int s = 0; s < surfCount; s++)
|
||||
{
|
||||
Material? src = mi.GetActiveMaterial(s);
|
||||
StandardMaterial3D copy = src is StandardMaterial3D sm
|
||||
? (StandardMaterial3D)sm.Duplicate()
|
||||
: new StandardMaterial3D { AlbedoColor = new Color(0.28f, 0.25f, 0.23f), Roughness = 0.78f, Metallic = 0.45f };
|
||||
if (applyScorch)
|
||||
{
|
||||
ApplyScorchTint(copy);
|
||||
}
|
||||
mi.SetSurfaceOverrideMaterial(s, copy);
|
||||
outMats.Add(copy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int count = cur.GetChildCount();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
PrepareInstanceMaterials(cur.GetChild(i), outMats, applyScorch);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyScorchTint(StandardMaterial3D mat)
|
||||
{
|
||||
Color c = mat.AlbedoColor;
|
||||
mat.AlbedoColor = new Color(c.R * 0.42f + 0.04f, c.G * 0.36f + 0.03f, c.B * 0.34f + 0.02f, c.A);
|
||||
mat.Roughness = MathF.Max(mat.Roughness, 0.78f);
|
||||
mat.EmissionEnabled = true;
|
||||
mat.Emission = new Color(0.95f, 0.32f, 0.06f);
|
||||
mat.EmissionEnergyMultiplier = 0.35f;
|
||||
}
|
||||
|
||||
private float NextFloat()
|
||||
{
|
||||
uint x = _rngState;
|
||||
|
|
|
|||
Loading…
Reference in a new issue