NetworkTransformSnapshot.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // snapshot for snapshot interpolation
  2. // https://gafferongames.com/post/snapshot_interpolation/
  3. // position, rotation, scale for compatibility for now.
  4. using UnityEngine;
  5. namespace Mirror
  6. {
  7. // NetworkTransform Snapshot
  8. public struct NTSnapshot : Snapshot
  9. {
  10. // time or sequence are needed to throw away older snapshots.
  11. //
  12. // glenn fiedler starts with a 16 bit sequence number.
  13. // supposedly this is meant as a simplified example.
  14. // in the end we need the remote timestamp for accurate interpolation
  15. // and buffering over time.
  16. //
  17. // note: in theory, IF server sends exactly(!) at the same interval then
  18. // the 16 bit ushort timestamp would be enough to calculate the
  19. // remote time (sequence * sendInterval). but Unity's update is
  20. // not guaranteed to run on the exact intervals / do catchup.
  21. // => remote timestamp is better for now
  22. //
  23. // [REMOTE TIME, NOT LOCAL TIME]
  24. // => DOUBLE for long term accuracy & batching gives us double anyway
  25. public double remoteTimestamp { get; set; }
  26. public double localTimestamp { get; set; }
  27. public Vector3 position;
  28. public Quaternion rotation;
  29. public Vector3 scale;
  30. public NTSnapshot(double remoteTimestamp, double localTimestamp, Vector3 position, Quaternion rotation, Vector3 scale)
  31. {
  32. this.remoteTimestamp = remoteTimestamp;
  33. this.localTimestamp = localTimestamp;
  34. this.position = position;
  35. this.rotation = rotation;
  36. this.scale = scale;
  37. }
  38. public static NTSnapshot Interpolate(NTSnapshot from, NTSnapshot to, double t)
  39. {
  40. // NOTE:
  41. // Vector3 & Quaternion components are float anyway, so we can
  42. // keep using the functions with 't' as float instead of double.
  43. return new NTSnapshot(
  44. // interpolated snapshot is applied directly. don't need timestamps.
  45. 0, 0,
  46. // lerp position/rotation/scale unclamped in case we ever need
  47. // to extrapolate. atm SnapshotInterpolation never does.
  48. Vector3.LerpUnclamped(from.position, to.position, (float)t),
  49. // IMPORTANT: LerpUnclamped(0, 60, 1.5) extrapolates to ~86.
  50. // SlerpUnclamped(0, 60, 1.5) extrapolates to 90!
  51. // (0, 90, 1.5) is even worse. for Lerp.
  52. // => Slerp works way better for our euler angles.
  53. Quaternion.SlerpUnclamped(from.rotation, to.rotation, (float)t),
  54. Vector3.LerpUnclamped(from.scale, to.scale, (float)t)
  55. );
  56. }
  57. }
  58. }