Table of Contents

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

string

DefaultSampleRate

public const float DefaultSampleRate = 20

Field Value

float

EmptyInterpolationName

public static readonly string EmptyInterpolationName

Field Value

string

SimulationFramesPerSecond

public const double SimulationFramesPerSecond = 60

Field Value

double

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

double

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

Interpolator

latencySettings

LatencySettings provides parameters used to tweak interpolation Delay.

public LatencySettings latencySettings

Field Value

LatencySettings

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

float

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

Smoothing

Properties

DefaultSettingsPath

Asset path to the default interpolation settings.

public static string DefaultSettingsPath { get; }

Property Value

string

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

double

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

bool

IsInterpolationNone

Returns true if the settings is the "None" interpolation mode.

public bool IsInterpolationNone { get; }

Property Value

bool

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

bool

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

double

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

double

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

double

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

double
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 T

The sample data.

stopped bool

The samples have stopped with this value.

sampleFrame AbsoluteSimulationFrame

The simulation frame at which the sample was generated by the entity authority.

localFrame AbsoluteSimulationFrame

The 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 T

The sample data.

stopped bool

The samples have stopped with this value.

isSampleTimeValid bool

False 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 double

The time at which the sample was generated by the entity authority.

localTime double

The 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

InterpolationResult<T>

Type Parameters

T

CreateDefault()

Creates a default interpolation object that uses linear interpolation.

public static InterpolationSettings CreateDefault()

Returns

InterpolationSettings

CreateEmpty()

Creates an empty interpolation object which simply snaps to the latest sample value without any blending.

public static InterpolationSettings CreateEmpty()

Returns

InterpolationSettings

GetBooleanAt(double)

public bool GetBooleanAt(double time)

Parameters

time double

Returns

bool

GetByteAt(double)

public byte GetByteAt(double time)

Parameters

time double

Returns

byte

GetBytesAt(double)

public byte[] GetBytesAt(double time)

Parameters

time double

Returns

byte[]

GetCharAt(double)

public char GetCharAt(double time)

Parameters

time double

Returns

char

GetColorAt(double)

public Color GetColorAt(double time)

Parameters

time double

Returns

Color

GetDoubleAt(double)

public double GetDoubleAt(double time)

Parameters

time double

Returns

double

GetEntityReferenceAt(double)

public Entity GetEntityReferenceAt(double time)

Parameters

time double

Returns

Entity

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

time double

The time at which to query the interpolation. Usually Time.

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)

public int GetIntAt(double time)

Parameters

time double

Returns

int

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)

public long GetLongAt(double time)

Parameters

time double

Returns

long

GetQuaternionAt(double)

public Quaternion GetQuaternionAt(double time)

Parameters

time double

Returns

Quaternion

GetSByteAt(double)

public sbyte GetSByteAt(double time)

Parameters

time double

Returns

sbyte

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)

public short GetShortAt(double time)

Parameters

time double

Returns

short

GetStringAt(double)

public string GetStringAt(double time)

Parameters

time double

Returns

string

GetUIntAt(double)

public uint GetUIntAt(double time)

Parameters

time double

Returns

uint

GetULongAt(double)

public ulong GetULongAt(double time)

Parameters

time double

Returns

ulong

GetUShortAt(double)

public ushort GetUShortAt(double time)

Parameters

time double

Returns

ushort

GetVector2At(double)

public Vector2 GetVector2At(double time)

Parameters

time double

Returns

Vector2

GetVector3At(double)

public Vector3 GetVector3At(double time)

Parameters

time double

Returns

Vector3

IsBeyondTeleportDistance(int, int)

public bool IsBeyondTeleportDistance(int a, int b)

Parameters

a int
b int

Returns

bool

IsBeyondTeleportDistance(float, float)

public bool IsBeyondTeleportDistance(float a, float b)

Parameters

a float
b float

Returns

bool

IsBeyondTeleportDistance(Quaternion, Quaternion)

public bool IsBeyondTeleportDistance(Quaternion a, Quaternion b)

Parameters

a Quaternion
b Quaternion

Returns

bool

IsBeyondTeleportDistance(Vector2, Vector2)

public bool IsBeyondTeleportDistance(Vector2 a, Vector2 b)

Parameters

a Vector2
b Vector2

Returns

bool

IsBeyondTeleportDistance(Vector3, Vector3)

public bool IsBeyondTeleportDistance(Vector3 a, Vector3 b)

Parameters

a Vector3
b Vector3

Returns

bool

PerformInterpolationBoolean(bool, double)

public bool PerformInterpolationBoolean(bool currentValue, double time)

Parameters

currentValue bool
time double

Returns

bool

PerformInterpolationByte(byte, double)

public byte PerformInterpolationByte(byte currentValue, double time)

Parameters

currentValue byte
time double

Returns

byte

PerformInterpolationBytes(byte[], double)

public byte[] PerformInterpolationBytes(byte[] currentValue, double time)

Parameters

currentValue byte[]
time double

Returns

byte[]

PerformInterpolationChar(char, double)

public char PerformInterpolationChar(char currentValue, double time)

Parameters

currentValue char
time double

Returns

char

PerformInterpolationColor(Color, double)

public Color PerformInterpolationColor(Color currentValue, double time)

Parameters

currentValue Color
time double

Returns

Color

PerformInterpolationDouble(double, double)

public double PerformInterpolationDouble(double currentValue, double time)

Parameters

currentValue double
time double

Returns

double

PerformInterpolationEntityReference(Entity, double)

public Entity PerformInterpolationEntityReference(Entity currentValue, double time)

Parameters

currentValue Entity
time double

Returns

Entity

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 float

The current value for the binding.

time double

The 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

currentValue int
time double

Returns

int

PerformInterpolationLong(long, double)

public long PerformInterpolationLong(long currentValue, double time)

Parameters

currentValue long
time double

Returns

long

PerformInterpolationQuaternion(Quaternion, double)

public Quaternion PerformInterpolationQuaternion(Quaternion currentValue, double time)

Parameters

currentValue Quaternion
time double

Returns

Quaternion

PerformInterpolationSByte(sbyte, double)

public sbyte PerformInterpolationSByte(sbyte currentValue, double time)

Parameters

currentValue sbyte
time double

Returns

sbyte

PerformInterpolationShort(short, double)

public short PerformInterpolationShort(short currentValue, double time)

Parameters

currentValue short
time double

Returns

short

PerformInterpolationString(string, double)

public string PerformInterpolationString(string currentValue, double time)

Parameters

currentValue string
time double

Returns

string

PerformInterpolationUInt(uint, double)

public long PerformInterpolationUInt(uint currentValue, double time)

Parameters

currentValue uint
time double

Returns

long

PerformInterpolationULong(ulong, double)

public ulong PerformInterpolationULong(ulong currentValue, double time)

Parameters

currentValue ulong
time double

Returns

ulong

PerformInterpolationUShort(ushort, double)

public ushort PerformInterpolationUShort(ushort currentValue, double time)

Parameters

currentValue ushort
time double

Returns

ushort

PerformInterpolationVector2(Vector2, double)

public Vector2 PerformInterpolationVector2(Vector2 currentValue, double time)

Parameters

currentValue Vector2
time double

Returns

Vector2

PerformInterpolationVector3(Vector3, double)

public Vector3 PerformInterpolationVector3(Vector3 currentValue, double time)

Parameters

currentValue Vector3
time 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

time double

The current time used to determine which samples are outdated. Usually Time.

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()