perf: PredictedRigidbody FixedUpdate caches sqr computations

This commit is contained in:
mischa 2024-03-14 14:59:50 +08:00 committed by MrGadget
parent c8f022584a
commit 39fbaabd3b

View File

@ -62,6 +62,7 @@ public class PredictedRigidbody : NetworkBehaviour
[Header("Reconciliation")] [Header("Reconciliation")]
[Tooltip("Correction threshold in meters. For example, 0.1 means that if the client is off by more than 10cm, it gets corrected.")] [Tooltip("Correction threshold in meters. For example, 0.1 means that if the client is off by more than 10cm, it gets corrected.")]
public double positionCorrectionThreshold = 0.10; public double positionCorrectionThreshold = 0.10;
double positionCorrectionThresholdSqr; // ² cached in Awake
[Tooltip("Correction threshold in degrees. For example, 5 means that if the client is off by more than 5 degrees, it gets corrected.")] [Tooltip("Correction threshold in degrees. For example, 5 means that if the client is off by more than 5 degrees, it gets corrected.")]
public double rotationCorrectionThreshold = 5; public double rotationCorrectionThreshold = 5;
@ -133,6 +134,7 @@ void Awake()
// cache ² computations // cache ² computations
motionSmoothingVelocityThresholdSqr = motionSmoothingVelocityThreshold * motionSmoothingVelocityThreshold; motionSmoothingVelocityThresholdSqr = motionSmoothingVelocityThreshold * motionSmoothingVelocityThreshold;
motionSmoothingAngularVelocityThresholdSqr = motionSmoothingAngularVelocityThreshold * motionSmoothingAngularVelocityThreshold; motionSmoothingAngularVelocityThresholdSqr = motionSmoothingAngularVelocityThreshold * motionSmoothingAngularVelocityThreshold;
positionCorrectionThresholdSqr = positionCorrectionThreshold * positionCorrectionThreshold;
} }
protected virtual void CopyRenderersAsGhost(GameObject destination, Material material) protected virtual void CopyRenderersAsGhost(GameObject destination, Material material)
@ -449,7 +451,11 @@ void FixedUpdate()
{ {
// TODO maybe don't reuse the correction thresholds? // TODO maybe don't reuse the correction thresholds?
tf.GetPositionAndRotation(out Vector3 position, out Quaternion rotation); tf.GetPositionAndRotation(out Vector3 position, out Quaternion rotation);
if (Vector3.Distance(lastRecorded.position, position) < positionCorrectionThreshold && // clean & simple:
// if (Vector3.Distance(lastRecorded.position, position) < positionCorrectionThreshold &&
// Quaternion.Angle(lastRecorded.rotation, rotation) < rotationCorrectionThreshold)
// faster:
if ((lastRecorded.position - position).sqrMagnitude < positionCorrectionThresholdSqr &&
Quaternion.Angle(lastRecorded.rotation, rotation) < rotationCorrectionThreshold) Quaternion.Angle(lastRecorded.rotation, rotation) < rotationCorrectionThreshold)
{ {
// Debug.Log($"FixedUpdate for {name}: taking optimized early return instead of recording state."); // Debug.Log($"FixedUpdate for {name}: taking optimized early return instead of recording state.");