761 lines
30 KiB
C#
761 lines
30 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Godot;
|
|
using RustToDust.Core;
|
|
using RustToDust.Ymir;
|
|
|
|
namespace RustToDust.Client;
|
|
|
|
/// <summary>
|
|
/// Renders the 3D arena entities from <see cref="ArenaSession"/>:
|
|
/// cover ruins, shrinking Safe Circle ring, real Brokkr vehicle/mech GLBs with hardpoint snap-on gear,
|
|
/// live secondary mechanism animations, part-by-cell debris detachment, 3 alien spectator tiers + status halos/lock lasers,
|
|
/// real gear/chassis salvage drops, and weapon tracers.
|
|
/// </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;
|
|
}
|
|
|
|
private sealed class DropVisualEntry
|
|
{
|
|
public required Node3D Node;
|
|
public VehicleVisualInstance? ChassisVisual;
|
|
public float BaseY;
|
|
public int TileX;
|
|
public int TileZ;
|
|
public int TileVersion;
|
|
}
|
|
|
|
private VehicleAssetCache _assetCache = null!;
|
|
private Node3D _staticGroup = null!;
|
|
private Node3D _actorsGroup = null!;
|
|
private Node3D _dropsGroup = null!;
|
|
private Node3D _tracersGroup = null!;
|
|
private DebrisSystem _debris = null!;
|
|
private MeshInstance3D _safeRingMesh = null!;
|
|
private ImmediateMesh _safeRingImm = null!;
|
|
|
|
private readonly Dictionary<int, ActorVisual> _contenderVisuals = new();
|
|
private readonly Dictionary<int, ActorVisual> _spectatorVisuals = new();
|
|
private readonly Dictionary<int, DropVisualEntry> _dropVisuals = new();
|
|
private readonly List<MeshInstance3D> _tracerPool = new();
|
|
|
|
private StandardMaterial3D _safeRingMat = null!;
|
|
private StandardMaterial3D _coverMat = null!;
|
|
private StandardMaterial3D _bermMat = null!;
|
|
private StandardMaterial3D _humanBeaconMat = null!;
|
|
private StandardMaterial3D _imperialBeaconMat = null!;
|
|
private StandardMaterial3D _cyanBeaconMat = null!;
|
|
private MatchState? _builtForState;
|
|
private float _lastSafeRingRadius = -1f;
|
|
|
|
public VehicleAssetCache AssetCache => _assetCache;
|
|
public DebrisSystem Debris => _debris;
|
|
|
|
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 DropGroupChildCount => _dropsGroup.GetChildCount();
|
|
|
|
public override void _Ready()
|
|
{
|
|
_assetCache = new VehicleAssetCache();
|
|
|
|
_staticGroup = new Node3D { Name = "StaticArena" };
|
|
_actorsGroup = new Node3D { Name = "Actors" };
|
|
_dropsGroup = new Node3D { Name = "GroundDrops" };
|
|
_tracersGroup = new Node3D { Name = "Tracers" };
|
|
_debris = new DebrisSystem { Name = "Debris" };
|
|
AddChild(_staticGroup);
|
|
AddChild(_actorsGroup);
|
|
AddChild(_dropsGroup);
|
|
AddChild(_tracersGroup);
|
|
AddChild(_debris);
|
|
|
|
_coverMat = new StandardMaterial3D { AlbedoColor = CoverRust, Roughness = 0.88f, Metallic = 0.25f };
|
|
_bermMat = new StandardMaterial3D { AlbedoColor = BermDark, Roughness = 0.94f, Metallic = 0.1f };
|
|
_humanBeaconMat = new StandardMaterial3D
|
|
{
|
|
AlbedoColor = new Color(OlivePaint.R, OlivePaint.G, OlivePaint.B, 0.72f),
|
|
Roughness = 0.55f,
|
|
Metallic = 0.35f,
|
|
EmissionEnabled = true,
|
|
Emission = OlivePaint,
|
|
EmissionEnergyMultiplier = 0.6f,
|
|
Transparency = BaseMaterial3D.TransparencyEnum.Alpha
|
|
};
|
|
_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
|
|
};
|
|
_cyanBeaconMat = new StandardMaterial3D
|
|
{
|
|
AlbedoColor = new Color(CyanSalvage.R, CyanSalvage.G, CyanSalvage.B, 0.72f),
|
|
EmissionEnabled = true,
|
|
Emission = CyanSalvage,
|
|
EmissionEnergyMultiplier = 1.5f,
|
|
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);
|
|
}
|
|
|
|
public override void _ExitTree()
|
|
{
|
|
_assetCache?.Dispose();
|
|
}
|
|
|
|
public void Sync(ArenaSession session, float elapsedSeconds, float dt = 1f / 60f)
|
|
{
|
|
var state = session.State;
|
|
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;
|
|
|
|
UpdateSafeCircleRing(state.Terrain, state.Spectators.SafeRadius);
|
|
SyncContenders(session, dt, hasCam, camPos);
|
|
SyncSpectators(session, elapsedSeconds, dt, hasCam, camPos);
|
|
SyncGroundDrops(state, elapsedSeconds, hasCam, camPos);
|
|
SyncTracers(state.Tracers);
|
|
_debris.Step(dt, state.Terrain);
|
|
}
|
|
|
|
private void RebuildStaticArena(MatchState state)
|
|
{
|
|
ClearChildren(_staticGroup);
|
|
|
|
for (int i = 0; i < state.ArenaCoverBoxes.Count; i++)
|
|
{
|
|
var b = state.ArenaCoverBoxes[i];
|
|
var mesh = new MeshInstance3D
|
|
{
|
|
Mesh = new BoxMesh { Size = new Vector3(b.HalfX * 2f, b.HalfY * 2f, b.HalfZ * 2f) },
|
|
MaterialOverride = _coverMat,
|
|
Position = new Vector3(b.X, b.Y, b.Z)
|
|
};
|
|
_staticGroup.AddChild(mesh);
|
|
}
|
|
|
|
for (int i = 0; i < state.ArenaBoundaryBoxes.Count; i++)
|
|
{
|
|
var b = state.ArenaBoundaryBoxes[i];
|
|
var mesh = new MeshInstance3D
|
|
{
|
|
Mesh = new BoxMesh { Size = new Vector3(b.HalfX * 2f, b.HalfY * 2f, b.HalfZ * 2f) },
|
|
MaterialOverride = _bermMat,
|
|
Position = new Vector3(b.X, b.Y, b.Z),
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
|
};
|
|
_staticGroup.AddChild(mesh);
|
|
}
|
|
}
|
|
|
|
private void ClearDynamicVisuals()
|
|
{
|
|
ClearChildren(_actorsGroup);
|
|
ClearChildren(_dropsGroup);
|
|
_debris.ClearAll();
|
|
_contenderVisuals.Clear();
|
|
_spectatorVisuals.Clear();
|
|
_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;
|
|
const float halfThick = 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(ArenaSession session, float dt, bool hasCam, Vector3 camPos)
|
|
{
|
|
var view = session.World.View;
|
|
var contenders = session.State.Contenders;
|
|
|
|
for (int i = 0; i < contenders.Count; i++)
|
|
{
|
|
var c = contenders[i];
|
|
if (!c.Alive)
|
|
{
|
|
if (_contenderVisuals.TryGetValue(c.Id, out var deadVis))
|
|
{
|
|
// Flush any final stripped plates/gear into debris before hiding wreck
|
|
deadVis.Visual.SyncDamageAndHeat(c.Grid, _debris, Vector3.Zero);
|
|
deadVis.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.IsPlayer ? 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;
|
|
|
|
Vector3 pos;
|
|
Vector3 vel;
|
|
var body = session.GetContenderBody(c.Id);
|
|
if (body is { } h)
|
|
{
|
|
int lane = h.Lane;
|
|
pos = new Vector3(view.PositionX[lane], view.PositionY[lane], view.PositionZ[lane]);
|
|
var q = new Quaternion(view.RotationX[lane], view.RotationY[lane], view.RotationZ[lane], view.RotationW[lane]);
|
|
vel = new Vector3(view.VelocityX[lane], view.VelocityY[lane], view.VelocityZ[lane]);
|
|
vis.Visual.Root.Transform = new Transform3D(new Basis(q), pos);
|
|
}
|
|
else
|
|
{
|
|
pos = new Vector3(c.X, c.Y, c.Z);
|
|
vel = Vector3.Zero;
|
|
vis.Visual.Root.Position = pos;
|
|
vis.Visual.Root.Rotation = new Vector3(0f, c.Yaw, 0f);
|
|
}
|
|
|
|
bool useStaticLod = false;
|
|
bool castShadows = true;
|
|
if (hasCam && !c.IsPlayer)
|
|
{
|
|
float distSq = pos.DistanceSquaredTo(camPos);
|
|
useStaticLod = vis.Visual.IsStaticLod ? distSq > 126f * 126f : distSq > 130f * 130f;
|
|
castShadows = distSq <= 150f * 150f;
|
|
}
|
|
vis.Visual.SetLodState(useStaticLod, castShadows);
|
|
|
|
if (!useStaticLod)
|
|
{
|
|
vis.Visual.SyncMountedGear(c.Grid);
|
|
VehicleRigAnimator.Update(vis.Visual, c, vel, vis.Visual.Root.Basis, dt);
|
|
vis.Visual.SyncDamageAndHeat(c.Grid, _debris, vel);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SyncSpectators(ArenaSession session, float elapsedSeconds, float dt, bool hasCam, Vector3 camPos)
|
|
{
|
|
var view = session.World.View;
|
|
var specs = session.State.Spectators.Spectators;
|
|
|
|
for (int i = 0; i < specs.Count; i++)
|
|
{
|
|
var s = specs[i];
|
|
if (!s.Alive)
|
|
{
|
|
if (_spectatorVisuals.TryGetValue(s.Id, out var deadVis))
|
|
{
|
|
deadVis.Visual.SyncDamageAndHeat(s.Grid, _debris, Vector3.Zero);
|
|
deadVis.Visual.Root.Visible = false;
|
|
if (deadVis.LaserMesh is not null)
|
|
{
|
|
deadVis.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 body = session.GetSpectatorBody(s.Id);
|
|
Vector3 specPos;
|
|
Vector3 vel;
|
|
if (body is { } h)
|
|
{
|
|
int lane = h.Lane;
|
|
specPos = new Vector3(view.PositionX[lane], view.PositionY[lane], view.PositionZ[lane]);
|
|
var q = new Quaternion(view.RotationX[lane], view.RotationY[lane], view.RotationZ[lane], view.RotationW[lane]);
|
|
vel = new Vector3(view.VelocityX[lane], view.VelocityY[lane], view.VelocityZ[lane]);
|
|
vis.Visual.Root.Transform = new Transform3D(new Basis(q), specPos);
|
|
}
|
|
else
|
|
{
|
|
specPos = new Vector3(s.X, s.Y, s.Z);
|
|
vel = Vector3.Zero;
|
|
vis.Visual.Root.Position = specPos;
|
|
vis.Visual.Root.Rotation = new Vector3(0f, s.Yaw, 0f);
|
|
}
|
|
|
|
bool useStaticLod = false;
|
|
bool castShadows = true;
|
|
if (hasCam)
|
|
{
|
|
float distSq = specPos.DistanceSquaredTo(camPos);
|
|
useStaticLod = vis.Visual.IsStaticLod ? distSq > 126f * 126f : distSq > 130f * 130f;
|
|
castShadows = distSq <= 150f * 150f;
|
|
}
|
|
vis.Visual.SetLodState(useStaticLod, castShadows);
|
|
|
|
ContenderState? target = s.TargetContenderId.HasValue &&
|
|
s.TargetContenderId.Value >= 0 &&
|
|
s.TargetContenderId.Value < session.State.Contenders.Count
|
|
? session.State.Contenders[s.TargetContenderId.Value]
|
|
: null;
|
|
|
|
if (!useStaticLod)
|
|
{
|
|
float aimYawWorld = s.Yaw;
|
|
float aimPitchDeg = 0f;
|
|
if (s.Stance != SpectatorStance.Neutral && target is { Alive: true })
|
|
{
|
|
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);
|
|
VehicleRigAnimator.Update(vis.Visual, in rigInput, vis.Visual.Root.Basis);
|
|
vis.Visual.SyncDamageAndHeat(s.Grid, _debris, vel);
|
|
}
|
|
|
|
// 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 })
|
|
{
|
|
Vector3 start = specPos + new Vector3(0f, s.Grid.Spec.HalfHeight * 0.7f, 0f);
|
|
Vector3 end = new Vector3(target.X, target.Y + target.Grid.Spec.HalfHeight * 0.3f, target.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)
|
|
{
|
|
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 (GodotObject.IsInstanceValid(pickedEntry.Node))
|
|
{
|
|
pickedEntry.Node.GetParent()?.RemoveChild(pickedEntry.Node);
|
|
pickedEntry.Node.QueueFree();
|
|
}
|
|
_dropVisuals.Remove(d.Id);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
bool isNew = false;
|
|
if (!_dropVisuals.TryGetValue(d.Id, out var entry))
|
|
{
|
|
Node3D node = BuildGroundDropVisual(d, out var chassisVis);
|
|
_dropsGroup.AddChild(node);
|
|
TerrainField.WorldToTile(d.X, d.Z, out int tx, out int tz);
|
|
entry = new DropVisualEntry
|
|
{
|
|
Node = node,
|
|
ChassisVisual = chassisVis,
|
|
BaseY = state.Terrain.SampleHeight(d.X, d.Z),
|
|
TileX = tx,
|
|
TileZ = tz,
|
|
TileVersion = state.Terrain.TileVersions[tx, tz]
|
|
};
|
|
_dropVisuals[d.Id] = entry;
|
|
isNew = true;
|
|
}
|
|
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);
|
|
isNew = true;
|
|
}
|
|
}
|
|
|
|
if (isNew)
|
|
{
|
|
entry.Node.Visible = true;
|
|
}
|
|
if (d.Module is not null)
|
|
{
|
|
float bob = MathF.Sin(elapsedSeconds * 2.5f + d.Id) * 0.12f;
|
|
entry.Node.Position = new Vector3(d.X, entry.BaseY + bob, d.Z);
|
|
entry.Node.Rotation = new Vector3(0f, elapsedSeconds * 0.9f + d.Id, 0f);
|
|
}
|
|
else
|
|
{
|
|
if (isNew)
|
|
{
|
|
entry.Node.Position = new Vector3(d.X, entry.BaseY, d.Z);
|
|
}
|
|
if (entry.ChassisVisual is { } cv)
|
|
{
|
|
bool useFarLod = false;
|
|
bool castShadows = true;
|
|
if (hasCam)
|
|
{
|
|
float d2 = new Vector3(d.X, entry.BaseY, d.Z).DistanceSquaredTo(camPos);
|
|
useFarLod = cv.IsStaticLod ? d2 > 126f * 126f : d2 > 130f * 130f;
|
|
castShadows = d2 <= 150f * 150f;
|
|
}
|
|
cv.SetLodState(useFarLod, castShadows);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SyncTracers(IReadOnlyList<TracerEvent> tracers)
|
|
{
|
|
while (_tracerPool.Count < tracers.Count)
|
|
{
|
|
var mat = new StandardMaterial3D
|
|
{
|
|
ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded,
|
|
EmissionEnabled = true
|
|
};
|
|
var mesh = new MeshInstance3D
|
|
{
|
|
Mesh = new BoxMesh { Size = new Vector3(0.14f, 0.14f, 1.0f) },
|
|
MaterialOverride = mat,
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
|
};
|
|
_tracersGroup.AddChild(mesh);
|
|
_tracerPool.Add(mesh);
|
|
}
|
|
|
|
for (int i = 0; i < _tracerPool.Count; i++)
|
|
{
|
|
var m = _tracerPool[i];
|
|
if (i >= tracers.Count)
|
|
{
|
|
m.Visible = false;
|
|
continue;
|
|
}
|
|
|
|
var t = tracers[i];
|
|
var start = new Vector3(t.StartX, t.StartY, t.StartZ);
|
|
var end = new Vector3(t.EndX, t.EndY, t.EndZ);
|
|
float dist = start.DistanceTo(end);
|
|
if (dist < 0.1f)
|
|
{
|
|
m.Visible = false;
|
|
continue;
|
|
}
|
|
|
|
m.Visible = true;
|
|
m.Position = (start + end) * 0.5f;
|
|
Vector3 dir = (end - start).Normalized();
|
|
Vector3 up = MathF.Abs(dir.Dot(Vector3.Up)) > 0.95f ? Vector3.Right : Vector3.Up;
|
|
m.LookAt(end, up);
|
|
m.Scale = new Vector3(1f, 1f, dist);
|
|
|
|
var col = new Color(
|
|
((t.ColorRgba >> 24) & 0xFF) / 255f,
|
|
((t.ColorRgba >> 16) & 0xFF) / 255f,
|
|
((t.ColorRgba >> 8) & 0xFF) / 255f,
|
|
1f);
|
|
if (m.MaterialOverride is StandardMaterial3D sm)
|
|
{
|
|
sm.AlbedoColor = col;
|
|
sm.Emission = col;
|
|
sm.EmissionEnergyMultiplier = 2.5f;
|
|
}
|
|
}
|
|
}
|
|
|
|
private Node3D BuildGroundDropVisual(GroundDrop drop, out VehicleVisualInstance? chassisVisual)
|
|
{
|
|
chassisVisual = null;
|
|
var root = new Node3D { Name = $"Drop_{drop.Id}" };
|
|
if (drop.Module is { } mod)
|
|
{
|
|
var beaconMat = mod.IsImperial ? _imperialBeaconMat : _humanBeaconMat;
|
|
root.AddChild(new MeshInstance3D
|
|
{
|
|
Mesh = new CylinderMesh { TopRadius = 0.72f, BottomRadius = 0.85f, Height = 0.14f, RadialSegments = 14 },
|
|
MaterialOverride = beaconMat,
|
|
Position = new Vector3(0f, 0.08f, 0f),
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
|
});
|
|
|
|
// Real Brokkr gear GLB hovering cleanly above the salvage beacon disc
|
|
Node3D gear = VehicleVisualInstance.CreateStandaloneGearVisual(_assetCache, mod);
|
|
gear.Scale *= 1.45f;
|
|
gear.Position += new Vector3(0f, 0.35f, 0f);
|
|
root.AddChild(gear);
|
|
|
|
if (mod.IsImperial)
|
|
{
|
|
root.AddChild(new MeshInstance3D
|
|
{
|
|
Mesh = new CylinderMesh { TopRadius = 0.06f, BottomRadius = 0.22f, Height = 5.5f, RadialSegments = 8 },
|
|
MaterialOverride = _imperialBeaconMat,
|
|
Position = new Vector3(0f, 2.9f, 0f),
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
|
});
|
|
}
|
|
}
|
|
else if (drop.Chassis is { } ch)
|
|
{
|
|
const float dropScale = 0.85f;
|
|
var hull = VehicleVisualInstance.Create(_assetCache, ch.Id, VehicleVisualRole.SalvageDrop, dropScale);
|
|
chassisVisual = hull;
|
|
// ModelRoot sits at -HalfHeight*dropScale inside hull.Root, so lifting hull.Root by +HalfHeight*dropScale grounds minY=0 on terrain
|
|
hull.Root.Position = new Vector3(0f, ch.HalfHeight * dropScale, 0f);
|
|
root.AddChild(hull.Root);
|
|
|
|
var ring = new MeshInstance3D
|
|
{
|
|
Mesh = new CylinderMesh { TopRadius = 2.6f, BottomRadius = 2.8f, Height = 0.14f, RadialSegments = 18 },
|
|
MaterialOverride = _cyanBeaconMat,
|
|
Position = new Vector3(0f, 0.12f, 0f),
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
|
};
|
|
root.AddChild(ring);
|
|
}
|
|
return root;
|
|
}
|
|
|
|
private static void ClearChildren(Node parent)
|
|
{
|
|
for (int i = parent.GetChildCount() - 1; i >= 0; i--)
|
|
{
|
|
var child = parent.GetChild(i);
|
|
parent.RemoveChild(child);
|
|
child.QueueFree();
|
|
}
|
|
}
|
|
}
|