Class InterpolationSettings
- Namespace
- Coherence.Interpolation
- Assembly
- Coherence.Interpolation.dll
Interpolation is a ScriptableObject used to smoothly interpolate between samples received over the network. It contains the settings and current state of the interpolation (including the SampleBuffer<T>) and an Interpolator that performs blending between samples. Each networked binding holds a separate ScriptableObject instance which is automatically instantiated at runtime. This allows each binding to operate independently from one-another. The following types are supported for interpolation: float, bool, int, long, Vector2, Vector3, Quaternion, Color, string and EntityReferences. Additional smoothing (using SmoothDamp) is supported for types: float, Vector2, Vector3 and Quaternion.
public class InterpolationSettings : ScriptableObject
- Inheritance
-
InterpolationSettings
Constructors
InterpolationSettings()
public InterpolationSettings()
Fields
DefaultInterpolationName
public static readonly string DefaultInterpolationName
Field Value
DefaultSampleRate
public const float DefaultSampleRate = 20
Field Value
EmptyInterpolationName
public static readonly string EmptyInterpolationName
Field Value
SimulationFramesPerSecond
public const double SimulationFramesPerSecond = 60
Field Value
Time
The current time for this binding. It will trail behind NetworkTime by Delay seconds in order to produce smooth movement.
public double Time
Field Value
interpolator
Reference to the Interpolator implementation that performs blending between samples. By default, latest sample interpolation is used, which simply snaps the value to the latest sample.
public Interpolator interpolator
Field Value
latencySettings
LatencySettings provides parameters used to tweak interpolation Delay.
public LatencySettings latencySettings
Field Value
maxDistance
If any two consecutive samples exceed the maximum distance, the buffer is cleared to teleport to the latest sample.
public float maxDistance
Field Value
smoothing
Smoothing is applied after the Interpolator blends values from samples around the current Time. This is useful to smooth out jerky movement, e.g., when moving into dead reckoning.
public Smoothing smoothing
Field Value
Properties
DefaultSettingsPath
Asset path to the default interpolation settings.
public static string DefaultSettingsPath { get; }
Property Value
Delay
The current internal latency for this binding, i.e., how many seconds this binding's Time trails behind NetworkTime.Time. Delay is lerped towards TargetDelay.
public double Delay { get; }
Property Value
IsAsset
Returns true if the ScriptableObject is an asset loaded from disk, false if runtime instantiated. See: https://docs.unity3d.com/ScriptReference/Object.GetInstanceID.html
public bool IsAsset { get; }
Property Value
IsInterpolationNone
Returns true if the settings is the "None" interpolation mode.
public bool IsInterpolationNone { get; }
Property Value
IsStopped
When we are completely done with interpolation and there is nothing else to interpolate over, interpolation is marked as stopped. In other words, when we completely interpolated (t = 1) to the latest received sample (which must be stopped!), IsStopped is set true, and reset to false when we receive another sample.
public bool IsStopped { get; }
Property Value
MeasuredSampleDuration
The duration between samples at which samples are actually generated and transmitted from the entity owner. This is measured from incoming samples by finding the longest duration between samples in the current SampleBuffer<T>.
public double MeasuredSampleDuration { get; }
Property Value
NetworkLatency
Latency between this client and the authority client that owns this entity, in seconds. This value is updated when new samples arrives so that Delay stays tuned to current network conditions. Network latency is scaled with networkLatencyFactor.
public double NetworkLatency { get; }
Property Value
SampleRate
The frequency at which samples are generated and transmitted from the entity owner. Higher sample rates incur lower Delay. Value is automatically initialize by the corresponding Binding. Use the Optimize window to configure the sample rate for a binding.
public double SampleRate { get; set; }
Property Value
TargetDelay
The target internal latency for this binding, i.e., how many seconds this binding's Time trails behind NetworkTime.Time. TargetDelay is computed from a number of factors, including sampling frequency, network latency and blending method.
public double TargetDelay { get; }
Property Value
- See Also
Methods
AppendSample<T>(T, bool, AbsoluteSimulationFrame, AbsoluteSimulationFrame)
Adds a sample to the sample buffer at the given frame.
public void AppendSample<T>(T value, bool stopped, AbsoluteSimulationFrame sampleFrame, AbsoluteSimulationFrame localFrame)
Parameters
value
TThe sample data.
stopped
boolThe samples have stopped with this value.
sampleFrame
AbsoluteSimulationFrameThe simulation frame at which the sample was generated by the entity authority.
localFrame
AbsoluteSimulationFrameThe current simulation frame on this machine, i.e. NetworkTime.ClientSimulationFrame.
Type Parameters
T
The type used for this binding, e.g. float, Vector3 or string.
AppendSample<T>(T, bool, bool, double, double)
Adds a sample to the sample buffer at the given time.
public void AppendSample<T>(T value, bool stopped, bool isSampleTimeValid, double sampleTime, double localTime)
Parameters
value
TThe sample data.
stopped
boolThe samples have stopped with this value.
isSampleTimeValid
boolFalse if received sampleFrame is invalid. Possibly because the sample wasn't streamed directly from the authority client, but was actually sent from the RS cache when we moved our live query or floating origin
sampleTime
doubleThe time at which the sample was generated by the entity authority.
localTime
doubleThe current time on this machine, i.e. NetworkTime.TimeAsDouble.
Type Parameters
T
The type used for this binding, e.g. float, Vector3 or string.
Awake()
public void Awake()
CalculateInterpolationPercentage<T>(double)
public InterpolationResult<T> CalculateInterpolationPercentage<T>(double time)
Parameters
time
double
Returns
Type Parameters
T
CreateDefault()
Creates a default interpolation object that uses linear interpolation.
public static InterpolationSettings CreateDefault()
Returns
CreateEmpty()
Creates an empty interpolation object which simply snaps to the latest sample value without any blending.
public static InterpolationSettings CreateEmpty()
Returns
GetBooleanAt(double)
See GetFloatAt(double).
public bool GetBooleanAt(double time)
Parameters
time
double
Returns
GetByteAt(double)
See GetFloatAt(double).
public byte GetByteAt(double time)
Parameters
time
double
Returns
GetBytesAt(double)
See GetFloatAt(double).
public byte[] GetBytesAt(double time)
Parameters
time
double
Returns
- byte[]
GetCharAt(double)
See GetFloatAt(double).
public char GetCharAt(double time)
Parameters
time
double
Returns
GetColorAt(double)
See GetFloatAt(double).
public Color GetColorAt(double time)
Parameters
time
double
Returns
GetDoubleAt(double)
See GetFloatAt(double).
public double GetDoubleAt(double time)
Parameters
time
double
Returns
GetEntityReferenceAt(double)
See GetFloatAt(double).
public Entity GetEntityReferenceAt(double time)
Parameters
time
double
Returns
GetFloatAt(double)
Queries the sample buffer for samples adjacent to the given time and performs blending between those samples using the interpolator.
public float GetFloatAt(double time)
Parameters
Returns
- float
Returns the interpolated value at the given time, or default if the buffer is empty, or the single sample value if the buffer holds a single sample.
GetIntAt(double)
See GetFloatAt(double).
public int GetIntAt(double time)
Parameters
time
double
Returns
GetLastSample<T>()
Return the last sample in the sample buffer, casted to the given type.
public Sample<T>? GetLastSample<T>()
Returns
- Sample<T>?
The last sample in the sample buffer.
Type Parameters
T
The type used for this binding, e.g. float, Vector3 or string.
GetLongAt(double)
See GetFloatAt(double).
public long GetLongAt(double time)
Parameters
time
double
Returns
GetQuaternionAt(double)
See GetFloatAt(double).
public Quaternion GetQuaternionAt(double time)
Parameters
time
double
Returns
GetSByteAt(double)
See GetFloatAt(double).
public sbyte GetSByteAt(double time)
Parameters
time
double
Returns
GetSampleBuffer<T>()
Return the sample buffer, casted to the given type. The first time this method is called, the sample buffer will be initialized with the given type. If this method has been previously been called with another generic type, an exception will be thrown due to type mismatch.
public SampleBuffer<T> GetSampleBuffer<T>()
Returns
- SampleBuffer<T>
The sample buffer.
Type Parameters
T
The type used for this binding, e.g. float, Vector3 or string.
GetShortAt(double)
See GetFloatAt(double).
public short GetShortAt(double time)
Parameters
time
double
Returns
GetStringAt(double)
See GetFloatAt(double).
public string GetStringAt(double time)
Parameters
time
double
Returns
GetUIntAt(double)
See GetFloatAt(double).
public uint GetUIntAt(double time)
Parameters
time
double
Returns
GetULongAt(double)
See GetFloatAt(double).
public ulong GetULongAt(double time)
Parameters
time
double
Returns
GetUShortAt(double)
See GetFloatAt(double).
public ushort GetUShortAt(double time)
Parameters
time
double
Returns
GetVector2At(double)
See GetFloatAt(double).
public Vector2 GetVector2At(double time)
Parameters
time
double
Returns
GetVector3At(double)
See GetFloatAt(double).
public Vector3 GetVector3At(double time)
Parameters
time
double
Returns
- Vector3
IsBeyondTeleportDistance(int, int)
public bool IsBeyondTeleportDistance(int a, int b)
Parameters
Returns
IsBeyondTeleportDistance(float, float)
public bool IsBeyondTeleportDistance(float a, float b)
Parameters
Returns
IsBeyondTeleportDistance(Quaternion, Quaternion)
public bool IsBeyondTeleportDistance(Quaternion a, Quaternion b)
Parameters
a
Quaternionb
Quaternion
Returns
IsBeyondTeleportDistance(Vector2, Vector2)
public bool IsBeyondTeleportDistance(Vector2 a, Vector2 b)
Parameters
Returns
IsBeyondTeleportDistance(Vector3, Vector3)
public bool IsBeyondTeleportDistance(Vector3 a, Vector3 b)
Parameters
a
Vector3b
Vector3
Returns
PerformInterpolationBoolean(bool, double)
public bool PerformInterpolationBoolean(bool currentValue, double time)
Parameters
Returns
PerformInterpolationByte(byte, double)
public byte PerformInterpolationByte(byte currentValue, double time)
Parameters
Returns
PerformInterpolationBytes(byte[], double)
public byte[] PerformInterpolationBytes(byte[] currentValue, double time)
Parameters
Returns
- byte[]
PerformInterpolationChar(char, double)
public char PerformInterpolationChar(char currentValue, double time)
Parameters
Returns
PerformInterpolationColor(Color, double)
public Color PerformInterpolationColor(Color currentValue, double time)
Parameters
Returns
PerformInterpolationDouble(double, double)
public double PerformInterpolationDouble(double currentValue, double time)
Parameters
Returns
PerformInterpolationEntityReference(Entity, double)
public Entity PerformInterpolationEntityReference(Entity currentValue, double time)
Parameters
Returns
PerformInterpolationFloat(float, double)
Performs interpolation on the given binding and returns its new value. Increments Time for this binding, taking Delay into account. Calculates the interpolated value for the latency-adjusted time using the interpolator. Applies additional smoothing for types that support it (float, double, Vector2, Vector3, Quaternion). If the sample buffer is empty, the currentValue will be returned. If the sample buffer contains a single sample, that sample value is returned with no blending or smoothing performed.
public float PerformInterpolationFloat(float currentValue, double time)
Parameters
currentValue
floatThe current value for the binding.
time
doubleThe current game time, usually NetworkTime.
Returns
- float
The interpolated value for this binding at the given time, or currentValue if the sample buffer is empty.
PerformInterpolationInt(int, double)
public int PerformInterpolationInt(int currentValue, double time)
Parameters
Returns
PerformInterpolationLong(long, double)
public long PerformInterpolationLong(long currentValue, double time)
Parameters
Returns
PerformInterpolationQuaternion(Quaternion, double)
public Quaternion PerformInterpolationQuaternion(Quaternion currentValue, double time)
Parameters
currentValue
Quaterniontime
double
Returns
PerformInterpolationSByte(sbyte, double)
public sbyte PerformInterpolationSByte(sbyte currentValue, double time)
Parameters
Returns
PerformInterpolationShort(short, double)
public short PerformInterpolationShort(short currentValue, double time)
Parameters
Returns
PerformInterpolationString(string, double)
public string PerformInterpolationString(string currentValue, double time)
Parameters
Returns
PerformInterpolationUInt(uint, double)
public long PerformInterpolationUInt(uint currentValue, double time)
Parameters
Returns
PerformInterpolationULong(ulong, double)
public ulong PerformInterpolationULong(ulong currentValue, double time)
Parameters
Returns
PerformInterpolationUShort(ushort, double)
public ushort PerformInterpolationUShort(ushort currentValue, double time)
Parameters
Returns
PerformInterpolationVector2(Vector2, double)
public Vector2 PerformInterpolationVector2(Vector2 currentValue, double time)
Parameters
Returns
PerformInterpolationVector3(Vector3, double)
public Vector3 PerformInterpolationVector3(Vector3 currentValue, double time)
Parameters
currentValue
Vector3time
double
Returns
- Vector3
RemoveOutdatedSamples<T>(double)
Removes outdated samples from the samples buffer. A sample is considered outdated if its timestamp is equal to or older than the given time. The number of samples left guarantees that both interpolation and extrapolation can happen.
public void RemoveOutdatedSamples<T>(double time)
Parameters
Type Parameters
T
Reset()
Resets the buffer and state variables. This is useful, e.g. when teleporting and re-parenting. Does not affect interpolation settings, i.e., serialized fields.
public void Reset()