Skip to content

Commit 2d53a3a

Browse files
fix
Fixing some additional issues and trying to find a reasonable balance.
1 parent 0d1f8ee commit 2d53a3a

File tree

2 files changed

+60
-42
lines changed

2 files changed

+60
-42
lines changed

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

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ internal struct CurrentState
118118

119119
public double StartTime;
120120
public double EndTime;
121-
public float TimeToTargetValue;
122-
public float DeltaTime;
123-
public float DeltaTimePredict;
124-
public float MaxDeltaTime;
121+
public double TimeToTargetValue;
122+
public double DeltaTime;
123+
public double DeltaTimePredict;
124+
public double MaxDeltaTime;
125125
public float LerpT;
126126
public float LerpTPredict;
127127
public bool TargetReached;
@@ -130,11 +130,13 @@ internal struct CurrentState
130130
public T PreviousValue;
131131
public T PredictValue;
132132
public T PredictTarget;
133+
public T Phase1Value;
134+
public T Phase2Value;
133135

134136
private float m_AverageDeltaTime;
135137

136138
public float AverageDeltaTime => m_AverageDeltaTime;
137-
public float FinalTimeToTarget => Mathf.Max(0.0f, TimeToTargetValue - DeltaTime);
139+
public double FinalTimeToTarget => Math.Max(0.0, TimeToTargetValue - DeltaTime);
138140

139141
public void AddDeltaTime(float deltaTime)
140142
{
@@ -147,20 +149,22 @@ public void AddDeltaTime(float deltaTime)
147149
m_AverageDeltaTime += deltaTime;
148150
m_AverageDeltaTime *= 0.5f;
149151
}
150-
DeltaTime = Math.Min(DeltaTime + m_AverageDeltaTime, TimeToTargetValue);
151-
DeltaTimePredict = Math.Min(DeltaTime + DeltaTimePredict, TimeToTargetValue + MaxDeltaTime);
152-
LerpT = TimeToTargetValue == 0.0f ? 1.0f : DeltaTime / TimeToTargetValue;
152+
DeltaTime = Math.Min(DeltaTime + deltaTime, TimeToTargetValue);
153+
DeltaTimePredict = Math.Min(DeltaTime + deltaTime, TimeToTargetValue + MaxDeltaTime);
154+
//DeltaTime = Math.Min(DeltaTime + m_AverageDeltaTime, TimeToTargetValue);
155+
//DeltaTimePredict = Math.Min(DeltaTimePredict + m_AverageDeltaTime, TimeToTargetValue + MaxDeltaTime);
156+
LerpT = (float)(TimeToTargetValue == 0.0 ? 1.0 : DeltaTime / TimeToTargetValue);
153157
if (PredictingNext)
154158
{
155-
LerpTPredict = TimeToTargetValue == 0.0f ? 1.0f : DeltaTimePredict / TimeToTargetValue;
159+
LerpTPredict = (float)(TimeToTargetValue == 0.0 ? 1.0 : DeltaTimePredict / TimeToTargetValue);
156160
}
157161
else
158162
{
159163
LerpTPredict = LerpT;
160164
}
161165
}
162166

163-
public void SetTimeToTarget(float timeToTarget)
167+
public void SetTimeToTarget(double timeToTarget)
164168
{
165169
DeltaTimePredict = 0.0f;
166170
LerpTPredict = 0.0f;
@@ -191,6 +195,8 @@ public void Reset(T currentValue)
191195
PreviousValue = currentValue;
192196
PredictValue = currentValue;
193197
PredictTarget = currentValue;
198+
Phase1Value = currentValue;
199+
Phase2Value = currentValue;
194200
TargetReached = false;
195201
PredictingNext = false;
196202
LerpT = 0.0f;
@@ -304,7 +310,7 @@ private void InternalReset(T targetValue, double serverTime, bool isAngularValue
304310
/// <param name="renderTime">render time: the time in "ticks ago" relative to the current tick latency</param>
305311
/// <param name="minDeltaTime">minimum time delta (defaults to tick frequency)</param>
306312
/// <param name="maxDeltaTime">maximum time delta which defines the maximum time duration when consuming more than one item from the buffer</param>
307-
private void TryConsumeFromBuffer(double renderTime, float minDeltaTime, float maxDeltaTime, bool isPredictedLerp)
313+
private void TryConsumeFromBuffer(double renderTime, double minDeltaTime, double maxDeltaTime, bool isPredictedLerp)
308314
{
309315
BufferedItem? previousItem = null;
310316
var startTime = 0.0;
@@ -348,6 +354,8 @@ private void TryConsumeFromBuffer(double renderTime, float minDeltaTime, float m
348354
alreadyHasBufferItem = true;
349355
InterpolateState.PredictValue = InterpolateState.CurrentValue;
350356
InterpolateState.PreviousValue = InterpolateState.CurrentValue;
357+
InterpolateState.Phase1Value = InterpolateState.CurrentValue;
358+
InterpolateState.Phase2Value = InterpolateState.CurrentValue;
351359
InterpolateState.SetTimeToTarget(minDeltaTime);
352360
InterpolateState.TimeToTargetValue = minDeltaTime;
353361
startTime = InterpolateState.Target.Value.TimeSent;
@@ -363,17 +371,19 @@ private void TryConsumeFromBuffer(double renderTime, float minDeltaTime, float m
363371
InterpolateState.TargetReached = false;
364372
InterpolateState.PredictingNext = false;
365373
startTime = InterpolateState.Target.Value.TimeSent;
374+
InterpolateState.Phase1Value = InterpolateState.PreviousValue;
375+
InterpolateState.Phase2Value = InterpolateState.PredictValue;
366376
InterpolateState.PredictTarget = target.Item;
367377
InterpolateState.MaxDeltaTime = maxDeltaTime;
368-
if (m_BufferQueue.TryPeek(out BufferedItem lookAheadItem))
369-
{
370-
InterpolateState.PredictTarget = Interpolate(target.Item, lookAheadItem.Item, InterpolateState.AverageDeltaTime);
371-
InterpolateState.PredictingNext = true;
372-
}
378+
}
379+
if (m_BufferQueue.TryPeek(out BufferedItem lookAheadItem))
380+
{
381+
InterpolateState.PredictTarget = Interpolate(target.Item, lookAheadItem.Item, InterpolateState.AverageDeltaTime);
382+
InterpolateState.PredictingNext = true;
373383
}
374384
// TODO: We might consider creating yet another queue to add these items to and assure that the time is accelerated
375385
// for each item as opposed to losing the resolution of the values.
376-
var timeToTarget = Mathf.Clamp((float)(target.TimeSent - startTime), minDeltaTime, maxDeltaTime);
386+
var timeToTarget = Math.Clamp((float)(target.TimeSent - startTime), minDeltaTime, maxDeltaTime);
377387
InterpolateState.SetTimeToTarget(timeToTarget);
378388
InterpolateState.Target = target;
379389
}
@@ -415,7 +425,7 @@ internal void ResetCurrentState()
415425
/// <param name="isLerpAndExtrapolate">Determines whether to use smooth dampening or extrapolation.</param>
416426
/// <param name="lerpSmoothing">Determines if lerp smoothing is enabled for this instance.</param>
417427
/// <returns>The newly interpolated value of type 'T'</returns>
418-
internal T Update(float deltaTime, double tickLatencyAsTime, float minDeltaTime, float maxDeltaTime, bool isLerpAndExtrapolate, bool lerpSmoothing)
428+
internal T Update(float deltaTime, double tickLatencyAsTime, double minDeltaTime, double maxDeltaTime, bool isLerpAndExtrapolate, bool lerpSmoothing)
419429
{
420430
TryConsumeFromBuffer(tickLatencyAsTime, minDeltaTime, maxDeltaTime, isLerpAndExtrapolate);
421431
// Only begin interpolation when there is a start and end point
@@ -429,27 +439,36 @@ internal T Update(float deltaTime, double tickLatencyAsTime, float minDeltaTime,
429439
// SmoothDampen or LerpExtrapolateBlend
430440
if (!isLerpAndExtrapolate)
431441
{
432-
InterpolateState.PreviousValue = SmoothDamp(InterpolateState.PreviousValue, InterpolateState.Target.Value.Item, ref m_RateOfChange, InterpolateState.TimeToTargetValue, InterpolateState.DeltaTime);
433-
var predictedTime = InterpolateState.PredictingNext ? InterpolateState.DeltaTime : Mathf.Min(InterpolateState.TimeToTargetValue, InterpolateState.DeltaTime + InterpolateState.AverageDeltaTime);
434-
InterpolateState.PredictValue = SmoothDamp(InterpolateState.PredictValue, InterpolateState.PredictTarget, ref m_PredictedRateOfChange, InterpolateState.TimeToTargetValue, predictedTime);
442+
//InterpolateState.PreviousValue = SmoothDamp(InterpolateState.PreviousValue, InterpolateState.Target.Value.Item, ref m_RateOfChange, (float)InterpolateState.TimeToTargetValue, (float)InterpolateState.DeltaTime);
443+
//var predictedTime = InterpolateState.PredictingNext ? InterpolateState.DeltaTime : Math.Min(InterpolateState.TimeToTargetValue, InterpolateState.DeltaTime + InterpolateState.AverageDeltaTime);
444+
//InterpolateState.PredictValue = SmoothDamp(InterpolateState.PredictValue, InterpolateState.PredictTarget, ref m_PredictedRateOfChange, (float)InterpolateState.TimeToTargetValue, (float)predictedTime);
445+
InterpolateState.Phase1Value = SmoothDamp(InterpolateState.CurrentValue, InterpolateState.Target.Value.Item, ref m_RateOfChange, (float)InterpolateState.TimeToTargetValue, (float)InterpolateState.DeltaTime);
446+
var predictedTime = InterpolateState.PredictingNext ? InterpolateState.DeltaTime : Math.Min(InterpolateState.TimeToTargetValue, InterpolateState.DeltaTime + InterpolateState.AverageDeltaTime);
447+
InterpolateState.PredictValue = SmoothDamp(InterpolateState.Phase1Value, InterpolateState.Target.Value.Item, ref m_PredictedRateOfChange, (float)InterpolateState.TimeToTargetValue, (float)predictedTime);
448+
// Determine if smooth dampening is enabled to get our lerp "t" time
449+
var timeDelta = lerpSmoothing ? deltaTime / MaximumInterpolationTime : deltaTime;
450+
// Lerp between the PreviousValue and PredictedValue using the calculated time delta
451+
InterpolateState.CurrentValue = Interpolate(InterpolateState.PreviousValue, InterpolateState.PredictValue, timeDelta);
435452
}
436453
else
437454
{
438-
InterpolateState.PreviousValue = Interpolate(InterpolateState.PreviousValue, InterpolateState.Target.Value.Item, InterpolateState.LerpT);
439-
InterpolateState.PredictValue = InterpolateUnclamped(InterpolateState.PredictValue, InterpolateState.Target.Value.Item, InterpolateState.LerpTPredict);
440-
}
455+
InterpolateState.PreviousValue = Interpolate(InterpolateState.Phase1Value, InterpolateState.Target.Value.Item, InterpolateState.LerpT);
441456

442-
// Lerp between the PreviousValue and PredictedValue (extrapolated) using this frame's delta time
443-
var targetValue = Interpolate(InterpolateState.PreviousValue, InterpolateState.PredictValue, deltaTime);
444-
if (lerpSmoothing)
445-
{
446-
// If lerp smoothing is enabled, then smooth current value towards the target value
447-
InterpolateState.CurrentValue = Interpolate(InterpolateState.CurrentValue, targetValue, deltaTime / MaximumInterpolationTime);
448-
}
449-
else
450-
{
451-
// Otherwise, just assign the target value.
452-
InterpolateState.CurrentValue = targetValue;
457+
// Note: InterpolateState.LerpTPredict is clamped to LerpT if we have no next target
458+
InterpolateState.PredictValue = InterpolateUnclamped(InterpolateState.Phase2Value, InterpolateState.Target.Value.Item, InterpolateState.LerpTPredict);
459+
460+
// Lerp between the PreviousValue and PredictedValue using this frame's delta time
461+
var targetValue = Interpolate(InterpolateState.PreviousValue, InterpolateState.PredictValue, deltaTime);
462+
if (lerpSmoothing)
463+
{
464+
// If lerp smoothing is enabled, then smooth current value towards the target value
465+
InterpolateState.CurrentValue = Interpolate(InterpolateState.CurrentValue, targetValue, deltaTime / MaximumInterpolationTime);
466+
}
467+
else
468+
{
469+
// Otherwise, just assign the target value.
470+
InterpolateState.CurrentValue = targetValue;
471+
}
453472
}
454473
}
455474
}

com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3923,13 +3923,12 @@ private void UpdateInterpolation()
39233923
{
39243924
AdjustForChangeInTransformSpace();
39253925

3926-
var cachedServerTime = m_CachedNetworkManager.ServerTime.Time;
3926+
var currentTime = m_CachedNetworkManager.ServerTime.Time;
39273927
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
39283928
var cachedDeltaTime = m_UseRigidbodyForMotion ? Time.fixedDeltaTime : Time.deltaTime;
39293929
#else
39303930
var cachedDeltaTime = Time.deltaTime;
39313931
#endif
3932-
39333932
// Optional user defined tick offset to be used to push the "render time" (the time that will be used to determine if a state update is available)
39343933
// back in order to provide more room for the interpolator to interpolate towards when latency conditions are impacting the frequency that state
39353934
// updates are received.
@@ -3951,10 +3950,10 @@ private void UpdateInterpolation()
39513950
}
39523951
}
39533952

3954-
var tickLatencyAsTime = m_CachedNetworkManager.LocalTime.TimeTicksAgo(tickLatency).Time;
3953+
var tickLatencyAsTime = m_CachedNetworkManager.ServerTime.TimeTicksAgo(tickLatency).Time;
39553954
// Smooth dampening and extrapolation specific:
39563955
// We clamp between the tick rate frequency and the tick latency x tick rate frequency
3957-
var minDeltaTime = m_CachedNetworkManager.LocalTime.FixedDeltaTime;
3956+
var minDeltaTime = m_CachedNetworkManager.ServerTime.FixedDeltaTimeAsDouble;
39583957

39593958
// Maximum delta time is the maximum time we will lerp between values. If the time exceeds this due to extreme
39603959
// latency then the value's interpolation rate will be accelerated to reach the goal and continue interpolating
@@ -3979,7 +3978,7 @@ private void UpdateInterpolation()
39793978

39803979
if (PositionInterpolationType == InterpolationTypes.Lerp)
39813980
{
3982-
m_PositionInterpolator.Update(cachedDeltaTime, tickLatencyAsTime, cachedServerTime, PositionLerpSmoothing);
3981+
m_PositionInterpolator.Update(cachedDeltaTime, tickLatencyAsTime, currentTime, PositionLerpSmoothing);
39833982
}
39843983
else
39853984
{
@@ -4009,7 +4008,7 @@ private void UpdateInterpolation()
40094008
// When using full precision Slerp towards the target rotation.
40104009
/// <see cref="BufferedLinearInterpolatorQuaternion.IsSlerp"/>
40114010
m_RotationInterpolator.IsSlerp = !UseHalfFloatPrecision;
4012-
m_RotationInterpolator.Update(cachedDeltaTime, tickLatencyAsTime, cachedServerTime, RotationLerpSmoothing);
4011+
m_RotationInterpolator.Update(cachedDeltaTime, tickLatencyAsTime, currentTime, RotationLerpSmoothing);
40134012
}
40144013
else
40154014
{
@@ -4035,7 +4034,7 @@ private void UpdateInterpolation()
40354034

40364035
if (ScaleInterpolationType == InterpolationTypes.Lerp)
40374036
{
4038-
m_ScaleInterpolator.Update(cachedDeltaTime, tickLatencyAsTime, cachedServerTime, ScaleLerpSmoothing);
4037+
m_ScaleInterpolator.Update(cachedDeltaTime, tickLatencyAsTime, currentTime, ScaleLerpSmoothing);
40394038
}
40404039
else
40414040
{

0 commit comments

Comments
 (0)