Optimize arena rendering and simulation loop to 60 FPS
This commit is contained in:
parent
805c169794
commit
0f36eed3a0
12 changed files with 1810 additions and 231 deletions
|
|
@ -26,6 +26,18 @@ public partial class ArenaRenderer : Node3D
|
|||
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!;
|
||||
|
|
@ -39,7 +51,7 @@ public partial class ArenaRenderer : Node3D
|
|||
|
||||
private readonly Dictionary<int, ActorVisual> _contenderVisuals = new();
|
||||
private readonly Dictionary<int, ActorVisual> _spectatorVisuals = new();
|
||||
private readonly Dictionary<int, Node3D> _dropVisuals = new();
|
||||
private readonly Dictionary<int, DropVisualEntry> _dropVisuals = new();
|
||||
private readonly List<MeshInstance3D> _tracerPool = new();
|
||||
|
||||
private StandardMaterial3D _safeRingMat = null!;
|
||||
|
|
@ -49,6 +61,7 @@ public partial class ArenaRenderer : Node3D
|
|||
private StandardMaterial3D _imperialBeaconMat = null!;
|
||||
private StandardMaterial3D _cyanBeaconMat = null!;
|
||||
private MatchState? _builtForState;
|
||||
private float _lastSafeRingRadius = -1f;
|
||||
|
||||
public VehicleAssetCache AssetCache => _assetCache;
|
||||
public DebrisSystem Debris => _debris;
|
||||
|
|
@ -149,10 +162,14 @@ public partial class ArenaRenderer : Node3D
|
|||
_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);
|
||||
SyncSpectators(session, elapsedSeconds, dt);
|
||||
SyncGroundDrops(state, elapsedSeconds);
|
||||
SyncContenders(session, dt, hasCam, camPos);
|
||||
SyncSpectators(session, elapsedSeconds, dt, hasCam, camPos);
|
||||
SyncGroundDrops(state, elapsedSeconds, hasCam, camPos);
|
||||
SyncTracers(state.Tracers);
|
||||
_debris.Step(dt, state.Terrain);
|
||||
}
|
||||
|
|
@ -180,7 +197,8 @@ public partial class ArenaRenderer : Node3D
|
|||
{
|
||||
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)
|
||||
Position = new Vector3(b.X, b.Y, b.Z),
|
||||
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
||||
};
|
||||
_staticGroup.AddChild(mesh);
|
||||
}
|
||||
|
|
@ -194,10 +212,17 @@ public partial class ArenaRenderer : Node3D
|
|||
_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);
|
||||
|
||||
|
|
@ -238,7 +263,7 @@ public partial class ArenaRenderer : Node3D
|
|||
_safeRingImm.SurfaceEnd();
|
||||
}
|
||||
|
||||
private void SyncContenders(ArenaSession session, float dt)
|
||||
private void SyncContenders(ArenaSession session, float dt, bool hasCam, Vector3 camPos)
|
||||
{
|
||||
var view = session.World.View;
|
||||
var contenders = session.State.Contenders;
|
||||
|
|
@ -266,6 +291,7 @@ public partial class ArenaRenderer : Node3D
|
|||
|
||||
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;
|
||||
|
|
@ -273,30 +299,45 @@ public partial class ArenaRenderer : Node3D
|
|||
|
||||
vis.Visual.Root.Visible = true;
|
||||
|
||||
Vector3 pos;
|
||||
Vector3 vel;
|
||||
var body = session.GetContenderBody(c.Id);
|
||||
if (body is { } h)
|
||||
{
|
||||
int lane = h.Lane;
|
||||
var pos = new Vector3(view.PositionX[lane], view.PositionY[lane], view.PositionZ[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 = new Vector3(c.X, c.Y, c.Z);
|
||||
vis.Visual.Root.Position = pos;
|
||||
vis.Visual.Root.Rotation = new Vector3(0f, c.Yaw, 0f);
|
||||
}
|
||||
|
||||
vis.Visual.SyncMountedGear(c.Grid);
|
||||
VehicleRigAnimator.Update(vis.Visual, c, vel, vis.Visual.Root.Basis, dt);
|
||||
vis.Visual.SyncDamageAndHeat(c.Grid, _debris, vel);
|
||||
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)
|
||||
private void SyncSpectators(ArenaSession session, float elapsedSeconds, float dt, bool hasCam, Vector3 camPos)
|
||||
{
|
||||
var view = session.World.View;
|
||||
var specs = session.State.Spectators.Spectators;
|
||||
|
|
@ -358,47 +399,62 @@ public partial class ArenaRenderer : Node3D
|
|||
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;
|
||||
|
||||
float aimYawWorld = s.Yaw;
|
||||
float aimPitchDeg = 0f;
|
||||
if (s.Stance != SpectatorStance.Neutral && target is { Alive: true })
|
||||
if (!useStaticLod)
|
||||
{
|
||||
float tdx = target.X - s.X;
|
||||
float tdz = target.Z - s.Z;
|
||||
if (tdx * tdx + tdz * tdz > 0.01f)
|
||||
float aimYawWorld = s.Yaw;
|
||||
float aimPitchDeg = 0f;
|
||||
if (s.Stance != SpectatorStance.Neutral && target is { Alive: true })
|
||||
{
|
||||
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));
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
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);
|
||||
|
|
@ -430,20 +486,26 @@ public partial class ArenaRenderer : Node3D
|
|||
float dist = start.DistanceTo(end);
|
||||
if (dist > 0.5f)
|
||||
{
|
||||
vis.LaserMesh.Visible = true;
|
||||
if (!vis.LastLaserVisible)
|
||||
{
|
||||
vis.LastLaserVisible = true;
|
||||
vis.LaserMesh.Visible = true;
|
||||
}
|
||||
vis.LaserMesh.GlobalPosition = (start + end) * 0.5f;
|
||||
Vector3 dir = (end - start) / dist;
|
||||
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
|
||||
else if (vis.LastLaserVisible)
|
||||
{
|
||||
vis.LastLaserVisible = false;
|
||||
vis.LaserMesh.Visible = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (vis.LastLaserVisible)
|
||||
{
|
||||
vis.LastLaserVisible = false;
|
||||
vis.LaserMesh.Visible = false;
|
||||
}
|
||||
}
|
||||
|
|
@ -495,39 +557,82 @@ public partial class ArenaRenderer : Node3D
|
|||
};
|
||||
}
|
||||
|
||||
private void SyncGroundDrops(MatchState state, float elapsedSeconds)
|
||||
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 pickedNode))
|
||||
if (_dropVisuals.TryGetValue(d.Id, out var pickedEntry))
|
||||
{
|
||||
if (GodotObject.IsInstanceValid(pickedNode))
|
||||
if (GodotObject.IsInstanceValid(pickedEntry.Node))
|
||||
{
|
||||
pickedNode.GetParent()?.RemoveChild(pickedNode);
|
||||
pickedNode.QueueFree();
|
||||
pickedEntry.Node.GetParent()?.RemoveChild(pickedEntry.Node);
|
||||
pickedEntry.Node.QueueFree();
|
||||
}
|
||||
_dropVisuals.Remove(d.Id);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!_dropVisuals.TryGetValue(d.Id, out var node))
|
||||
bool isNew = false;
|
||||
if (!_dropVisuals.TryGetValue(d.Id, out var entry))
|
||||
{
|
||||
node = BuildGroundDropVisual(d);
|
||||
Node3D node = BuildGroundDropVisual(d, out var chassisVis);
|
||||
_dropsGroup.AddChild(node);
|
||||
_dropVisuals[d.Id] = 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;
|
||||
}
|
||||
}
|
||||
|
||||
node.Visible = true;
|
||||
float bob = d.Module is not null ? MathF.Sin(elapsedSeconds * 2.5f + d.Id) * 0.12f : 0f;
|
||||
float baseY = state.Terrain.SampleHeight(d.X, d.Z);
|
||||
node.Position = new Vector3(d.X, baseY + bob, d.Z);
|
||||
if (isNew)
|
||||
{
|
||||
entry.Node.Visible = true;
|
||||
}
|
||||
if (d.Module is not null)
|
||||
{
|
||||
node.Rotation = new Vector3(0f, elapsedSeconds * 0.9f + d.Id, 0f);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -591,8 +696,9 @@ public partial class ArenaRenderer : Node3D
|
|||
}
|
||||
}
|
||||
|
||||
private Node3D BuildGroundDropVisual(GroundDrop drop)
|
||||
private Node3D BuildGroundDropVisual(GroundDrop drop, out VehicleVisualInstance? chassisVisual)
|
||||
{
|
||||
chassisVisual = null;
|
||||
var root = new Node3D { Name = $"Drop_{drop.Id}" };
|
||||
if (drop.Module is { } mod)
|
||||
{
|
||||
|
|
@ -601,7 +707,8 @@ public partial class ArenaRenderer : Node3D
|
|||
{
|
||||
Mesh = new CylinderMesh { TopRadius = 0.72f, BottomRadius = 0.85f, Height = 0.14f, RadialSegments = 14 },
|
||||
MaterialOverride = beaconMat,
|
||||
Position = new Vector3(0f, 0.08f, 0f)
|
||||
Position = new Vector3(0f, 0.08f, 0f),
|
||||
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
||||
});
|
||||
|
||||
// Real Brokkr gear GLB hovering cleanly above the salvage beacon disc
|
||||
|
|
@ -616,7 +723,8 @@ public partial class ArenaRenderer : Node3D
|
|||
{
|
||||
Mesh = new CylinderMesh { TopRadius = 0.06f, BottomRadius = 0.22f, Height = 5.5f, RadialSegments = 8 },
|
||||
MaterialOverride = _imperialBeaconMat,
|
||||
Position = new Vector3(0f, 2.9f, 0f)
|
||||
Position = new Vector3(0f, 2.9f, 0f),
|
||||
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -624,6 +732,7 @@ public partial class ArenaRenderer : Node3D
|
|||
{
|
||||
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);
|
||||
|
|
@ -632,7 +741,8 @@ public partial class ArenaRenderer : Node3D
|
|||
{
|
||||
Mesh = new CylinderMesh { TopRadius = 2.6f, BottomRadius = 2.8f, Height = 0.14f, RadialSegments = 18 },
|
||||
MaterialOverride = _cyanBeaconMat,
|
||||
Position = new Vector3(0f, 0.12f, 0f)
|
||||
Position = new Vector3(0f, 0.12f, 0f),
|
||||
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
|
||||
};
|
||||
root.AddChild(ring);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue