Skip to content

Commit 92c8052

Browse files
fix: Only substitute the retention limit at a maximum interpolation time of 1.0
Clamping to k_MaximumLerpSmoothRetention flattened everything above it, so 0.995 smoothed at the same rate as 0.99 and stopped honoring "higher is smoother". Clamp to the legal range first and substitute only at the freezing endpoint. Also corrects the lerp smoothing remarks: LegacyLerp still applies deltaTime / MaximumInterpolationTime per frame, so the frame rate independence claim only holds for Lerp and SmoothDampening.
1 parent cd0422f commit 92c8052

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,11 @@ internal void ResetCurrentState()
443443
[MethodImpl(MethodImplOptions.AggressiveInlining)]
444444
private float GetLerpSmoothTime(float deltaTime)
445445
{
446-
var retained = Mathf.Clamp(MaximumInterpolationTime, 0.0f, k_MaximumLerpSmoothRetention);
446+
var retained = Mathf.Clamp01(MaximumInterpolationTime);
447+
if (retained >= 1.0f)
448+
{
449+
retained = k_MaximumLerpSmoothRetention;
450+
}
447451
return 1.0f - Mathf.Pow(retained, deltaTime * k_LerpSmoothReferenceFrameRate);
448452
}
449453

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,8 +1238,9 @@ public enum InterpolationTypes
12381238
/// <remarks>
12391239
/// When enabled, the <see cref="BufferedLinearInterpolator{T}"/> will apply a final lerping pass towards
12401240
/// the interpolated result at a rate determined by <see cref="PositionMaxInterpolationTime"/>.<br />
1241-
/// This is frame rate independent for all <see cref="InterpolationTypes"/>, but the same value will not
1242-
/// produce the same result under <see cref="InterpolationTypes.LegacyLerp"/> as it does under the others.
1241+
/// This smoothing pass is frame rate independent under <see cref="InterpolationTypes.Lerp"/> and
1242+
/// <see cref="InterpolationTypes.SmoothDampening"/>. <see cref="InterpolationTypes.LegacyLerp"/> keeps its
1243+
/// original frame rate dependent smoothing, so the same value does not produce the same result there.
12431244
/// </remarks>
12441245
public bool PositionLerpSmoothing = true;
12451246
private bool m_PreviousPositionLerpSmoothing;
@@ -1262,8 +1263,9 @@ public enum InterpolationTypes
12621263
/// <remarks>
12631264
/// When enabled, the <see cref="BufferedLinearInterpolator{T}"/> will apply a final lerping pass towards
12641265
/// the interpolated result at a rate determined by <see cref="RotationMaxInterpolationTime"/>.<br />
1265-
/// This is frame rate independent for all <see cref="InterpolationTypes"/>, but the same value will not
1266-
/// produce the same result under <see cref="InterpolationTypes.LegacyLerp"/> as it does under the others.
1266+
/// This smoothing pass is frame rate independent under <see cref="InterpolationTypes.Lerp"/> and
1267+
/// <see cref="InterpolationTypes.SmoothDampening"/>. <see cref="InterpolationTypes.LegacyLerp"/> keeps its
1268+
/// original frame rate dependent smoothing, so the same value does not produce the same result there.
12671269
/// </remarks>
12681270
public bool RotationLerpSmoothing = true;
12691271
private bool m_PreviousRotationLerpSmoothing;
@@ -1286,8 +1288,9 @@ public enum InterpolationTypes
12861288
/// <remarks>
12871289
/// When enabled, the <see cref="BufferedLinearInterpolator{T}"/> will apply a final lerping pass towards
12881290
/// the interpolated result at a rate determined by <see cref="ScaleMaxInterpolationTime"/>.<br />
1289-
/// This is frame rate independent for all <see cref="InterpolationTypes"/>, but the same value will not
1290-
/// produce the same result under <see cref="InterpolationTypes.LegacyLerp"/> as it does under the others.
1291+
/// This smoothing pass is frame rate independent under <see cref="InterpolationTypes.Lerp"/> and
1292+
/// <see cref="InterpolationTypes.SmoothDampening"/>. <see cref="InterpolationTypes.LegacyLerp"/> keeps its
1293+
/// original frame rate dependent smoothing, so the same value does not produce the same result there.
12911294
/// </remarks>
12921295
public bool ScaleLerpSmoothing = true;
12931296
private bool m_PreviousScaleLerpSmoothing;

com.unity.netcode.gameobjects/Tests/Editor/InterpolatorTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,22 @@ public void LerpSmoothingIsFrameRateIndependent()
382382
$"{atTwoFortyFps} at 240fps. The smoothing rate is scaling with the frame rate.");
383383
}
384384

385+
/// <summary>
386+
/// Only 1.0f is substituted for, so settings below it keep their own smoothing rate and a heavier
387+
/// setting stays smoother than a lighter one.
388+
/// </summary>
389+
[Test]
390+
public void LerpSmoothingPreservesSettingsBelowTheMaximum()
391+
{
392+
// 0.99f is the retention substituted for 1.0f, so clamping to it would collapse these two.
393+
var lighter = RunLerpSmoothing(0.99f, 1.0f / 60.0f, true);
394+
var heavier = RunLerpSmoothing(0.995f, 1.0f / 60.0f, true);
395+
396+
Assert.That(heavier, Is.LessThan(lighter),
397+
$"0.995 converged to {heavier} and 0.99 to {lighter} over the same motion. A higher maximum " +
398+
"interpolation time has to retain more of the previous value, so it cannot converge first.");
399+
}
400+
385401
#endregion
386402
}
387403
}

0 commit comments

Comments
 (0)