feature: NetworkTransform interpolation can be disabled (#3378)

* Interpolation off bools for NetworkTransform.

Gives a snap-like effect to position, rotation and scaling.

* Update Assets/Mirror/Components/NetworkTransformBase.cs

* Update NetworkTransformBase.cs

---------

Co-authored-by: mischa <16416509+vis2k@users.noreply.github.com>
This commit is contained in:
JesusLuvsYooh 2023-02-16 03:19:02 +00:00 committed by GitHub
parent 569938c8c7
commit e0e262678b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 10 deletions

View File

@ -42,11 +42,21 @@ public abstract class NetworkTransformBase : NetworkBehaviour
public readonly SortedList<double, TransformSnapshot> serverSnapshots = new SortedList<double, TransformSnapshot>();
// selective sync //////////////////////////////////////////////////////
[Header("Selective Sync & Interpolation\nDon't change these at Runtime")]
[Header("Selective Sync\nDon't change these at Runtime")]
public bool syncPosition = true; // do not change at runtime!
public bool syncRotation = true; // do not change at runtime!
public bool syncScale = false; // do not change at runtime! rare. off by default.
// interpolation is on by default, but can be disabled to jump to
// the destination immediately. some projects need this.
[Header("Interpolation")]
[Tooltip("Set to false to have a snap-like effect on position movement.")]
public bool interpolatePosition = true;
[Tooltip("Set to false to have a snap-like effect on rotations.")]
public bool interpolateRotation = true;
[Tooltip("Set to false to remove scale smoothing. Example use-case: Instant flipping of sprites that use -X and +X for direction.")]
public bool interpolateScale = true;
// debugging ///////////////////////////////////////////////////////////
[Header("Debug")]
public bool showGizmos;
@ -141,7 +151,7 @@ protected void AddSnapshot(SortedList<double, TransformSnapshot> snapshots, doub
//
// NOTE: stuck detection is unnecessary here.
// we always set transform.position anyway, we can't get stuck.
protected virtual void Apply(TransformSnapshot interpolated)
protected virtual void Apply(TransformSnapshot interpolated, TransformSnapshot endGoal)
{
// local position/rotation for VR support
//
@ -150,9 +160,15 @@ protected virtual void Apply(TransformSnapshot interpolated)
// -> we still interpolated
// -> but simply don't apply it. if the user doesn't want to sync
// scale, then we should not touch scale etc.
if (syncPosition) target.localPosition = interpolated.position;
if (syncRotation) target.localRotation = interpolated.rotation;
if (syncScale) target.localScale = interpolated.scale;
if (syncPosition)
target.localPosition = interpolatePosition ? interpolated.position : endGoal.position;
if (syncRotation)
target.localRotation = interpolateRotation ? interpolated.rotation : endGoal.rotation;
if (syncScale)
target.localScale = interpolateScale ? interpolated.scale : endGoal.scale;
}
// client->server teleport to force position without interpolation.

View File

@ -82,7 +82,7 @@ void UpdateServer()
// interpolate & apply
TransformSnapshot computed = TransformSnapshot.Interpolate(from, to, t);
Apply(computed);
Apply(computed, to);
}
}
@ -129,7 +129,7 @@ void UpdateClient()
// interpolate & apply
TransformSnapshot computed = TransformSnapshot.Interpolate(from, to, t);
Apply(computed);
Apply(computed, to);
}

View File

@ -144,7 +144,7 @@ void UpdateServer()
// interpolate & apply
TransformSnapshot computed = TransformSnapshot.Interpolate(from, to, t);
Apply(computed);
Apply(computed, to);
}
}
}
@ -235,7 +235,7 @@ void UpdateClient()
// interpolate & apply
TransformSnapshot computed = TransformSnapshot.Interpolate(from, to, t);
Apply(computed);
Apply(computed, to);
}
}
}

View File

@ -11,7 +11,7 @@ public class NetworkTransformExposed : NetworkTransform
{
public new TransformSnapshot Construct() => base.Construct();
public new void Apply(TransformSnapshot interpolated) =>
base.Apply(interpolated);
base.Apply(interpolated, interpolated);
public new void OnClientToServerSync(Vector3? position, Quaternion? rotation, Vector3? scale) =>
base.OnClientToServerSync(position, rotation, scale);
public new void OnServerToClientSync(Vector3? position, Quaternion? rotation, Vector3? scale) =>