using System; using Godot; namespace RustToDust.Client; /// /// Flow-coupled GPU particle system (, 65,536 capacity) driven by /// the global rtd_flow_field + Tri3D curl streams for /// stochastic opaque HDR sparks, buoyant embers, flow-entrained dust motes, and muzzle starbursts. /// public partial class GpuSparksSystem : Node3D { public const int Capacity = 65536; 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("res://gpu_sparks_process.gdshader") ?? throw new InvalidOperationException("Failed to load res://gpu_sparks_process.gdshader"); Shader drawShader = GD.Load("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.026f) { 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.28f, NextSigned()); Vector3 dir = (n * 0.65f + rand).Normalized(); float spd = speed * (0.55f + 0.85f * NextFloat()); float life = 0.24f + 0.34f * NextFloat(); Vector3 spawnPos = pos + new Vector3(NextSigned() * 0.14f, NextFloat() * 0.12f, NextSigned() * 0.14f); EmitRaw(spawnPos, dir * spd, hdrColor, widthM, life, TypeVelocitySpark, 0.85f); } } public void EmitEmberBurst(Vector3 pos, int count, Color hdrColor, float spreadSpeed, float widthM = 0.030f) { EnsureInitialized(); for (int i = 0; i < count; i++) { Vector3 vel = new(NextSigned() * spreadSpeed, 2.5f + NextFloat() * 5.5f, NextSigned() * spreadSpeed); float life = 0.90f + NextFloat() * 1.25f; EmitRaw(pos + new Vector3(NextSigned() * 0.7f, NextFloat() * 0.6f, NextSigned() * 0.7f), vel, hdrColor, widthM, life, TypeBuoyantEmber, 1.5f); } } 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.8f, 2.6f, 0.65f, 0.98f); int sparkCount = isContender ? 14 : 8; for (int i = 0; i < sparkCount; i++) { Vector3 vel = new(NextSigned() * 7.5f, -(11f + NextFloat() * 7f), NextSigned() * 7.5f); EmitRaw(podBasePos + new Vector3(NextSigned() * 0.7f, 0.1f, NextSigned() * 0.7f), vel, sparkHdr, 0.028f * scale, 0.24f + NextFloat() * 0.16f, 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.78f * washT); int moteCount = isContender ? 24 : 14; 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.6f, MathF.Sin(ang) * spd); EmitRaw(groundPos + new Vector3(MathF.Cos(ang) * 0.9f, 0f, MathF.Sin(ang) * 0.9f), vel, dustCol, 0.055f * scale, 0.55f + NextFloat() * 0.30f, 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 ? 160 : 90, new Color(5.2f, 2.8f, 0.70f, 0.98f), 19f * scale, 0.028f); EmitEmberBurst(pos + normal * 0.45f, isContender ? 85 : 48, new Color(4.0f, 1.6f, 0.35f, 0.95f), 7.5f * scale, 0.032f); EmitDustMotes(pos + normal * 0.25f, normal, isContender ? 75 : 45, groundTint with { A = 0.82f }, 10f * scale, 0.058f); } 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; }