This page is a work in progress and could be pending updates.

Interpolators

Overview

Interpolators allow access to snapshot and interpolation data for Networked Properties, for use in Render(), LateUpdate() and Update(). Returned state and interpolation data for a Networked Property takes into account the timeframe of NetworkBehaviour.InterpolationDataSource.

There are two interpolators available.

  • Interpolator<T>: An accessor for Networked Property From and To snapshot values, and interpolation data for automated interpolation handling.
  • RawInterpolator: An accessor for Networked Property From and To snapshot values and interpolation data. Requires casting to the correct backing type.

NOTE: RawInterpolator interpolator requires unsafe code.

Back To Top

Interpolator<T>

Creates an accessor for Networked Property From and To snapshot values, and exposes interpolation data for automated interpolation handling.

GetInterpolator<T>(string PropertyName)

Returns a cacheable Interpolator<T> for a specific property of the calling NetworkBehaviour instance.

Back To Top

Value

Exposes a Value property which will return the interpolated value for the associated Networked Property, using the timeframe specified by NetworkBehaviour.InterpolationDataSource.

Back To Top

GetValues() And TryGetValues()

Snapshot values and interpolation data can also be acquired with the GetValues() or TryGetValues() methods, which return the From value, To value, and a float alpha value (t value for Lerp), for use in custom interpolation handling. The timeframe used by default is determined by NetworkBehaviour.InterpolationDataSource, but may be overridden to specifically use the local/predicted timeframe with the arguments force = true.

Back To Top

Handled Types

Allowed types are:

  • float
  • Vector3
  • Vector2
  • int (rounding occurs)
  • bool (N.B: This returns the From value, as boolean values cannot be interpolated)
  • NetworkBool (N.B: This returns the From value, as boolean values cannot be interpolated)

Back To Top

Usage Example

public class InterpolatorExample : NetworkBehaviour 
{
  public float DegreesPerSecond = 360f;

  [Networked]
  public float Rotation { get; set; }

  public Interpolator<float> NetFloatInterpolator;

  public override void Spawned() {
    NetFloatInterpolator = GetInterpolator<float>(nameof(Rotation));
  }

  public override void FixedUpdateNetwork() {
    // Rotate the object at a fixed rate for each tick.
    Rotation += DegreesPerSecond * Runner.DeltaTime;
  }

  public override void Render() {
    // Get the interpolated value.
    // Note that Value uses the timeframe specified by the InterpolationDataSource value.
    transform.rotation = Quaternion.Euler(0, 0, NetFloatInterpolator.Value);
  }
}

Back To Top

RawInterpolator

For types not supported by Interpolator<T>, the RawInterpolator allows unsafe access to Networked Property snapshot states as well as interpolation information for current render timeframes with the RawInterpolator.GetValues() methods.

GetValues() And TryGetValues()

Snapshot values and interpolation data can also be acquired with the GetValues() or TryGetValues() methods, which return the From value, To value, and a float alpha value (t value for Lerp), for use in custom interpolation handling. The timeframe used by default is determined by NetworkBehaviour.InterpolationDataSource, but may be overridden to specifically use the local/predicted timeframe with the arguments force = true.

The From and To values MUST be cast to the correct property type, as this is an unsafe mode and pointers are being used.

Back To Top

GetInterpolator(string PropertyName)

Returns a cacheable RawInterpolator for a specific property of the calling NetworkBehaviour instance.

public struct CustomStruct : INetworkStruct 
{
  public Vector3    Position;
  public Quaternion Rotation;
}

public unsafe class RawInterpolatorExample : NetworkBehaviour 
{

  [Networked]
  public CustomStruct Data { get; set; }

  RawInterpolator _dataInterpolator;

  public override void Spawned() 
  {
    _dataInterpolator = GetInterpolator(nameof(Data));
  }

  public override void Render() 
  {
    if (_dataInterpolator.TryGetValues(out var f, out var t, out var alpha)) 
    {
      var from = (CustomStruct*) f;
      var to   = (CustomStruct*) t;
      transform.position = Vector3.Lerp(from->Position, to->Position, alpha);
    }
  }
}

Back To Top

Network Collections

The RawInterpolator has methods specifically for getting the From snapshot, To snapshot, and interpolation alpha data from Fusion's collection types: * TryGetArray<T>(), * TryGetDictionary<T>() * TryGetLinkedList<T>()


using UnityEngine;
using Fusion;

public class CollectionRawInterpolator : NetworkBehaviour
{
  [Networked, Capacity(8)]
  public NetworkArray<float> NetArray { get; } = MakeInitializer(new float[8] { 0, 1, 2, 3, 4, 5, 6, 7 });

  RawInterpolator _interpolator;

  public override void Spawned()
  {
    // Get the RawInterpolator for our property
    _interpolator = GetInterpolator(nameof(NetArray));
  }

  public override void FixedUpdateNetwork()
  {
    // Increment each value by one every tick.
    for (int i = 0; i < NetArray.Length; i++)
    {
      NetArray.Set(i, NetArray[i] + 1);
    }
  }

  public override void Render()
  {
    // Get the From, To and alpha data, and manually interpolate the values
    if (_interpolator.TryGetArray(NetArray, out var from, out var to, out float alpha)) 
    {
      var str = $"alpha: {alpha} values: \n";

      for (int i = 0; i < NetArray.Length; i++) {
        var lerped = Mathf.Lerp(from[i], to[i], alpha);
        str +=  $"{from[i]} -> {to[i]} = {lerped}\n";
      }

      Debug.Log(str);
    } 
  }
}


To Document Top