Skip to content

Commit

Permalink
perf(PredictedRigidbody): revert syncing 'sleeping' because it cut be…
Browse files Browse the repository at this point in the history
…nchmark performance in half (!)
  • Loading branch information
miwarnec committed Mar 27, 2024
1 parent fa64d7a commit dba373b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
31 changes: 13 additions & 18 deletions Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs
Expand Up @@ -19,7 +19,6 @@ namespace Mirror
public class PredictedRigidbody : NetworkBehaviour
{
Transform tf; // this component is performance critical. cache .transform getter!
Renderer rend;

// Prediction sometimes moves the Rigidbody to a ghost object.
// .predictedRigidbody is always kept up to date to wherever the RB is.
Expand Down Expand Up @@ -93,10 +92,6 @@ public class PredictedRigidbody : NetworkBehaviour
[Tooltip("Reduce sends while velocity==0. Client's objects may slightly move due to gravity/physics, so we still want to send corrections occasionally even if an object is idle on the server the whole time.")]
public bool reduceSendsWhileIdle = true;

[Header("Debugging")]
[Tooltip("Useful to debug objects not coming to rest. This color codes the local objects which are already asleep on the server.")]
public bool showRemoteSleeping = false;

// Rigidbody & Collider are moved out into a separate object.
// this way the visual object can smoothly follow.
protected GameObject physicsCopy;
Expand All @@ -119,7 +114,6 @@ public class PredictedRigidbody : NetworkBehaviour
protected virtual void Awake()
{
tf = transform;
rend = GetComponent<Renderer>();
predictedRigidbody = GetComponent<Rigidbody>();
if (predictedRigidbody == null) throw new InvalidOperationException($"Prediction: {name} is missing a Rigidbody component.");
predictedRigidbodyTransform = predictedRigidbody.transform;
Expand All @@ -138,9 +132,6 @@ protected virtual void Awake()
motionSmoothingVelocityThresholdSqr = motionSmoothingVelocityThreshold * motionSmoothingVelocityThreshold;
motionSmoothingAngularVelocityThresholdSqr = motionSmoothingAngularVelocityThreshold * motionSmoothingAngularVelocityThreshold;
positionCorrectionThresholdSqr = positionCorrectionThreshold * positionCorrectionThreshold;

// renderer
originalColor = rend.material.color;
}

protected virtual void CopyRenderersAsGhost(GameObject destination, Material material)
Expand Down Expand Up @@ -670,7 +661,7 @@ void ApplyState(double timestamp, Vector3 position, Quaternion rotation, Vector3

// process a received server state.
// compares it against our history and applies corrections if needed.
void OnReceivedState(double timestamp, RigidbodyState state, bool sleeping)
void OnReceivedState(double timestamp, RigidbodyState state)//, bool sleeping)
{
// always update remote state ghost
if (remoteCopy != null)
Expand All @@ -680,11 +671,13 @@ void OnReceivedState(double timestamp, RigidbodyState state, bool sleeping)
remoteCopyTransform.localScale = tf.lossyScale; // world scale! see CreateGhosts comment.
}


// DO NOT SYNC SLEEPING! this cuts benchmark performance in half(!!!)
// color code remote sleeping objects to debug objects coming to rest
if (showRemoteSleeping)
{
rend.material.color = sleeping ? Color.gray : originalColor;
}
// if (showRemoteSleeping)
// {
// rend.material.color = sleeping ? Color.gray : originalColor;
// }

// performance: get Rigidbody position & rotation only once,
// and together via its transform
Expand Down Expand Up @@ -849,8 +842,9 @@ public override void OnSerialize(NetworkWriter writer, bool initialState)
position,
rotation,
predictedRigidbody.velocity,
predictedRigidbody.angularVelocity,
predictedRigidbody.IsSleeping());
predictedRigidbody.angularVelocity);//,
// DO NOT SYNC SLEEPING! this cuts benchmark performance in half(!!!)
// predictedRigidbody.IsSleeping());
writer.WritePredictedSyncData(data);
}

Expand All @@ -875,7 +869,8 @@ public override void OnDeserialize(NetworkReader reader, bool initialState)
Quaternion rotation = data.rotation;
Vector3 velocity = data.velocity;
Vector3 angularVelocity = data.angularVelocity;
bool sleeping = data.sleeping != 0;
// DO NOT SYNC SLEEPING! this cuts benchmark performance in half(!!!)
// bool sleeping = data.sleeping != 0;

// server sends state at the end of the frame.
// parse and apply the server's delta time to our timestamp.
Expand All @@ -889,7 +884,7 @@ public override void OnDeserialize(NetworkReader reader, bool initialState)
if (oneFrameAhead) timestamp += serverDeltaTime;

// process received state
OnReceivedState(timestamp, new RigidbodyState(timestamp, Vector3.zero, position, Quaternion.identity, rotation, Vector3.zero, velocity, Vector3.zero, angularVelocity), sleeping);
OnReceivedState(timestamp, new RigidbodyState(timestamp, Vector3.zero, position, Quaternion.identity, rotation, Vector3.zero, velocity, Vector3.zero, angularVelocity));//, sleeping);
}

protected override void OnValidate()
Expand Down
Expand Up @@ -22,17 +22,19 @@ public struct PredictedSyncData
public Quaternion rotation; // 16 bytes (word aligned)
public Vector3 velocity; // 12 bytes (word aligned)
public Vector3 angularVelocity; // 12 bytes (word aligned)
public byte sleeping; // 1 byte: bool isn't blittable
// DO NOT SYNC SLEEPING! this cuts benchmark performance in half(!!!)
// public byte sleeping; // 1 byte: bool isn't blittable

// constructor for convenience
public PredictedSyncData(float deltaTime, Vector3 position, Quaternion rotation, Vector3 velocity, Vector3 angularVelocity, bool sleeping)
public PredictedSyncData(float deltaTime, Vector3 position, Quaternion rotation, Vector3 velocity, Vector3 angularVelocity)//, bool sleeping)
{
this.deltaTime = deltaTime;
this.position = position;
this.rotation = rotation;
this.velocity = velocity;
this.angularVelocity = angularVelocity;
this.sleeping = sleeping ? (byte)1 : (byte)0;
// DO NOT SYNC SLEEPING! this cuts benchmark performance in half(!!!)
// this.sleeping = sleeping ? (byte)1 : (byte)0;
}
}

Expand Down

0 comments on commit dba373b

Please sign in to comment.