Skip to content

Commit 4e705fe

Browse files
update
Minor adjustments after a long debug session...
1 parent e7eaecd commit 4e705fe

File tree

2 files changed

+55
-45
lines changed

2 files changed

+55
-45
lines changed

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

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ public BufferedItem(T item, double timeSent, int itemId)
115115
internal struct CurrentState
116116
{
117117
public BufferedItem? Target;
118-
119118
public double StartTime;
120119
public double EndTime;
121120
public double TimeToTargetValue;
@@ -129,7 +128,6 @@ internal struct CurrentState
129128
public T CurrentValue;
130129
public T PreviousValue;
131130
public T PredictValue;
132-
public T PredictTarget;
133131
public T Phase1Value;
134132
public T Phase2Value;
135133

@@ -151,8 +149,6 @@ public void AddDeltaTime(float deltaTime)
151149
}
152150
DeltaTime = Math.Min(DeltaTime + deltaTime, TimeToTargetValue);
153151
DeltaTimePredict = Math.Min(DeltaTime + deltaTime, TimeToTargetValue + MaxDeltaTime);
154-
//DeltaTime = Math.Min(DeltaTime + m_AverageDeltaTime, TimeToTargetValue);
155-
//DeltaTimePredict = Math.Min(DeltaTimePredict + m_AverageDeltaTime, TimeToTargetValue + MaxDeltaTime);
156152
LerpT = (float)(TimeToTargetValue == 0.0 ? 1.0 : DeltaTime / TimeToTargetValue);
157153
if (PredictingNext)
158154
{
@@ -174,12 +170,6 @@ public void SetTimeToTarget(double timeToTarget)
174170
TimeToTargetValue = timeToTarget;
175171
}
176172

177-
public void ResetDelta()
178-
{
179-
m_AverageDeltaTime = 0.0f;
180-
DeltaTime = 0.0f;
181-
}
182-
183173
public bool TargetTimeAproximatelyReached(float adjustForNext = 1.0f)
184174
{
185175
if (!Target.HasValue)
@@ -195,7 +185,6 @@ public void Reset(T currentValue)
195185
CurrentValue = currentValue;
196186
PreviousValue = currentValue;
197187
PredictValue = currentValue;
198-
PredictTarget = currentValue;
199188
Phase1Value = currentValue;
200189
Phase2Value = currentValue;
201190
TargetReached = false;
@@ -207,7 +196,7 @@ public void Reset(T currentValue)
207196
TimeToTargetValue = 0.0f;
208197
DeltaTime = 0.0f;
209198
DeltaTimePredict = 0.0f;
210-
ResetDelta();
199+
m_AverageDeltaTime = 0.0f;
211200
}
212201
}
213202

@@ -332,7 +321,7 @@ private void TryConsumeFromBuffer(double renderTime, double minDeltaTime, double
332321
if (!noStateSet)
333322
{
334323
potentialItemNeedsProcessing = (potentialItem.TimeSent <= renderTime) && potentialItem.TimeSent >= InterpolateState.Target.Value.TimeSent;
335-
currentTargetTimeReached = InterpolateState.TargetTimeAproximatelyReached(potentialItemNeedsProcessing ? 1.15f : 0.85f);
324+
currentTargetTimeReached = InterpolateState.TargetTimeAproximatelyReached(potentialItemNeedsProcessing ? 1.25f : 0.75f);
336325
if (!potentialItemNeedsProcessing && !InterpolateState.TargetReached)
337326
{
338327
InterpolateState.TargetReached = IsAproximately(InterpolateState.CurrentValue, InterpolateState.Target.Value.Item);
@@ -371,23 +360,20 @@ private void TryConsumeFromBuffer(double renderTime, double minDeltaTime, double
371360
InterpolateState.TargetReached = false;
372361
InterpolateState.PredictingNext = false;
373362
startTime = InterpolateState.Target.Value.TimeSent;
374-
InterpolateState.Phase1Value = InterpolateState.PreviousValue;
375-
InterpolateState.Phase2Value = InterpolateState.PredictValue;
376-
InterpolateState.PredictTarget = target.Item;
363+
if (isPredictedLerp)
364+
{
365+
InterpolateState.Phase1Value = InterpolateState.PreviousValue;
366+
InterpolateState.Phase2Value = Interpolate(InterpolateState.Phase1Value, target.Item, InterpolateState.AverageDeltaTime);
367+
}
377368
InterpolateState.MaxDeltaTime = maxDeltaTime;
378369
}
379-
if (m_BufferQueue.TryPeek(out BufferedItem lookAheadItem))
380-
{
381-
InterpolateState.PredictTarget = Interpolate(target.Item, lookAheadItem.Item, InterpolateState.AverageDeltaTime);
382-
InterpolateState.PredictingNext = true;
383-
}
370+
InterpolateState.PredictingNext = m_BufferQueue.Count > 0;
384371
// TODO: We might consider creating yet another queue to add these items to and assure that the time is accelerated
385372
// for each item as opposed to losing the resolution of the values.
386373
var timeToTarget = Math.Clamp((float)(target.TimeSent - startTime), minDeltaTime, maxDeltaTime);
387374
InterpolateState.SetTimeToTarget(timeToTarget);
388375
InterpolateState.Target = target;
389376
}
390-
InterpolateState.ResetDelta();
391377
}
392378
}
393379
else
@@ -435,37 +421,32 @@ internal T Update(float deltaTime, double tickLatencyAsTime, double minDeltaTime
435421
if (!InterpolateState.TargetReached)
436422
{
437423
InterpolateState.AddDeltaTime(deltaTime);
438-
424+
var targetValue = InterpolateState.CurrentValue;
439425
// SmoothDampen or LerpExtrapolateBlend
440426
if (!isLerpAndExtrapolate)
441427
{
442428
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.Target.Value.Item, ref m_PredictedRateOfChange, (float)InterpolateState.TimeToTargetValue, (float)predictedTime);
445-
// Determine if smooth dampening is enabled to get our lerp "t" time
446-
var timeDelta = lerpSmoothing ? deltaTime / MaximumInterpolationTime : deltaTime;
447-
// Lerp between the PreviousValue and PredictedValue using the calculated time delta
448-
InterpolateState.CurrentValue = Interpolate(InterpolateState.PreviousValue, InterpolateState.PredictValue, timeDelta);
429+
InterpolateState.PredictValue = SmoothDamp(InterpolateState.PredictValue, InterpolateState.Target.Value.Item, ref m_PredictedRateOfChange, (float)InterpolateState.TimeToTargetValue, (float)(InterpolateState.DeltaTime + deltaTime));
449430
}
450431
else
451432
{
452433
InterpolateState.PreviousValue = Interpolate(InterpolateState.Phase1Value, InterpolateState.Target.Value.Item, InterpolateState.LerpT);
453-
454434
// Note: InterpolateState.LerpTPredict is clamped to LerpT if we have no next target
455435
InterpolateState.PredictValue = InterpolateUnclamped(InterpolateState.Phase2Value, InterpolateState.Target.Value.Item, InterpolateState.LerpTPredict);
436+
}
456437

457-
// Lerp between the PreviousValue and PredictedValue using this frame's delta time
458-
var targetValue = Interpolate(InterpolateState.PreviousValue, InterpolateState.PredictValue, deltaTime);
459-
if (lerpSmoothing)
460-
{
461-
// If lerp smoothing is enabled, then smooth current value towards the target value
462-
InterpolateState.CurrentValue = Interpolate(InterpolateState.CurrentValue, targetValue, deltaTime / MaximumInterpolationTime);
463-
}
464-
else
465-
{
466-
// Otherwise, just assign the target value.
467-
InterpolateState.CurrentValue = targetValue;
468-
}
438+
// Lerp between the PreviousValue and PredictedValue using the calculated time delta
439+
targetValue = Interpolate(InterpolateState.PreviousValue, InterpolateState.PredictValue, deltaTime);
440+
441+
if (lerpSmoothing)
442+
{
443+
// If lerp smoothing is enabled, then smooth current value towards the target value
444+
InterpolateState.CurrentValue = Interpolate(InterpolateState.CurrentValue, targetValue, deltaTime / MaximumInterpolationTime);
445+
}
446+
else
447+
{
448+
// Otherwise, just assign the target value.
449+
InterpolateState.CurrentValue = targetValue;
469450
}
470451
}
471452
}
@@ -522,7 +503,6 @@ private void TryConsumeFromBuffer(double renderTime, double serverTime)
522503
InterpolateState.EndTime = target.TimeSent;
523504
InterpolateState.Target = target;
524505
}
525-
InterpolateState.ResetDelta();
526506
}
527507
}
528508
else

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3916,8 +3916,24 @@ public void Teleport(Vector3 newPosition, Quaternion newRotation, Vector3 newSca
39163916
#region UPDATES AND AUTHORITY CHECKS
39173917
private NetworkTransformTickRegistration m_NetworkTransformTickRegistration;
39183918

3919+
#if DEBUG_LINEARBUFFER
3920+
public struct NTPositionStats
3921+
{
3922+
public int Tick;
3923+
public double Time;
3924+
public double TicksAgoTime;
3925+
public int BufferCount;
3926+
public BufferedLinearInterpolator<Vector3>.CurrentState CurrentState;
3927+
}
39193928

3929+
public List<NTPositionStats> PositionStats = new List<NTPositionStats>();
3930+
public bool GatherStats;
39203931

3932+
public void ClearStats()
3933+
{
3934+
PositionStats.Clear();
3935+
}
3936+
#endif
39213937
// Non-Authority
39223938
private void UpdateInterpolation()
39233939
{
@@ -3927,7 +3943,7 @@ private void UpdateInterpolation()
39273943
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
39283944
var cachedDeltaTime = m_UseRigidbodyForMotion ? m_CachedNetworkManager.RealTimeProvider.FixedDeltaTime : m_CachedNetworkManager.RealTimeProvider.DeltaTime;
39293945
#else
3930-
var cachedDeltaTime = Time.deltaTime;
3946+
var cachedDeltaTime = m_CachedNetworkManager.RealTimeProvider.DeltaTime;
39313947
#endif
39323948
// 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)
39333949
// back in order to provide more room for the interpolator to interpolate towards when latency conditions are impacting the frequency that state
@@ -4042,6 +4058,21 @@ private void UpdateInterpolation()
40424058
ScaleInterpolationType == InterpolationTypes.LerpExtrapolateBlend, ScaleLerpSmoothing);
40434059
}
40444060
}
4061+
4062+
#if DEBUG_LINEARBUFFER
4063+
if (GatherStats)
4064+
{
4065+
var posStats = new NTPositionStats()
4066+
{
4067+
Tick = timeSystem.Tick,
4068+
Time = timeSystem.Time,
4069+
TicksAgoTime = tickLatencyAsTime,
4070+
BufferCount = m_PositionInterpolator.m_BufferQueue.Count,
4071+
CurrentState = m_PositionInterpolator.InterpolateState,
4072+
};
4073+
PositionStats.Add(posStats);
4074+
}
4075+
#endif
40454076
}
40464077

40474078
/// <inheritdoc cref="INetworkUpdateSystem.OnUpdate"/>
@@ -4067,7 +4098,6 @@ public virtual void OnUpdate()
40674098
UpdateInterpolation();
40684099
}
40694100

4070-
40714101
// Apply the current authoritative state
40724102
ApplyAuthoritativeState();
40734103
}

0 commit comments

Comments
 (0)