Skip to content

Commit 4fe72f5

Browse files
update
Some more adjustments to permissions and some final clean up of BufferedLinearInterpolator<T>
1 parent 08dc79a commit 4fe72f5

File tree

3 files changed

+97
-81
lines changed

3 files changed

+97
-81
lines changed

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

Lines changed: 95 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,68 @@ namespace Unity.Netcode
1111
/// <typeparam name="T">The type of interpolated value</typeparam>
1212
public abstract class BufferedLinearInterpolator<T> where T : struct
1313
{
14+
// Constant absolute value for max buffer count instead of dynamic time based value. This is in case we have very low tick rates, so
15+
// that we don't have a very small buffer because of this.
16+
private const int k_BufferCountLimit = 100;
1417
private const float k_AproximatePrecision = 0.0001f;
18+
private const double k_SmallValue = 9.999999439624929E-11; // copied from Vector3's equal operator
19+
20+
#region Legacy notes
21+
// Buffer consumption scenarios
22+
// Perfect case consumption
23+
// | 1 | 2 | 3 |
24+
// | 2 | 3 | 4 | consume 1
25+
// | 3 | 4 | 5 | consume 2
26+
// | 4 | 5 | 6 | consume 3
27+
// | 5 | 6 | 7 | consume 4
28+
// jittered case
29+
// | 1 | 2 | 3 |
30+
// | 2 | 3 | | consume 1
31+
// | 3 | | | consume 2
32+
// | 4 | 5 | 6 | consume 3
33+
// | 5 | 6 | 7 | consume 4
34+
// bursted case (assuming max count is 5)
35+
// | 1 | 2 | 3 |
36+
// | 2 | 3 | | consume 1
37+
// | 3 | | | consume 2
38+
// | | | | consume 3
39+
// | | | |
40+
// | 4 | 5 | 6 | 7 | 8 | --> consume all and teleport to last value <8> --> this is the nuclear option, ideally this example would consume 4 and 5
41+
// instead of jumping to 8, but since in OnValueChange we don't yet have an updated server time (updated in pre-update) to know which value
42+
// we should keep and which we should drop, we don't have enough information to do this. Another thing would be to not have the burst in the first place.
43+
#endregion
44+
45+
#region Properties being deprecated
46+
/// <summary>
47+
/// The legacy list of <see cref="BufferedItem"/> items.
48+
/// </summary>
49+
/// <remarks>
50+
/// This is replaced by the <see cref="m_BufferQueue"/> of type <see cref="Queue{T}"/>.
51+
/// </remarks>
52+
[Obsolete("This list is no longer used and will be deprecated.", false)]
53+
protected internal readonly List<BufferedItem> m_Buffer = new List<BufferedItem>();
54+
55+
/// <summary>
56+
/// ** Deprecating **
57+
/// The starting value of type <see cref="T"/> to interpolate from.
58+
/// </summary>
59+
[Obsolete("This property will be deprecated.", false)]
60+
protected internal T m_InterpStartValue;
61+
62+
/// <summary>
63+
/// ** Deprecating **
64+
/// The current value of type <see cref="T"/>.
65+
/// </summary>
66+
[Obsolete("This property will be deprecated.", false)]
67+
protected internal T m_CurrentInterpValue;
68+
69+
/// <summary>
70+
/// ** Deprecating **
71+
/// The end (or target) value of type <see cref="T"/> to interpolate towards.
72+
/// </summary>
73+
[Obsolete("This property will be deprecated.", false)]
74+
protected internal T m_InterpEndValue;
75+
#endregion
1576

1677
/// <summary>
1778
/// Represents a buffered item measurement.
@@ -44,6 +105,13 @@ public BufferedItem(T item, double timeSent, int itemId)
44105
ItemId = itemId;
45106
}
46107
}
108+
109+
/// <summary>
110+
/// The current internal state of the <see cref="BufferedLinearInterpolator{T}"/>.
111+
/// </summary>
112+
/// <remarks>
113+
/// Not public API ready yet.
114+
/// </remarks>
47115
internal struct CurrentState
48116
{
49117
public BufferedItem? Target;
@@ -105,35 +173,6 @@ public void Reset(T currentValue)
105173
}
106174
}
107175

108-
// Buffer consumption scenarios
109-
// Perfect case consumption
110-
// | 1 | 2 | 3 |
111-
// | 2 | 3 | 4 | consume 1
112-
// | 3 | 4 | 5 | consume 2
113-
// | 4 | 5 | 6 | consume 3
114-
// | 5 | 6 | 7 | consume 4
115-
// jittered case
116-
// | 1 | 2 | 3 |
117-
// | 2 | 3 | | consume 1
118-
// | 3 | | | consume 2
119-
// | 4 | 5 | 6 | consume 3
120-
// | 5 | 6 | 7 | consume 4
121-
// bursted case (assuming max count is 5)
122-
// | 1 | 2 | 3 |
123-
// | 2 | 3 | | consume 1
124-
// | 3 | | | consume 2
125-
// | | | | consume 3
126-
// | | | |
127-
// | 4 | 5 | 6 | 7 | 8 | --> consume all and teleport to last value <8> --> this is the nuclear option, ideally this example would consume 4 and 5
128-
// instead of jumping to 8, but since in OnValueChange we don't yet have an updated server time (updated in pre-update) to know which value
129-
// we should keep and which we should drop, we don't have enough information to do this. Another thing would be to not have the burst in the first place.
130-
131-
// Constant absolute value for max buffer count instead of dynamic time based value. This is in case we have very low tick rates, so
132-
// that we don't have a very small buffer because of this.
133-
private const int k_BufferCountLimit = 100;
134-
135-
private const double k_SmallValue = 9.999999439624929E-11; // copied from Vector3's equal operator
136-
137176
/// <summary>
138177
/// 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).
139178
/// </summary>
@@ -145,61 +184,27 @@ public void Reset(T currentValue)
145184
[Range(0.016f, 1.0f)]
146185
public float MaximumInterpolationTime = 0.1f;
147186

148-
/// <summary>
149-
/// The maximum Lerp "t" boundary when using standard lerping for interpolation
150-
/// </summary>
151-
internal float MaxInterpolationBound = 3.0f;
152-
153-
private int m_BufferCount;
154-
155-
private BufferedItem m_LastBufferedItemReceived;
156-
private int m_NbItemsReceivedThisFrame;
157-
158-
private double m_LastMeasurementAddedTime = 0.0f;
159-
internal bool EndOfBuffer => m_BufferQueue.Count == 0;
160-
161-
internal bool InLocalSpace;
162-
163-
/// <summary>
164-
/// The current interpolation state
165-
/// </summary>
166-
internal CurrentState InterpolateState;
167-
168187
/// <summary>
169188
/// The current buffered items received by the authority.
170189
/// </summary>
171190
protected internal readonly Queue<BufferedItem> m_BufferQueue = new Queue<BufferedItem>(k_BufferCountLimit);
172191

173192
/// <summary>
174-
/// The legacy list of <see cref="BufferedItem"/> items.
175-
/// </summary>
176-
/// <remarks>
177-
/// This is replaced by the <see cref="m_BufferQueue"/> of type <see cref="Queue{T}"/>.
178-
/// </remarks>
179-
[Obsolete("This list is no longer used and will be deprecated.", false)]
180-
protected internal readonly List<BufferedItem> m_Buffer = new List<BufferedItem>();
181-
182-
/// <summary>
183-
/// ** Deprecating **
184-
/// The starting value of type <see cref="T"/> to interpolate from.
185-
/// </summary>
186-
[Obsolete("This property will be deprecated.", false)]
187-
protected internal T m_InterpStartValue;
188-
189-
/// <summary>
190-
/// ** Deprecating **
191-
/// The current value of type <see cref="T"/>.
193+
/// The current interpolation state
192194
/// </summary>
193-
[Obsolete("This property will be deprecated.", false)]
194-
protected internal T m_CurrentInterpValue;
195+
internal CurrentState InterpolateState;
195196

196197
/// <summary>
197-
/// ** Deprecating **
198-
/// The end (or target) value of type <see cref="T"/> to interpolate towards.
198+
/// The maximum Lerp "t" boundary when using standard lerping for interpolation
199199
/// </summary>
200-
[Obsolete("This property will be deprecated.", false)]
201-
protected internal T m_InterpEndValue;
200+
internal float MaxInterpolationBound = 3.0f;
201+
internal bool EndOfBuffer => m_BufferQueue.Count == 0;
202+
internal bool InLocalSpace;
202203

204+
private double m_LastMeasurementAddedTime = 0.0f;
205+
private int m_BufferCount;
206+
private int m_NbItemsReceivedThisFrame;
207+
private BufferedItem m_LastBufferedItemReceived;
203208
/// <summary>
204209
/// Represents the rate of change for the value being interpolated when smooth dampening is enabled.
205210
/// </summary>
@@ -210,12 +215,10 @@ public void Reset(T currentValue)
210215
/// </summary>
211216
private T m_PredictedRateOfChange;
212217

213-
private bool m_IsAngularValue;
214-
215218
/// <summary>
216219
/// When true, the value <see cref="T"/> is an angular numeric representation.
217220
/// </summary>
218-
protected bool IsAngularValue => m_IsAngularValue;
221+
private protected bool m_IsAngularValue;
219222

220223
/// <summary>
221224
/// Resets interpolator to the defaults.
@@ -237,10 +240,12 @@ public void Clear()
237240
/// This is used when first synchronizing/initializing and when telporting an object.
238241
/// </remarks>
239242
/// <param name="targetValue">The target value to reset the interpolator to</param>
240-
/// <param name="serverTime">The current server time</param>
243+
/// <param name="serverTime">The current server time</param>
241244
/// <param name="isAngularValue">When rotation is expressed as Euler values (i.e. Vector3 and/or float) this helps determine what kind of smooth dampening to use.</param>
242245
public void ResetTo(T targetValue, double serverTime, bool isAngularValue = false)
243246
{
247+
// Clear the interpolator
248+
Clear();
244249
InternalReset(targetValue, serverTime, isAngularValue);
245250
}
246251

@@ -518,8 +523,8 @@ public void AddMeasurement(T newMeasurement, double sentTime)
518523
{
519524
// Clear the interpolator
520525
Clear();
521-
// Reset to the new value
522-
InternalReset(newMeasurement, sentTime, IsAngularValue, false);
526+
// Reset to the new value but don't automatically add the measurement (prevents recursion)
527+
InternalReset(newMeasurement, sentTime, m_IsAngularValue, false);
523528
m_LastMeasurementAddedTime = sentTime;
524529
m_LastBufferedItemReceived = new BufferedItem(newMeasurement, sentTime, m_BufferCount);
525530
// Next line keeps renderTime above m_StartTimeConsumed. Fixes pause/unpause issues
@@ -569,6 +574,9 @@ public T GetInterpolatedValue()
569574
/// <summary>
570575
/// An alternate smoothing method to Lerp.
571576
/// </summary>
577+
/// <remarks>
578+
/// Not public API ready yet.
579+
/// </remarks>
572580
/// <param name="current">Current item <see cref="T"/> value.</param>
573581
/// <param name="target">Target item <see cref="T"/> value.</param>
574582
/// <param name="rateOfChange">The velocity of change.</param>
@@ -584,6 +592,9 @@ private protected virtual T SmoothDamp(T current, T target, ref T rateOfChange,
584592
/// <summary>
585593
/// Determines if two values of type <see cref="T"/> are close to the same value.
586594
/// </summary>
595+
/// <remarks>
596+
/// Not public API ready yet.
597+
/// </remarks>
587598
/// <param name="first">First value of type <see cref="T"/>.</param>
588599
/// <param name="second">Second value of type <see cref="T"/>.</param>
589600
/// <param name="precision">The precision of the aproximation.</param>
@@ -605,6 +616,11 @@ protected internal virtual T OnConvertTransformSpace(Transform transform, T item
605616
return default;
606617
}
607618

619+
/// <summary>
620+
/// Invoked by <see cref="Components.NetworkTransform"/> when the transform has transitioned between local to world or vice versa.
621+
/// </summary>
622+
/// <param name="transform">The transform that the <see cref="Components.NetworkTransform"/> is associated with.</param>
623+
/// <param name="inLocalSpace">Whether the <see cref="Components.NetworkTransform"/> is now being tracked in local or world spaced.</param>
608624
internal void ConvertTransformSpace(Transform transform, bool inLocalSpace)
609625
{
610626
var count = m_BufferQueue.Count;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private protected override bool IsAproximately(float first, float second, float
2929
/// <inheritdoc />
3030
private protected override float SmoothDamp(float current, float target, ref float rateOfChange, float duration, float deltaTime, float maxSpeed = float.PositiveInfinity)
3131
{
32-
if (IsAngularValue)
32+
if (m_IsAngularValue)
3333
{
3434
return Mathf.SmoothDampAngle(current, target, ref rateOfChange, duration, maxSpeed, deltaTime);
3535
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private protected override bool IsAproximately(Vector3 first, Vector3 second, fl
6161
/// <inheritdoc />
6262
private protected override Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 rateOfChange, float duration, float deltaTime, float maxSpeed)
6363
{
64-
if (IsAngularValue)
64+
if (m_IsAngularValue)
6565
{
6666
current.x = Mathf.SmoothDampAngle(current.x, target.x, ref rateOfChange.x, duration, maxSpeed, deltaTime);
6767
current.y = Mathf.SmoothDampAngle(current.y, target.y, ref rateOfChange.y, duration, maxSpeed, deltaTime);

0 commit comments

Comments
 (0)