488 lines
17 KiB
C#
488 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using Godot;
|
|
using RustToDust.Core;
|
|
|
|
namespace RustToDust.Client;
|
|
|
|
/// <summary>
|
|
/// Live 4x4 Parallax Occlusion Mapping (POM) combat decal pool rendered in 1 draw call
|
|
/// using <c>decal_pom.gdshader</c> and <c>assets/decals/decal_atlas_*.png</c>.
|
|
/// Supports terrain impacts, static cover impacts, and vehicle-cell-attached scars
|
|
/// that cool smoothly over 3.5s, ride with moving limbs/turrets, and fade when armor detaches.
|
|
/// </summary>
|
|
public partial class PomDecalPool : Node3D
|
|
{
|
|
public const int MaxDecals = 256;
|
|
private const float HeatCoolDuration = 3.5f;
|
|
private const float DetachFadeDuration = 0.35f;
|
|
private const float EndFadeDuration = 2.0f;
|
|
|
|
private struct DecalSlot
|
|
{
|
|
public bool Active;
|
|
public float Age;
|
|
public float Lifetime;
|
|
public float TileIndex;
|
|
public float InitialHeat;
|
|
public float BaseOpacity;
|
|
public float DepthScale;
|
|
public Transform3D WorldXform;
|
|
|
|
public bool IsAttached;
|
|
public VehicleVisualInstance? Visual;
|
|
public Node3D? Pivot;
|
|
public Transform3D LocalToPivot;
|
|
public ChassisGrid? Grid;
|
|
public int CellX;
|
|
public int CellY;
|
|
public float DetachedFadeRemaining;
|
|
}
|
|
|
|
private readonly DecalSlot[] _slots = new DecalSlot[MaxDecals];
|
|
private int _writeCursor;
|
|
private MatchState? _boundState;
|
|
|
|
private MultiMesh _multiMesh = null!;
|
|
private MultiMeshInstance3D _instanceNode = null!;
|
|
private ShaderMaterial _material = null!;
|
|
private ImageTexture _albedoTex = null!;
|
|
private ImageTexture _normalTex = null!;
|
|
private ImageTexture _ormhTex = null!;
|
|
|
|
public int ActiveCount { get; private set; }
|
|
|
|
public override void _Ready()
|
|
{
|
|
Shader shader = GD.Load<Shader>("res://decal_pom.gdshader")
|
|
?? throw new InvalidOperationException("Failed to load res://decal_pom.gdshader");
|
|
|
|
string vehiclesDir = VehicleCatalogLoader.LocateVehiclesDirectory();
|
|
string assetsDir = Directory.GetParent(vehiclesDir)?.FullName
|
|
?? throw new DirectoryNotFoundException("Could not locate assets directory.");
|
|
string decalsDir = Path.Combine(assetsDir, "decals");
|
|
|
|
_albedoTex = LoadTextureWithMips(Path.Combine(decalsDir, "decal_atlas_albedo.png"));
|
|
_normalTex = LoadTextureWithMips(Path.Combine(decalsDir, "decal_atlas_normal.png"));
|
|
_ormhTex = LoadTextureWithMips(Path.Combine(decalsDir, "decal_atlas_ormh.png"));
|
|
|
|
_material = new ShaderMaterial { Shader = shader };
|
|
_material.SetShaderParameter("albedo_tex", _albedoTex);
|
|
_material.SetShaderParameter("normal_tex", _normalTex);
|
|
_material.SetShaderParameter("ormh_tex", _ormhTex);
|
|
_material.SetShaderParameter("use_instance_custom", true);
|
|
_material.SetShaderParameter("default_depth_scale", 0.06f);
|
|
_material.SetShaderParameter("normal_offset", 0.012f);
|
|
_material.SetShaderParameter("heat_emission_energy", 2.8f);
|
|
|
|
_multiMesh = new MultiMesh
|
|
{
|
|
TransformFormat = MultiMesh.TransformFormatEnum.Transform3D,
|
|
UseCustomData = true,
|
|
Mesh = new QuadMesh { Size = Vector2.One },
|
|
InstanceCount = MaxDecals,
|
|
VisibleInstanceCount = 0
|
|
};
|
|
|
|
_instanceNode = new MultiMeshInstance3D
|
|
{
|
|
Name = "PomDecalsBatch",
|
|
Multimesh = _multiMesh,
|
|
MaterialOverride = _material,
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off,
|
|
CustomAabb = new Aabb(new Vector3(-1600f, -60f, -1600f), new Vector3(3200f, 450f, 3200f)),
|
|
PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off,
|
|
Visible = false
|
|
};
|
|
AddChild(_instanceNode);
|
|
}
|
|
|
|
public void EnsureInitialized()
|
|
{
|
|
if (_multiMesh is null)
|
|
{
|
|
_Ready();
|
|
}
|
|
}
|
|
|
|
public void ClearAll()
|
|
{
|
|
Array.Clear(_slots, 0, _slots.Length);
|
|
_writeCursor = 0;
|
|
ActiveCount = 0;
|
|
if (_multiMesh is not null)
|
|
{
|
|
_multiMesh.VisibleInstanceCount = 0;
|
|
_instanceNode.Visible = false;
|
|
}
|
|
}
|
|
|
|
public void ConsumeEvents(
|
|
IClientArenaSession session,
|
|
IReadOnlyList<ISimEvent> events,
|
|
float dt,
|
|
Func<bool, int, VehicleVisualInstance?> getVisual)
|
|
{
|
|
EnsureInitialized();
|
|
var state = session.ViewState;
|
|
if (!ReferenceEquals(_boundState, state))
|
|
{
|
|
_boundState = state;
|
|
ClearAll();
|
|
}
|
|
|
|
for (int i = 0; i < events.Count; i++)
|
|
{
|
|
switch (events[i])
|
|
{
|
|
case MatchResetEvent:
|
|
ClearAll();
|
|
break;
|
|
case HitImpactEvent hit:
|
|
StampHit(in hit, state, getVisual);
|
|
break;
|
|
case DropPodEvent dpe:
|
|
if ((DropPodEventKind)dpe.Kind == DropPodEventKind.PodTouchdownBlowPetals)
|
|
{
|
|
bool isContender = dpe.PodKind == (byte)OrbitalPodKind.Contender;
|
|
Vector3 pos = new(dpe.X, dpe.Y, dpe.Z);
|
|
Vector3 nrm = new(dpe.NormalX, dpe.NormalY, dpe.NormalZ);
|
|
if (nrm.LengthSquared() < 1e-4f) nrm = Vector3.Up;
|
|
nrm = nrm.Normalized();
|
|
Transform3D xf = BuildSurfaceTransform(pos + nrm * 0.05f, nrm, isContender ? 6.2f : 3.8f, dpe.PodId * 1.13f);
|
|
AllocateSlot(new DecalSlot
|
|
{
|
|
Active = true,
|
|
Age = 0f,
|
|
Lifetime = isContender ? 34f : 24f,
|
|
TileIndex = 15f,
|
|
InitialHeat = 1.0f,
|
|
BaseOpacity = 0.98f,
|
|
DepthScale = 0.095f,
|
|
WorldXform = xf,
|
|
DetachedFadeRemaining = -1f
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
UpdateAndCommit(dt);
|
|
}
|
|
|
|
public void PrewarmShader(Vector3 nearPos)
|
|
{
|
|
EnsureInitialized();
|
|
if (ActiveCount > 0)
|
|
{
|
|
return;
|
|
}
|
|
AllocateSlot(new DecalSlot
|
|
{
|
|
Active = true,
|
|
Age = 0f,
|
|
Lifetime = 0.04f,
|
|
TileIndex = 12f,
|
|
InitialHeat = 0f,
|
|
BaseOpacity = 0.03f,
|
|
DepthScale = 0.02f,
|
|
WorldXform = BuildSurfaceTransform(nearPos, Vector3.Up, 0.05f, 0f),
|
|
DetachedFadeRemaining = -1f
|
|
});
|
|
UpdateAndCommit(0f);
|
|
}
|
|
|
|
public void StampTestDecal(
|
|
Vector3 worldPos,
|
|
Vector3 worldNormal,
|
|
float size,
|
|
int tileIndex,
|
|
float heat = 0.95f,
|
|
float depth = 0.085f)
|
|
{
|
|
EnsureInitialized();
|
|
Transform3D xf = BuildSurfaceTransform(worldPos, worldNormal, size, (float)_writeCursor * 1.17f);
|
|
AllocateSlot(new DecalSlot
|
|
{
|
|
Active = true,
|
|
Age = 0f,
|
|
Lifetime = 26f,
|
|
TileIndex = tileIndex,
|
|
InitialHeat = heat,
|
|
BaseOpacity = 1f,
|
|
DepthScale = depth,
|
|
WorldXform = xf,
|
|
DetachedFadeRemaining = -1f
|
|
});
|
|
UpdateAndCommit(0f);
|
|
}
|
|
|
|
private void StampHit(
|
|
in HitImpactEvent ev,
|
|
MatchState state,
|
|
Func<bool, int, VehicleVisualInstance?> getVisual)
|
|
{
|
|
var targetKind = (HitTargetKind)ev.TargetKind;
|
|
var weaponClass = (WeaponVisualClass)ev.WeaponClass;
|
|
if (ev.IsAirburst && targetKind == HitTargetKind.StaticCover && ev.TargetId < 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
bool isVehicle = targetKind == HitTargetKind.Contender || targetKind == HitTargetKind.Spectator;
|
|
int tile;
|
|
float size;
|
|
float heat;
|
|
float depth;
|
|
float lifetime;
|
|
|
|
switch (weaponClass)
|
|
{
|
|
case WeaponVisualClass.ThermalBeam:
|
|
tile = string.Equals(ev.WeaponId, "gilded_lance", StringComparison.Ordinal) || !isVehicle
|
|
? 14 // molten plasma crater
|
|
: 14;
|
|
size = isVehicle
|
|
? (string.Equals(ev.WeaponId, "gilded_lance", StringComparison.Ordinal) ? 0.78f : 0.56f)
|
|
: (string.Equals(ev.WeaponId, "gilded_lance", StringComparison.Ordinal) ? 2.10f : 1.35f);
|
|
heat = 1.0f;
|
|
depth = 0.095f;
|
|
lifetime = 26f;
|
|
break;
|
|
|
|
case WeaponVisualClass.Explosive:
|
|
bool isRecoilless = string.Equals(ev.WeaponId, "recoilless", StringComparison.Ordinal);
|
|
tile = isVehicle ? 13 : 15; // shrapnel gouge on armor, blast scorch on ground
|
|
size = isVehicle ? (isRecoilless ? 0.88f : 0.66f) : (isRecoilless ? 3.20f : 1.75f);
|
|
heat = isRecoilless ? 0.98f : 0.82f;
|
|
depth = isRecoilless ? 0.095f : 0.075f;
|
|
lifetime = 28f;
|
|
break;
|
|
|
|
case WeaponVisualClass.Autocannon:
|
|
tile = (ev.EventId % 3 == 0) ? 13 : 12; // AP hole or shrapnel gouge
|
|
size = isVehicle ? 0.54f : 0.92f;
|
|
heat = 0.90f;
|
|
depth = 0.088f;
|
|
lifetime = 24f;
|
|
break;
|
|
|
|
default: // KineticLight (mg_762, pd_flak)
|
|
tile = string.Equals(ev.WeaponId, "pd_flak", StringComparison.Ordinal) ? 13 : 12;
|
|
size = isVehicle ? 0.36f : 0.58f;
|
|
heat = 0.78f;
|
|
depth = 0.078f;
|
|
lifetime = 20f;
|
|
break;
|
|
}
|
|
|
|
Vector3 hitPos = 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();
|
|
|
|
if (isVehicle)
|
|
{
|
|
bool isContender = targetKind == HitTargetKind.Contender;
|
|
VehicleVisualInstance? vis = getVisual(isContender, ev.TargetId);
|
|
if (vis is null || !vis.Root.IsInsideTree() || !vis.Root.Visible)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ChassisGrid? grid = null;
|
|
if (isContender)
|
|
{
|
|
if (ev.TargetId >= 0 && ev.TargetId < state.Contenders.Count)
|
|
grid = state.Contenders[ev.TargetId].Grid;
|
|
}
|
|
else
|
|
{
|
|
var specs = state.Spectators.Spectators;
|
|
for (int si = 0; si < specs.Count; si++)
|
|
{
|
|
if (specs[si].Id == ev.TargetId)
|
|
{
|
|
grid = specs[si].Grid;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (grid is null || grid.IsDestroyed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Vector3 rayDir = (vis.Root.GlobalPosition - hitPos);
|
|
if (rayDir.LengthSquared() < 1e-4f) rayDir = -normal;
|
|
rayDir = rayDir.Normalized();
|
|
Vector3 rayOrigin = hitPos - rayDir * 0.75f;
|
|
|
|
if (vis.TryRayHitAttachedPart(rayOrigin, rayDir, out Node3D? hitPivot, out Transform3D localToPivot, size) &&
|
|
hitPivot is not null)
|
|
{
|
|
AllocateSlot(new DecalSlot
|
|
{
|
|
Active = true,
|
|
Age = 0f,
|
|
Lifetime = lifetime,
|
|
TileIndex = tile,
|
|
InitialHeat = heat,
|
|
BaseOpacity = 1f,
|
|
DepthScale = depth,
|
|
IsAttached = true,
|
|
Visual = vis,
|
|
Pivot = hitPivot,
|
|
LocalToPivot = localToPivot,
|
|
Grid = grid,
|
|
CellX = ev.CellX,
|
|
CellY = ev.CellY,
|
|
DetachedFadeRemaining = -1f
|
|
});
|
|
return;
|
|
}
|
|
|
|
// Fallback: attach to Root if sculpted geometry ray glanced past edge
|
|
Transform3D worldXf = BuildSurfaceTransform(hitPos, normal, size, ev.EventId * 0.73f);
|
|
AllocateSlot(new DecalSlot
|
|
{
|
|
Active = true,
|
|
Age = 0f,
|
|
Lifetime = lifetime,
|
|
TileIndex = tile,
|
|
InitialHeat = heat,
|
|
BaseOpacity = 1f,
|
|
DepthScale = depth,
|
|
IsAttached = true,
|
|
Visual = vis,
|
|
Pivot = vis.Root,
|
|
LocalToPivot = vis.Root.GlobalTransform.AffineInverse() * worldXf,
|
|
Grid = grid,
|
|
CellX = ev.CellX,
|
|
CellY = ev.CellY,
|
|
DetachedFadeRemaining = -1f
|
|
});
|
|
return;
|
|
}
|
|
|
|
// Terrain or StaticCover decal
|
|
Transform3D groundXf = BuildSurfaceTransform(hitPos + normal * 0.045f, normal, size, ev.EventId * 1.13f);
|
|
AllocateSlot(new DecalSlot
|
|
{
|
|
Active = true,
|
|
Age = 0f,
|
|
Lifetime = lifetime,
|
|
TileIndex = tile,
|
|
InitialHeat = heat,
|
|
BaseOpacity = 0.96f,
|
|
DepthScale = depth,
|
|
WorldXform = groundXf,
|
|
DetachedFadeRemaining = -1f
|
|
});
|
|
}
|
|
|
|
private void UpdateAndCommit(float dt)
|
|
{
|
|
float stepDt = float.IsFinite(dt) && dt > 0f ? Math.Clamp(dt, 0f, 0.05f) : 0f;
|
|
int visibleIdx = 0;
|
|
|
|
for (int i = 0; i < MaxDecals; i++)
|
|
{
|
|
ref DecalSlot s = ref _slots[i];
|
|
if (!s.Active)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
s.Age += stepDt;
|
|
if (s.Age >= s.Lifetime)
|
|
{
|
|
s.Active = false;
|
|
continue;
|
|
}
|
|
|
|
Transform3D xf = s.WorldXform;
|
|
float opacity = s.BaseOpacity;
|
|
|
|
if (s.IsAttached)
|
|
{
|
|
if (s.Visual is null || s.Pivot is null || !GodotObject.IsInstanceValid(s.Pivot) ||
|
|
!s.Pivot.IsInsideTree() || !s.Visual.Root.Visible)
|
|
{
|
|
s.Active = false;
|
|
continue;
|
|
}
|
|
|
|
if (s.DetachedFadeRemaining < 0f && s.Grid is not null)
|
|
{
|
|
bool cellStripped = s.Grid.IsDestroyed ||
|
|
(s.CellX >= 0 && s.CellX < s.Grid.Spec.Width &&
|
|
s.CellY >= 0 && s.CellY < s.Grid.Spec.Height &&
|
|
s.Grid.Armor[s.CellX, s.CellY] <= 0.01f);
|
|
if (cellStripped)
|
|
{
|
|
s.DetachedFadeRemaining = DetachFadeDuration;
|
|
}
|
|
}
|
|
|
|
if (s.DetachedFadeRemaining >= 0f)
|
|
{
|
|
s.DetachedFadeRemaining -= stepDt;
|
|
if (s.DetachedFadeRemaining <= 0f)
|
|
{
|
|
s.Active = false;
|
|
continue;
|
|
}
|
|
opacity *= Math.Clamp(s.DetachedFadeRemaining / DetachFadeDuration, 0f, 1f);
|
|
}
|
|
|
|
xf = s.Pivot.GlobalTransform * s.LocalToPivot;
|
|
}
|
|
|
|
float remaining = s.Lifetime - s.Age;
|
|
if (remaining < EndFadeDuration)
|
|
{
|
|
opacity *= Math.Clamp(remaining / EndFadeDuration, 0f, 1f);
|
|
}
|
|
|
|
float heatGlow = s.InitialHeat * MathF.Max(0f, 1f - s.Age / HeatCoolDuration);
|
|
_multiMesh.SetInstanceTransform(visibleIdx, xf);
|
|
_multiMesh.SetInstanceCustomData(visibleIdx, new Color(s.TileIndex, heatGlow, opacity, s.DepthScale));
|
|
visibleIdx++;
|
|
}
|
|
|
|
ActiveCount = visibleIdx;
|
|
_multiMesh.VisibleInstanceCount = visibleIdx;
|
|
_instanceNode.Visible = visibleIdx > 0;
|
|
}
|
|
|
|
private void AllocateSlot(in DecalSlot slot)
|
|
{
|
|
_slots[_writeCursor] = slot;
|
|
_writeCursor = (_writeCursor + 1) % MaxDecals;
|
|
}
|
|
|
|
private static Transform3D BuildSurfaceTransform(Vector3 worldPos, Vector3 worldNormal, float size, float spinRad)
|
|
{
|
|
Vector3 n = worldNormal.LengthSquared() > 1e-6f ? worldNormal.Normalized() : Vector3.Up;
|
|
Vector3 upRef = MathF.Abs(n.Y) > 0.92f ? Vector3.Forward : Vector3.Up;
|
|
Vector3 right = upRef.Cross(n).Normalized();
|
|
Vector3 up = n.Cross(right).Normalized();
|
|
Basis spin = new(n, spinRad);
|
|
right = spin * right;
|
|
up = spin * up;
|
|
return new Transform3D(new Basis(right * size, up * size, n), worldPos);
|
|
}
|
|
|
|
private static ImageTexture LoadTextureWithMips(string path)
|
|
{
|
|
var img = Image.LoadFromFile(path);
|
|
if (img is null)
|
|
{
|
|
throw new FileNotFoundException($"Could not load POM decal texture '{path}'.", path);
|
|
}
|
|
img.GenerateMipmaps();
|
|
return ImageTexture.CreateFromImage(img);
|
|
}
|
|
}
|