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
|
|
@ -84,6 +84,9 @@ public sealed class VehicleVisualInstance
|
|||
private readonly VehicleRigAnimator.RigState _rigState = new();
|
||||
|
||||
private readonly string _chassisId;
|
||||
private MeshInstance3D? _lodStaticMesh;
|
||||
private bool _isStaticLod;
|
||||
private bool _castShadows = true;
|
||||
|
||||
private VehicleVisualInstance(
|
||||
VehicleAssetCache cache,
|
||||
|
|
@ -120,6 +123,49 @@ public sealed class VehicleVisualInstance
|
|||
public VehicleRigAnimator.RigState RigState => _rigState;
|
||||
public IReadOnlyDictionary<string, BoundJoint> Joints => _joints;
|
||||
public int BoundHardpointCount => _hardpoints.Count;
|
||||
public bool IsStaticLod => _isStaticLod;
|
||||
|
||||
public void SetLodState(bool useStaticLod, bool castShadows)
|
||||
{
|
||||
if (_lodStaticMesh is null)
|
||||
{
|
||||
if (ModelRoot is MeshInstance3D dropMi)
|
||||
{
|
||||
if (_isStaticLod != useStaticLod)
|
||||
{
|
||||
_isStaticLod = useStaticLod;
|
||||
dropMi.Mesh = useStaticLod ? _template.BakedLodFarMesh : _template.BakedStaticMesh;
|
||||
}
|
||||
if (_castShadows != castShadows)
|
||||
{
|
||||
_castShadows = castShadows;
|
||||
dropMi.CastShadow = castShadows
|
||||
? GeometryInstance3D.ShadowCastingSetting.On
|
||||
: GeometryInstance3D.ShadowCastingSetting.Off;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (_isStaticLod != useStaticLod)
|
||||
{
|
||||
_isStaticLod = useStaticLod;
|
||||
ModelRoot.Visible = !useStaticLod;
|
||||
ModelRoot.PhysicsInterpolationMode = useStaticLod
|
||||
? Node.PhysicsInterpolationModeEnum.Off
|
||||
: Node.PhysicsInterpolationModeEnum.Inherit;
|
||||
ModelRoot.ProcessMode = useStaticLod
|
||||
? Node.ProcessModeEnum.Disabled
|
||||
: Node.ProcessModeEnum.Inherit;
|
||||
_lodStaticMesh.Visible = useStaticLod;
|
||||
}
|
||||
if (_castShadows != castShadows)
|
||||
{
|
||||
_castShadows = castShadows;
|
||||
_lodStaticMesh.CastShadow = castShadows
|
||||
? GeometryInstance3D.ShadowCastingSetting.On
|
||||
: GeometryInstance3D.ShadowCastingSetting.Off;
|
||||
}
|
||||
}
|
||||
|
||||
public static VehicleVisualInstance Create(
|
||||
VehicleAssetCache cache,
|
||||
|
|
@ -136,18 +182,51 @@ public sealed class VehicleVisualInstance
|
|||
: visualScale;
|
||||
|
||||
var root = new Node3D { Name = $"Actor_{chassisId}" };
|
||||
Vector3 modelScale = new(effectiveScale, effectiveScale, effectiveScale);
|
||||
// Align glTF grounded minY=0 to physics chassis bottom (-halfHeight)
|
||||
Vector3 modelPos = new(0f, -halfHeight - template.MinY * effectiveScale, 0f);
|
||||
|
||||
if (role == VehicleVisualRole.SalvageDrop)
|
||||
{
|
||||
var dropMesh = new MeshInstance3D
|
||||
{
|
||||
Name = $"Model_{pkg.Id}",
|
||||
Mesh = template.BakedStaticMesh,
|
||||
Scale = modelScale,
|
||||
Position = modelPos
|
||||
};
|
||||
ApplyRoleTint(dropMesh, role);
|
||||
root.AddChild(dropMesh);
|
||||
return new VehicleVisualInstance(cache, template, root, dropMesh, chassisId);
|
||||
}
|
||||
|
||||
if (template.SceneTemplate.Duplicate() is not Node3D modelRoot)
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to duplicate GLB scene template for '{pkg.Id}'.");
|
||||
}
|
||||
modelRoot.Name = $"Model_{pkg.Id}";
|
||||
modelRoot.Scale = new Vector3(effectiveScale, effectiveScale, effectiveScale);
|
||||
// Align glTF grounded minY=0 to physics chassis bottom (-halfHeight)
|
||||
modelRoot.Position = new Vector3(0f, -halfHeight - template.MinY * effectiveScale, 0f);
|
||||
modelRoot.Scale = modelScale;
|
||||
modelRoot.Position = modelPos;
|
||||
root.AddChild(modelRoot);
|
||||
|
||||
var instance = new VehicleVisualInstance(cache, template, root, modelRoot, chassisId);
|
||||
var lodMesh = new MeshInstance3D
|
||||
{
|
||||
Name = $"LodStatic_{pkg.Id}",
|
||||
Mesh = template.BakedLodFarMesh,
|
||||
Scale = modelScale,
|
||||
Position = modelPos,
|
||||
Visible = false
|
||||
};
|
||||
if (role != VehicleVisualRole.Player)
|
||||
{
|
||||
ApplyRoleTint(lodMesh, role);
|
||||
}
|
||||
root.AddChild(lodMesh);
|
||||
|
||||
var instance = new VehicleVisualInstance(cache, template, root, modelRoot, chassisId)
|
||||
{
|
||||
_lodStaticMesh = lodMesh
|
||||
};
|
||||
instance.BindAll(role);
|
||||
return instance;
|
||||
}
|
||||
|
|
@ -758,7 +837,7 @@ public sealed class VehicleVisualInstance
|
|||
public float ComputeMountedMaxY()
|
||||
{
|
||||
float maxY = float.NegativeInfinity;
|
||||
AccumulateMaxYRecursive(ModelRoot, Transform3D.Identity, ref maxY);
|
||||
AccumulateMaxYRecursive(ModelRoot, Transform3D.Identity, ref maxY, isRoot: true);
|
||||
if (!float.IsFinite(maxY))
|
||||
{
|
||||
return ModelRoot.Position.Y + _template.MaxY * ModelRoot.Scale.Y;
|
||||
|
|
@ -766,12 +845,12 @@ public sealed class VehicleVisualInstance
|
|||
return maxY;
|
||||
}
|
||||
|
||||
private static void AccumulateMaxYRecursive(Node cur, Transform3D parentToRoot, ref float maxY)
|
||||
private static void AccumulateMaxYRecursive(Node cur, Transform3D parentToRoot, ref float maxY, bool isRoot = false)
|
||||
{
|
||||
Transform3D toRoot = parentToRoot;
|
||||
if (cur is Node3D n3d)
|
||||
{
|
||||
if (!n3d.Visible)
|
||||
if (!isRoot && !n3d.Visible)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue