1027 lines
34 KiB
C#
1027 lines
34 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using Godot;
|
|
using RustToDust.Core;
|
|
|
|
namespace RustToDust.Client;
|
|
|
|
/// <summary>
|
|
/// CPU authority for all volumetric smoke, fire, retro-plume, plasma, and thermal-pool emitters
|
|
/// (<= 128) plus 3D flow-field impulses (<= 64) consumed by <see cref="VolumetricCompositorEffect"/>.
|
|
/// Packs std430-aligned snapshots under a short lock for zero-allocation GPU upload.
|
|
/// </summary>
|
|
public sealed class VolumetricEmitterPool
|
|
{
|
|
public const int MaxEmitters = 128;
|
|
public const int MaxFlowImpulses = 64;
|
|
public const int EmitterStrideBytes = 64;
|
|
public const int ImpulseStrideBytes = 48;
|
|
|
|
public const uint EmitterSmoke = 0u;
|
|
public const uint EmitterFireball = 1u;
|
|
public const uint EmitterRetroPlume = 2u;
|
|
public const uint EmitterPlasmaBurst = 3u;
|
|
public const uint EmitterThermalPool = 4u;
|
|
|
|
public const uint FlowShockShell = 0u;
|
|
public const uint FlowRetroDownwash = 1u;
|
|
public const uint FlowThermalUpdraft = 2u;
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 4)]
|
|
public struct GpuVolumetricEmitter
|
|
{
|
|
public float Ax, Ay, Az, RadiusA;
|
|
public float Bx, By, Bz, Temp;
|
|
public float DensityMult, RadiusB, FlowSpeed, PackedFlags;
|
|
public float R, G, B, Softness;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 4)]
|
|
public struct GpuFlowImpulse
|
|
{
|
|
public float PosX, PosY, PosZ, Radius;
|
|
public float DirX, DirY, DirZ, Strength;
|
|
public float ShellThickness, ShockPressure, TypeBitsFloat, FalloffExp;
|
|
}
|
|
|
|
private struct EmitterSlot
|
|
{
|
|
public bool Active;
|
|
public uint Type;
|
|
public int Priority;
|
|
public byte Seed;
|
|
public Vector3 StartPos;
|
|
public Vector3 EndPos;
|
|
public Vector3 VelStart;
|
|
public Vector3 VelEnd;
|
|
public float Drag;
|
|
public float Buoyancy;
|
|
public float RadiusA0;
|
|
public float RadiusA1;
|
|
public float RadiusB0;
|
|
public float RadiusB1;
|
|
public float Density0;
|
|
public float Density1;
|
|
public float Temp0;
|
|
public float Temp1;
|
|
public float FlowSpeed;
|
|
public float Softness;
|
|
public Color Tint0;
|
|
public Color Tint1;
|
|
public float Age;
|
|
public float Lifetime;
|
|
}
|
|
|
|
private struct ImpulseSlot
|
|
{
|
|
public bool Active;
|
|
public uint Type;
|
|
public int Priority;
|
|
public Vector3 Pos;
|
|
public Vector3 Dir;
|
|
public float Radius0;
|
|
public float Radius1;
|
|
public float Strength0;
|
|
public float Strength1;
|
|
public float ShellThickness;
|
|
public float ShockPressure0;
|
|
public float ShockPressure1;
|
|
public float FalloffExp;
|
|
public float Age;
|
|
public float Lifetime;
|
|
}
|
|
|
|
private readonly EmitterSlot[] _emitters = new EmitterSlot[MaxEmitters];
|
|
private readonly ImpulseSlot[] _impulses = new ImpulseSlot[MaxFlowImpulses];
|
|
|
|
private readonly object _sync = new();
|
|
private readonly byte[] _emitterBytes = new byte[MaxEmitters * EmitterStrideBytes];
|
|
private readonly byte[] _impulseBytes = new byte[MaxFlowImpulses * ImpulseStrideBytes];
|
|
private int _packedEmitterCount;
|
|
private int _packedImpulseCount;
|
|
private float _elapsedTime;
|
|
private float _lastStepDt = 1f / 60f;
|
|
private uint _rng = 0x51F29D4Bu;
|
|
private byte _seedSeq;
|
|
|
|
public int ActiveEmitterCount { get; private set; }
|
|
public int ActiveImpulseCount { get; private set; }
|
|
|
|
public void ClearAll()
|
|
{
|
|
Array.Clear(_emitters, 0, _emitters.Length);
|
|
Array.Clear(_impulses, 0, _impulses.Length);
|
|
ActiveEmitterCount = 0;
|
|
ActiveImpulseCount = 0;
|
|
lock (_sync)
|
|
{
|
|
_packedEmitterCount = 0;
|
|
_packedImpulseCount = 0;
|
|
Array.Clear(_emitterBytes, 0, _emitterBytes.Length);
|
|
Array.Clear(_impulseBytes, 0, _impulseBytes.Length);
|
|
}
|
|
}
|
|
|
|
public void Step(float dt)
|
|
{
|
|
float stepDt = float.IsFinite(dt) && dt > 0f ? Math.Clamp(dt, 0f, 0.05f) : 0f;
|
|
_elapsedTime += stepDt;
|
|
|
|
Span<GpuVolumetricEmitter> gpuEmitters = stackalloc GpuVolumetricEmitter[MaxEmitters];
|
|
int emCount = 0;
|
|
for (int i = 0; i < MaxEmitters; i++)
|
|
{
|
|
ref EmitterSlot s = ref _emitters[i];
|
|
if (!s.Active)
|
|
{
|
|
continue;
|
|
}
|
|
if (stepDt > 0f)
|
|
{
|
|
s.Age += stepDt;
|
|
if (s.Age >= s.Lifetime)
|
|
{
|
|
s.Active = false;
|
|
continue;
|
|
}
|
|
if (s.Drag > 0f)
|
|
{
|
|
float damp = MathF.Exp(-s.Drag * stepDt);
|
|
s.VelStart *= damp;
|
|
s.VelEnd *= damp;
|
|
}
|
|
s.VelStart.Y += s.Buoyancy * stepDt;
|
|
s.VelEnd.Y += s.Buoyancy * stepDt;
|
|
s.StartPos += s.VelStart * stepDt;
|
|
s.EndPos += s.VelEnd * stepDt;
|
|
}
|
|
|
|
float t = Math.Clamp(s.Age / MathF.Max(0.01f, s.Lifetime), 0f, 1f);
|
|
float density = Mathf.Lerp(s.Density0, s.Density1, t);
|
|
// Smooth fade-out tail in last 25% of lifetime
|
|
if (t > 0.75f)
|
|
{
|
|
density *= 1f - (t - 0.75f) / 0.25f;
|
|
}
|
|
if (density <= 0.002f)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float rA = MathF.Max(0.25f, Mathf.Lerp(s.RadiusA0, s.RadiusA1, t));
|
|
float rB = MathF.Max(0.25f, Mathf.Lerp(s.RadiusB0, s.RadiusB1, t));
|
|
float tempT = MathF.Pow(t, 1.45f);
|
|
float temp = MathF.Max(0f, Mathf.Lerp(s.Temp0, s.Temp1, tempT));
|
|
Color tint = s.Tint0.Lerp(s.Tint1, t);
|
|
|
|
uint ageByte = (uint)Math.Clamp((int)(t * 255f), 0, 255);
|
|
uint packedFlags = (s.Type & 0xFu) | (ageByte << 4) | ((uint)s.Seed << 12);
|
|
|
|
gpuEmitters[emCount++] = new GpuVolumetricEmitter
|
|
{
|
|
Ax = s.StartPos.X,
|
|
Ay = s.StartPos.Y,
|
|
Az = s.StartPos.Z,
|
|
RadiusA = rA,
|
|
Bx = s.EndPos.X,
|
|
By = s.EndPos.Y,
|
|
Bz = s.EndPos.Z,
|
|
Temp = temp,
|
|
DensityMult = density,
|
|
RadiusB = rB,
|
|
FlowSpeed = s.FlowSpeed,
|
|
PackedFlags = BitConverter.UInt32BitsToSingle(packedFlags),
|
|
R = MathF.Max(0f, tint.R),
|
|
G = MathF.Max(0f, tint.G),
|
|
B = MathF.Max(0f, tint.B),
|
|
Softness = s.Softness
|
|
};
|
|
}
|
|
|
|
Span<GpuFlowImpulse> gpuImpulses = stackalloc GpuFlowImpulse[MaxFlowImpulses];
|
|
int impCount = 0;
|
|
for (int i = 0; i < MaxFlowImpulses; i++)
|
|
{
|
|
ref ImpulseSlot s = ref _impulses[i];
|
|
if (!s.Active)
|
|
{
|
|
continue;
|
|
}
|
|
if (stepDt > 0f)
|
|
{
|
|
s.Age += stepDt;
|
|
if (s.Age >= s.Lifetime)
|
|
{
|
|
s.Active = false;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
float t = Math.Clamp(s.Age / MathF.Max(0.01f, s.Lifetime), 0f, 1f);
|
|
float radius = MathF.Max(0.5f, Mathf.Lerp(s.Radius0, s.Radius1, t));
|
|
float strength = Mathf.Lerp(s.Strength0, s.Strength1, t);
|
|
float pressure = MathF.Max(0f, Mathf.Lerp(s.ShockPressure0, s.ShockPressure1, t));
|
|
|
|
gpuImpulses[impCount++] = new GpuFlowImpulse
|
|
{
|
|
PosX = s.Pos.X,
|
|
PosY = s.Pos.Y,
|
|
PosZ = s.Pos.Z,
|
|
Radius = radius,
|
|
DirX = s.Dir.X,
|
|
DirY = s.Dir.Y,
|
|
DirZ = s.Dir.Z,
|
|
Strength = strength,
|
|
ShellThickness = s.ShellThickness,
|
|
ShockPressure = pressure,
|
|
TypeBitsFloat = BitConverter.UInt32BitsToSingle(s.Type),
|
|
FalloffExp = s.FalloffExp
|
|
};
|
|
}
|
|
|
|
ActiveEmitterCount = emCount;
|
|
ActiveImpulseCount = impCount;
|
|
|
|
lock (_sync)
|
|
{
|
|
_packedEmitterCount = emCount;
|
|
_packedImpulseCount = impCount;
|
|
if (stepDt > 0f)
|
|
{
|
|
_lastStepDt = stepDt;
|
|
}
|
|
if (emCount > 0)
|
|
{
|
|
MemoryMarshal.AsBytes(gpuEmitters.Slice(0, emCount)).CopyTo(_emitterBytes);
|
|
}
|
|
if (impCount > 0)
|
|
{
|
|
MemoryMarshal.AsBytes(gpuImpulses.Slice(0, impCount)).CopyTo(_impulseBytes);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void CopyRenderSnapshot(
|
|
byte[] dstEmitters,
|
|
byte[] dstImpulses,
|
|
out int emitterCount,
|
|
out int impulseCount,
|
|
out float elapsedTime,
|
|
out float dt)
|
|
{
|
|
lock (_sync)
|
|
{
|
|
emitterCount = _packedEmitterCount;
|
|
impulseCount = _packedImpulseCount;
|
|
elapsedTime = _elapsedTime;
|
|
dt = _lastStepDt;
|
|
if (emitterCount > 0)
|
|
{
|
|
Buffer.BlockCopy(_emitterBytes, 0, dstEmitters, 0, emitterCount * EmitterStrideBytes);
|
|
}
|
|
if (impulseCount > 0)
|
|
{
|
|
Buffer.BlockCopy(_impulseBytes, 0, dstImpulses, 0, impulseCount * ImpulseStrideBytes);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SpawnEmitter(
|
|
uint type,
|
|
Vector3 startPos,
|
|
Vector3 endPos,
|
|
Vector3 velStart,
|
|
Vector3 velEnd,
|
|
float radiusA0,
|
|
float radiusA1,
|
|
float radiusB0,
|
|
float radiusB1,
|
|
float density0,
|
|
float density1,
|
|
float temp0,
|
|
float temp1,
|
|
Color tint0,
|
|
Color tint1,
|
|
float lifetime,
|
|
float flowSpeed = 0.55f,
|
|
float softness = 0.36f,
|
|
float drag = 1.2f,
|
|
float buoyancy = 0.45f,
|
|
int priority = 5)
|
|
{
|
|
int slot = FindOrEvictEmitterSlot(priority);
|
|
if (slot < 0)
|
|
{
|
|
return;
|
|
}
|
|
_emitters[slot] = new EmitterSlot
|
|
{
|
|
Active = true,
|
|
Type = type,
|
|
Priority = priority,
|
|
Seed = _seedSeq++,
|
|
StartPos = startPos,
|
|
EndPos = endPos,
|
|
VelStart = velStart,
|
|
VelEnd = velEnd,
|
|
Drag = drag,
|
|
Buoyancy = buoyancy,
|
|
RadiusA0 = radiusA0,
|
|
RadiusA1 = radiusA1,
|
|
RadiusB0 = radiusB0,
|
|
RadiusB1 = radiusB1,
|
|
Density0 = density0,
|
|
Density1 = density1,
|
|
Temp0 = temp0,
|
|
Temp1 = temp1,
|
|
FlowSpeed = flowSpeed,
|
|
Softness = softness,
|
|
Tint0 = tint0,
|
|
Tint1 = tint1,
|
|
Age = 0f,
|
|
Lifetime = MathF.Max(0.05f, lifetime)
|
|
};
|
|
ActiveEmitterCount = CountActiveEmitters();
|
|
}
|
|
|
|
public void SpawnFlowImpulse(
|
|
uint type,
|
|
Vector3 pos,
|
|
Vector3 dir,
|
|
float radius0,
|
|
float radius1,
|
|
float strength0,
|
|
float strength1,
|
|
float shellThickness,
|
|
float shockPressure0,
|
|
float shockPressure1,
|
|
float lifetime,
|
|
float falloffExp = 1.6f,
|
|
int priority = 5)
|
|
{
|
|
int slot = FindOrEvictImpulseSlot(priority);
|
|
if (slot < 0)
|
|
{
|
|
return;
|
|
}
|
|
_impulses[slot] = new ImpulseSlot
|
|
{
|
|
Active = true,
|
|
Type = type,
|
|
Priority = priority,
|
|
Pos = pos,
|
|
Dir = dir.LengthSquared() > 1e-5f ? dir.Normalized() : Vector3.Up,
|
|
Radius0 = radius0,
|
|
Radius1 = radius1,
|
|
Strength0 = strength0,
|
|
Strength1 = strength1,
|
|
ShellThickness = shellThickness,
|
|
ShockPressure0 = shockPressure0,
|
|
ShockPressure1 = shockPressure1,
|
|
FalloffExp = falloffExp,
|
|
Age = 0f,
|
|
Lifetime = MathF.Max(0.04f, lifetime)
|
|
};
|
|
ActiveImpulseCount = CountActiveImpulses();
|
|
}
|
|
|
|
public void EmitChassisExplosion(Vector3 pos, float massKg, Color groundTint)
|
|
{
|
|
Color gLin = groundTint.SrgbToLinear();
|
|
float scale = Math.Clamp(massKg / 2800f, 0.85f, 1.55f);
|
|
// Primary fireball -> rising billowing soot capsule (3.5s)
|
|
SpawnEmitter(
|
|
EmitterFireball,
|
|
startPos: pos + new Vector3(0f, 0.4f, 0f),
|
|
endPos: pos + new Vector3(NextSigned() * 0.4f, 2.2f * scale, NextSigned() * 0.4f),
|
|
velStart: new Vector3(NextSigned() * 0.6f, 1.4f, NextSigned() * 0.6f),
|
|
velEnd: new Vector3(NextSigned() * 0.9f, 3.8f * scale, NextSigned() * 0.9f),
|
|
radiusA0: 1.8f * scale,
|
|
radiusA1: 5.2f * scale,
|
|
radiusB0: 2.2f * scale,
|
|
radiusB1: 6.4f * scale,
|
|
density0: 2.35f,
|
|
density1: 0.80f,
|
|
temp0: 1.95f,
|
|
temp1: 0.10f,
|
|
tint0: new Color(0.10f, 0.08f, 0.07f),
|
|
tint1: new Color(0.05f, 0.048f, 0.048f),
|
|
lifetime: 3.4f,
|
|
flowSpeed: 0.85f,
|
|
softness: 0.42f,
|
|
drag: 0.95f,
|
|
buoyancy: 0.90f,
|
|
priority: 10);
|
|
|
|
// Low ground dust skirt pushed outward by blast
|
|
SpawnEmitter(
|
|
EmitterSmoke,
|
|
startPos: pos - new Vector3(1.5f, 0.1f, 0f),
|
|
endPos: pos + new Vector3(1.5f, 0.2f, 0f),
|
|
velStart: Vector3.Up * 0.5f,
|
|
velEnd: Vector3.Up * 1.1f,
|
|
radiusA0: 2.4f * scale,
|
|
radiusA1: 6.8f * scale,
|
|
radiusB0: 2.4f * scale,
|
|
radiusB1: 6.8f * scale,
|
|
density0: 0.80f,
|
|
density1: 0.10f,
|
|
temp0: 0.30f,
|
|
temp1: 0f,
|
|
tint0: gLin * 0.85f,
|
|
tint1: gLin * 0.50f,
|
|
lifetime: 2.2f,
|
|
flowSpeed: 0.65f,
|
|
softness: 0.48f,
|
|
drag: 1.4f,
|
|
buoyancy: 0.25f,
|
|
priority: 9);
|
|
|
|
// Expanding radial ShockShell (~90 m/s) + buoyant ThermalUpdraft column
|
|
SpawnFlowImpulse(
|
|
FlowShockShell,
|
|
pos,
|
|
Vector3.Up,
|
|
radius0: 1.6f * scale,
|
|
radius1: 24.0f * scale,
|
|
strength0: 90f,
|
|
strength1: 14f,
|
|
shellThickness: 3.4f,
|
|
shockPressure0: 1.4f,
|
|
shockPressure1: 0.0f,
|
|
lifetime: 0.40f,
|
|
priority: 10);
|
|
|
|
SpawnFlowImpulse(
|
|
FlowThermalUpdraft,
|
|
pos,
|
|
Vector3.Up,
|
|
radius0: 5.5f * scale,
|
|
radius1: 8.5f * scale,
|
|
strength0: 13.5f,
|
|
strength1: 3.5f,
|
|
shellThickness: 0f,
|
|
shockPressure0: 0f,
|
|
shockPressure1: 0f,
|
|
lifetime: 3.2f,
|
|
priority: 9);
|
|
}
|
|
|
|
public void EmitExplosiveHit(Vector3 pos, Vector3 normal, Color groundTint, WeaponVisualClass wClass, bool isRecoilless, bool isAirburst)
|
|
{
|
|
bool isPlasma = wClass == WeaponVisualClass.PlasmaPpc;
|
|
bool isSwarm = wClass == WeaponVisualClass.SwarmSmart;
|
|
bool isThermal = wClass == WeaponVisualClass.ThermalBeam;
|
|
float scale = isRecoilless || wClass == WeaponVisualClass.RocketSalvo
|
|
? 1.35f
|
|
: (isAirburst ? 1.15f : (isThermal ? 0.58f : 0.90f));
|
|
|
|
uint type = (isPlasma || isSwarm || isThermal) ? EmitterPlasmaBurst : EmitterFireball;
|
|
Color gLin = groundTint.SrgbToLinear();
|
|
Color tint0 = isPlasma
|
|
? new Color(0.10f, 0.52f, 0.72f)
|
|
: isSwarm
|
|
? new Color(0.24f, 0.62f, 0.14f)
|
|
: isThermal
|
|
? new Color(0.12f, 0.22f, 0.28f)
|
|
: gLin * 0.30f + new Color(0.05f, 0.045f, 0.04f);
|
|
Color tint1 = new(0.05f, 0.048f, 0.045f);
|
|
|
|
float temp0 = (isPlasma || isThermal) ? 1.65f : (isAirburst || isRecoilless || wClass == WeaponVisualClass.RocketSalvo ? 2.10f : 1.70f);
|
|
float life = (isRecoilless || wClass == WeaponVisualClass.RocketSalvo) ? 1.55f : (isThermal ? 0.50f : 0.95f);
|
|
|
|
SpawnEmitter(
|
|
type,
|
|
startPos: pos + normal * 0.25f,
|
|
endPos: pos + normal * (1.2f * scale) + Vector3.Up * (0.9f * scale),
|
|
velStart: normal * 1.4f,
|
|
velEnd: normal * 2.4f + Vector3.Up * 1.8f,
|
|
radiusA0: 0.9f * scale,
|
|
radiusA1: 2.8f * scale,
|
|
radiusB0: 1.1f * scale,
|
|
radiusB1: 3.4f * scale,
|
|
density0: 1.85f,
|
|
density1: 0.26f,
|
|
temp0: temp0,
|
|
temp1: 0.0f,
|
|
tint0: tint0,
|
|
tint1: tint1,
|
|
lifetime: life,
|
|
flowSpeed: 0.75f,
|
|
softness: 0.42f,
|
|
drag: 1.4f,
|
|
buoyancy: 0.65f,
|
|
priority: isRecoilless ? 8 : 6);
|
|
|
|
if (isRecoilless || wClass is WeaponVisualClass.RocketSalvo or WeaponVisualClass.PlasmaPpc or WeaponVisualClass.Explosive)
|
|
{
|
|
SpawnFlowImpulse(
|
|
FlowShockShell,
|
|
pos,
|
|
normal,
|
|
radius0: 0.8f,
|
|
radius1: 11.0f * scale,
|
|
strength0: 54f * scale,
|
|
strength1: 8f,
|
|
shellThickness: 2.4f,
|
|
shockPressure0: 1.05f,
|
|
shockPressure1: 0.0f,
|
|
lifetime: 0.26f,
|
|
priority: 7);
|
|
}
|
|
}
|
|
|
|
public void EmitMuzzleBreechPuff(Vector3 start, Vector3 end, Color groundTint, WeaponVisualClass wClass, bool isRecoilless)
|
|
{
|
|
Vector3 dir = (end - start).LengthSquared() > 1e-4f ? (end - start).Normalized() : Vector3.Forward;
|
|
float scale = isRecoilless ? 1.25f : 0.85f;
|
|
Color gLin = groundTint.SrgbToLinear();
|
|
Color smokeTint = new(
|
|
Mathf.Lerp(0.24f, gLin.R, 0.35f),
|
|
Mathf.Lerp(0.22f, gLin.G, 0.35f),
|
|
Mathf.Lerp(0.20f, gLin.B, 0.35f));
|
|
|
|
SpawnEmitter(
|
|
EmitterSmoke,
|
|
startPos: start + dir * 0.2f,
|
|
endPos: start + dir * (2.6f * scale),
|
|
velStart: dir * 2.8f + Vector3.Up * 0.8f,
|
|
velEnd: dir * 4.2f + Vector3.Up * 1.4f,
|
|
radiusA0: 0.48f * scale,
|
|
radiusA1: 1.55f * scale,
|
|
radiusB0: 0.75f * scale,
|
|
radiusB1: 2.15f * scale,
|
|
density0: 1.35f,
|
|
density1: 0.18f,
|
|
temp0: isRecoilless ? 0.65f : 0.35f,
|
|
temp1: 0f,
|
|
tint0: smokeTint,
|
|
tint1: smokeTint * 0.75f,
|
|
lifetime: isRecoilless ? 0.82f : 0.50f,
|
|
flowSpeed: 0.6f,
|
|
softness: 0.44f,
|
|
drag: 2.2f,
|
|
buoyancy: 0.55f,
|
|
priority: 6);
|
|
|
|
if (isRecoilless || wClass == WeaponVisualClass.RocketSalvo)
|
|
{
|
|
// Backblast / recoil puff & impulse
|
|
SpawnFlowImpulse(
|
|
FlowShockShell,
|
|
start,
|
|
dir,
|
|
radius0: 0.6f,
|
|
radius1: 6.5f * scale,
|
|
strength0: 28f,
|
|
strength1: 4f,
|
|
shellThickness: 1.8f,
|
|
shockPressure0: 0.55f,
|
|
shockPressure1: 0f,
|
|
lifetime: 0.18f,
|
|
priority: 5);
|
|
}
|
|
}
|
|
|
|
public void EmitOrdnanceDetonation(Vector3 pos, Color accentCol, float splashRadius)
|
|
{
|
|
float scale = Math.Clamp(splashRadius / 4.2f, 0.75f, 1.85f);
|
|
SpawnEmitter(
|
|
EmitterFireball,
|
|
startPos: pos,
|
|
endPos: pos + Vector3.Up * (1.8f * scale),
|
|
velStart: Vector3.Up * 1.4f,
|
|
velEnd: Vector3.Up * 3.2f,
|
|
radiusA0: 1.2f * scale,
|
|
radiusA1: 3.8f * scale,
|
|
radiusB0: 1.4f * scale,
|
|
radiusB1: 4.5f * scale,
|
|
density0: 2.15f,
|
|
density1: 0.42f,
|
|
temp0: 2.05f,
|
|
temp1: 0.10f,
|
|
tint0: accentCol * 0.32f + new Color(0.07f, 0.06f, 0.055f),
|
|
tint1: new Color(0.055f, 0.05f, 0.048f),
|
|
lifetime: 2.1f,
|
|
flowSpeed: 0.8f,
|
|
softness: 0.40f,
|
|
drag: 1.1f,
|
|
buoyancy: 0.85f,
|
|
priority: 9);
|
|
|
|
SpawnFlowImpulse(
|
|
FlowShockShell,
|
|
pos,
|
|
Vector3.Up,
|
|
radius0: 1.0f,
|
|
radius1: Math.Clamp(splashRadius * 3.2f, 9f, 24f),
|
|
strength0: 76f,
|
|
strength1: 12f,
|
|
shellThickness: 2.8f,
|
|
shockPressure0: 1.25f,
|
|
shockPressure1: 0f,
|
|
lifetime: 0.34f,
|
|
priority: 9);
|
|
|
|
SpawnFlowImpulse(
|
|
FlowThermalUpdraft,
|
|
pos,
|
|
Vector3.Up,
|
|
radius0: 3.5f * scale,
|
|
radius1: 6.0f * scale,
|
|
strength0: 9.5f,
|
|
strength1: 2.5f,
|
|
shellThickness: 0f,
|
|
shockPressure0: 0f,
|
|
shockPressure1: 0f,
|
|
lifetime: 2.2f,
|
|
priority: 7);
|
|
}
|
|
|
|
public void EmitOrdnanceTrailCapsule(Vector3 tailPos, Vector3 pos, Vector3 vel, Color accentCol, bool isRocket)
|
|
{
|
|
if (ActiveEmitterCount >= 112)
|
|
{
|
|
return;
|
|
}
|
|
Vector3 back = vel.LengthSquared() > 1f ? -vel.Normalized() : Vector3.Up;
|
|
Vector3 segStart = pos;
|
|
Vector3 segEnd = pos.DistanceSquaredTo(tailPos) > 0.25f ? tailPos : pos + back * 2.2f;
|
|
SpawnEmitter(
|
|
isRocket ? EmitterFireball : EmitterSmoke,
|
|
startPos: segStart,
|
|
endPos: segEnd,
|
|
velStart: back * 1.4f + Vector3.Up * 0.5f,
|
|
velEnd: back * 0.8f + Vector3.Up * 1.1f,
|
|
radiusA0: isRocket ? 0.55f : 0.38f,
|
|
radiusA1: isRocket ? 1.85f : 1.15f,
|
|
radiusB0: isRocket ? 0.72f : 0.48f,
|
|
radiusB1: isRocket ? 2.25f : 1.35f,
|
|
density0: isRocket ? 0.92f : 0.58f,
|
|
density1: 0.06f,
|
|
temp0: isRocket ? 0.75f : 0.10f,
|
|
temp1: 0f,
|
|
tint0: isRocket ? new Color(0.62f, 0.56f, 0.50f) : accentCol * 0.65f,
|
|
tint1: new Color(0.32f, 0.30f, 0.28f),
|
|
lifetime: isRocket ? 0.95f : 0.55f,
|
|
flowSpeed: 0.65f,
|
|
softness: 0.46f,
|
|
drag: 1.6f,
|
|
buoyancy: 0.45f,
|
|
priority: 4);
|
|
}
|
|
|
|
public void EmitThermalPoolPlume(Vector3 pos, float radius, Color accentCol)
|
|
{
|
|
if (ActiveEmitterCount >= 116)
|
|
{
|
|
return;
|
|
}
|
|
float r = Math.Clamp(radius * 0.55f, 1.4f, 4.5f);
|
|
SpawnEmitter(
|
|
EmitterThermalPool,
|
|
startPos: pos + new Vector3(NextSigned() * r * 0.45f, 0.2f, NextSigned() * r * 0.45f),
|
|
endPos: pos + new Vector3(NextSigned() * r * 0.35f, 2.8f, NextSigned() * r * 0.35f),
|
|
velStart: Vector3.Up * 1.1f,
|
|
velEnd: Vector3.Up * 2.2f,
|
|
radiusA0: r * 0.55f,
|
|
radiusA1: r * 0.85f,
|
|
radiusB0: r * 0.40f,
|
|
radiusB1: r * 0.95f,
|
|
density0: 0.78f,
|
|
density1: 0.10f,
|
|
temp0: 0.85f,
|
|
temp1: 0.15f,
|
|
tint0: accentCol,
|
|
tint1: accentCol * 0.55f,
|
|
lifetime: 0.75f,
|
|
flowSpeed: 0.75f,
|
|
softness: 0.45f,
|
|
drag: 1.0f,
|
|
buoyancy: 0.85f,
|
|
priority: 5);
|
|
|
|
SpawnFlowImpulse(
|
|
FlowThermalUpdraft,
|
|
pos,
|
|
Vector3.Up,
|
|
radius0: radius,
|
|
radius1: radius * 1.15f,
|
|
strength0: 7.5f,
|
|
strength1: 7.5f,
|
|
shellThickness: 0f,
|
|
shockPressure0: 0f,
|
|
shockPressure1: 0f,
|
|
lifetime: 0.45f,
|
|
priority: 4);
|
|
}
|
|
|
|
public void EmitPodReentryPlume(Vector3 podPos, Vector3 podVel, float heatGlow, bool isContender)
|
|
{
|
|
float scale = isContender ? 1.25f : 0.85f;
|
|
Vector3 upTrail = podVel.LengthSquared() > 1f ? -podVel.Normalized() : Vector3.Up;
|
|
SpawnEmitter(
|
|
EmitterRetroPlume,
|
|
startPos: podPos + upTrail * 0.8f,
|
|
endPos: podPos + upTrail * (4.5f * scale),
|
|
velStart: upTrail * 2.5f,
|
|
velEnd: upTrail * 4.0f,
|
|
radiusA0: 0.75f * scale,
|
|
radiusA1: 1.85f * scale,
|
|
radiusB0: 1.15f * scale,
|
|
radiusB1: 2.65f * scale,
|
|
density0: 0.90f,
|
|
density1: 0.08f,
|
|
temp0: 0.72f + heatGlow * 0.45f,
|
|
temp1: 0.04f,
|
|
tint0: new Color(0.55f, 0.42f, 0.34f),
|
|
tint1: new Color(0.28f, 0.26f, 0.25f),
|
|
lifetime: 0.42f,
|
|
flowSpeed: 0.90f,
|
|
softness: 0.42f,
|
|
drag: 1.6f,
|
|
buoyancy: 0.4f,
|
|
priority: isContender ? 7 : 4);
|
|
}
|
|
|
|
public void EmitPodRetroPlume(Vector3 podBasePos, float agl, Color groundTint, bool isContender)
|
|
{
|
|
float scale = isContender ? 1.25f : 0.88f;
|
|
float jetLen = MathF.Min(12f * scale, MathF.Max(3.5f, agl * 0.6f));
|
|
SpawnEmitter(
|
|
EmitterRetroPlume,
|
|
startPos: podBasePos,
|
|
endPos: podBasePos - new Vector3(0f, jetLen, 0f),
|
|
velStart: new Vector3(0f, -8f, 0f),
|
|
velEnd: new Vector3(NextSigned() * 2.5f, -4f, NextSigned() * 2.5f),
|
|
radiusA0: 0.85f * scale,
|
|
radiusA1: 1.6f * scale,
|
|
radiusB0: 1.65f * scale,
|
|
radiusB1: 3.4f * scale,
|
|
density0: 1.85f,
|
|
density1: 0.24f,
|
|
temp0: 1.55f,
|
|
temp1: 0.14f,
|
|
tint0: new Color(0.60f, 0.48f, 0.38f),
|
|
tint1: groundTint * 0.72f,
|
|
lifetime: 0.26f,
|
|
flowSpeed: 1.1f,
|
|
softness: 0.40f,
|
|
drag: 1.8f,
|
|
buoyancy: 0.2f,
|
|
priority: isContender ? 9 : 6);
|
|
|
|
if (agl < 65f)
|
|
{
|
|
float washT = Math.Clamp(1f - agl / 65f, 0.20f, 1f);
|
|
Vector3 groundPos = podBasePos - new Vector3(0f, MathF.Max(0f, agl), 0f) + new Vector3(0f, 0.6f, 0f);
|
|
SpawnEmitter(
|
|
EmitterSmoke,
|
|
startPos: groundPos + new Vector3(-1.8f * scale, 0f, 0f),
|
|
endPos: groundPos + new Vector3(1.8f * scale, 0.4f, 0f),
|
|
velStart: new Vector3(-3.5f * washT, 1.1f, NextSigned() * 2.2f),
|
|
velEnd: new Vector3(3.5f * washT, 1.3f, NextSigned() * 2.2f),
|
|
radiusA0: 1.6f * scale,
|
|
radiusA1: 4.8f * scale * washT + 1.4f,
|
|
radiusB0: 1.6f * scale,
|
|
radiusB1: 4.8f * scale * washT + 1.4f,
|
|
density0: 0.82f * washT,
|
|
density1: 0.08f,
|
|
temp0: 0.10f,
|
|
temp1: 0f,
|
|
tint0: groundTint,
|
|
tint1: groundTint * 0.78f,
|
|
lifetime: 0.72f,
|
|
flowSpeed: 0.85f,
|
|
softness: 0.48f,
|
|
drag: 1.6f,
|
|
buoyancy: 0.35f,
|
|
priority: isContender ? 8 : 5);
|
|
|
|
SpawnFlowImpulse(
|
|
FlowRetroDownwash,
|
|
groundPos,
|
|
Vector3.Down,
|
|
radius0: 14.0f * scale,
|
|
radius1: 18.0f * scale,
|
|
strength0: 42f * washT,
|
|
strength1: 30f * washT,
|
|
shellThickness: 0f,
|
|
shockPressure0: 0.65f * washT,
|
|
shockPressure1: 0.35f * washT,
|
|
lifetime: 0.14f,
|
|
priority: isContender ? 9 : 6);
|
|
}
|
|
}
|
|
|
|
public void EmitPodTouchdownPlume(Vector3 pos, Vector3 normal, Color groundTint, bool isContender)
|
|
{
|
|
float scale = isContender ? 1.40f : 0.95f;
|
|
SpawnEmitter(
|
|
EmitterFireball,
|
|
startPos: pos + normal * 0.6f,
|
|
endPos: pos + normal * (2.6f * scale),
|
|
velStart: normal * 1.8f,
|
|
velEnd: normal * 3.4f,
|
|
radiusA0: 1.6f * scale,
|
|
radiusA1: 5.5f * scale,
|
|
radiusB0: 1.9f * scale,
|
|
radiusB1: 6.2f * scale,
|
|
density0: 1.85f,
|
|
density1: 0.30f,
|
|
temp0: 1.35f,
|
|
temp1: 0.0f,
|
|
tint0: groundTint * 0.82f,
|
|
tint1: groundTint * 0.55f,
|
|
lifetime: isContender ? 1.85f : 1.15f,
|
|
flowSpeed: 0.85f,
|
|
softness: 0.46f,
|
|
drag: 1.3f,
|
|
buoyancy: 0.65f,
|
|
priority: isContender ? 10 : 7);
|
|
|
|
SpawnFlowImpulse(
|
|
FlowShockShell,
|
|
pos + normal * 0.4f,
|
|
normal,
|
|
radius0: 1.8f,
|
|
radius1: 22.0f * scale,
|
|
strength0: 68f * scale,
|
|
strength1: 12f,
|
|
shellThickness: 3.2f,
|
|
shockPressure0: 1.1f,
|
|
shockPressure1: 0f,
|
|
lifetime: 0.35f,
|
|
priority: 10);
|
|
}
|
|
|
|
public void EmitThermalVentPlume(Vector3 pos, float thermalRatio)
|
|
{
|
|
if (ActiveEmitterCount >= 112)
|
|
{
|
|
return;
|
|
}
|
|
float heatT = Math.Clamp((thermalRatio - 0.85f) / 0.25f, 0f, 1f);
|
|
SpawnEmitter(
|
|
EmitterSmoke,
|
|
startPos: pos,
|
|
endPos: pos + new Vector3(0f, 1.4f, 0f),
|
|
velStart: new Vector3(NextSigned() * 0.6f, 2.2f, NextSigned() * 0.6f),
|
|
velEnd: new Vector3(NextSigned() * 0.8f, 3.0f, NextSigned() * 0.8f),
|
|
radiusA0: 0.45f,
|
|
radiusA1: 1.45f + heatT * 0.4f,
|
|
radiusB0: 0.55f,
|
|
radiusB1: 1.65f + heatT * 0.5f,
|
|
density0: 0.72f,
|
|
density1: 0.06f,
|
|
temp0: 0.25f + heatT * 0.45f,
|
|
temp1: 0f,
|
|
tint0: new Color(0.82f, 0.72f - heatT * 0.22f, 0.62f - heatT * 0.35f),
|
|
tint1: new Color(0.45f, 0.42f, 0.40f),
|
|
lifetime: 0.65f,
|
|
flowSpeed: 0.6f,
|
|
softness: 0.45f,
|
|
drag: 1.5f,
|
|
buoyancy: 0.75f,
|
|
priority: 4);
|
|
}
|
|
|
|
public void EmitDamageSmokePlume(Vector3 pos, float coreHpRatio)
|
|
{
|
|
if (ActiveEmitterCount >= 112)
|
|
{
|
|
return;
|
|
}
|
|
float dmgT = Math.Clamp((0.35f - coreHpRatio) / 0.35f, 0f, 1f);
|
|
SpawnEmitter(
|
|
dmgT > 0.35f ? EmitterFireball : EmitterSmoke,
|
|
startPos: pos,
|
|
endPos: pos + new Vector3(0f, 1.8f, 0f),
|
|
velStart: new Vector3(NextSigned() * 0.5f, 2.1f, NextSigned() * 0.5f),
|
|
velEnd: new Vector3(NextSigned() * 0.8f, 3.2f, NextSigned() * 0.8f),
|
|
radiusA0: 0.62f,
|
|
radiusA1: 1.95f + dmgT * 0.65f,
|
|
radiusB0: 0.75f,
|
|
radiusB1: 2.35f + dmgT * 0.75f,
|
|
density0: 1.65f,
|
|
density1: 0.24f,
|
|
temp0: dmgT > 0.30f ? 0.85f + dmgT * 0.55f : 0.12f,
|
|
temp1: 0f,
|
|
tint0: new Color(0.11f, 0.10f, 0.095f),
|
|
tint1: new Color(0.06f, 0.06f, 0.06f),
|
|
lifetime: 0.95f,
|
|
flowSpeed: 0.7f,
|
|
softness: 0.42f,
|
|
drag: 1.1f,
|
|
buoyancy: 0.85f,
|
|
priority: 5);
|
|
}
|
|
|
|
public void EmitFootstepOrTirePuff(Vector3 pos, Color groundTint, float scale)
|
|
{
|
|
// Strictly gated under 48 active emitters so combat explosions never drop slots and desert stays within GPU budget
|
|
if (ActiveEmitterCount >= 48)
|
|
{
|
|
return;
|
|
}
|
|
Color gLin = groundTint.SrgbToLinear();
|
|
SpawnEmitter(
|
|
EmitterSmoke,
|
|
startPos: pos,
|
|
endPos: pos + new Vector3(NextSigned() * 0.4f, 0.45f, NextSigned() * 0.4f),
|
|
velStart: new Vector3(NextSigned() * 1.1f, 0.9f, NextSigned() * 1.1f),
|
|
velEnd: new Vector3(NextSigned() * 1.4f, 1.4f, NextSigned() * 1.4f),
|
|
radiusA0: 0.48f * scale,
|
|
radiusA1: 1.55f * scale,
|
|
radiusB0: 0.58f * scale,
|
|
radiusB1: 1.75f * scale,
|
|
density0: 0.38f,
|
|
density1: 0.04f,
|
|
temp0: 0f,
|
|
temp1: 0f,
|
|
tint0: gLin * 1.1f,
|
|
tint1: gLin * 0.75f,
|
|
lifetime: 0.44f,
|
|
flowSpeed: 0.45f,
|
|
softness: 0.50f,
|
|
drag: 2.0f,
|
|
buoyancy: 0.35f,
|
|
priority: 1);
|
|
}
|
|
|
|
private int FindOrEvictEmitterSlot(int priority)
|
|
{
|
|
int oldestIdx = -1;
|
|
int lowestPri = int.MaxValue;
|
|
float oldestAge = -1f;
|
|
for (int i = 0; i < MaxEmitters; i++)
|
|
{
|
|
if (!_emitters[i].Active)
|
|
{
|
|
return i;
|
|
}
|
|
if (_emitters[i].Priority < lowestPri ||
|
|
(_emitters[i].Priority == lowestPri && _emitters[i].Age > oldestAge))
|
|
{
|
|
lowestPri = _emitters[i].Priority;
|
|
oldestAge = _emitters[i].Age;
|
|
oldestIdx = i;
|
|
}
|
|
}
|
|
return lowestPri <= priority ? oldestIdx : -1;
|
|
}
|
|
|
|
private int FindOrEvictImpulseSlot(int priority)
|
|
{
|
|
int oldestIdx = -1;
|
|
int lowestPri = int.MaxValue;
|
|
float oldestAge = -1f;
|
|
for (int i = 0; i < MaxFlowImpulses; i++)
|
|
{
|
|
if (!_impulses[i].Active)
|
|
{
|
|
return i;
|
|
}
|
|
if (_impulses[i].Priority < lowestPri ||
|
|
(_impulses[i].Priority == lowestPri && _impulses[i].Age > oldestAge))
|
|
{
|
|
lowestPri = _impulses[i].Priority;
|
|
oldestAge = _impulses[i].Age;
|
|
oldestIdx = i;
|
|
}
|
|
}
|
|
return lowestPri <= priority ? oldestIdx : -1;
|
|
}
|
|
|
|
private int CountActiveEmitters()
|
|
{
|
|
int count = 0;
|
|
for (int i = 0; i < MaxEmitters; i++)
|
|
{
|
|
if (_emitters[i].Active) count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
private int CountActiveImpulses()
|
|
{
|
|
int count = 0;
|
|
for (int i = 0; i < MaxFlowImpulses; i++)
|
|
{
|
|
if (_impulses[i].Active) count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
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;
|
|
}
|