Skip to content

Commit ff552c5

Browse files
update
- Removed the lerp smoothing parameter and made it an internal bool (don't need to add to API and it should be set through the NetworkTransform anyway). - Added additional BufferedLinearInterpolator debug code. - Added additional PreUpdate pass for NetworkTransforms using Rigidbody motion in order to reset a fixed time delta. - Added incemental fixed time delta when the FixedUpdate is invoked multiple times within a single frame. - Added internal time sync counter. - Added a "1/3rd" rule to smooth dampening and LerpExtrapolateBlend when smooth lerp is enabled in order to still be able to balance between smoothing the final lerped value and reaching the target (or getting much closer).
1 parent 8005e8c commit ff552c5

File tree

5 files changed

+192
-32
lines changed

5 files changed

+192
-32
lines changed

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ public void Reset(T currentValue)
200200
}
201201
}
202202

203+
internal bool LerpSmoothEnabled;
204+
203205
/// <summary>
204206
/// Determines how much smoothing will be applied to the 2nd lerp when using the <see cref="Update(float, double, double)"/> (i.e. lerping and not smooth dampening).
205207
/// </summary>
@@ -293,7 +295,7 @@ private void InternalReset(T targetValue, double serverTime, bool isAngularValue
293295
}
294296
}
295297

296-
#region Smooth Dampening Interpolation
298+
#region Smooth Dampening and Lerp Extrapolate Blend Interpolation Handling
297299
/// <summary>
298300
/// TryConsumeFromBuffer: Smooth Dampening Version
299301
/// </summary>
@@ -308,6 +310,7 @@ private void TryConsumeFromBuffer(double renderTime, double minDeltaTime, double
308310
var noStateSet = !InterpolateState.Target.HasValue;
309311
var potentialItemNeedsProcessing = false;
310312
var currentTargetTimeReached = false;
313+
var dequeuedCount = 0;
311314

312315
while (m_BufferQueue.TryPeek(out BufferedItem potentialItem))
313316
{
@@ -321,7 +324,7 @@ private void TryConsumeFromBuffer(double renderTime, double minDeltaTime, double
321324
if (!noStateSet)
322325
{
323326
potentialItemNeedsProcessing = (potentialItem.TimeSent <= renderTime) && potentialItem.TimeSent >= InterpolateState.Target.Value.TimeSent;
324-
currentTargetTimeReached = InterpolateState.TargetTimeAproximatelyReached(potentialItemNeedsProcessing ? 1.25f : 0.75f);
327+
currentTargetTimeReached = InterpolateState.TargetTimeAproximatelyReached(potentialItemNeedsProcessing ? 1.0f : 0.85f) || InterpolateState.TargetReached;
325328
if (!potentialItemNeedsProcessing && !InterpolateState.TargetReached)
326329
{
327330
InterpolateState.TargetReached = IsAproximately(InterpolateState.CurrentValue, InterpolateState.Target.Value.Item);
@@ -337,6 +340,7 @@ private void TryConsumeFromBuffer(double renderTime, double minDeltaTime, double
337340
{
338341
if (m_BufferQueue.TryDequeue(out BufferedItem target))
339342
{
343+
dequeuedCount++;
340344
if (!InterpolateState.Target.HasValue)
341345
{
342346
InterpolateState.Target = target;
@@ -358,20 +362,18 @@ private void TryConsumeFromBuffer(double renderTime, double minDeltaTime, double
358362
{
359363
alreadyHasBufferItem = true;
360364
InterpolateState.TargetReached = false;
361-
InterpolateState.PredictingNext = false;
362365
startTime = InterpolateState.Target.Value.TimeSent;
363366
if (isPredictedLerp)
364367
{
365368
InterpolateState.Phase1Value = InterpolateState.PreviousValue;
366-
InterpolateState.Phase2Value = Interpolate(InterpolateState.Phase1Value, target.Item, InterpolateState.AverageDeltaTime);
369+
InterpolateState.Phase2Value = Interpolate(InterpolateState.PredictValue, target.Item, InterpolateState.AverageDeltaTime);
367370
}
368371
InterpolateState.MaxDeltaTime = maxDeltaTime;
372+
InterpolateState.PredictingNext = m_BufferQueue.Count > 0;
369373
}
370-
InterpolateState.PredictingNext = m_BufferQueue.Count > 0;
371374
// TODO: We might consider creating yet another queue to add these items to and assure that the time is accelerated
372375
// for each item as opposed to losing the resolution of the values.
373-
var timeToTarget = Math.Clamp((float)(target.TimeSent - startTime), minDeltaTime, maxDeltaTime);
374-
InterpolateState.SetTimeToTarget(timeToTarget);
376+
InterpolateState.SetTimeToTarget(Math.Clamp((float)(target.TimeSent - startTime), minDeltaTime, maxDeltaTime * dequeuedCount));
375377
InterpolateState.Target = target;
376378
}
377379
}
@@ -394,6 +396,8 @@ internal void ResetCurrentState()
394396
if (InterpolateState.Target.HasValue)
395397
{
396398
InterpolateState.Reset(InterpolateState.CurrentValue);
399+
m_RateOfChange = default;
400+
m_PredictedRateOfChange = default;
397401
}
398402
}
399403

@@ -411,7 +415,7 @@ internal void ResetCurrentState()
411415
/// <param name="isLerpAndExtrapolate">Determines whether to use smooth dampening or extrapolation.</param>
412416
/// <param name="lerpSmoothing">Determines if lerp smoothing is enabled for this instance.</param>
413417
/// <returns>The newly interpolated value of type 'T'</returns>
414-
internal T Update(float deltaTime, double tickLatencyAsTime, double minDeltaTime, double maxDeltaTime, bool isLerpAndExtrapolate, bool lerpSmoothing)
418+
internal T Update(float deltaTime, double tickLatencyAsTime, double minDeltaTime, double maxDeltaTime, bool isLerpAndExtrapolate)
415419
{
416420
TryConsumeFromBuffer(tickLatencyAsTime, minDeltaTime, maxDeltaTime, isLerpAndExtrapolate);
417421
// Only begin interpolation when there is a start and end point
@@ -420,6 +424,8 @@ internal T Update(float deltaTime, double tickLatencyAsTime, double minDeltaTime
420424
// As long as the target hasn't been reached, interpolate or smooth dampen.
421425
if (!InterpolateState.TargetReached)
422426
{
427+
// Increases the time delta relative to the time to target.
428+
// Also calculates the LerpT and LerpTPredicted values.
423429
InterpolateState.AddDeltaTime(deltaTime);
424430
var targetValue = InterpolateState.CurrentValue;
425431
// SmoothDampen or LerpExtrapolateBlend
@@ -438,10 +444,13 @@ internal T Update(float deltaTime, double tickLatencyAsTime, double minDeltaTime
438444
// Lerp between the PreviousValue and PredictedValue using the calculated time delta
439445
targetValue = Interpolate(InterpolateState.PreviousValue, InterpolateState.PredictValue, deltaTime);
440446

441-
if (lerpSmoothing)
447+
// If lerp smoothing is enabled, then smooth current value towards the target value
448+
if (LerpSmoothEnabled)
442449
{
443-
// If lerp smoothing is enabled, then smooth current value towards the target value
444-
InterpolateState.CurrentValue = Interpolate(InterpolateState.CurrentValue, targetValue, deltaTime / MaximumInterpolationTime);
450+
// Progress by 1/3rd of the way towards the target in order to assure the target is reached sooner
451+
var oneThirdPoint = Interpolate(InterpolateState.CurrentValue, targetValue, 0.3333f);
452+
// Apply the smooth lerp from the oneThirpoint to the target to help smooth the final value
453+
InterpolateState.CurrentValue = Interpolate(oneThirdPoint, targetValue, deltaTime / MaximumInterpolationTime);
445454
}
446455
else
447456
{
@@ -530,7 +539,7 @@ private void TryConsumeFromBuffer(double renderTime, double serverTime)
530539
/// <param name="serverTime">current server time</param>
531540
/// <param name="lerpSmoothing">Determines if lerp smoothing is enabled for this instance.</param>
532541
/// <returns>The newly interpolated value of type 'T'</returns>
533-
public T Update(float deltaTime, double renderTime, double serverTime, bool lerpSmoothing = true)
542+
public T Update(float deltaTime, double renderTime, double serverTime)
534543
{
535544
TryConsumeFromBuffer(renderTime, serverTime);
536545
// Only interpolate when there is a start and end point and we have not already reached the end value
@@ -557,7 +566,7 @@ public T Update(float deltaTime, double renderTime, double serverTime, bool lerp
557566
}
558567
var target = Interpolate(InterpolateState.PreviousValue, InterpolateState.Target.Value.Item, t);
559568

560-
if (lerpSmoothing)
569+
if (LerpSmoothEnabled)
561570
{
562571
// Assure our MaximumInterpolationTime is valid and that the second lerp time ranges between deltaTime and 1.0f.
563572
var secondLerpTime = Mathf.Clamp(deltaTime / MaximumInterpolationTime, deltaTime, 1.0f);

0 commit comments

Comments
 (0)