Skip to content

Commit 46a4854

Browse files
refactor: expose BufferedLinearInterpolator to public (#1907)
Co-authored-by: Sam Bellomo <[email protected]>
1 parent dfe19d5 commit 46a4854

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

com.unity.netcode.gameobjects/Components/Interpolator/BufferedLinearInterpolator.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
namespace Unity.Netcode
66
{
7-
87
/// <summary>
98
/// Solves for incoming values that are jittered
109
/// Partially solves for message loss. Unclamped lerping helps hide this, but not completely
1110
/// </summary>
12-
/// <typeparam name="T"></typeparam>
13-
internal abstract class BufferedLinearInterpolator<T> where T : struct
11+
public abstract class BufferedLinearInterpolator<T> where T : struct
1412
{
1513
private struct BufferedItem
1614
{
@@ -24,6 +22,10 @@ public BufferedItem(T item, double timeSent)
2422
}
2523
}
2624

25+
/// <summary>
26+
/// There’s two factors affecting interpolation: buffering (set in NetworkManager’s NetworkTimeSystem) and interpolation time, which is the amount of time it’ll take to reach the target. This is to affect the second one.
27+
/// </summary>
28+
public float MaximumInterpolationTime = 0.1f;
2729

2830
private const double k_SmallValue = 9.999999439624929E-11; // copied from Vector3's equal operator
2931

@@ -69,13 +71,19 @@ public BufferedItem(T item, double timeSent)
6971

7072
private bool InvalidState => m_Buffer.Count == 0 && m_LifetimeConsumedCount == 0;
7173

74+
/// <summary>
75+
/// Resets Interpolator to initial state
76+
/// </summary>
7277
public void Clear()
7378
{
7479
m_Buffer.Clear();
7580
m_EndTimeConsumed = 0.0d;
7681
m_StartTimeConsumed = 0.0d;
7782
}
7883

84+
/// <summary>
85+
/// Teleports current interpolation value to targetValue.
86+
/// </summary>
7987
public void ResetTo(T targetValue, double serverTime)
8088
{
8189
m_LifetimeConsumedCount = 1;
@@ -89,7 +97,6 @@ public void ResetTo(T targetValue, double serverTime)
8997
Update(0, serverTime, serverTime);
9098
}
9199

92-
93100
// todo if I have value 1, 2, 3 and I'm treating 1 to 3, I shouldn't interpolate between 1 and 3, I should interpolate from 1 to 2, then from 2 to 3 to get the best path
94101
private void TryConsumeFromBuffer(double renderTime, double serverTime)
95102
{
@@ -205,14 +212,16 @@ public T Update(float deltaTime, double renderTime, double serverTime)
205212
}
206213

207214
var target = InterpolateUnclamped(m_InterpStartValue, m_InterpEndValue, t);
208-
float maxInterpTime = 0.1f;
209-
m_CurrentInterpValue = Interpolate(m_CurrentInterpValue, target, deltaTime / maxInterpTime); // second interpolate to smooth out extrapolation jumps
215+
m_CurrentInterpValue = Interpolate(m_CurrentInterpValue, target, deltaTime / MaximumInterpolationTime); // second interpolate to smooth out extrapolation jumps
210216
}
211217

212218
m_NbItemsReceivedThisFrame = 0;
213219
return m_CurrentInterpValue;
214220
}
215221

222+
/// <summary>
223+
/// Add measurements to be used during interpolation. These will be buffered before being made available to be displayed as "latest value".
224+
/// </summary>
216225
public void AddMeasurement(T newMeasurement, double sentTime)
217226
{
218227
m_NbItemsReceivedThisFrame++;
@@ -239,17 +248,25 @@ public void AddMeasurement(T newMeasurement, double sentTime)
239248
}
240249
}
241250

251+
/// <summary>
252+
/// Gets latest value from the interpolator. This is updated every update as time goes by.
253+
/// </summary>
242254
public T GetInterpolatedValue()
243255
{
244256
return m_CurrentInterpValue;
245257
}
246258

259+
/// <summary>
260+
/// Method to override and adapted to the generic type. This assumes interpolation for that value will be clamped.
261+
/// </summary>
247262
protected abstract T Interpolate(T start, T end, float time);
263+
/// <summary>
264+
/// Method to override and adapted to the generic type. This assumes interpolation for that value will not be clamped.
265+
/// </summary>
248266
protected abstract T InterpolateUnclamped(T start, T end, float time);
249267
}
250268

251-
252-
internal class BufferedLinearInterpolatorFloat : BufferedLinearInterpolator<float>
269+
public class BufferedLinearInterpolatorFloat : BufferedLinearInterpolator<float>
253270
{
254271
protected override float InterpolateUnclamped(float start, float end, float time)
255272
{
@@ -262,7 +279,7 @@ protected override float Interpolate(float start, float end, float time)
262279
}
263280
}
264281

265-
internal class BufferedLinearInterpolatorQuaternion : BufferedLinearInterpolator<Quaternion>
282+
public class BufferedLinearInterpolatorQuaternion : BufferedLinearInterpolator<Quaternion>
266283
{
267284
protected override Quaternion InterpolateUnclamped(Quaternion start, Quaternion end, float time)
268285
{

0 commit comments

Comments
 (0)