perf: PredictedRigidbody smoothFollowThreshold squared caching

This commit is contained in:
mischa 2024-03-15 12:35:57 +08:00 committed by MrGadget
parent 237ffac7a9
commit eb93ce4e3d

View File

@ -111,6 +111,7 @@ public class PredictedRigidbody : NetworkBehaviour
// protected Rigidbody physicsCopyRigidbody => rb; // caching to avoid GetComponent // protected Rigidbody physicsCopyRigidbody => rb; // caching to avoid GetComponent
// protected Collider physicsCopyCollider; // caching to avoid GetComponent // protected Collider physicsCopyCollider; // caching to avoid GetComponent
float smoothFollowThreshold; // caching to avoid calculation in LateUpdate float smoothFollowThreshold; // caching to avoid calculation in LateUpdate
float smoothFollowThresholdSqr; // caching to avoid calculation in LateUpdate
// we also create one extra ghost for the exact known server state. // we also create one extra ghost for the exact known server state.
protected GameObject remoteCopy; protected GameObject remoteCopy;
@ -130,6 +131,7 @@ void Awake()
// cache some threshold to avoid calculating them in LateUpdate // cache some threshold to avoid calculating them in LateUpdate
float colliderSize = GetComponentInChildren<Collider>().bounds.size.magnitude; float colliderSize = GetComponentInChildren<Collider>().bounds.size.magnitude;
smoothFollowThreshold = colliderSize * teleportDistanceMultiplier; smoothFollowThreshold = colliderSize * teleportDistanceMultiplier;
smoothFollowThresholdSqr = smoothFollowThreshold * smoothFollowThreshold;
// cache initial position/rotation/scale to be used when moving physics components (configurable joints' range of motion) // cache initial position/rotation/scale to be used when moving physics components (configurable joints' range of motion)
initialPosition = tf.position; initialPosition = tf.position;
@ -325,8 +327,14 @@ protected virtual void SmoothFollowPhysicsCopy()
predictedRigidbodyTransform.GetPositionAndRotation(out Vector3 physicsPosition, out Quaternion physicsRotation); // faster than Rigidbody .position and .rotation predictedRigidbodyTransform.GetPositionAndRotation(out Vector3 physicsPosition, out Quaternion physicsRotation); // faster than Rigidbody .position and .rotation
float deltaTime = Time.deltaTime; float deltaTime = Time.deltaTime;
float distance = Vector3.Distance(currentPosition, physicsPosition); // slow and simple version:
if (distance > smoothFollowThreshold) // float distance = Vector3.Distance(currentPosition, physicsPosition);
// if (distance > smoothFollowThreshold)
// faster version
Vector3 delta = physicsPosition - currentPosition;
float sqrDistance = Vector3.SqrMagnitude(delta);
float distance = Mathf.Sqrt(sqrDistance);
if (sqrDistance > smoothFollowThresholdSqr)
{ {
tf.SetPositionAndRotation(physicsPosition, physicsRotation); // faster than .position and .rotation manually tf.SetPositionAndRotation(physicsPosition, physicsRotation); // faster than .position and .rotation manually
Debug.Log($"[PredictedRigidbody] Teleported because distance to physics copy = {distance:F2} > threshold {smoothFollowThreshold:F2}"); Debug.Log($"[PredictedRigidbody] Teleported because distance to physics copy = {distance:F2} > threshold {smoothFollowThreshold:F2}");