SnapshotInterpolationSettings.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // snapshot interpolation settings struct.
  2. // can easily be exposed in Unity inspectors.
  3. using System;
  4. using UnityEngine;
  5. namespace Mirror
  6. {
  7. // class so we can define defaults easily
  8. [Serializable]
  9. public class SnapshotInterpolationSettings
  10. {
  11. // decrease bufferTime at runtime to see the catchup effect.
  12. // increase to see slowdown.
  13. // 'double' so we can have very precise dynamic adjustment without rounding
  14. [Header("Buffering")]
  15. [Tooltip("Local simulation is behind by sendInterval * multiplier seconds.\n\nThis guarantees that we always have enough snapshots in the buffer to mitigate lags & jitter.\n\nIncrease this if the simulation isn't smooth. By default, it should be around 2.")]
  16. public double bufferTimeMultiplier = 2;
  17. [Tooltip("If a client can't process snapshots fast enough, don't store too many.")]
  18. public int bufferLimit = 32;
  19. // catchup /////////////////////////////////////////////////////////////
  20. // catchup thresholds in 'frames'.
  21. // half a frame might be too aggressive.
  22. [Header("Catchup / Slowdown")]
  23. [Tooltip("Slowdown begins when the local timeline is moving too fast towards remote time. Threshold is in frames worth of snapshots.\n\nThis needs to be negative.\n\nDon't modify unless you know what you are doing.")]
  24. public float catchupNegativeThreshold = -1; // careful, don't want to run out of snapshots
  25. [Tooltip("Catchup begins when the local timeline is moving too slow and getting too far away from remote time. Threshold is in frames worth of snapshots.\n\nThis needs to be positive.\n\nDon't modify unless you know what you are doing.")]
  26. public float catchupPositiveThreshold = 1;
  27. [Tooltip("Local timeline acceleration in % while catching up.")]
  28. [Range(0, 1)]
  29. public double catchupSpeed = 0.02f; // see snap interp demo. 1% is too slow.
  30. [Tooltip("Local timeline slowdown in % while slowing down.")]
  31. [Range(0, 1)]
  32. public double slowdownSpeed = 0.04f; // slow down a little faster so we don't encounter empty buffer (= jitter)
  33. [Tooltip("Catchup/Slowdown is adjusted over n-second exponential moving average.")]
  34. public int driftEmaDuration = 1; // shouldn't need to modify this, but expose it anyway
  35. // dynamic buffer time adjustment //////////////////////////////////////
  36. // dynamically adjusts bufferTimeMultiplier for smooth results.
  37. // to understand how this works, try this manually:
  38. //
  39. // - disable dynamic adjustment
  40. // - set jitter = 0.2 (20% is a lot!)
  41. // - notice some stuttering
  42. // - disable interpolation to see just how much jitter this really is(!)
  43. // - enable interpolation again
  44. // - manually increase bufferTimeMultiplier to 3-4
  45. // ... the cube slows down (blue) until it's smooth
  46. // - with dynamic adjustment enabled, it will set 4 automatically
  47. // ... the cube slows down (blue) until it's smooth as well
  48. //
  49. // note that 20% jitter is extreme.
  50. // for this to be perfectly smooth, set the safety tolerance to '2'.
  51. // but realistically this is not necessary, and '1' is enough.
  52. [Header("Dynamic Adjustment")]
  53. [Tooltip("Automatically adjust bufferTimeMultiplier for smooth results.\nSets a low multiplier on stable connections, and a high multiplier on jittery connections.")]
  54. public bool dynamicAdjustment = true;
  55. [Tooltip("Safety buffer that is always added to the dynamic bufferTimeMultiplier adjustment.")]
  56. public float dynamicAdjustmentTolerance = 1; // 1 is realistically just fine, 2 is very very safe even for 20% jitter. can be half a frame too. (see above comments)
  57. [Tooltip("Dynamic adjustment is computed over n-second exponential moving average standard deviation.")]
  58. public int deliveryTimeEmaDuration = 2; // 1-2s recommended to capture average delivery time
  59. }
  60. }