247 lines
7.9 KiB
C#
247 lines
7.9 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using Godot;
|
||
|
|
|
||
|
|
namespace RustToDust.Client;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Unified MultiMeshInstance3D part pool sharing <c>mech_paint.gdshader</c> in vertex-material mode.
|
||
|
|
/// Collapses all live vehicle limbs, armor chunks, mounted gear, salvage drops, and tumbling debris
|
||
|
|
/// sharing the same single-surface <see cref="ArrayMesh"/> into one <see cref="MultiMeshInstance3D"/>
|
||
|
|
/// draw call with per-instance <c>INSTANCE_CUSTOM = (scorch, heat_glow, role_tint_index, alpha)</c>.
|
||
|
|
/// </summary>
|
||
|
|
public partial class MultiMeshPartPool : Node3D
|
||
|
|
{
|
||
|
|
private readonly record struct PendingInstance(
|
||
|
|
Transform3D Xform,
|
||
|
|
Color Custom,
|
||
|
|
bool WantsShadow,
|
||
|
|
Vector3 WorldCenter,
|
||
|
|
float ScaledRadius);
|
||
|
|
|
||
|
|
private sealed class PoolBucket
|
||
|
|
{
|
||
|
|
public required ArrayMesh Mesh;
|
||
|
|
public required Vector3 LocalCenter;
|
||
|
|
public required float LocalRadius;
|
||
|
|
public required MultiMesh MultiMesh;
|
||
|
|
public required MultiMeshInstance3D Node;
|
||
|
|
public readonly List<PendingInstance> Pending = new(16);
|
||
|
|
}
|
||
|
|
|
||
|
|
private readonly Dictionary<ulong, PoolBucket> _buckets = new();
|
||
|
|
private readonly List<PoolBucket> _bucketList = new();
|
||
|
|
private readonly Plane[] _frustumPlanes = new Plane[6];
|
||
|
|
private ShaderMaterial _sharedMat = null!;
|
||
|
|
private bool _hasFrustum;
|
||
|
|
private Vector3 _camPos;
|
||
|
|
|
||
|
|
public int BucketCount => _bucketList.Count;
|
||
|
|
public int ActiveBucketCount { get; private set; }
|
||
|
|
public int TotalSubmittedInstances { get; private set; }
|
||
|
|
|
||
|
|
public override void _Ready()
|
||
|
|
{
|
||
|
|
Shader shader = GD.Load<Shader>("res://mech_paint.gdshader")
|
||
|
|
?? throw new InvalidOperationException("Failed to load res://mech_paint.gdshader for MultiMeshPartPool.");
|
||
|
|
_sharedMat = new ShaderMaterial { Shader = shader };
|
||
|
|
_sharedMat.SetShaderParameter("use_vertex_material", true);
|
||
|
|
_sharedMat.SetShaderParameter("heat_emission_energy", 2.6f);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void EnsureInitialized()
|
||
|
|
{
|
||
|
|
if (_sharedMat is null)
|
||
|
|
{
|
||
|
|
_Ready();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void BeginFrame(Camera3D? cam)
|
||
|
|
{
|
||
|
|
EnsureInitialized();
|
||
|
|
TotalSubmittedInstances = 0;
|
||
|
|
|
||
|
|
for (int i = 0; i < _bucketList.Count; i++)
|
||
|
|
{
|
||
|
|
_bucketList[i].Pending.Clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (cam is not null && cam.IsInsideTree())
|
||
|
|
{
|
||
|
|
_camPos = cam.GlobalPosition;
|
||
|
|
var planes = cam.GetFrustum();
|
||
|
|
if (planes.Count >= 6)
|
||
|
|
{
|
||
|
|
for (int i = 0; i < 6; i++)
|
||
|
|
{
|
||
|
|
_frustumPlanes[i] = planes[i];
|
||
|
|
}
|
||
|
|
_hasFrustum = true;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
_hasFrustum = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Submit(
|
||
|
|
ArrayMesh mesh,
|
||
|
|
in Transform3D worldXform,
|
||
|
|
in Color customData,
|
||
|
|
bool wantsShadow)
|
||
|
|
{
|
||
|
|
if (mesh is null || mesh.GetSurfaceCount() == 0)
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
PoolBucket bucket = GetOrCreateBucket(mesh);
|
||
|
|
|
||
|
|
Basis b = worldXform.Basis;
|
||
|
|
float maxScaleSq = MathF.Max(
|
||
|
|
b.Column0.LengthSquared(),
|
||
|
|
MathF.Max(b.Column1.LengthSquared(), b.Column2.LengthSquared()));
|
||
|
|
float scale = MathF.Sqrt(MathF.Max(0.0001f, maxScaleSq));
|
||
|
|
float scaledRadius = bucket.LocalRadius * scale;
|
||
|
|
Vector3 worldCenter = worldXform * bucket.LocalCenter;
|
||
|
|
|
||
|
|
if (_hasFrustum && !IsSphereInFrustum(worldCenter, scaledRadius))
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool castShadow = wantsShadow && bucket.LocalRadius >= 0.55f;
|
||
|
|
if (castShadow && _hasFrustum)
|
||
|
|
{
|
||
|
|
float maxShadowDist = 85f + scaledRadius;
|
||
|
|
if (worldCenter.DistanceSquaredTo(_camPos) > maxShadowDist * maxShadowDist)
|
||
|
|
{
|
||
|
|
castShadow = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bucket.Pending.Add(new PendingInstance(worldXform, customData, castShadow, worldCenter, scaledRadius));
|
||
|
|
TotalSubmittedInstances++;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void EndFrame()
|
||
|
|
{
|
||
|
|
int active = 0;
|
||
|
|
for (int i = 0; i < _bucketList.Count; i++)
|
||
|
|
{
|
||
|
|
PoolBucket bucket = _bucketList[i];
|
||
|
|
int count = bucket.Pending.Count;
|
||
|
|
if (count == 0)
|
||
|
|
{
|
||
|
|
bucket.MultiMesh.VisibleInstanceCount = 0;
|
||
|
|
bucket.Node.Visible = false;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
active++;
|
||
|
|
if (bucket.MultiMesh.InstanceCount < count)
|
||
|
|
{
|
||
|
|
bucket.MultiMesh.InstanceCount = Math.Max(16, count * 2);
|
||
|
|
}
|
||
|
|
|
||
|
|
Vector3 aabbMin = new(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
|
||
|
|
Vector3 aabbMax = new(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity);
|
||
|
|
bool anyShadow = false;
|
||
|
|
|
||
|
|
for (int k = 0; k < count; k++)
|
||
|
|
{
|
||
|
|
PendingInstance inst = bucket.Pending[k];
|
||
|
|
bucket.MultiMesh.SetInstanceTransform(k, inst.Xform);
|
||
|
|
bucket.MultiMesh.SetInstanceCustomData(k, inst.Custom);
|
||
|
|
if (inst.WantsShadow)
|
||
|
|
{
|
||
|
|
anyShadow = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
Vector3 r = new(inst.ScaledRadius, inst.ScaledRadius, inst.ScaledRadius);
|
||
|
|
Vector3 lo = inst.WorldCenter - r;
|
||
|
|
Vector3 hi = inst.WorldCenter + r;
|
||
|
|
aabbMin = new Vector3(MathF.Min(aabbMin.X, lo.X), MathF.Min(aabbMin.Y, lo.Y), MathF.Min(aabbMin.Z, lo.Z));
|
||
|
|
aabbMax = new Vector3(MathF.Max(aabbMax.X, hi.X), MathF.Max(aabbMax.Y, hi.Y), MathF.Max(aabbMax.Z, hi.Z));
|
||
|
|
}
|
||
|
|
|
||
|
|
bucket.MultiMesh.VisibleInstanceCount = count;
|
||
|
|
bucket.Node.CustomAabb = new Aabb(aabbMin, aabbMax - aabbMin);
|
||
|
|
bucket.Node.CastShadow = anyShadow
|
||
|
|
? GeometryInstance3D.ShadowCastingSetting.On
|
||
|
|
: GeometryInstance3D.ShadowCastingSetting.Off;
|
||
|
|
bucket.Node.Visible = true;
|
||
|
|
}
|
||
|
|
ActiveBucketCount = active;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ClearAll()
|
||
|
|
{
|
||
|
|
for (int i = 0; i < _bucketList.Count; i++)
|
||
|
|
{
|
||
|
|
PoolBucket bucket = _bucketList[i];
|
||
|
|
bucket.Pending.Clear();
|
||
|
|
bucket.MultiMesh.VisibleInstanceCount = 0;
|
||
|
|
bucket.Node.Visible = false;
|
||
|
|
}
|
||
|
|
ActiveBucketCount = 0;
|
||
|
|
TotalSubmittedInstances = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
private PoolBucket GetOrCreateBucket(ArrayMesh mesh)
|
||
|
|
{
|
||
|
|
ulong id = mesh.GetInstanceId();
|
||
|
|
if (_buckets.TryGetValue(id, out PoolBucket? existing))
|
||
|
|
{
|
||
|
|
return existing;
|
||
|
|
}
|
||
|
|
|
||
|
|
Aabb aabb = mesh.GetAabb();
|
||
|
|
Vector3 localCenter = aabb.GetCenter();
|
||
|
|
float localRadius = MathF.Max(0.2f, aabb.Size.Length() * 0.5f);
|
||
|
|
|
||
|
|
var mm = new MultiMesh
|
||
|
|
{
|
||
|
|
TransformFormat = MultiMesh.TransformFormatEnum.Transform3D,
|
||
|
|
UseCustomData = true,
|
||
|
|
Mesh = mesh,
|
||
|
|
InstanceCount = 16,
|
||
|
|
VisibleInstanceCount = 0
|
||
|
|
};
|
||
|
|
var mmi = new MultiMeshInstance3D
|
||
|
|
{
|
||
|
|
Name = $"MMPool_{_bucketList.Count}",
|
||
|
|
Multimesh = mm,
|
||
|
|
MaterialOverride = _sharedMat,
|
||
|
|
Visible = false,
|
||
|
|
CastShadow = GeometryInstance3D.ShadowCastingSetting.Off,
|
||
|
|
PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off
|
||
|
|
};
|
||
|
|
AddChild(mmi);
|
||
|
|
|
||
|
|
var created = new PoolBucket
|
||
|
|
{
|
||
|
|
Mesh = mesh,
|
||
|
|
LocalCenter = localCenter,
|
||
|
|
LocalRadius = localRadius,
|
||
|
|
MultiMesh = mm,
|
||
|
|
Node = mmi
|
||
|
|
};
|
||
|
|
_buckets[id] = created;
|
||
|
|
_bucketList.Add(created);
|
||
|
|
return created;
|
||
|
|
}
|
||
|
|
|
||
|
|
private bool IsSphereInFrustum(Vector3 center, float radius)
|
||
|
|
{
|
||
|
|
float margin = radius + 1.5f;
|
||
|
|
for (int i = 0; i < 6; i++)
|
||
|
|
{
|
||
|
|
if (_frustumPlanes[i].DistanceTo(center) > margin)
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|