using System; using System.Collections.Generic; using Godot; using RustToDust.Core; namespace RustToDust.Client; /// /// Single-draw-call GPU billboard particle pool ( backed by /// particle_billboard.gdshader) plus the strictly capped [2] /// dynamic combat flash pool for Forward+ Vulkan. /// public partial class ParticlePool : Node3D { public const int MaxParticles = 512; public const int MaxFlashLights = 2; public const float ModeDustSmoke = 0f; public const float ModeMuzzleStarburst = 1f; public const float ModeSparkStreak = 2f; public const float ModeSteamFire = 3f; private struct ParticleSlot { public bool Active; public float Mode; public Vector3 Position; public Vector3 Velocity; public float Gravity; public float Drag; public float StartSize; public float EndSize; public float AspectY; public float Rotation; public float AngularVel; public float Emissive; public Color ColorStart; public Color ColorEnd; public float Age; public float Lifetime; } private struct FlashLightSlot { public OmniLight3D Light; public bool Active; public int Priority; public float Age; public float Duration; public float StartEnergy; public float Range; public Color Color; } private readonly ParticleSlot[] _particles = new ParticleSlot[MaxParticles]; private readonly FlashLightSlot[] _flashLights = new FlashLightSlot[MaxFlashLights]; // Bounded rolling dedupe buffers (zero polling cursor on MatchState) private readonly int[] _recentShotIds = new int[64]; private int _recentShotCursor; private readonly long[] _recentHitIds = new long[64]; private int _recentHitCursor; private MultiMesh _multiMesh = null!; private MultiMeshInstance3D _instanceNode = null!; private ShaderMaterial _material = null!; private int _writeCursor; private int _warmupFramesRemaining; private bool _initialized; private uint _rng = 0x7A3B91E5u; public int ActiveParticleCount { get; private set; } public int FlashLightPoolSize => MaxFlashLights; public int ActiveFlashLightCount { get { int count = 0; for (int i = 0; i < _flashLights.Length; i++) { if (_flashLights[i].Active) count++; } return count; } } public int PeakFlashLightCount { get; private set; } public override void _Ready() { EnsureInitialized(); } public void EnsureInitialized() { if (_initialized) { return; } _initialized = true; Shader shader = GD.Load("res://particle_billboard.gdshader") ?? throw new InvalidOperationException("Failed to load res://particle_billboard.gdshader"); _material = new ShaderMaterial { Shader = shader }; _multiMesh = new MultiMesh { TransformFormat = MultiMesh.TransformFormatEnum.Transform3D, UseColors = true, UseCustomData = true, Mesh = new QuadMesh { Size = Vector2.One }, InstanceCount = MaxParticles, VisibleInstanceCount = 0 }; _instanceNode = new MultiMeshInstance3D { Name = "ParticleBillboardBatch", Multimesh = _multiMesh, MaterialOverride = _material, CastShadow = GeometryInstance3D.ShadowCastingSetting.Off, CustomAabb = new Aabb(new Vector3(-1600f, -60f, -1600f), new Vector3(3200f, 500f, 3200f)), PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off, Visible = false }; AddChild(_instanceNode); for (int i = 0; i < MaxFlashLights; i++) { var light = new OmniLight3D { Name = $"CombatFlashLight_{i}", ShadowEnabled = false, OmniAttenuation = 1.6f, OmniRange = 14f, LightEnergy = 0f, LightVolumetricFogEnergy = 0f, Visible = false }; AddChild(light); _flashLights[i] = new FlashLightSlot { Light = light }; } } /// /// Pre-warms the pooled nodes on startup for 1 rendered frame /// with near-zero energy so Forward+ compiles lit shader variants before combat begins. /// public void PrewarmFlashLights(Vector3 nearPos, Vector3 forwardHoriz) { EnsureInitialized(); _warmupFramesRemaining = 2; Vector3 fwd = forwardHoriz.LengthSquared() > 1e-4f ? forwardHoriz.Normalized() : Vector3.Forward; for (int i = 0; i < _flashLights.Length; i++) { ref FlashLightSlot slot = ref _flashLights[i]; slot.Active = false; slot.Priority = 0; slot.Age = 0f; slot.Duration = 0f; slot.StartEnergy = 0f; slot.Range = 22f; slot.Light.Position = i == 0 ? nearPos + new Vector3(0f, 2f, 0f) : nearPos + fwd * 72f + new Vector3(0f, 3f, 0f); slot.Light.LightColor = Colors.White; slot.Light.OmniRange = 22f; slot.Light.OmniAttenuation = 1.6f; slot.Light.LightEnergy = 0.001f; slot.Light.Visible = true; } // Also pre-warm the particle billboard shader on frame 0 with 1 micro-quad _multiMesh.SetInstanceTransform(0, new Transform3D(Basis.FromScale(new Vector3(0.01f, 0.01f, 1f)), nearPos)); _multiMesh.SetInstanceColor(0, new Color(1f, 1f, 1f, 0.01f)); _multiMesh.SetInstanceCustomData(0, new Color(ModeDustSmoke, 0f, 0f, 0f)); _multiMesh.VisibleInstanceCount = 1; _instanceNode.Visible = true; } public void ClearAll() { EnsureInitialized(); Array.Clear(_particles, 0, _particles.Length); Array.Clear(_recentShotIds, 0, _recentShotIds.Length); Array.Clear(_recentHitIds, 0, _recentHitIds.Length); _recentShotCursor = 0; _recentHitCursor = 0; _writeCursor = 0; _warmupFramesRemaining = 0; ActiveParticleCount = 0; PeakFlashLightCount = 0; _multiMesh.VisibleInstanceCount = 0; _instanceNode.Visible = false; for (int i = 0; i < _flashLights.Length; i++) { _flashLights[i].Active = false; _flashLights[i].Age = 0f; _flashLights[i].Duration = 0f; _flashLights[i].Light.Visible = false; _flashLights[i].Light.LightEnergy = 0f; } } public void ConsumeEvents( IClientArenaSession session, IReadOnlyList events, Vector3 camPos, bool hasCam, Func sampleGroundTint) { EnsureInitialized(); var state = session.ViewState; for (int i = 0; i < events.Count; i++) { switch (events[i]) { case MatchResetEvent: ClearAll(); break; case ShotFiredEvent shot: HandleShotFired(shot, camPos, hasCam, sampleGroundTint); break; case HitImpactEvent hit: HandleHitImpact(hit, camPos, hasCam, sampleGroundTint); break; case ContenderEliminatedEvent elim: if ((elim.VictimId == session.LocalContenderId || session.IsContenderVisible(elim.VictimId)) && elim.VictimId >= 0 && elim.VictimId < state.Contenders.Count) { var v = state.Contenders[elim.VictimId]; EmitChassisExplosion(new Vector3(v.X, v.Y + 0.6f, v.Z), v.Grid.Spec.MassKg, camPos, hasCam, sampleGroundTint); } break; case SpectatorDestroyedEvent sdet: { if (!session.IsSpectatorVisible(sdet.SpectatorId)) { break; } var specs = state.Spectators.Spectators; for (int s = 0; s < specs.Count; s++) { if (specs[s].Id == sdet.SpectatorId) { var sp = specs[s]; EmitChassisExplosion(new Vector3(sp.X, sp.Y + 0.5f, sp.Z), sp.Grid.Spec.MassKg, camPos, hasCam, sampleGroundTint); break; } } break; } case DropPodEvent dpe: if ((DropPodEventKind)dpe.Kind == DropPodEventKind.PodTouchdownBlowPetals) { bool isLocal = dpe.ContenderId == session.LocalContenderId; bool isContender = dpe.PodKind == (byte)OrbitalPodKind.Contender; Vector3 pos = new(dpe.X, dpe.Y, dpe.Z); Vector3 nrm = new(dpe.NormalX, dpe.NormalY, dpe.NormalZ); if (nrm.LengthSquared() < 1e-4f) nrm = Vector3.Up; nrm = nrm.Normalized(); EmitPodTouchdownBurst(pos, nrm, sampleGroundTint(pos.X, pos.Z), isContender, isLocal, camPos, hasCam); } else if ((DropPodEventKind)dpe.Kind == DropPodEventKind.PodRetroBurnStart) { bool isLocal = dpe.ContenderId == session.LocalContenderId; Vector3 pos = new(dpe.X, dpe.Y, dpe.Z); if (isLocal || !hasCam || pos.DistanceSquaredTo(camPos) <= 220f * 220f) { SpawnFlashLight(pos - new Vector3(0f, 0.8f, 0f), new Color(1.0f, 0.62f, 0.22f), 4.5f, 15f, 0.14f, priority: isLocal ? 9 : 4); } } break; } } } public void EmitFootstepDust(in FootstepPulse pulse, Color groundTint, Vector3 camPos, bool hasCam) { EnsureInitialized(); Vector3 footPos = new(pulse.WorldPos.X, pulse.WorldPos.Y + 0.14f, pulse.WorldPos.Z); if (hasCam && footPos.DistanceSquaredTo(camPos) > 115f * 115f) { return; } float massScale = Math.Clamp(pulse.MassKg / 2600f, 0.72f, 1.55f); float loadScale = Math.Clamp(pulse.LoadG, 0.5f, 1.8f); int count = pulse.MassKg >= 3600f ? 3 : 2; Color dustStart = new(groundTint.R, groundTint.G, groundTint.B, 0.50f); Color dustEnd = new(groundTint.R * 0.92f, groundTint.G * 0.92f, groundTint.B * 0.92f, 0.0f); for (int i = 0; i < count; i++) { float ang = (i / (float)count) * MathF.Tau + NextFloat() * 0.8f; float speed = (2.2f + NextFloat() * 2.6f) * massScale * MathF.Sqrt(loadScale); Vector3 vel = new(MathF.Cos(ang) * speed, 0.85f + NextFloat() * 1.1f, MathF.Sin(ang) * speed); SpawnParticle(new ParticleSlot { Active = true, Mode = ModeDustSmoke, Position = footPos + new Vector3(MathF.Cos(ang) * 0.25f, 0f, MathF.Sin(ang) * 0.25f), Velocity = vel, Gravity = 0.25f, Drag = 2.8f, StartSize = 0.55f * massScale, EndSize = MathF.Min(2.2f, 1.55f * massScale), AspectY = 1.0f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.4f, Emissive = 0f, ColorStart = dustStart, ColorEnd = dustEnd, Lifetime = 0.35f + NextFloat() * 0.18f }); } } public void EmitTireDust(Vector3 worldPos, Vector3 vehicleVel, Color groundTint, float intensity, Vector3 camPos, bool hasCam) { EnsureInitialized(); if (hasCam && worldPos.DistanceSquaredTo(camPos) > 95f * 95f) { return; } float scale = Math.Clamp(intensity, 0.5f, 1.5f); Color dustStart = new(groundTint.R, groundTint.G, groundTint.B, 0.42f); Color dustEnd = new(groundTint.R * 0.95f, groundTint.G * 0.95f, groundTint.B * 0.95f, 0.0f); SpawnParticle(new ParticleSlot { Active = true, Mode = ModeDustSmoke, Position = worldPos + new Vector3(NextSigned() * 0.3f, 0.18f, NextSigned() * 0.3f), Velocity = -vehicleVel * 0.15f + new Vector3(NextSigned() * 1.1f, 0.9f + NextFloat() * 0.8f, NextSigned() * 1.1f), Gravity = 0.15f, Drag = 2.4f, StartSize = 0.50f * scale, EndSize = MathF.Min(2.1f, 1.65f * scale), AspectY = 1.0f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.2f, Emissive = 0f, ColorStart = dustStart, ColorEnd = dustEnd, Lifetime = 0.36f + NextFloat() * 0.16f }); } public void EmitThermalVent(Vector3 worldPos, float thermalRatio, Vector3 camPos, bool hasCam) { EnsureInitialized(); if (hasCam && worldPos.DistanceSquaredTo(camPos) > 105f * 105f) { return; } float heatT = Math.Clamp((thermalRatio - 0.85f) / 0.25f, 0f, 1f); SpawnParticle(new ParticleSlot { Active = true, Mode = ModeSteamFire, Position = worldPos + new Vector3(NextSigned() * 0.25f, 0.15f, NextSigned() * 0.25f), Velocity = new Vector3(NextSigned() * 0.9f, 2.2f + NextFloat() * 1.5f, NextSigned() * 0.9f), Gravity = -0.35f, Drag = 1.6f, StartSize = 0.40f, EndSize = 1.25f + heatT * 0.35f, AspectY = 1.0f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.8f, Emissive = 0.35f + heatT * 0.85f, ColorStart = new Color(0.95f, 0.88f - heatT * 0.32f, 0.78f - heatT * 0.50f, 0.62f), ColorEnd = new Color(0.65f, 0.58f, 0.54f, 0.0f), Lifetime = 0.36f + NextFloat() * 0.16f }); if (heatT > 0.35f) { SpawnParticle(new ParticleSlot { Active = true, Mode = ModeSparkStreak, Position = worldPos, Velocity = new Vector3(NextSigned() * 3.5f, 2.5f + NextFloat() * 3.5f, NextSigned() * 3.5f), Gravity = 9.0f, Drag = 0.6f, StartSize = 0.24f, EndSize = 0.10f, AspectY = 1.3f, Rotation = NextFloat() * MathF.Tau, AngularVel = 0f, Emissive = 2.6f, ColorStart = new Color(1.0f, 0.62f, 0.16f, 0.95f), ColorEnd = new Color(0.9f, 0.24f, 0.06f, 0.0f), Lifetime = 0.22f + NextFloat() * 0.12f }); } } public void EmitDamageSmokeAndFire(Vector3 worldPos, float coreHpRatio, Vector3 camPos, bool hasCam) { EnsureInitialized(); if (hasCam && worldPos.DistanceSquaredTo(camPos) > 125f * 125f) { return; } float dmgT = Math.Clamp((0.35f - coreHpRatio) / 0.35f, 0f, 1f); SpawnParticle(new ParticleSlot { Active = true, Mode = ModeDustSmoke, Position = worldPos + new Vector3(NextSigned() * 0.35f, 0.35f, NextSigned() * 0.35f), Velocity = new Vector3(NextSigned() * 0.8f, 2.4f + NextFloat() * 1.4f, NextSigned() * 0.8f), Gravity = -0.35f, Drag = 1.2f, StartSize = 0.58f, EndSize = MathF.Min(2.2f, 1.65f + dmgT * 0.55f), AspectY = 1.0f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.1f, Emissive = 0f, ColorStart = new Color(0.18f, 0.16f, 0.15f, 0.70f), ColorEnd = new Color(0.10f, 0.10f, 0.10f, 0.0f), Lifetime = 0.55f + NextFloat() * 0.25f }); if (dmgT > 0.3f && NextFloat() < 0.65f) { SpawnParticle(new ParticleSlot { Active = true, Mode = ModeSteamFire, Position = worldPos + new Vector3(NextSigned() * 0.22f, 0.25f, NextSigned() * 0.22f), Velocity = new Vector3(NextSigned() * 0.6f, 2.1f + NextFloat() * 1.1f, NextSigned() * 0.6f), Gravity = -0.4f, Drag = 1.8f, StartSize = 0.45f, EndSize = 0.95f, AspectY = 1.0f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 2.0f, Emissive = 2.2f, ColorStart = new Color(1.0f, 0.56f, 0.14f, 0.92f), ColorEnd = new Color(0.65f, 0.16f, 0.04f, 0.0f), Lifetime = 0.26f + NextFloat() * 0.12f }); } } public void TriggerFlashLightForTest(Vector3 pos, Color color, float energy, float range, float duration, int priority) { EnsureInitialized(); SpawnFlashLight(pos, color, energy, range, duration, priority); } public void AdvanceAndCommit(float dt) { EnsureInitialized(); float stepDt = float.IsFinite(dt) && dt > 0f ? Math.Clamp(dt, 0f, 0.05f) : 0f; // 1. Advance particles and pack active billboards into MultiMesh int visibleCount = 0; for (int i = 0; i < MaxParticles; i++) { ref ParticleSlot p = ref _particles[i]; if (!p.Active) { continue; } if (stepDt > 0f) { p.Age += stepDt; if (p.Age >= p.Lifetime) { p.Active = false; continue; } if (p.Drag > 0f) { p.Velocity *= MathF.Exp(-p.Drag * stepDt); } p.Velocity.Y -= p.Gravity * stepDt; p.Position += p.Velocity * stepDt; p.Rotation += p.AngularVel * stepDt; } float ageNorm = Math.Clamp(p.Age / MathF.Max(0.01f, p.Lifetime), 0f, 1f); float size = Mathf.Lerp(p.StartSize, p.EndSize, ageNorm); Color col = p.ColorStart.Lerp(p.ColorEnd, ageNorm); var scale = new Vector3(size, size * p.AspectY, 1f); _multiMesh.SetInstanceTransform(visibleCount, new Transform3D(Basis.FromScale(scale), p.Position)); _multiMesh.SetInstanceColor(visibleCount, col); _multiMesh.SetInstanceCustomData(visibleCount, new Color(p.Mode, ageNorm, p.Emissive, p.Rotation)); visibleCount++; } ActiveParticleCount = visibleCount; _multiMesh.VisibleInstanceCount = visibleCount; _instanceNode.Visible = visibleCount > 0; // 2. Advance pooled OmniLight3D[2] dynamic flashes int activeLights = 0; for (int i = 0; i < _flashLights.Length; i++) { ref FlashLightSlot slot = ref _flashLights[i]; if (!slot.Active) { continue; } if (stepDt > 0f) { slot.Age += stepDt; if (slot.Age >= slot.Duration) { slot.Active = false; slot.Light.Visible = false; slot.Light.LightEnergy = 0f; continue; } } float t = Math.Clamp(slot.Age / MathF.Max(0.01f, slot.Duration), 0f, 1f); float decay = MathF.Pow(1f - t, 1.8f); slot.Light.LightColor = slot.Color; slot.Light.OmniRange = slot.Range; slot.Light.LightEnergy = slot.StartEnergy * decay; slot.Light.Visible = slot.Light.LightEnergy > 0.01f; if (slot.Light.Visible) { activeLights++; } } if (_warmupFramesRemaining > 0) { _warmupFramesRemaining--; if (_warmupFramesRemaining == 0) { for (int i = 0; i < _flashLights.Length; i++) { if (!_flashLights[i].Active) { _flashLights[i].Light.Visible = false; _flashLights[i].Light.LightEnergy = 0f; } } } } if (activeLights > PeakFlashLightCount) { PeakFlashLightCount = activeLights; } } private void HandleShotFired( ShotFiredEvent shot, Vector3 camPos, bool hasCam, Func sampleGroundTint) { if (shot.TracerId != 0) { for (int k = 0; k < _recentShotIds.Length; k++) { if (_recentShotIds[k] == shot.TracerId) { return; } } _recentShotIds[_recentShotCursor] = shot.TracerId; _recentShotCursor = (_recentShotCursor + 1) % _recentShotIds.Length; } Vector3 start = new(shot.StartX, shot.StartY, shot.StartZ); Vector3 end = new(shot.EndX, shot.EndY, shot.EndZ); if (hasCam && start.DistanceSquaredTo(camPos) > 120f * 120f) { return; } var wClass = (WeaponVisualClass)shot.WeaponClass; bool isRecoilless = string.Equals(shot.WeaponId, "recoilless", StringComparison.Ordinal); bool isLance = string.Equals(shot.WeaponId, "gilded_lance", StringComparison.Ordinal); bool isPd = string.Equals(shot.WeaponId, "pd_flak", StringComparison.Ordinal); switch (wClass) { case WeaponVisualClass.KineticLight: { float flashSize = isPd ? 0.62f : 0.44f; SpawnParticle(new ParticleSlot { Active = true, Mode = ModeMuzzleStarburst, Position = start, Velocity = Vector3.Zero, StartSize = flashSize, EndSize = flashSize * 0.45f, AspectY = 1f, Rotation = NextFloat() * MathF.Tau, Emissive = 2.2f, ColorStart = new Color(1.0f, 0.85f, 0.42f, 0.95f), ColorEnd = new Color(0.95f, 0.55f, 0.16f, 0.0f), Lifetime = 0.065f }); break; } case WeaponVisualClass.Autocannon: { SpawnParticle(new ParticleSlot { Active = true, Mode = ModeMuzzleStarburst, Position = start, Velocity = Vector3.Zero, StartSize = 1.05f, EndSize = 0.45f, AspectY = 1f, Rotation = NextFloat() * MathF.Tau, Emissive = 2.8f, ColorStart = new Color(1.0f, 0.72f, 0.25f, 0.98f), ColorEnd = new Color(0.95f, 0.40f, 0.10f, 0.0f), Lifetime = 0.08f }); for (int p = 0; p < 2; p++) { SpawnParticle(new ParticleSlot { Active = true, Mode = ModeDustSmoke, Position = start + new Vector3(NextSigned() * 0.2f, NextFloat() * 0.15f, NextSigned() * 0.2f), Velocity = new Vector3(NextSigned() * 0.9f, 1.1f + NextFloat() * 0.8f, NextSigned() * 0.9f), Gravity = -0.15f, Drag = 2.2f, StartSize = 0.35f, EndSize = 1.15f, AspectY = 1f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.4f, Emissive = 0f, ColorStart = new Color(0.55f, 0.50f, 0.44f, 0.46f), ColorEnd = new Color(0.42f, 0.40f, 0.38f, 0.0f), Lifetime = 0.32f }); } if (!hasCam || start.DistanceSquaredTo(camPos) <= 45f * 45f) { SpawnFlashLight(start, new Color(1.0f, 0.68f, 0.24f), 2.8f, 10.5f, 0.10f, priority: 3); } break; } case WeaponVisualClass.Explosive: { float flashSize = isRecoilless ? 1.55f : 0.85f; SpawnParticle(new ParticleSlot { Active = true, Mode = ModeMuzzleStarburst, Position = start, Velocity = Vector3.Zero, StartSize = flashSize, EndSize = flashSize * 0.48f, AspectY = 1f, Rotation = NextFloat() * MathF.Tau, Emissive = 3.2f, ColorStart = new Color(1.0f, 0.68f, 0.22f, 0.98f), ColorEnd = new Color(0.95f, 0.32f, 0.08f, 0.0f), Lifetime = 0.095f }); int puffs = isRecoilless ? 4 : 2; Color gTint = sampleGroundTint(start.X, start.Z); Color smokeCol = new( Mathf.Lerp(0.56f, gTint.R, 0.35f), Mathf.Lerp(0.52f, gTint.G, 0.35f), Mathf.Lerp(0.46f, gTint.B, 0.35f), 0.58f); for (int p = 1; p <= puffs; p++) { float frac = p / (float)(puffs + 1); Vector3 smokePos = start.Lerp(end, frac); SpawnParticle(new ParticleSlot { Active = true, Mode = ModeDustSmoke, Position = smokePos, Velocity = new Vector3(NextSigned() * 0.6f, 1.3f + NextFloat() * 0.6f, NextSigned() * 0.6f), Gravity = -0.2f, Drag = 1.8f, StartSize = isRecoilless ? 0.48f : 0.30f, EndSize = isRecoilless ? 1.65f : 0.95f, AspectY = 1f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.2f, Emissive = 0f, ColorStart = smokeCol, ColorEnd = smokeCol with { A = 0f }, Lifetime = isRecoilless ? 0.48f : 0.32f }); } if (!hasCam || start.DistanceSquaredTo(camPos) <= 65f * 65f) { SpawnFlashLight(start, new Color(1.0f, 0.58f, 0.18f), isRecoilless ? 4.2f : 2.6f, isRecoilless ? 14f : 10f, 0.13f, priority: 5); } break; } case WeaponVisualClass.ThermalBeam: { Color beamCol = isLance ? new Color(1.0f, 0.78f, 0.28f, 0.96f) : new Color(0.32f, 0.90f, 1.0f, 0.96f); SpawnParticle(new ParticleSlot { Active = true, Mode = ModeMuzzleStarburst, Position = start, Velocity = Vector3.Zero, StartSize = isLance ? 1.25f : 0.85f, EndSize = 0.42f, AspectY = 1f, Rotation = NextFloat() * MathF.Tau, Emissive = 3.4f, ColorStart = beamCol, ColorEnd = beamCol with { A = 0f }, Lifetime = 0.09f }); if (!hasCam || start.DistanceSquaredTo(camPos) <= 45f * 45f) { SpawnFlashLight(start, beamCol with { A = 1f }, isLance ? 3.5f : 2.6f, 11f, 0.12f, priority: 4); } break; } } } private void HandleHitImpact( HitImpactEvent hit, Vector3 camPos, bool hasCam, Func sampleGroundTint) { if (hit.EventId != 0) { for (int k = 0; k < _recentHitIds.Length; k++) { if (_recentHitIds[k] == hit.EventId) { return; } } _recentHitIds[_recentHitCursor] = hit.EventId; _recentHitCursor = (_recentHitCursor + 1) % _recentHitIds.Length; } Vector3 pos = new(hit.HitX, hit.HitY, hit.HitZ); if (hasCam && pos.DistanceSquaredTo(camPos) > 125f * 125f) { return; } Vector3 normal = new(hit.NormalX, hit.NormalY, hit.NormalZ); if (normal.LengthSquared() < 1e-4f) normal = Vector3.Up; normal = normal.Normalized(); var wClass = (WeaponVisualClass)hit.WeaponClass; bool isTerrain = hit.TargetKind == (byte)HitTargetKind.Terrain; Color groundTint = isTerrain ? sampleGroundTint(pos.X, pos.Z) : new Color(0.46f, 0.42f, 0.38f, 1f); if (hit.IsAirburst || (wClass == WeaponVisualClass.KineticLight && string.Equals(hit.WeaponId, "pd_flak", StringComparison.Ordinal))) { SpawnParticle(new ParticleSlot { Active = true, Mode = ModeSteamFire, Position = pos, Velocity = Vector3.Zero, StartSize = 0.50f, EndSize = 2.15f, AspectY = 1f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.5f, Emissive = 1.6f, ColorStart = new Color(1.0f, 0.55f, 0.16f, 0.90f), ColorEnd = new Color(0.24f, 0.16f, 0.12f, 0.0f), Lifetime = 0.35f }); SpawnSparkBurst(pos, normal, 3, new Color(1.0f, 0.68f, 0.24f, 0.98f), 10f); return; } switch (wClass) { case WeaponVisualClass.KineticLight: if (isTerrain) { EmitImpactDustPlume(pos, normal, groundTint, count: 2, size: 1.0f); } else { SpawnSparkBurst(pos, normal, 3, new Color(1.0f, 0.88f, 0.45f, 0.96f), 8.5f); } break; case WeaponVisualClass.Autocannon: EmitImpactDustPlume(pos, normal, groundTint, count: 2, size: 1.45f); SpawnSparkBurst(pos, normal, 3, new Color(1.0f, 0.72f, 0.26f, 0.98f), 11.5f); break; case WeaponVisualClass.Explosive: { bool isRecoilless = string.Equals(hit.WeaponId, "recoilless", StringComparison.Ordinal); EmitImpactDustPlume(pos, normal, groundTint, count: isRecoilless ? 4 : 2, size: isRecoilless ? 1.85f : 1.35f); for (int f = 0; f < (isRecoilless ? 3 : 2); f++) { SpawnParticle(new ParticleSlot { Active = true, Mode = ModeSteamFire, Position = pos + normal * 0.25f + new Vector3(NextSigned() * 0.5f, NextFloat() * 0.4f, NextSigned() * 0.5f), Velocity = normal * (1.8f + NextFloat() * 1.8f) + new Vector3(NextSigned(), 1.2f, NextSigned()), Gravity = -0.3f, Drag = 1.6f, StartSize = isRecoilless ? 0.95f : 0.65f, EndSize = isRecoilless ? 3.1f : 2.0f, AspectY = 1f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.4f, Emissive = 2.6f, ColorStart = new Color(1.0f, 0.64f, 0.18f, 0.95f), ColorEnd = new Color(0.28f, 0.16f, 0.12f, 0.0f), Lifetime = isRecoilless ? 0.38f : 0.35f }); } SpawnSparkBurst(pos, normal, isRecoilless ? 5 : 3, new Color(1.0f, 0.64f, 0.20f, 0.98f), 14f); if (!hasCam || pos.DistanceSquaredTo(camPos) <= 75f * 75f) { SpawnFlashLight(pos + normal * 0.45f, new Color(1.0f, 0.55f, 0.16f), isRecoilless ? 4.8f : 3.2f, isRecoilless ? 14.5f : 11f, 0.14f, priority: 7); } break; } case WeaponVisualClass.ThermalBeam: SpawnParticle(new ParticleSlot { Active = true, Mode = ModeSteamFire, Position = pos, Velocity = normal * 1.4f, StartSize = 0.60f, EndSize = 1.75f, AspectY = 1f, Rotation = NextFloat() * MathF.Tau, Emissive = 2.8f, ColorStart = new Color(0.45f, 0.94f, 1.0f, 0.94f), ColorEnd = new Color(0.95f, 0.52f, 0.16f, 0.0f), Lifetime = 0.35f }); SpawnSparkBurst(pos, normal, 4, new Color(0.55f, 0.92f, 1.0f, 0.98f), 12f); break; } } private void EmitChassisExplosion( Vector3 pos, float massKg, Vector3 camPos, bool hasCam, Func sampleGroundTint) { if (hasCam && pos.DistanceSquaredTo(camPos) > 160f * 160f) { return; } float sizeScale = Math.Clamp(massKg / 2800f, 0.85f, 1.55f); Color gTint = sampleGroundTint(pos.X, pos.Z); for (int i = 0; i < 4; i++) { SpawnParticle(new ParticleSlot { Active = true, Mode = ModeSteamFire, Position = pos + new Vector3(NextSigned() * 0.8f, NextFloat() * 0.9f, NextSigned() * 0.8f), Velocity = new Vector3(NextSigned() * 2.4f, 2.2f + NextFloat() * 2.8f, NextSigned() * 2.4f), Gravity = -0.35f, Drag = 1.4f, StartSize = 1.1f * sizeScale, EndSize = MathF.Min(3.8f, 2.7f * sizeScale), AspectY = 1f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.5f, Emissive = 3.0f, ColorStart = new Color(1.0f, 0.62f, 0.16f, 0.98f), ColorEnd = new Color(0.22f, 0.12f, 0.08f, 0.0f), Lifetime = 0.42f + NextFloat() * 0.18f }); } for (int i = 0; i < 5; i++) { SpawnParticle(new ParticleSlot { Active = true, Mode = ModeDustSmoke, Position = pos + new Vector3(NextSigned() * 1.1f, NextFloat() * 0.8f, NextSigned() * 1.1f), Velocity = new Vector3(NextSigned() * 2.8f, 3.0f + NextFloat() * 2.8f, NextSigned() * 2.8f), Gravity = -0.35f, Drag = 1.1f, StartSize = 1.0f * sizeScale, EndSize = MathF.Min(2.2f, 1.55f * sizeScale), AspectY = 1f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.1f, Emissive = 0f, ColorStart = new Color(0.20f * 0.6f + gTint.R * 0.2f, 0.18f * 0.6f + gTint.G * 0.2f, 0.16f * 0.6f + gTint.B * 0.2f, 0.80f), ColorEnd = new Color(0.10f, 0.10f, 0.10f, 0.0f), Lifetime = 0.72f + NextFloat() * 0.30f }); } SpawnSparkBurst(pos, Vector3.Up, 6, new Color(1.0f, 0.68f, 0.22f, 0.98f), 17f); if (!hasCam || pos.DistanceSquaredTo(camPos) <= 95f * 95f) { SpawnFlashLight(pos + new Vector3(0f, 1.2f, 0f), new Color(1.0f, 0.56f, 0.14f), 6.2f, 16f, 0.18f, priority: 10); } } public void EmitPodReentryStreak(Vector3 podPos, Vector3 podVel, float heatGlow, bool isContender, Vector3 camPos, bool hasCam) { EnsureInitialized(); if (hasCam && podPos.DistanceSquaredTo(camPos) > 650f * 650f) { return; } float scale = isContender ? 1.25f : 0.85f; Vector3 upTrail = (-podVel.Normalized()) * (3.5f + NextFloat() * 3.0f); SpawnParticle(new ParticleSlot { Active = true, Mode = ModeSteamFire, Position = podPos + new Vector3(NextSigned() * 0.55f, 0.6f, NextSigned() * 0.55f), Velocity = upTrail + new Vector3(NextSigned() * 1.1f, 1.8f, NextSigned() * 1.1f), Gravity = -1.5f, Drag = 2.2f, StartSize = 0.85f * scale, EndSize = 0.28f * scale, AspectY = 2.0f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 2.5f, Emissive = 2.4f + heatGlow * 1.4f, ColorStart = new Color(1.0f, 0.64f, 0.20f, 0.88f), ColorEnd = new Color(0.75f, 0.22f, 0.06f, 0.0f), Lifetime = 0.20f + NextFloat() * 0.10f }); } public void EmitPodRetroBurn( Vector3 podBasePos, float agl, Color groundTint, bool isContender, bool isLocal, Vector3 camPos, bool hasCam) { EnsureInitialized(); float distSq = hasCam ? podBasePos.DistanceSquaredTo(camPos) : 0f; if (hasCam && distSq > 480f * 480f) { return; } float scale = isContender ? 1.20f : 0.85f; // Downward fiery retro plume for (int i = 0; i < 2; i++) { SpawnParticle(new ParticleSlot { Active = true, Mode = ModeSteamFire, Position = podBasePos + new Vector3(NextSigned() * 0.65f, 0.05f, NextSigned() * 0.65f), Velocity = new Vector3(NextSigned() * 2.2f, -(14f + NextFloat() * 9f), NextSigned() * 2.2f), Gravity = -2.0f, Drag = 2.4f, StartSize = 0.72f * scale, EndSize = 1.65f * scale, AspectY = 1.45f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 3.0f, Emissive = 3.1f, ColorStart = new Color(1.0f, 0.72f, 0.26f, 0.94f), ColorEnd = new Color(0.85f, 0.24f, 0.05f, 0.0f), Lifetime = 0.16f + NextFloat() * 0.08f }); } // Hot retro ember spark if (NextFloat() < 0.7f) { SpawnParticle(new ParticleSlot { Active = true, Mode = ModeSparkStreak, Position = podBasePos + new Vector3(NextSigned() * 0.7f, 0.1f, NextSigned() * 0.7f), Velocity = new Vector3(NextSigned() * 6.5f, -(9f + NextFloat() * 6f), NextSigned() * 6.5f), Gravity = 6.0f, Drag = 1.2f, StartSize = 0.26f, EndSize = 0.10f, AspectY = 1.6f, Rotation = NextFloat() * MathF.Tau, AngularVel = 0f, Emissive = 3.2f, ColorStart = new Color(1.0f, 0.78f, 0.30f, 0.96f), ColorEnd = new Color(0.95f, 0.28f, 0.06f, 0.0f), Lifetime = 0.18f + NextFloat() * 0.08f }); } // Ground dust wash when < 55m AGL if (agl < 55f && (!hasCam || distSq <= 240f * 240f)) { float washT = Math.Clamp(1f - agl / 55f, 0.15f, 1f); Vector3 groundPos = podBasePos - new Vector3(0f, MathF.Max(0f, agl), 0f) + new Vector3(0f, 0.25f, 0f); Color dustStart = new(groundTint.R, groundTint.G, groundTint.B, 0.56f * washT); Color dustEnd = new(groundTint.R * 0.9f, groundTint.G * 0.9f, groundTint.B * 0.9f, 0.0f); int dustCount = isContender ? 2 : 1; for (int d = 0; d < dustCount; d++) { float ang = NextFloat() * MathF.Tau; float spd = (6.5f + NextFloat() * 7.0f) * washT * scale; SpawnParticle(new ParticleSlot { Active = true, Mode = ModeDustSmoke, Position = groundPos + new Vector3(MathF.Cos(ang) * 0.9f, 0f, MathF.Sin(ang) * 0.9f), Velocity = new Vector3(MathF.Cos(ang) * spd, 1.2f + NextFloat() * 1.4f, MathF.Sin(ang) * spd), Gravity = 0.35f, Drag = 2.6f, StartSize = 0.85f * scale, EndSize = 2.35f * scale, AspectY = 1.0f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.8f, Emissive = 0f, ColorStart = dustStart, ColorEnd = dustEnd, Lifetime = 0.42f + NextFloat() * 0.18f }); } } if (isLocal || !hasCam || distSq <= 95f * 95f) { SpawnFlashLight(podBasePos - new Vector3(0f, 0.6f, 0f), new Color(1.0f, 0.60f, 0.18f), 3.8f, 14f, 0.09f, priority: isLocal ? 8 : 3); } } public void EmitPodTouchdownBurst( Vector3 pos, Vector3 normal, Color groundTint, bool isContender, bool isLocal, Vector3 camPos, bool hasCam) { EnsureInitialized(); float distSq = hasCam ? pos.DistanceSquaredTo(camPos) : 0f; if (!isLocal && hasCam && distSq > 360f * 360f) { return; } float scale = isContender ? 1.35f : 0.95f; int dustCount = isContender ? 7 : 4; Color dustStart = new(groundTint.R, groundTint.G, groundTint.B, 0.68f); Color dustEnd = new(groundTint.R * 0.88f, groundTint.G * 0.88f, groundTint.B * 0.88f, 0.0f); for (int i = 0; i < dustCount; i++) { float ang = (i / (float)dustCount) * MathF.Tau + NextFloat() * 0.4f; float spd = (7.5f + NextFloat() * 6.5f) * scale; SpawnParticle(new ParticleSlot { Active = true, Mode = ModeDustSmoke, Position = pos + normal * 0.35f + new Vector3(MathF.Cos(ang) * 1.1f, 0f, MathF.Sin(ang) * 1.1f), Velocity = new Vector3(MathF.Cos(ang) * spd, 1.8f + NextFloat() * 2.2f, MathF.Sin(ang) * spd), Gravity = 0.45f, Drag = 2.2f, StartSize = 0.95f * scale, EndSize = 2.55f * scale, AspectY = 1.0f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.6f, Emissive = 0f, ColorStart = dustStart, ColorEnd = dustEnd, Lifetime = 0.55f + NextFloat() * 0.25f }); } SpawnSparkBurst(pos + normal * 0.5f, normal, isContender ? 8 : 4, new Color(1.0f, 0.68f, 0.20f, 0.98f), 16f * scale); if (isLocal || !hasCam || distSq <= 140f * 140f) { SpawnFlashLight(pos + normal * 1.2f, new Color(1.0f, 0.58f, 0.16f), 5.8f, 18f, 0.18f, priority: isLocal ? 10 : 6); } } private void EmitImpactDustPlume(Vector3 pos, Vector3 normal, Color groundTint, int count, float size) { Color c0 = new(groundTint.R, groundTint.G, groundTint.B, 0.60f); Color c1 = new(groundTint.R * 0.88f, groundTint.G * 0.88f, groundTint.B * 0.88f, 0.0f); float endSize = MathF.Min(2.2f, size * 1.15f); for (int i = 0; i < count; i++) { Vector3 vel = normal * (1.8f + NextFloat() * 2.4f) + new Vector3(NextSigned() * 1.6f, 0.8f + NextFloat() * 1.2f, NextSigned() * 1.6f); SpawnParticle(new ParticleSlot { Active = true, Mode = ModeDustSmoke, Position = pos + normal * 0.12f, Velocity = vel, Gravity = 0.45f, Drag = 2.0f, StartSize = size * 0.40f, EndSize = endSize, AspectY = 1f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 1.4f, Emissive = 0f, ColorStart = c0, ColorEnd = c1, Lifetime = 0.35f + NextFloat() * 0.16f }); } } private void SpawnSparkBurst(Vector3 pos, Vector3 normal, int count, Color color, float speed) { for (int i = 0; i < count; i++) { Vector3 rand = new(NextSigned(), MathF.Abs(NextSigned()) + 0.35f, NextSigned()); Vector3 dir = (normal * 0.75f + rand).Normalized(); SpawnParticle(new ParticleSlot { Active = true, Mode = ModeSparkStreak, Position = pos, Velocity = dir * (speed * (0.6f + 0.75f * NextFloat())), Gravity = 11.0f, Drag = 0.8f, StartSize = 0.26f, EndSize = 0.10f, AspectY = 1.45f, Rotation = NextFloat() * MathF.Tau, AngularVel = NextSigned() * 6f, Emissive = 2.8f, ColorStart = color, ColorEnd = color with { A = 0f }, Lifetime = 0.16f + 0.10f * NextFloat() }); } } private void SpawnParticle(in ParticleSlot slot) { _particles[_writeCursor] = slot; _writeCursor = (_writeCursor + 1) % MaxParticles; } private void SpawnFlashLight(Vector3 pos, Color color, float energy, float range, float duration, int priority) { int targetIdx = -1; for (int i = 0; i < _flashLights.Length; i++) { if (!_flashLights[i].Active) { targetIdx = i; break; } } if (targetIdx < 0) { int lowestPri = int.MaxValue; float oldestAge = -1f; for (int i = 0; i < _flashLights.Length; i++) { if (_flashLights[i].Priority < lowestPri || (_flashLights[i].Priority == lowestPri && _flashLights[i].Age > oldestAge)) { lowestPri = _flashLights[i].Priority; oldestAge = _flashLights[i].Age; targetIdx = i; } } if (targetIdx < 0 || lowestPri > priority) { return; } } float clampedRange = Math.Clamp(range, 6.5f, 22f); ref FlashLightSlot slot = ref _flashLights[targetIdx]; slot.Active = true; slot.Priority = priority; slot.Age = 0f; slot.Duration = MathF.Max(0.04f, duration); slot.StartEnergy = energy; slot.Range = clampedRange; slot.Color = color; slot.Light.Position = pos; slot.Light.LightColor = color; slot.Light.OmniRange = clampedRange; slot.Light.LightEnergy = energy; slot.Light.Visible = true; int active = ActiveFlashLightCount; if (active > PeakFlashLightCount) { PeakFlashLightCount = active; } } 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; }