1343 lines
53 KiB
C#
1343 lines
53 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Godot;
|
|
using RustToDust.Core;
|
|
|
|
namespace RustToDust.Client;
|
|
|
|
/// <summary>
|
|
/// Renders the 3D arena entities from <see cref="IClientArenaSession"/>:
|
|
/// cover ruins, shrinking Safe Circle ring, unified <see cref="MultiMeshPartPool"/> for all
|
|
/// 15 Brokkr vehicles/mechs, mounted gear, salvage drops, and tumbling debris,
|
|
/// live 1-draw-call <see cref="PomDecalPool"/> combat scars, and pooled multi-class weapon VFX.
|
|
/// </summary>
|
|
public partial class ArenaRenderer : Node3D
|
|
{
|
|
private static readonly Color OlivePaint = Color.FromHtml("#9bb06a");
|
|
private static readonly Color TouristGold = Color.FromHtml("#e6b838");
|
|
private static readonly Color CyanSalvage = Color.FromHtml("#44d9e8");
|
|
private static readonly Color CoverRust = Color.FromHtml("#4d433b");
|
|
private static readonly Color BermDark = Color.FromHtml("#2c2723");
|
|
|
|
private sealed class ActorVisual
|
|
{
|
|
public required VehicleVisualInstance Visual;
|
|
public MeshInstance3D? HaloMesh;
|
|
public StandardMaterial3D? HaloMat;
|
|
public MeshInstance3D? LaserMesh;
|
|
public SpectatorStance? LastHaloStance;
|
|
public bool LastLaserVisible;
|
|
public float RigDtAccum;
|
|
public int RigSkipFrames;
|
|
}
|
|
|
|
private sealed class DropVisualEntry
|
|
{
|
|
public VehicleVisualInstance? ChassisVisual;
|
|
public ArrayMesh? GearMesh;
|
|
public Vector3 GearScale;
|
|
public float GearYOffset;
|
|
public bool IsModule;
|
|
public bool IsImperial;
|
|
public float BaseY;
|
|
public int TileX;
|
|
public int TileZ;
|
|
public int TileVersion;
|
|
}
|
|
|
|
private enum VfxShape
|
|
{
|
|
Beam,
|
|
Sphere,
|
|
Ring
|
|
}
|
|
|
|
private struct TransientVfx
|
|
{
|
|
public VfxShape Shape;
|
|
public Vector3 Position;
|
|
public Vector3 Velocity;
|
|
public Basis BaseRotation;
|
|
public Vector3 StartScale;
|
|
public Vector3 EndScale;
|
|
public Color ColorStart;
|
|
public Color ColorEnd;
|
|
public float Age;
|
|
public float Lifetime;
|
|
}
|
|
|
|
private const int MaxVfxBeams = 512;
|
|
private const int MaxVfxSpheres = 512;
|
|
private const int MaxVfxRings = 128;
|
|
|
|
private VehicleAssetCache _assetCache = null!;
|
|
private Node3D _staticGroup = null!;
|
|
private Node3D _actorsGroup = null!;
|
|
private MultiMeshPartPool _partPool = null!;
|
|
private PomDecalPool _pomDecals = null!;
|
|
private DebrisSystem _debris = null!;
|
|
private ParticlePool _particles = null!;
|
|
private MeshInstance3D _safeRingMesh = null!;
|
|
private ImmediateMesh _safeRingImm = null!;
|
|
|
|
// Pooled GroundDrop beacons & pillars
|
|
private MultiMesh _dropDiscMm = null!;
|
|
private MultiMeshInstance3D _dropDiscMmi = null!;
|
|
private MultiMesh _dropPillarMm = null!;
|
|
private MultiMeshInstance3D _dropPillarMmi = null!;
|
|
|
|
// Pooled weapon VFX MultiMeshes (beams/tracers, explosive detonation cores, shockwave rings)
|
|
private ShaderMaterial _vfxMat = null!;
|
|
private MultiMesh _vfxBeamMm = null!;
|
|
private MultiMeshInstance3D _vfxBeamMmi = null!;
|
|
private MultiMesh _vfxSphereMm = null!;
|
|
private MultiMeshInstance3D _vfxSphereMmi = null!;
|
|
private MultiMesh _vfxRingMm = null!;
|
|
private MultiMeshInstance3D _vfxRingMmi = null!;
|
|
private readonly List<TransientVfx> _transientVfx = new(128);
|
|
private int _activeBeamCount;
|
|
|
|
// Continuous emitter rate limiters (keyed by (IsContender, EntityId))
|
|
private readonly Dictionary<(bool IsContender, int Id), float> _tireDustNextTime = new();
|
|
private readonly Dictionary<(bool IsContender, int Id), float> _thermalVentNextTime = new();
|
|
private readonly Dictionary<(bool IsContender, int Id), float> _damageSmokeNextTime = new();
|
|
private float _vfxClock;
|
|
|
|
private readonly Dictionary<int, ActorVisual> _contenderVisuals = new();
|
|
private readonly Dictionary<int, ActorVisual> _spectatorVisuals = new();
|
|
private readonly Dictionary<int, DropVisualEntry> _dropVisuals = new();
|
|
|
|
private StandardMaterial3D _safeRingMat = null!;
|
|
private StandardMaterial3D _coverMat = null!;
|
|
private StandardMaterial3D _bermMat = null!;
|
|
private StandardMaterial3D _imperialBeaconMat = null!;
|
|
private MatchState? _builtForState;
|
|
private float _lastSafeRingRadius = -1f;
|
|
|
|
public VehicleAssetCache AssetCache => _assetCache;
|
|
public DebrisSystem Debris => _debris;
|
|
public ParticlePool Particles => _particles;
|
|
internal MultiMeshPartPool PartPool => _partPool;
|
|
internal PomDecalPool PomDecals => _pomDecals;
|
|
|
|
public VehicleVisualInstance? GetContenderVisual(int contenderId) =>
|
|
_contenderVisuals.TryGetValue(contenderId, out var vis) ? vis.Visual : null;
|
|
|
|
public VehicleVisualInstance? GetSpectatorVisual(int spectatorId) =>
|
|
_spectatorVisuals.TryGetValue(spectatorId, out var vis) ? vis.Visual : null;
|
|
|
|
internal float? GetSpectatorHaloLocalY(int spectatorId) =>
|
|
_spectatorVisuals.TryGetValue(spectatorId, out var vis) && vis.HaloMesh is not null
|
|
? vis.HaloMesh.Position.Y
|
|
: null;
|
|
|
|
internal int ActiveDropVisualCount => _dropVisuals.Count;
|
|
internal int TransientVfxCount => _transientVfx.Count;
|
|
|
|
internal bool TryGetChassisDropVisual(int dropId, out VehicleVisualInstance? visual, out float baseY)
|
|
{
|
|
if (_dropVisuals.TryGetValue(dropId, out var entry) && entry.ChassisVisual is not null)
|
|
{
|
|
visual = entry.ChassisVisual;
|
|
baseY = entry.BaseY;
|
|
return true;
|
|
}
|
|
visual = null;
|
|
baseY = 0f;
|
|
return false;
|
|
}
|
|
|
|
public override void _Ready()
|
|
{
|
|
_assetCache = new VehicleAssetCache();
|
|
|
|
_staticGroup = new Node3D { Name = "StaticArena" };
|
|
_actorsGroup = new Node3D { Name = "Actors" };
|
|
_partPool = new MultiMeshPartPool { Name = "PartPool" };
|
|
_pomDecals = new PomDecalPool { Name = "PomDecals" };
|
|
_debris = new DebrisSystem { Name = "Debris" };
|
|
_particles = new ParticlePool { Name = "ParticlePool" };
|
|
AddChild(_staticGroup);
|
|
AddChild(_actorsGroup);
|
|
AddChild(_partPool);
|
|
AddChild(_pomDecals);
|
|
AddChild(_debris);
|
|
AddChild(_particles);
|
|
|
|
_coverMat = new StandardMaterial3D { AlbedoColor = CoverRust, Roughness = 0.88f, Metallic = 0.25f };
|
|
_bermMat = new StandardMaterial3D { AlbedoColor = BermDark, Roughness = 0.94f, Metallic = 0.1f };
|
|
_imperialBeaconMat = new StandardMaterial3D
|
|
{
|
|
AlbedoColor = new Color(TouristGold.R, TouristGold.G, TouristGold.B, 0.82f),
|
|
Roughness = 0.35f,
|
|
Metallic = 0.8f,
|
|
EmissionEnabled = true,
|
|
Emission = TouristGold,
|
|
EmissionEnergyMultiplier = 1.4f,
|
|
Transparency = BaseMaterial3D.TransparencyEnum.Alpha
|
|
};
|
|
|
|
_safeRingImm = new ImmediateMesh();
|
|
_safeRingMat = new StandardMaterial3D
|
|
{
|
|
AlbedoColor = new Color(1.0f, 0.38f, 0.14f, 0.78f),
|
|
EmissionEnabled = true,
|
|
Emission = new Color(1.0f, 0.32f, 0.08f),
|
|
EmissionEnergyMultiplier = 1.6f,
|
|
Transparency = BaseMaterial3D.TransparencyEnum.Alpha,
|
|
ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded,
|
|
CullMode = BaseMaterial3D.CullModeEnum.Disabled
|
|
};
|
|
_safeRingMesh = new MeshInstance3D
|
|
{
|
|
Name = "SafeRing",
|
|
Mesh = _safeRingImm,
|
|
MaterialOverride = _safeRingMat,
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
|
};
|
|
AddChild(_safeRingMesh);
|
|
|
|
InitPooledVfxAndBeacons();
|
|
}
|
|
|
|
private void InitPooledVfxAndBeacons()
|
|
{
|
|
var vfxShader = new Shader
|
|
{
|
|
Code = """
|
|
shader_type spatial;
|
|
render_mode unshaded, cull_back, blend_mix, depth_draw_never;
|
|
varying vec4 v_inst;
|
|
void vertex() {
|
|
v_inst = INSTANCE_CUSTOM;
|
|
}
|
|
void fragment() {
|
|
ALBEDO = max(v_inst.rgb, vec3(0.0));
|
|
ALPHA = clamp(v_inst.a, 0.0, 1.0);
|
|
}
|
|
"""
|
|
};
|
|
_vfxMat = new ShaderMaterial { Shader = vfxShader };
|
|
|
|
_vfxBeamMm = new MultiMesh
|
|
{
|
|
TransformFormat = MultiMesh.TransformFormatEnum.Transform3D,
|
|
UseCustomData = true,
|
|
Mesh = new BoxMesh { Size = Vector3.One },
|
|
InstanceCount = MaxVfxBeams,
|
|
VisibleInstanceCount = 0
|
|
};
|
|
_vfxBeamMmi = new MultiMeshInstance3D
|
|
{
|
|
Name = "VfxBeams",
|
|
Multimesh = _vfxBeamMm,
|
|
MaterialOverride = _vfxMat,
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off,
|
|
CustomAabb = new Aabb(new Vector3(-1600f, -60f, -1600f), new Vector3(3200f, 450f, 3200f)),
|
|
PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off,
|
|
Visible = false
|
|
};
|
|
AddChild(_vfxBeamMmi);
|
|
|
|
_vfxSphereMm = new MultiMesh
|
|
{
|
|
TransformFormat = MultiMesh.TransformFormatEnum.Transform3D,
|
|
UseCustomData = true,
|
|
Mesh = new SphereMesh { Radius = 0.5f, Height = 1.0f, RadialSegments = 10, Rings = 5 },
|
|
InstanceCount = MaxVfxSpheres,
|
|
VisibleInstanceCount = 0
|
|
};
|
|
_vfxSphereMmi = new MultiMeshInstance3D
|
|
{
|
|
Name = "VfxSpheres",
|
|
Multimesh = _vfxSphereMm,
|
|
MaterialOverride = _vfxMat,
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off,
|
|
CustomAabb = new Aabb(new Vector3(-1600f, -60f, -1600f), new Vector3(3200f, 450f, 3200f)),
|
|
PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off,
|
|
Visible = false
|
|
};
|
|
AddChild(_vfxSphereMmi);
|
|
|
|
_vfxRingMm = new MultiMesh
|
|
{
|
|
TransformFormat = MultiMesh.TransformFormatEnum.Transform3D,
|
|
UseCustomData = true,
|
|
Mesh = new TorusMesh { InnerRadius = 0.38f, OuterRadius = 0.50f, Rings = 16, RingSegments = 6 },
|
|
InstanceCount = MaxVfxRings,
|
|
VisibleInstanceCount = 0
|
|
};
|
|
_vfxRingMmi = new MultiMeshInstance3D
|
|
{
|
|
Name = "VfxRings",
|
|
Multimesh = _vfxRingMm,
|
|
MaterialOverride = _vfxMat,
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off,
|
|
CustomAabb = new Aabb(new Vector3(-1600f, -60f, -1600f), new Vector3(3200f, 450f, 3200f)),
|
|
PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off,
|
|
Visible = false
|
|
};
|
|
AddChild(_vfxRingMmi);
|
|
|
|
_dropDiscMm = new MultiMesh
|
|
{
|
|
TransformFormat = MultiMesh.TransformFormatEnum.Transform3D,
|
|
UseCustomData = true,
|
|
Mesh = new CylinderMesh { TopRadius = 0.72f, BottomRadius = 0.85f, Height = 0.14f, RadialSegments = 14 },
|
|
InstanceCount = 128,
|
|
VisibleInstanceCount = 0
|
|
};
|
|
_dropDiscMmi = new MultiMeshInstance3D
|
|
{
|
|
Name = "DropBeaconDiscs",
|
|
Multimesh = _dropDiscMm,
|
|
MaterialOverride = _vfxMat,
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off,
|
|
CustomAabb = new Aabb(new Vector3(-1600f, -60f, -1600f), new Vector3(3200f, 450f, 3200f)),
|
|
PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off,
|
|
Visible = false
|
|
};
|
|
AddChild(_dropDiscMmi);
|
|
|
|
_dropPillarMm = new MultiMesh
|
|
{
|
|
TransformFormat = MultiMesh.TransformFormatEnum.Transform3D,
|
|
UseCustomData = false,
|
|
Mesh = new CylinderMesh { TopRadius = 0.06f, BottomRadius = 0.22f, Height = 5.5f, RadialSegments = 8 },
|
|
InstanceCount = 64,
|
|
VisibleInstanceCount = 0
|
|
};
|
|
_dropPillarMmi = new MultiMeshInstance3D
|
|
{
|
|
Name = "ImperialDropPillars",
|
|
Multimesh = _dropPillarMm,
|
|
MaterialOverride = _imperialBeaconMat,
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off,
|
|
CustomAabb = new Aabb(new Vector3(-1600f, -60f, -1600f), new Vector3(3200f, 450f, 3200f)),
|
|
PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off,
|
|
Visible = false
|
|
};
|
|
AddChild(_dropPillarMmi);
|
|
}
|
|
|
|
public override void _ExitTree()
|
|
{
|
|
_assetCache?.Dispose();
|
|
}
|
|
|
|
public void Sync(IClientArenaSession session, float elapsedSeconds, float dt = 1f / 60f, float alpha = 1f)
|
|
{
|
|
var state = session.ViewState;
|
|
if (!ReferenceEquals(_builtForState, state))
|
|
{
|
|
RebuildStaticArena(state);
|
|
ClearDynamicVisuals();
|
|
_builtForState = state;
|
|
}
|
|
|
|
Camera3D? cam = GetViewport()?.GetCamera3D();
|
|
bool hasCam = cam is not null && cam.IsInsideTree();
|
|
Vector3 camPos = hasCam ? cam!.GlobalPosition : Vector3.Zero;
|
|
|
|
_partPool.BeginFrame(hasCam ? cam : null);
|
|
UpdateSafeCircleRing(state.Terrain, state.Spectators.SafeRadius);
|
|
SyncContenders(session, dt, alpha, hasCam, camPos);
|
|
SyncSpectators(session, elapsedSeconds, dt, alpha, hasCam, camPos);
|
|
SyncGroundDrops(state, elapsedSeconds, hasCam, camPos);
|
|
_debris.Step(dt, state.Terrain);
|
|
_debris.SubmitToPool(_partPool);
|
|
_partPool.EndFrame();
|
|
|
|
SyncTracerBeams(state);
|
|
}
|
|
|
|
public void ConsumeEvents(
|
|
IClientArenaSession session,
|
|
IReadOnlyList<ISimEvent> events,
|
|
IReadOnlyList<FootstepPulse> footsteps,
|
|
float dt,
|
|
Func<float, float, Color> sampleGroundTint)
|
|
{
|
|
Camera3D? cam = GetViewport()?.GetCamera3D();
|
|
bool hasCam = cam is not null && cam.IsInsideTree();
|
|
Vector3 camPos = hasCam ? cam!.GlobalPosition : Vector3.Zero;
|
|
|
|
float stepDt = float.IsFinite(dt) && dt > 0f ? Math.Clamp(dt, 0f, 0.05f) : 0f;
|
|
_vfxClock += stepDt;
|
|
|
|
var state = session.ViewState;
|
|
_pomDecals.ConsumeEvents(session, events, stepDt, ResolveEntityVisual);
|
|
_particles.ConsumeEvents(session, events, camPos, hasCam, sampleGroundTint);
|
|
|
|
for (int i = 0; i < footsteps.Count; i++)
|
|
{
|
|
FootstepPulse pulse = footsteps[i];
|
|
Color gTint = sampleGroundTint(pulse.WorldPos.X, pulse.WorldPos.Z);
|
|
_particles.EmitFootstepDust(in pulse, gTint, camPos, hasCam);
|
|
}
|
|
|
|
EmitContinuousActorParticles(session, hasCam, camPos, sampleGroundTint);
|
|
UpdateExplosiveRingsAndCores(events, stepDt);
|
|
_particles.AdvanceAndCommit(stepDt);
|
|
}
|
|
|
|
private VehicleVisualInstance? ResolveEntityVisual(bool isContender, int id) =>
|
|
isContender ? GetContenderVisual(id) : GetSpectatorVisual(id);
|
|
|
|
private void RebuildStaticArena(MatchState state)
|
|
{
|
|
ClearChildren(_staticGroup);
|
|
|
|
var quadArrays = new SurfaceTool[4];
|
|
for (int i = 0; i < state.ArenaCoverBoxes.Count; i++)
|
|
{
|
|
var b = state.ArenaCoverBoxes[i];
|
|
int qIdx = (b.X >= 0f ? 1 : 0) | (b.Z >= 0f ? 2 : 0);
|
|
var st = quadArrays[qIdx];
|
|
if (st is null)
|
|
{
|
|
st = new SurfaceTool();
|
|
st.Begin(Mesh.PrimitiveType.Triangles);
|
|
quadArrays[qIdx] = st;
|
|
}
|
|
AppendBoxToSurfaceTool(st, new Vector3(b.X, b.Y, b.Z), new Vector3(b.HalfX * 2f, b.HalfY * 2f, b.HalfZ * 2f));
|
|
}
|
|
|
|
for (int q = 0; q < 4; q++)
|
|
{
|
|
if (quadArrays[q] is null) continue;
|
|
quadArrays[q].GenerateNormals();
|
|
_staticGroup.AddChild(new MeshInstance3D
|
|
{
|
|
Name = $"CoverQuad_{q}",
|
|
Mesh = quadArrays[q].Commit(),
|
|
MaterialOverride = _coverMat
|
|
});
|
|
}
|
|
|
|
if (state.ArenaBoundaryBoxes.Count > 0)
|
|
{
|
|
var bermSt = new SurfaceTool();
|
|
bermSt.Begin(Mesh.PrimitiveType.Triangles);
|
|
for (int i = 0; i < state.ArenaBoundaryBoxes.Count; i++)
|
|
{
|
|
var b = state.ArenaBoundaryBoxes[i];
|
|
AppendBoxToSurfaceTool(bermSt, new Vector3(b.X, b.Y, b.Z), new Vector3(b.HalfX * 2f, b.HalfY * 2f, b.HalfZ * 2f));
|
|
}
|
|
bermSt.GenerateNormals();
|
|
_staticGroup.AddChild(new MeshInstance3D
|
|
{
|
|
Name = "BoundaryBerms",
|
|
Mesh = bermSt.Commit(),
|
|
MaterialOverride = _bermMat,
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
|
});
|
|
}
|
|
}
|
|
|
|
private static void AppendBoxToSurfaceTool(SurfaceTool st, Vector3 center, Vector3 size)
|
|
{
|
|
Vector3 h = size * 0.5f;
|
|
Vector3 p000 = center + new Vector3(-h.X, -h.Y, -h.Z);
|
|
Vector3 p100 = center + new Vector3( h.X, -h.Y, -h.Z);
|
|
Vector3 p110 = center + new Vector3( h.X, h.Y, -h.Z);
|
|
Vector3 p010 = center + new Vector3(-h.X, h.Y, -h.Z);
|
|
Vector3 p001 = center + new Vector3(-h.X, -h.Y, h.Z);
|
|
Vector3 p101 = center + new Vector3( h.X, -h.Y, h.Z);
|
|
Vector3 p111 = center + new Vector3( h.X, h.Y, h.Z);
|
|
Vector3 p011 = center + new Vector3(-h.X, h.Y, h.Z);
|
|
|
|
AddQuad(st, p001, p101, p111, p011);
|
|
AddQuad(st, p100, p000, p010, p110);
|
|
AddQuad(st, p101, p100, p110, p111);
|
|
AddQuad(st, p000, p001, p011, p010);
|
|
AddQuad(st, p011, p111, p110, p010);
|
|
AddQuad(st, p000, p100, p101, p001);
|
|
}
|
|
|
|
private static void AddQuad(SurfaceTool st, Vector3 a, Vector3 b, Vector3 c, Vector3 d)
|
|
{
|
|
st.AddVertex(a); st.AddVertex(b); st.AddVertex(c);
|
|
st.AddVertex(a); st.AddVertex(c); st.AddVertex(d);
|
|
}
|
|
|
|
private void ClearDynamicVisuals()
|
|
{
|
|
ClearChildren(_actorsGroup);
|
|
_partPool.ClearAll();
|
|
_pomDecals.ClearAll();
|
|
_debris.ClearAll();
|
|
_particles.ClearAll();
|
|
_transientVfx.Clear();
|
|
_tireDustNextTime.Clear();
|
|
_thermalVentNextTime.Clear();
|
|
_damageSmokeNextTime.Clear();
|
|
_vfxClock = 0f;
|
|
_activeBeamCount = 0;
|
|
if (_vfxBeamMm is not null)
|
|
{
|
|
_vfxBeamMm.VisibleInstanceCount = 0;
|
|
_vfxBeamMmi.Visible = false;
|
|
}
|
|
if (_vfxSphereMm is not null)
|
|
{
|
|
_vfxSphereMm.VisibleInstanceCount = 0;
|
|
_vfxSphereMmi.Visible = false;
|
|
}
|
|
if (_vfxRingMm is not null)
|
|
{
|
|
_vfxRingMm.VisibleInstanceCount = 0;
|
|
_vfxRingMmi.Visible = false;
|
|
}
|
|
_contenderVisuals.Clear();
|
|
_spectatorVisuals.Clear();
|
|
foreach (var kv in _dropVisuals)
|
|
{
|
|
if (kv.Value.ChassisVisual is { } cv && GodotObject.IsInstanceValid(cv.Root))
|
|
{
|
|
cv.Root.QueueFree();
|
|
}
|
|
}
|
|
_dropVisuals.Clear();
|
|
_lastSafeRingRadius = -1f;
|
|
}
|
|
|
|
private void UpdateSafeCircleRing(TerrainField terrain, float safeRadius)
|
|
{
|
|
if (_lastSafeRingRadius >= 0f && MathF.Abs(safeRadius - _lastSafeRingRadius) < 0.15f)
|
|
{
|
|
return;
|
|
}
|
|
_lastSafeRingRadius = safeRadius;
|
|
|
|
_safeRingImm.ClearSurfaces();
|
|
_safeRingImm.SurfaceBegin(Mesh.PrimitiveType.Triangles);
|
|
|
|
const int segments = 160;
|
|
float halfThick = terrain.MapSize > 2000f ? 2.2f : 1.1f;
|
|
float rInner = MathF.Max(2f, safeRadius - halfThick);
|
|
float rOuter = safeRadius + halfThick;
|
|
|
|
for (int i = 0; i < segments; i++)
|
|
{
|
|
float a0 = (i / (float)segments) * MathF.Tau;
|
|
float a1 = ((i + 1) / (float)segments) * MathF.Tau;
|
|
float c0 = MathF.Cos(a0), s0 = MathF.Sin(a0);
|
|
float c1 = MathF.Cos(a1), s1 = MathF.Sin(a1);
|
|
|
|
float x0i = c0 * rInner, z0i = s0 * rInner;
|
|
float x0o = c0 * rOuter, z0o = s0 * rOuter;
|
|
float x1i = c1 * rInner, z1i = s1 * rInner;
|
|
float x1o = c1 * rOuter, z1o = s1 * rOuter;
|
|
|
|
float y0 = terrain.SampleHeight(c0 * safeRadius, s0 * safeRadius) + 0.42f;
|
|
float y1 = terrain.SampleHeight(c1 * safeRadius, s1 * safeRadius) + 0.42f;
|
|
|
|
var p0i = new Vector3(x0i, y0, z0i);
|
|
var p0o = new Vector3(x0o, y0, z0o);
|
|
var p1i = new Vector3(x1i, y1, z1i);
|
|
var p1o = new Vector3(x1o, y1, z1o);
|
|
|
|
_safeRingImm.SurfaceAddVertex(p0i);
|
|
_safeRingImm.SurfaceAddVertex(p0o);
|
|
_safeRingImm.SurfaceAddVertex(p1o);
|
|
|
|
_safeRingImm.SurfaceAddVertex(p0i);
|
|
_safeRingImm.SurfaceAddVertex(p1o);
|
|
_safeRingImm.SurfaceAddVertex(p1i);
|
|
}
|
|
|
|
_safeRingImm.SurfaceEnd();
|
|
}
|
|
|
|
private void SyncContenders(IClientArenaSession session, float dt, float alpha, bool hasCam, Vector3 camPos)
|
|
{
|
|
var contenders = session.ViewState.Contenders;
|
|
int localId = session.LocalContenderId;
|
|
|
|
for (int i = 0; i < contenders.Count; i++)
|
|
{
|
|
var c = contenders[i];
|
|
if (!c.Alive || !session.IsContenderVisible(c.Id))
|
|
{
|
|
if (_contenderVisuals.TryGetValue(c.Id, out var hiddenVis))
|
|
{
|
|
if (!c.Alive)
|
|
{
|
|
hiddenVis.Visual.SyncDamageAndHeat(c.Grid, _debris, Vector3.Zero);
|
|
}
|
|
hiddenVis.Visual.Root.Visible = false;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (!_contenderVisuals.TryGetValue(c.Id, out var vis) || vis.Visual.ChassisId != c.Grid.Spec.Id)
|
|
{
|
|
if (vis is not null)
|
|
{
|
|
vis.Visual.Root.QueueFree();
|
|
}
|
|
|
|
var role = c.Id == localId ? VehicleVisualRole.Player : VehicleVisualRole.Rival;
|
|
var instance = VehicleVisualInstance.Create(_assetCache, c.Grid.Spec.Id, role);
|
|
instance.SyncMountedGear(c.Grid);
|
|
_actorsGroup.AddChild(instance.Root);
|
|
vis = new ActorVisual { Visual = instance };
|
|
_contenderVisuals[c.Id] = vis;
|
|
}
|
|
|
|
vis.Visual.Root.Visible = true;
|
|
|
|
var (interpPos, interpRot, interpVel) = session.GetInterpolatedContenderPose(c.Id, alpha);
|
|
Vector3 pos = new Vector3(interpPos.X, interpPos.Y, interpPos.Z);
|
|
Vector3 vel = new Vector3(interpVel.X, interpVel.Y, interpVel.Z);
|
|
var q = new Quaternion(interpRot.X, interpRot.Y, interpRot.Z, interpRot.W);
|
|
vis.Visual.Root.Transform = new Transform3D(new Basis(q), pos);
|
|
|
|
float distSq = hasCam ? pos.DistanceSquaredTo(camPos) : 0f;
|
|
bool castShadows = !hasCam || c.Id == localId || distSq <= 55f * 55f;
|
|
bool useStaticLod = hasCam && c.Id != localId && !vis.Visual.HasAnyStrippedCell &&
|
|
(vis.Visual.IsStaticLod ? distSq > 64f * 64f : distSq > 68f * 68f);
|
|
vis.Visual.SetLodState(useStaticLod, castShadows);
|
|
|
|
vis.Visual.SyncMountedGear(c.Grid);
|
|
if (!useStaticLod)
|
|
{
|
|
if (!hasCam || c.Id == localId || distSq <= 55f * 55f)
|
|
{
|
|
vis.RigDtAccum = 0f;
|
|
vis.RigSkipFrames = 0;
|
|
if (c.Grid.Spec.IsWalker && session.TrySampleInterpolatedWalkerPose(true, c.Id, alpha, out var walkerSnap))
|
|
{
|
|
VehicleRigAnimator.Update(vis.Visual, c, vel, vis.Visual.Root.Basis, dt, in walkerSnap);
|
|
}
|
|
else
|
|
{
|
|
VehicleRigAnimator.Update(vis.Visual, c, vel, vis.Visual.Root.Basis, dt);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
vis.RigDtAccum += dt;
|
|
vis.RigSkipFrames++;
|
|
if (vis.RigSkipFrames >= 2)
|
|
{
|
|
if (c.Grid.Spec.IsWalker && session.TrySampleInterpolatedWalkerPose(true, c.Id, alpha, out var walkerSnap))
|
|
{
|
|
VehicleRigAnimator.Update(vis.Visual, c, vel, vis.Visual.Root.Basis, vis.RigDtAccum, in walkerSnap);
|
|
}
|
|
else
|
|
{
|
|
VehicleRigAnimator.Update(vis.Visual, c, vel, vis.Visual.Root.Basis, vis.RigDtAccum);
|
|
}
|
|
vis.RigDtAccum = 0f;
|
|
vis.RigSkipFrames = 0;
|
|
}
|
|
}
|
|
}
|
|
vis.Visual.SyncDamageAndHeat(c.Grid, _debris, vel);
|
|
vis.Visual.SubmitToPool(_partPool);
|
|
}
|
|
}
|
|
|
|
private void SyncSpectators(IClientArenaSession session, float elapsedSeconds, float dt, float alpha, bool hasCam, Vector3 camPos)
|
|
{
|
|
var specs = session.ViewState.Spectators.Spectators;
|
|
|
|
for (int i = 0; i < specs.Count; i++)
|
|
{
|
|
var s = specs[i];
|
|
if (!s.Alive || !session.IsSpectatorVisible(s.Id))
|
|
{
|
|
if (_spectatorVisuals.TryGetValue(s.Id, out var hiddenVis))
|
|
{
|
|
if (!s.Alive)
|
|
{
|
|
hiddenVis.Visual.SyncDamageAndHeat(s.Grid, _debris, Vector3.Zero);
|
|
}
|
|
hiddenVis.Visual.Root.Visible = false;
|
|
if (hiddenVis.LaserMesh is not null)
|
|
{
|
|
hiddenVis.LaserMesh.Visible = false;
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (!_spectatorVisuals.TryGetValue(s.Id, out var vis))
|
|
{
|
|
var role = s.Tier switch
|
|
{
|
|
SpectatorTier.Tourist => VehicleVisualRole.SpectatorTourist,
|
|
SpectatorTier.Patron => VehicleVisualRole.SpectatorPatron,
|
|
_ => VehicleVisualRole.SpectatorExemplar
|
|
};
|
|
var instance = VehicleVisualInstance.Create(
|
|
_assetCache,
|
|
s.Grid.Spec.Id,
|
|
role,
|
|
visualScale: 1f,
|
|
targetHalfHeight: s.Grid.Spec.HalfHeight);
|
|
instance.SyncMountedGear(s.Grid);
|
|
_actorsGroup.AddChild(instance.Root);
|
|
vis = CreateSpectatorVisualWrapper(instance);
|
|
_spectatorVisuals[s.Id] = vis;
|
|
}
|
|
|
|
vis.Visual.Root.Visible = true;
|
|
var (interpPos, interpRot, interpVel) = session.GetInterpolatedSpectatorPose(s.Id, alpha);
|
|
Vector3 specPos = new Vector3(interpPos.X, interpPos.Y, interpPos.Z);
|
|
Vector3 vel = new Vector3(interpVel.X, interpVel.Y, interpVel.Z);
|
|
var q = new Quaternion(interpRot.X, interpRot.Y, interpRot.Z, interpRot.W);
|
|
vis.Visual.Root.Transform = new Transform3D(new Basis(q), specPos);
|
|
|
|
vis.Visual.SyncDamageAndHeat(s.Grid, _debris, vel);
|
|
|
|
bool useStaticLod = false;
|
|
bool castShadows = true;
|
|
if (hasCam)
|
|
{
|
|
float distSq = specPos.DistanceSquaredTo(camPos);
|
|
castShadows = distSq <= 55f * 55f;
|
|
if (!vis.Visual.HasAnyStrippedCell)
|
|
{
|
|
useStaticLod = vis.Visual.IsStaticLod ? distSq > 50f * 50f : distSq > 55f * 55f;
|
|
}
|
|
}
|
|
vis.Visual.SetLodState(useStaticLod, castShadows);
|
|
|
|
ContenderState? target = s.TargetContenderId.HasValue &&
|
|
s.TargetContenderId.Value >= 0 &&
|
|
s.TargetContenderId.Value < session.ViewState.Contenders.Count
|
|
? session.ViewState.Contenders[s.TargetContenderId.Value]
|
|
: null;
|
|
|
|
if (!useStaticLod)
|
|
{
|
|
float aimYawWorld = s.Yaw;
|
|
float aimPitchDeg = 0f;
|
|
if (s.Stance != SpectatorStance.Neutral && target is { Alive: true } &&
|
|
(target.Id == session.LocalContenderId || session.IsContenderVisible(target.Id)))
|
|
{
|
|
float tdx = target.X - s.X;
|
|
float tdz = target.Z - s.Z;
|
|
if (tdx * tdx + tdz * tdz > 0.01f)
|
|
{
|
|
aimYawWorld = MathF.Atan2(tdx, tdz);
|
|
float horiz = MathF.Sqrt(tdx * tdx + tdz * tdz);
|
|
float eyeY = MatchRules.GetObserverEyeY(s.Y, s.Grid.Spec.HalfHeight);
|
|
float targetY = MatchRules.GetTargetAimY(target.Y, target.Grid.Spec.HalfHeight);
|
|
aimPitchDeg = Mathf.RadToDeg(MathF.Atan2(targetY - eyeY, horiz));
|
|
}
|
|
}
|
|
|
|
var rigInput = new VehicleRigDriveInput(
|
|
Velocity: vel,
|
|
Yaw: s.Yaw,
|
|
AimYawWorld: aimYawWorld,
|
|
AimPitchDeg: aimPitchDeg,
|
|
SteerCmd: 0f,
|
|
ThrottleCmd: vel.Length() > 0.5f ? 0.6f : 0f,
|
|
VerticalThrottleCmd: 0f,
|
|
EngineThermalPerf: s.Grid.ComputeEngineThermalPerformance(),
|
|
DeltaTime: dt,
|
|
IsGrounded: !s.Grid.Spec.IsVtol);
|
|
|
|
vis.Visual.SyncMountedGear(s.Grid);
|
|
if (s.Grid.Spec.IsWalker && session.TrySampleInterpolatedWalkerPose(false, s.Id, alpha, out var walkerSnap))
|
|
{
|
|
VehicleRigAnimator.Update(vis.Visual, in rigInput, vis.Visual.Root.Basis, in walkerSnap);
|
|
}
|
|
else
|
|
{
|
|
VehicleRigAnimator.Update(vis.Visual, in rigInput, vis.Visual.Root.Basis);
|
|
}
|
|
}
|
|
|
|
vis.Visual.SubmitToPool(_partPool);
|
|
|
|
// Status halo
|
|
if (vis.HaloMat is not null && vis.HaloMesh is not null &&
|
|
(vis.LastHaloStance != s.Stance || s.Stance == SpectatorStance.Provoked))
|
|
{
|
|
vis.LastHaloStance = s.Stance;
|
|
if (s.Stance == SpectatorStance.Neutral)
|
|
{
|
|
vis.HaloMat.AlbedoColor = new Color(0.88f, 0.65f, 0.25f, 0.45f);
|
|
vis.HaloMat.Emission = new Color(0.88f, 0.65f, 0.25f);
|
|
vis.HaloMat.EmissionEnergyMultiplier = 0.6f;
|
|
}
|
|
else if (s.Stance == SpectatorStance.Cleared)
|
|
{
|
|
vis.HaloMat.AlbedoColor = new Color(1.0f, 0.22f, 0.18f, 0.9f);
|
|
vis.HaloMat.Emission = new Color(1.0f, 0.2f, 0.15f);
|
|
vis.HaloMat.EmissionEnergyMultiplier = 2.0f;
|
|
}
|
|
else
|
|
{
|
|
float pulse = 1.6f + 1.2f * MathF.Sin(elapsedSeconds * 10f);
|
|
vis.HaloMat.AlbedoColor = new Color(1.0f, 0.1f, 0.1f, 0.95f);
|
|
vis.HaloMat.Emission = new Color(1.0f, 0.08f, 0.08f);
|
|
vis.HaloMat.EmissionEnergyMultiplier = pulse;
|
|
}
|
|
}
|
|
|
|
// Lock-on laser beam when Cleared or Provoked
|
|
if (vis.LaserMesh is not null)
|
|
{
|
|
if (s.Stance != SpectatorStance.Neutral && s.HasTargetLos && target is { Alive: true } &&
|
|
(target.Id == session.LocalContenderId || session.IsContenderVisible(target.Id)))
|
|
{
|
|
Vector3 start = specPos + new Vector3(0f, s.Grid.Spec.HalfHeight * 0.7f, 0f);
|
|
var (targetPos, _, _) = session.GetInterpolatedContenderPose(target.Id, alpha);
|
|
Vector3 end = new Vector3(targetPos.X, targetPos.Y + target.Grid.Spec.HalfHeight * 0.3f, targetPos.Z);
|
|
float dist = start.DistanceTo(end);
|
|
if (dist > 0.5f)
|
|
{
|
|
if (!vis.LastLaserVisible)
|
|
{
|
|
vis.LastLaserVisible = true;
|
|
vis.LaserMesh.Visible = true;
|
|
}
|
|
vis.LaserMesh.GlobalPosition = (start + end) * 0.5f;
|
|
Vector3 dir = (end - start).Normalized();
|
|
Vector3 up = MathF.Abs(dir.Dot(Vector3.Up)) > 0.99f ? Vector3.Right : Vector3.Up;
|
|
vis.LaserMesh.LookAt(end, up);
|
|
vis.LaserMesh.Scale = new Vector3(1f, 1f, dist);
|
|
}
|
|
else if (vis.LastLaserVisible)
|
|
{
|
|
vis.LastLaserVisible = false;
|
|
vis.LaserMesh.Visible = false;
|
|
}
|
|
}
|
|
else if (vis.LastLaserVisible)
|
|
{
|
|
vis.LastLaserVisible = false;
|
|
vis.LaserMesh.Visible = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private ActorVisual CreateSpectatorVisualWrapper(VehicleVisualInstance instance)
|
|
{
|
|
float haloY = MathF.Max(2.4f, instance.ComputeMountedMaxY() + 1.15f);
|
|
var haloMat = new StandardMaterial3D
|
|
{
|
|
ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded,
|
|
EmissionEnabled = true,
|
|
Transparency = BaseMaterial3D.TransparencyEnum.Alpha
|
|
};
|
|
var halo = new MeshInstance3D
|
|
{
|
|
Mesh = new CylinderMesh { TopRadius = 2.4f, BottomRadius = 2.4f, Height = 0.1f, RadialSegments = 16 },
|
|
MaterialOverride = haloMat,
|
|
Position = new Vector3(0f, haloY, 0f),
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
|
};
|
|
instance.Root.AddChild(halo);
|
|
|
|
var laserMat = new StandardMaterial3D
|
|
{
|
|
AlbedoColor = new Color(1f, 0.15f, 0.15f, 0.85f),
|
|
EmissionEnabled = true,
|
|
Emission = new Color(1f, 0.12f, 0.1f),
|
|
EmissionEnergyMultiplier = 2.2f,
|
|
ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded,
|
|
Transparency = BaseMaterial3D.TransparencyEnum.Alpha
|
|
};
|
|
var laser = new MeshInstance3D
|
|
{
|
|
Mesh = new BoxMesh { Size = new Vector3(0.08f, 0.08f, 1.0f) },
|
|
MaterialOverride = laserMat,
|
|
Visible = false,
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
|
};
|
|
_actorsGroup.AddChild(laser);
|
|
|
|
return new ActorVisual
|
|
{
|
|
Visual = instance,
|
|
HaloMesh = halo,
|
|
HaloMat = haloMat,
|
|
LaserMesh = laser
|
|
};
|
|
}
|
|
|
|
private void SyncGroundDrops(MatchState state, float elapsedSeconds, bool hasCam, Vector3 camPos)
|
|
{
|
|
int discCount = 0;
|
|
int pillarCount = 0;
|
|
|
|
for (int i = 0; i < state.GroundDrops.Count; i++)
|
|
{
|
|
var d = state.GroundDrops[i];
|
|
if (d.PickedUp)
|
|
{
|
|
if (_dropVisuals.TryGetValue(d.Id, out var pickedEntry))
|
|
{
|
|
if (pickedEntry.ChassisVisual is { } cv && GodotObject.IsInstanceValid(cv.Root))
|
|
{
|
|
cv.Root.GetParent()?.RemoveChild(cv.Root);
|
|
cv.Root.QueueFree();
|
|
}
|
|
_dropVisuals.Remove(d.Id);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (!_dropVisuals.TryGetValue(d.Id, out var entry))
|
|
{
|
|
state.Terrain.MapWorldToTile(d.X, d.Z, out int tx, out int tz);
|
|
float baseY = state.Terrain.SampleHeight(d.X, d.Z);
|
|
if (d.Module is { } mod)
|
|
{
|
|
VehicleVisualInstance.ResolveDropGearVisual(_assetCache, mod, out ArrayMesh gMesh, out Vector3 gScale, out float gYOff);
|
|
entry = new DropVisualEntry
|
|
{
|
|
IsModule = true,
|
|
IsImperial = mod.IsImperial,
|
|
GearMesh = gMesh,
|
|
GearScale = gScale * 1.45f,
|
|
GearYOffset = gYOff * 1.45f + 0.35f,
|
|
BaseY = baseY,
|
|
TileX = tx,
|
|
TileZ = tz,
|
|
TileVersion = state.Terrain.TileVersions[tx, tz]
|
|
};
|
|
}
|
|
else
|
|
{
|
|
var spec = d.Chassis!;
|
|
var vis = VehicleVisualInstance.Create(
|
|
_assetCache,
|
|
spec.Id,
|
|
VehicleVisualRole.SalvageDrop,
|
|
visualScale: 0.72f);
|
|
vis.Root.Position = new Vector3(d.X, baseY + spec.HalfHeight * 0.72f, d.Z);
|
|
_actorsGroup.AddChild(vis.Root);
|
|
entry = new DropVisualEntry
|
|
{
|
|
IsModule = false,
|
|
ChassisVisual = vis,
|
|
BaseY = baseY,
|
|
TileX = tx,
|
|
TileZ = tz,
|
|
TileVersion = state.Terrain.TileVersions[tx, tz]
|
|
};
|
|
}
|
|
_dropVisuals[d.Id] = entry;
|
|
}
|
|
else
|
|
{
|
|
int ver = state.Terrain.TileVersions[entry.TileX, entry.TileZ];
|
|
if (ver != entry.TileVersion)
|
|
{
|
|
entry.TileVersion = ver;
|
|
entry.BaseY = state.Terrain.SampleHeight(d.X, d.Z);
|
|
if (entry.ChassisVisual is { } cv)
|
|
{
|
|
cv.Root.Position = new Vector3(d.X, entry.BaseY + d.Chassis!.HalfHeight * 0.72f, d.Z);
|
|
}
|
|
}
|
|
}
|
|
|
|
float distSq = hasCam ? new Vector3(d.X, entry.BaseY, d.Z).DistanceSquaredTo(camPos) : 0f;
|
|
if (entry.IsModule)
|
|
{
|
|
if (hasCam && distSq > 240f * 240f)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float bob = MathF.Sin(elapsedSeconds * 2.5f + d.Id) * 0.12f;
|
|
float yaw = elapsedSeconds * 0.9f + d.Id;
|
|
|
|
if (entry.GearMesh is not null)
|
|
{
|
|
var gearXf = new Transform3D(
|
|
new Basis(Vector3.Up, yaw) * Basis.FromScale(entry.GearScale),
|
|
new Vector3(d.X, entry.BaseY + bob + entry.GearYOffset, d.Z));
|
|
_partPool.Submit(entry.GearMesh, in gearXf, new Color(0f, 0f, 5f, 1f), wantsShadow: false);
|
|
}
|
|
|
|
if (discCount < _dropDiscMm.InstanceCount)
|
|
{
|
|
_dropDiscMm.SetInstanceTransform(discCount, new Transform3D(Basis.Identity, new Vector3(d.X, entry.BaseY + bob + 0.08f, d.Z)));
|
|
Color dCol = entry.IsImperial
|
|
? new Color(TouristGold.R * 2.2f, TouristGold.G * 2.2f, TouristGold.B * 2.2f, 0.82f)
|
|
: new Color(OlivePaint.R * 1.5f, OlivePaint.G * 1.5f, OlivePaint.B * 1.5f, 0.72f);
|
|
_dropDiscMm.SetInstanceCustomData(discCount, dCol);
|
|
discCount++;
|
|
}
|
|
|
|
if (entry.IsImperial && pillarCount < _dropPillarMm.InstanceCount)
|
|
{
|
|
_dropPillarMm.SetInstanceTransform(pillarCount, new Transform3D(Basis.Identity, new Vector3(d.X, entry.BaseY + bob + 2.9f, d.Z)));
|
|
pillarCount++;
|
|
}
|
|
}
|
|
else if (entry.ChassisVisual is { } cv)
|
|
{
|
|
bool useFarLod = false;
|
|
bool castShadows = true;
|
|
if (hasCam)
|
|
{
|
|
useFarLod = cv.IsStaticLod ? distSq > 50f * 50f : distSq > 55f * 55f;
|
|
castShadows = distSq <= 70f * 70f;
|
|
}
|
|
cv.SetLodState(useFarLod, castShadows);
|
|
cv.SubmitToPool(_partPool);
|
|
|
|
if (discCount < _dropDiscMm.InstanceCount)
|
|
{
|
|
_dropDiscMm.SetInstanceTransform(discCount, new Transform3D(Basis.FromScale(new Vector3(2.4f, 1f, 2.4f)), new Vector3(d.X, entry.BaseY + 0.06f, d.Z)));
|
|
_dropDiscMm.SetInstanceCustomData(discCount, new Color(CyanSalvage.R * 1.8f, CyanSalvage.G * 1.8f, CyanSalvage.B * 1.8f, 0.65f));
|
|
discCount++;
|
|
}
|
|
}
|
|
}
|
|
|
|
_dropDiscMm.VisibleInstanceCount = discCount;
|
|
_dropDiscMmi.Visible = discCount > 0;
|
|
_dropPillarMm.VisibleInstanceCount = pillarCount;
|
|
_dropPillarMmi.Visible = pillarCount > 0;
|
|
}
|
|
|
|
private void SyncTracerBeams(MatchState state)
|
|
{
|
|
int beamIdx = 0;
|
|
var tracers = state.Tracers;
|
|
for (int i = 0; i < tracers.Count; i++)
|
|
{
|
|
var t = tracers[i];
|
|
Vector3 start = new(t.StartX, t.StartY, t.StartZ);
|
|
Vector3 end = new(t.EndX, t.EndY, t.EndZ);
|
|
Vector3 delta = end - start;
|
|
float dist = delta.Length();
|
|
if (dist < 0.1f)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Vector3 dir = delta / dist;
|
|
Vector3 up = MathF.Abs(dir.Dot(Vector3.Up)) > 0.95f ? Vector3.Right : Vector3.Up;
|
|
Basis lookBasis = Basis.LookingAt(dir, up);
|
|
Vector3 mid = (start + end) * 0.5f;
|
|
|
|
switch (t.WeaponClass)
|
|
{
|
|
case WeaponVisualClass.KineticLight:
|
|
{
|
|
bool isPd = string.Equals(t.WeaponId, "pd_flak", StringComparison.Ordinal);
|
|
Color col = isPd
|
|
? new Color(2.8f, 1.35f, 0.35f, 0.94f)
|
|
: new Color(2.6f, 2.35f, 1.15f, 0.92f);
|
|
EmitBeam(ref beamIdx, lookBasis, mid, new Vector3(isPd ? 0.09f : 0.06f, isPd ? 0.09f : 0.06f, dist), col);
|
|
break;
|
|
}
|
|
|
|
case WeaponVisualClass.Autocannon:
|
|
{
|
|
EmitBeam(ref beamIdx, lookBasis, mid, new Vector3(0.20f, 0.20f, dist), new Color(2.5f, 0.85f, 0.14f, 0.72f));
|
|
EmitBeam(ref beamIdx, lookBasis, mid, new Vector3(0.08f, 0.08f, dist), new Color(3.6f, 2.7f, 1.3f, 0.98f));
|
|
break;
|
|
}
|
|
|
|
case WeaponVisualClass.Explosive:
|
|
{
|
|
bool isRecoilless = string.Equals(t.WeaponId, "recoilless", StringComparison.Ordinal);
|
|
float w = isRecoilless ? 0.22f : 0.12f;
|
|
EmitBeam(ref beamIdx, lookBasis, mid, new Vector3(w, w, dist), new Color(3.2f, 1.25f, 0.25f, 0.96f));
|
|
break;
|
|
}
|
|
|
|
case WeaponVisualClass.ThermalBeam:
|
|
{
|
|
bool isLance = string.Equals(t.WeaponId, "gilded_lance", StringComparison.Ordinal);
|
|
float outerW = isLance ? 0.34f : 0.20f;
|
|
float coreW = isLance ? 0.13f : 0.08f;
|
|
Color outerCol = isLance
|
|
? new Color(2.8f, 1.95f, 0.45f, 0.62f)
|
|
: new Color(0.45f, 2.35f, 2.95f, 0.65f);
|
|
Color coreCol = isLance
|
|
? new Color(1.6f, 3.6f, 3.8f, 0.98f)
|
|
: new Color(2.4f, 3.4f, 3.8f, 0.98f);
|
|
EmitBeam(ref beamIdx, lookBasis, mid, new Vector3(outerW, outerW, dist), outerCol);
|
|
EmitBeam(ref beamIdx, lookBasis, mid, new Vector3(coreW, coreW, dist), coreCol);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
_activeBeamCount = beamIdx;
|
|
_vfxBeamMm.VisibleInstanceCount = beamIdx;
|
|
_vfxBeamMmi.Visible = beamIdx > 0;
|
|
}
|
|
|
|
private void UpdateExplosiveRingsAndCores(IReadOnlyList<ISimEvent> events, float stepDt)
|
|
{
|
|
for (int i = _transientVfx.Count - 1; i >= 0; i--)
|
|
{
|
|
TransientVfx v = _transientVfx[i];
|
|
v.Age += stepDt;
|
|
if (v.Age >= v.Lifetime)
|
|
{
|
|
_transientVfx.RemoveAt(i);
|
|
continue;
|
|
}
|
|
v.Position += v.Velocity * stepDt;
|
|
_transientVfx[i] = v;
|
|
}
|
|
|
|
for (int i = 0; i < events.Count; i++)
|
|
{
|
|
if (events[i] is not HitImpactEvent ev)
|
|
{
|
|
continue;
|
|
}
|
|
if ((WeaponVisualClass)ev.WeaponClass != WeaponVisualClass.Explosive || ev.IsAirburst)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Vector3 pos = new(ev.HitX, ev.HitY, ev.HitZ);
|
|
Vector3 normal = new(ev.NormalX, ev.NormalY, ev.NormalZ);
|
|
if (normal.LengthSquared() < 1e-4f) normal = Vector3.Up;
|
|
normal = normal.Normalized();
|
|
|
|
bool isRecoilless = string.Equals(ev.WeaponId, "recoilless", StringComparison.Ordinal);
|
|
float detSize = isRecoilless ? 3.2f : 1.7f;
|
|
float ringSize = isRecoilless ? 6.2f : 2.9f;
|
|
AddTransient(new TransientVfx
|
|
{
|
|
Shape = VfxShape.Sphere,
|
|
Position = pos + normal * 0.25f,
|
|
Velocity = normal * 1.4f,
|
|
BaseRotation = Basis.Identity,
|
|
StartScale = Vector3.One * 0.5f,
|
|
EndScale = Vector3.One * detSize,
|
|
ColorStart = new Color(3.6f, 2.0f, 0.55f, 0.92f),
|
|
ColorEnd = new Color(0.85f, 0.28f, 0.08f, 0.0f),
|
|
Age = 0f,
|
|
Lifetime = isRecoilless ? 0.28f : 0.20f
|
|
});
|
|
|
|
Vector3 upRef = MathF.Abs(normal.Y) > 0.92f ? Vector3.Forward : Vector3.Up;
|
|
Vector3 right = upRef.Cross(normal).Normalized();
|
|
Vector3 fwd = normal.Cross(right).Normalized();
|
|
Basis ringBasis = new(right, normal, fwd);
|
|
AddTransient(new TransientVfx
|
|
{
|
|
Shape = VfxShape.Ring,
|
|
Position = pos + normal * 0.18f,
|
|
Velocity = Vector3.Zero,
|
|
BaseRotation = ringBasis,
|
|
StartScale = new Vector3(0.7f, 1.0f, 0.7f),
|
|
EndScale = new Vector3(ringSize, 0.4f, ringSize),
|
|
ColorStart = new Color(3.2f, 1.65f, 0.45f, 0.92f),
|
|
ColorEnd = new Color(1.4f, 0.55f, 0.20f, 0.0f),
|
|
Age = 0f,
|
|
Lifetime = isRecoilless ? 0.32f : 0.22f
|
|
});
|
|
}
|
|
|
|
int sphereIdx = 0;
|
|
int ringIdx = 0;
|
|
for (int i = 0; i < _transientVfx.Count; i++)
|
|
{
|
|
TransientVfx v = _transientVfx[i];
|
|
float frac = Math.Clamp(v.Age / MathF.Max(0.01f, v.Lifetime), 0f, 1f);
|
|
Vector3 sc = v.StartScale.Lerp(v.EndScale, frac);
|
|
Color col = v.ColorStart.Lerp(v.ColorEnd, frac);
|
|
Transform3D xf = new(v.BaseRotation * Basis.FromScale(sc), v.Position);
|
|
|
|
if (v.Shape == VfxShape.Sphere && sphereIdx < MaxVfxSpheres)
|
|
{
|
|
_vfxSphereMm.SetInstanceTransform(sphereIdx, xf);
|
|
_vfxSphereMm.SetInstanceCustomData(sphereIdx, col);
|
|
sphereIdx++;
|
|
}
|
|
else if (v.Shape == VfxShape.Ring && ringIdx < MaxVfxRings)
|
|
{
|
|
_vfxRingMm.SetInstanceTransform(ringIdx, xf);
|
|
_vfxRingMm.SetInstanceCustomData(ringIdx, col);
|
|
ringIdx++;
|
|
}
|
|
}
|
|
|
|
_vfxSphereMm.VisibleInstanceCount = sphereIdx;
|
|
_vfxSphereMmi.Visible = sphereIdx > 0;
|
|
_vfxRingMm.VisibleInstanceCount = ringIdx;
|
|
_vfxRingMmi.Visible = ringIdx > 0;
|
|
}
|
|
|
|
private void EmitContinuousActorParticles(
|
|
IClientArenaSession session,
|
|
bool hasCam,
|
|
Vector3 camPos,
|
|
Func<float, float, Color> sampleGroundTint)
|
|
{
|
|
var state = session.ViewState;
|
|
var contenders = state.Contenders;
|
|
for (int i = 0; i < contenders.Count; i++)
|
|
{
|
|
var c = contenders[i];
|
|
if (!c.Alive || !session.IsContenderVisible(c.Id))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Vector3 pos = _contenderVisuals.TryGetValue(c.Id, out var av)
|
|
? av.Visual.Root.GlobalPosition
|
|
: new Vector3(c.X, c.Y, c.Z);
|
|
if (hasCam && pos.DistanceSquaredTo(camPos) > 125f * 125f)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var key = (true, c.Id);
|
|
float coreHpRatio = c.Grid.MaxCoreHp > 0f ? c.Grid.CoreHp / c.Grid.MaxCoreHp : 1f;
|
|
float thermalRatio = c.MaxThermalRatio > 0f ? c.MaxThermalRatio : c.Grid.ComputeMaxThermalRatio();
|
|
|
|
if ((c.Grid.Spec.Mobility == MobilityKind.Wheeled || c.Grid.Spec.Mobility == MobilityKind.Tracked) &&
|
|
c.Speed > 8.5f)
|
|
{
|
|
float nextDust = _tireDustNextTime.GetValueOrDefault(key);
|
|
if (_vfxClock >= nextDust)
|
|
{
|
|
_tireDustNextTime[key] = _vfxClock + 0.09f; // ~11 Hz
|
|
float sinY = MathF.Sin(c.Yaw);
|
|
float cosY = MathF.Cos(c.Yaw);
|
|
Vector3 rearPos = pos - new Vector3(sinY, 0f, cosY) * (c.Grid.Spec.HalfLength * 0.72f);
|
|
rearPos.Y = state.Terrain.SampleHeight(rearPos.X, rearPos.Z) + 0.12f;
|
|
Vector3 vel = new(c.VelX, 0f, c.VelZ);
|
|
_particles.EmitTireDust(
|
|
rearPos,
|
|
vel,
|
|
sampleGroundTint(rearPos.X, rearPos.Z),
|
|
Math.Clamp(c.Speed / 16f, 0.7f, 1.35f),
|
|
camPos,
|
|
hasCam);
|
|
}
|
|
}
|
|
|
|
if (thermalRatio >= 0.85f)
|
|
{
|
|
float nextVent = _thermalVentNextTime.GetValueOrDefault(key);
|
|
if (_vfxClock >= nextVent)
|
|
{
|
|
_thermalVentNextTime[key] = _vfxClock + 0.13f; // ~7.7 Hz
|
|
Vector3 ventPos = ResolveHotAnchorWorld(av?.Visual, c.Grid, pos, c.Grid.Spec.HalfHeight);
|
|
_particles.EmitThermalVent(ventPos, thermalRatio, camPos, hasCam);
|
|
}
|
|
}
|
|
|
|
if (coreHpRatio < 0.35f)
|
|
{
|
|
float nextSmoke = _damageSmokeNextTime.GetValueOrDefault(key);
|
|
if (_vfxClock >= nextSmoke)
|
|
{
|
|
_damageSmokeNextTime[key] = _vfxClock + 0.13f; // ~7.7 Hz
|
|
Vector3 dmgPos = pos + new Vector3(0f, c.Grid.Spec.HalfHeight * 0.6f, 0f);
|
|
_particles.EmitDamageSmokeAndFire(dmgPos, coreHpRatio, camPos, hasCam);
|
|
}
|
|
}
|
|
}
|
|
|
|
var specs = state.Spectators.Spectators;
|
|
for (int i = 0; i < specs.Count; i++)
|
|
{
|
|
var s = specs[i];
|
|
if (!s.Alive || !session.IsSpectatorVisible(s.Id))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Vector3 pos = _spectatorVisuals.TryGetValue(s.Id, out var av)
|
|
? av.Visual.Root.GlobalPosition
|
|
: new Vector3(s.X, s.Y, s.Z);
|
|
if (hasCam && pos.DistanceSquaredTo(camPos) > 125f * 125f)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var key = (false, s.Id);
|
|
float coreHpRatio = s.Grid.MaxCoreHp > 0f ? s.Grid.CoreHp / s.Grid.MaxCoreHp : 1f;
|
|
float thermalRatio = s.Grid.ComputeMaxThermalRatio();
|
|
|
|
if (thermalRatio >= 0.85f)
|
|
{
|
|
float nextVent = _thermalVentNextTime.GetValueOrDefault(key);
|
|
if (_vfxClock >= nextVent)
|
|
{
|
|
_thermalVentNextTime[key] = _vfxClock + 0.11f;
|
|
Vector3 ventPos = ResolveHotAnchorWorld(av?.Visual, s.Grid, pos, s.Grid.Spec.HalfHeight);
|
|
_particles.EmitThermalVent(ventPos, thermalRatio, camPos, hasCam);
|
|
}
|
|
}
|
|
|
|
if (coreHpRatio < 0.35f)
|
|
{
|
|
float nextSmoke = _damageSmokeNextTime.GetValueOrDefault(key);
|
|
if (_vfxClock >= nextSmoke)
|
|
{
|
|
_damageSmokeNextTime[key] = _vfxClock + 0.11f;
|
|
Vector3 dmgPos = pos + new Vector3(0f, s.Grid.Spec.HalfHeight * 0.6f, 0f);
|
|
_particles.EmitDamageSmokeAndFire(dmgPos, coreHpRatio, camPos, hasCam);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static Vector3 ResolveHotAnchorWorld(VehicleVisualInstance? vis, ChassisGrid grid, Vector3 fallbackPos, float halfHeight)
|
|
{
|
|
if (vis is not null && !vis.IsStaticLod)
|
|
{
|
|
var hps = grid.Spec.Hardpoints;
|
|
for (int i = 0; i < hps.Length; i++)
|
|
{
|
|
if (hps[i].Kind == HardpointKind.Radiator || hps[i].Kind == HardpointKind.Engine)
|
|
{
|
|
Node3D? anchor = vis.GetHardpointAnchorNode(hps[i].Id);
|
|
if (anchor is not null && GodotObject.IsInstanceValid(anchor) && anchor.IsInsideTree())
|
|
{
|
|
return anchor.GlobalPosition;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return fallbackPos + new Vector3(0f, halfHeight * 0.7f, 0f);
|
|
}
|
|
|
|
private void EmitBeam(ref int beamIdx, Basis lookBasis, Vector3 mid, Vector3 scale, Color color)
|
|
{
|
|
if (beamIdx >= MaxVfxBeams)
|
|
{
|
|
return;
|
|
}
|
|
_vfxBeamMm.SetInstanceTransform(beamIdx, new Transform3D(lookBasis * Basis.FromScale(scale), mid));
|
|
_vfxBeamMm.SetInstanceCustomData(beamIdx, color);
|
|
beamIdx++;
|
|
}
|
|
|
|
private void AddTransient(in TransientVfx vfx)
|
|
{
|
|
if (_transientVfx.Count >= 256)
|
|
{
|
|
_transientVfx.RemoveAt(0);
|
|
}
|
|
_transientVfx.Add(vfx);
|
|
}
|
|
|
|
private static void ClearChildren(Node parent)
|
|
{
|
|
for (int i = parent.GetChildCount() - 1; i >= 0; i--)
|
|
{
|
|
Node ch = parent.GetChild(i);
|
|
parent.RemoveChild(ch);
|
|
ch.QueueFree();
|
|
}
|
|
}
|
|
}
|