Skip to content

Commit 56908ad

Browse files
update
some code clean up and additional comments
1 parent d264c0e commit 56908ad

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

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

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
306306
/// Whether or not z component of position will be replicated
307307
/// </summary>
308308
public bool SyncPositionZ = true;
309+
310+
private bool SynchronizePosition
311+
{
312+
get
313+
{
314+
return SyncPositionX || SyncPositionY || SyncPositionZ;
315+
}
316+
}
317+
309318
/// <summary>
310319
/// Whether or not x component of rotation will be replicated
311320
/// </summary>
@@ -318,6 +327,15 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
318327
/// Whether or not z component of rotation will be replicated
319328
/// </summary>
320329
public bool SyncRotAngleZ = true;
330+
331+
private bool SynchronizeRotation
332+
{
333+
get
334+
{
335+
return SyncRotAngleX || SyncRotAngleY || SyncRotAngleZ;
336+
}
337+
}
338+
321339
/// <summary>
322340
/// Whether or not x component of scale will be replicated
323341
/// </summary>
@@ -331,6 +349,15 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
331349
/// </summary>
332350
public bool SyncScaleZ = true;
333351

352+
353+
private bool SynchronizeScale
354+
{
355+
get
356+
{
357+
return SyncScaleX || SyncScaleY || SyncScaleZ;
358+
}
359+
}
360+
334361
/// <summary>
335362
/// The current position threshold value
336363
/// Any changes to the position that exceeds the current threshold value will be replicated
@@ -352,7 +379,6 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
352379
/// </summary>
353380
public float ScaleThreshold = ScaleThresholdDefault;
354381

355-
356382
/// <summary>
357383
/// Sets whether the transform should be treated as local (true) or world (false) space.
358384
/// </summary>
@@ -651,6 +677,11 @@ private void ApplyAuthoritativeState()
651677
// InLocalSpace Read:
652678
InLocalSpace = networkState.InLocalSpace;
653679

680+
// NOTE ABOUT INTERPOLATING AND BELOW CODE:
681+
// We always apply the interpolated state for any axis we are synchronizing even when the state has no deltas
682+
// to properly extrapolate. Extrapolation is stopped on the non-authoritative side 1 tick after the original
683+
// state was applied.
684+
654685
// Update the position values that were changed in this state update
655686
if (networkState.HasPositionX)
656687
{
@@ -698,7 +729,7 @@ private void ApplyAuthoritativeState()
698729
interpolatedRotAngles.z = isTeleporting || !Interpolate ? networkState.RotAngleZ : eulerAngles.z;
699730
}
700731
}
701-
else if (Interpolate && SyncRotAngleX || SyncRotAngleY || SyncRotAngleZ)
732+
else if (Interpolate && SynchronizeRotation)
702733
{
703734
var eulerAngles = m_RotationInterpolator.GetInterpolatedValue().eulerAngles;
704735
interpolatedRotAngles = eulerAngles;
@@ -723,19 +754,31 @@ private void ApplyAuthoritativeState()
723754
{
724755
interpolatedScale.x = isTeleporting || !Interpolate ? networkState.ScaleX : m_ScaleXInterpolator.GetInterpolatedValue();
725756
}
757+
else if (Interpolate && SyncScaleX)
758+
{
759+
interpolatedScale.x = m_ScaleXInterpolator.GetInterpolatedValue();
760+
}
726761

727762
if (networkState.HasScaleY)
728763
{
729764
interpolatedScale.y = isTeleporting || !Interpolate ? networkState.ScaleY : m_ScaleYInterpolator.GetInterpolatedValue();
730765
}
766+
else if (Interpolate && SyncScaleY)
767+
{
768+
interpolatedScale.y = m_ScaleYInterpolator.GetInterpolatedValue();
769+
}
731770

732771
if (networkState.HasScaleZ)
733772
{
734773
interpolatedScale.z = isTeleporting || !Interpolate ? networkState.ScaleZ : m_ScaleZInterpolator.GetInterpolatedValue();
735774
}
775+
else if (Interpolate && SyncScaleZ)
776+
{
777+
interpolatedScale.z = m_ScaleZInterpolator.GetInterpolatedValue();
778+
}
736779

737780
// Apply the new position
738-
if (networkState.HasPositionChange || Interpolate && (SyncPositionX || SyncPositionY || SyncPositionZ))
781+
if (networkState.HasPositionChange || Interpolate && SynchronizePosition)
739782
{
740783
if (InLocalSpace)
741784
{
@@ -748,7 +791,7 @@ private void ApplyAuthoritativeState()
748791
}
749792

750793
// Apply the new rotation
751-
if (networkState.HasRotAngleChange || Interpolate && (SyncRotAngleX || SyncRotAngleY || SyncRotAngleZ))
794+
if (networkState.HasRotAngleChange || Interpolate && SynchronizeRotation)
752795
{
753796
if (InLocalSpace)
754797
{
@@ -761,7 +804,7 @@ private void ApplyAuthoritativeState()
761804
}
762805

763806
// Apply the new scale
764-
if (networkState.HasScaleChange || Interpolate && (SyncScaleX || SyncScaleY || SyncScaleZ))
807+
if (networkState.HasScaleChange || Interpolate && SynchronizeScale)
765808
{
766809
transform.localScale = interpolatedScale;
767810
}
@@ -990,18 +1033,25 @@ public void SetMaxInterpolationBound(float maxInterpolationBound)
9901033
m_ScaleZInterpolator.MaxInterpolationBound = maxInterpolationBound;
9911034
}
9921035

1036+
/// <summary>
1037+
/// Create interpolators when first instantiated to avoid memory allocations if the
1038+
/// associated NetworkObject persists (i.e. despawned but not destroyed or pools)
1039+
/// </summary>
9931040
private void Awake()
9941041
{
995-
// we only want to create our interpolators during Awake so that, when pooled, we do not create tons
996-
// of gc thrash each time objects wink out and are re-used
1042+
// Rotation is a single Quaternion since each Euler axis will affect the quaternion's final value
1043+
m_RotationInterpolator = new BufferedLinearInterpolatorQuaternion();
1044+
1045+
// All other interpolators are BufferedLinearInterpolatorFloats
9971046
m_PositionXInterpolator = new BufferedLinearInterpolatorFloat();
9981047
m_PositionYInterpolator = new BufferedLinearInterpolatorFloat();
9991048
m_PositionZInterpolator = new BufferedLinearInterpolatorFloat();
1000-
m_RotationInterpolator = new BufferedLinearInterpolatorQuaternion(); // rotation is a single Quaternion since each euler axis will affect the quaternion's final value
10011049
m_ScaleXInterpolator = new BufferedLinearInterpolatorFloat();
10021050
m_ScaleYInterpolator = new BufferedLinearInterpolatorFloat();
10031051
m_ScaleZInterpolator = new BufferedLinearInterpolatorFloat();
10041052

1053+
// Used to quickly iteration over the BufferedLinearInterpolatorFloat
1054+
// instances
10051055
if (m_AllFloatInterpolators.Count == 0)
10061056
{
10071057
m_AllFloatInterpolators.Add(m_PositionXInterpolator);

0 commit comments

Comments
 (0)