Cut 10b: unified 3D flow field, sorted-tile volumetrics, GPU kite sparks, and capsule beams

This commit is contained in:
Meta Vens 2026-12-05 19:24:10 +01:00
commit ffe777b9d4
17 changed files with 3611 additions and 1203 deletions

212
client/GpuSparksSystem.cs Normal file
View file

@ -0,0 +1,212 @@
using System;
using Godot;
namespace RustToDust.Client;
/// <summary>
/// Flow-coupled GPU particle system (<see cref="GpuParticles3D"/>, 8,192 capacity) driven by
/// the global <c>rtd_flow_field</c> <see cref="Texture3Drd"/> for tiny kite-stretched HDR sparks,
/// buoyant embers, flow-entrained dust motes, and 0.08s muzzle starbursts.
/// </summary>
public partial class GpuSparksSystem : Node3D
{
public const int Capacity = 8192;
public const int TypeVelocitySpark = 0;
public const int TypeBuoyantEmber = 1;
public const int TypeDustMote = 2;
public const int TypeMuzzleStarburst = 3;
private const uint EmitFlagsAll = (uint)(
GpuParticles3D.EmitFlags.Position |
GpuParticles3D.EmitFlags.Velocity |
GpuParticles3D.EmitFlags.Color |
GpuParticles3D.EmitFlags.Custom);
private GpuParticles3D _particles = null!;
private ShaderMaterial _processMat = null!;
private ShaderMaterial _drawMat = null!;
private bool _initialized;
private uint _rng = 0x9E3779B9u;
public int TotalEmittedCount { get; private set; }
public GpuParticles3D ParticlesNode => _particles;
public override void _Ready()
{
EnsureInitialized();
}
public void EnsureInitialized()
{
if (_initialized)
{
return;
}
_initialized = true;
Shader processShader = GD.Load<Shader>("res://gpu_sparks_process.gdshader")
?? throw new InvalidOperationException("Failed to load res://gpu_sparks_process.gdshader");
Shader drawShader = GD.Load<Shader>("res://gpu_sparks_draw.gdshader")
?? throw new InvalidOperationException("Failed to load res://gpu_sparks_draw.gdshader");
_processMat = new ShaderMaterial { Shader = processShader };
_drawMat = new ShaderMaterial { Shader = drawShader };
var quad = new QuadMesh
{
Size = Vector2.One,
Material = _drawMat
};
_particles = new GpuParticles3D
{
Name = "GpuSparks3D",
Amount = Capacity,
LocalCoords = false,
Emitting = true,
AmountRatio = 0.0f, // Keep GPU process active with zero automatic spawns
Lifetime = 2.5f,
FixedFps = 0,
Explosiveness = 0.0f,
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off,
VisibilityAabb = new Aabb(new Vector3(-1800f, -200f, -1800f), new Vector3(3600f, 2200f, 3600f)),
PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off,
ProcessMaterial = _processMat,
DrawPass1 = quad
};
AddChild(_particles);
}
public void ClearAll()
{
EnsureInitialized();
TotalEmittedCount = 0;
_particles.Restart();
_particles.Emitting = true;
_particles.AmountRatio = 0.0f;
}
public void Prewarm(Vector3 nearPos)
{
EnsureInitialized();
EmitRaw(nearPos + new Vector3(0f, -20f, 0f), Vector3.Up * 0.1f, new Color(1f, 1f, 1f, 0.01f), 0.02f, 0.05f, TypeVelocitySpark, 1.0f);
TotalEmittedCount = 0;
}
public void EmitMuzzleStarburst(Vector3 pos, Color hdrColor, float sizeM, float lifetimeSec)
{
EnsureInitialized();
EmitRaw(pos, Vector3.Zero, hdrColor, Math.Clamp(sizeM, 0.25f, 0.90f), Math.Clamp(lifetimeSec, 0.05f, 0.14f), TypeMuzzleStarburst, 0f);
}
public void EmitSparkBurst(Vector3 pos, Vector3 normal, int count, Color hdrColor, float speed, float widthM = 0.042f)
{
EnsureInitialized();
Vector3 n = normal.LengthSquared() > 1e-4f ? normal.Normalized() : Vector3.Up;
for (int i = 0; i < count; i++)
{
Vector3 rand = new(NextSigned(), MathF.Abs(NextSigned()) + 0.30f, NextSigned());
Vector3 dir = (n * 0.72f + rand).Normalized();
float spd = speed * (0.55f + 0.80f * NextFloat());
float life = 0.22f + 0.26f * NextFloat();
EmitRaw(pos, dir * spd, hdrColor, widthM, life, TypeVelocitySpark, 0.85f);
}
}
public void EmitEmberBurst(Vector3 pos, int count, Color hdrColor, float spreadSpeed, float widthM = 0.055f)
{
EnsureInitialized();
for (int i = 0; i < count; i++)
{
Vector3 vel = new(NextSigned() * spreadSpeed, 2.5f + NextFloat() * 5.0f, NextSigned() * spreadSpeed);
float life = 0.85f + NextFloat() * 1.15f;
EmitRaw(pos + new Vector3(NextSigned() * 0.6f, NextFloat() * 0.6f, NextSigned() * 0.6f), vel, hdrColor, widthM, life, TypeBuoyantEmber, 1.6f);
}
}
public void EmitDustMotes(Vector3 pos, Vector3 normal, int count, Color dustColor, float speed, float widthM = 0.065f)
{
EnsureInitialized();
Vector3 n = normal.LengthSquared() > 1e-4f ? normal.Normalized() : Vector3.Up;
for (int i = 0; i < count; i++)
{
float ang = NextFloat() * MathF.Tau;
float spd = speed * (0.55f + 0.70f * NextFloat());
Vector3 vel = new Vector3(MathF.Cos(ang) * spd, 0.8f + NextFloat() * 1.4f, MathF.Sin(ang) * spd) + n * (0.4f * spd);
float life = 0.45f + NextFloat() * 0.45f;
EmitRaw(pos + new Vector3(NextSigned() * 0.35f, 0.08f, NextSigned() * 0.35f), vel, dustColor, widthM, life, TypeDustMote, 3.6f);
}
}
public void EmitThermalPoolEmber(Vector3 pos, float radius, Color accentCol)
{
EnsureInitialized();
float ang = NextFloat() * MathF.Tau;
float r = MathF.Sqrt(NextFloat()) * MathF.Max(0.5f, radius * 0.85f);
Vector3 p = pos + new Vector3(MathF.Cos(ang) * r, 0.18f, MathF.Sin(ang) * r);
Vector3 vel = new(NextSigned() * 0.7f, 2.0f + NextFloat() * 1.8f, NextSigned() * 0.7f);
Color hdr = new(accentCol.R * 3.8f, accentCol.G * 3.8f, accentCol.B * 3.8f, 0.95f);
EmitRaw(p, vel, hdr, 0.055f, 0.65f + NextFloat() * 0.45f, TypeBuoyantEmber, 1.4f);
}
public void EmitPodRetroSparksAndDust(Vector3 podBasePos, float agl, Color groundTint, bool isContender)
{
EnsureInitialized();
float scale = isContender ? 1.2f : 0.85f;
Color sparkHdr = new(4.2f, 2.4f, 0.75f, 0.98f);
for (int i = 0; i < 2; i++)
{
Vector3 vel = new(NextSigned() * 6.5f, -(11f + NextFloat() * 7f), NextSigned() * 6.5f);
EmitRaw(podBasePos + new Vector3(NextSigned() * 0.6f, 0.1f, NextSigned() * 0.6f), vel, sparkHdr, 0.048f * scale, 0.24f + NextFloat() * 0.14f, TypeVelocitySpark, 1.2f);
}
if (agl < 60f)
{
float washT = Math.Clamp(1f - agl / 60f, 0.2f, 1f);
Vector3 groundPos = podBasePos - new Vector3(0f, MathF.Max(0f, agl), 0f) + new Vector3(0f, 0.22f, 0f);
Color dustCol = new(groundTint.R * 1.1f, groundTint.G * 1.1f, groundTint.B * 1.1f, 0.75f * washT);
int moteCount = isContender ? 4 : 2;
for (int d = 0; d < moteCount; d++)
{
float ang = NextFloat() * MathF.Tau;
float spd = (7.5f + NextFloat() * 8.5f) * washT * scale;
Vector3 vel = new(MathF.Cos(ang) * spd, 1.0f + NextFloat() * 1.4f, MathF.Sin(ang) * spd);
EmitRaw(groundPos + new Vector3(MathF.Cos(ang) * 0.9f, 0f, MathF.Sin(ang) * 0.9f), vel, dustCol, 0.07f * scale, 0.55f + NextFloat() * 0.25f, TypeDustMote, 3.4f);
}
}
}
public void EmitPodTouchdownSparksAndDust(Vector3 pos, Vector3 normal, Color groundTint, bool isContender)
{
EnsureInitialized();
float scale = isContender ? 1.3f : 0.95f;
EmitSparkBurst(pos + normal * 0.45f, normal, isContender ? 16 : 8, new Color(4.5f, 2.5f, 0.75f, 0.98f), 18f * scale, 0.048f);
EmitEmberBurst(pos + normal * 0.45f, isContender ? 10 : 5, new Color(3.6f, 1.6f, 0.45f, 0.95f), 6.5f * scale, 0.055f);
EmitDustMotes(pos + normal * 0.25f, normal, isContender ? 14 : 8, groundTint with { A = 0.80f }, 10f * scale, 0.075f);
}
private void EmitRaw(Vector3 pos, Vector3 vel, Color hdrColor, float widthM, float lifetimeSec, int type, float drag)
{
float packedTypeAndDrag = type + Math.Clamp(drag, 0f, 19.9f) * 0.05f;
_particles.EmitParticle(
new Transform3D(Basis.Identity, pos),
vel,
hdrColor,
new Color(widthM, 0f, lifetimeSec, packedTypeAndDrag),
EmitFlagsAll);
TotalEmittedCount++;
}
private float NextFloat()
{
uint x = _rng;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
_rng = x;
return (x & 0x00FFFFFFu) / 16777216f;
}
private float NextSigned() => NextFloat() * 2f - 1f;
}