Skip to content

Commit d15b193

Browse files
update
Updating some tests with these changes (WIP) Migrating away from direct transform access and using a cached transform approach.
1 parent d09cb92 commit d15b193

File tree

3 files changed

+169
-99
lines changed

3 files changed

+169
-99
lines changed

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

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,7 @@ public Quaternion GetSpaceRelativeRotation(bool getCurrentState = false)
17191719
{
17201720
if (!getCurrentState || CanCommitToTransform)
17211721
{
1722-
return InLocalSpace ? transform.localRotation : transform.rotation;
1722+
return InLocalSpace ? CachedTransform.localRotation : CachedTransform.rotation;
17231723
}
17241724
else
17251725
{
@@ -1750,7 +1750,7 @@ public Vector3 GetScale(bool getCurrentState = false)
17501750
{
17511751
if (!getCurrentState || CanCommitToTransform)
17521752
{
1753-
return transform.localScale;
1753+
return CachedTransform.localScale;
17541754
}
17551755
else
17561756
{
@@ -1904,10 +1904,9 @@ protected override void OnSynchronize<T>(ref BufferSerializer<T> serializer)
19041904
if (serializer.IsWriter)
19051905
{
19061906
SynchronizeState.FlagStates.IsTeleportingNextFrame = true;
1907-
var transformToCommit = transform;
19081907
// If we are using Half Float Precision, then we want to only synchronize the authority's m_HalfPositionState.FullPosition in order for
19091908
// for the non-authority side to be able to properly synchronize delta position updates.
1910-
CheckForStateChange(ref SynchronizeState, ref transformToCommit, true, targetClientId);
1909+
CheckForStateChange(ref SynchronizeState, true, targetClientId);
19111910
SynchronizeState.NetworkSerialize(serializer);
19121911
LastTickSync = SynchronizeState.GetNetworkTick();
19131912
OnAuthorityPushTransformState(ref SynchronizeState);
@@ -1972,7 +1971,7 @@ internal void TryCommitTransformToServer(Transform transformToCommit, double dir
19721971
// If we are authority, update the authoritative state
19731972
if (CanCommitToTransform)
19741973
{
1975-
OnUpdateAuthoritativeState(ref transformToCommit);
1974+
OnUpdateAuthoritativeState();
19761975
}
19771976
else // Non-Authority
19781977
{
@@ -2012,7 +2011,7 @@ protected virtual void OnAuthorityPushTransformState(ref NetworkTransformState n
20122011
/// If there are any transform delta states, this method will synchronize the
20132012
/// state with all non-authority instances.
20142013
/// </summary>
2015-
private void TryCommitTransform(ref Transform transformToCommit, bool synchronize = false, bool settingState = false)
2014+
private void TryCommitTransform(bool synchronize = false, bool settingState = false)
20162015
{
20172016
// Only the server or the owner is allowed to commit a transform
20182017
if (!IsServer && !IsOwner)
@@ -2029,7 +2028,7 @@ private void TryCommitTransform(ref Transform transformToCommit, bool synchroniz
20292028
}
20302029
#endif
20312030
// If the transform has deltas (returns dirty) or if an explicitly set state is pending
2032-
if (m_LocalAuthoritativeNetworkState.ExplicitSet || CheckForStateChange(ref m_LocalAuthoritativeNetworkState, ref transformToCommit, synchronize, forceState: settingState))
2031+
if (m_LocalAuthoritativeNetworkState.ExplicitSet || CheckForStateChange(ref m_LocalAuthoritativeNetworkState, synchronize, forceState: settingState))
20332032
{
20342033
// If the state was explicitly set, then update the network tick to match the locally calculate tick
20352034
if (m_LocalAuthoritativeNetworkState.ExplicitSet)
@@ -2041,7 +2040,7 @@ private void TryCommitTransform(ref Transform transformToCommit, bool synchroniz
20412040
if (SwitchTransformSpaceWhenParented && m_LocalAuthoritativeNetworkState.ExplicitSet && m_LocalAuthoritativeNetworkState.IsDirty && transform.parent != null && !m_LocalAuthoritativeNetworkState.InLocalSpace)
20422041
{
20432042
InLocalSpace = true;
2044-
CheckForStateChange(ref m_LocalAuthoritativeNetworkState, ref transformToCommit, synchronize, forceState: true);
2043+
CheckForStateChange(ref m_LocalAuthoritativeNetworkState, synchronize, forceState: true);
20452044
}
20462045
}
20472046

@@ -2139,7 +2138,7 @@ internal NetworkTransformState ApplyLocalNetworkState(Transform transform)
21392138
m_LocalAuthoritativeNetworkState.ClearBitSetForNextTick();
21402139

21412140
// Now check the transform for any threshold value changes
2142-
CheckForStateChange(ref m_LocalAuthoritativeNetworkState, ref transform);
2141+
CheckForStateChange(ref m_LocalAuthoritativeNetworkState);
21432142

21442143
// Return the entire state to be used by the integration test
21452144
return m_LocalAuthoritativeNetworkState;
@@ -2150,6 +2149,7 @@ internal NetworkTransformState ApplyLocalNetworkState(Transform transform)
21502149
/// </summary>
21512150
internal bool ApplyTransformToNetworkState(ref NetworkTransformState networkState, double dirtyTime, Transform transformToUse)
21522151
{
2152+
CachedTransform = transformToUse;
21532153
m_CachedNetworkManager = NetworkManager;
21542154
// Apply the interpolate and PostionDeltaCompression flags, otherwise we get false positives whether something changed or not.
21552155
networkState.FlagStates.UseInterpolation = Interpolate;
@@ -2159,14 +2159,14 @@ internal bool ApplyTransformToNetworkState(ref NetworkTransformState networkStat
21592159
networkState.UseUnreliableDeltas = UseUnreliableDeltas;
21602160
m_HalfPositionState = new NetworkDeltaPosition(Vector3.zero, 0, math.bool3(SyncPositionX, SyncPositionY, SyncPositionZ));
21612161

2162-
return CheckForStateChange(ref networkState, ref transformToUse);
2162+
return CheckForStateChange(ref networkState);
21632163
}
21642164

21652165
/// <summary>
21662166
/// Applies the transform to the <see cref="NetworkTransformState"/> specified.
21672167
/// </summary>
21682168
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2169-
private bool CheckForStateChange(ref NetworkTransformState networkState, ref Transform transformToUse, bool isSynchronization = false, ulong targetClientId = 0, bool forceState = false)
2169+
private bool CheckForStateChange(ref NetworkTransformState networkState, bool isSynchronization = false, ulong targetClientId = 0, bool forceState = false)
21702170
{
21712171
// As long as we are not doing our first synchronization and we are sending unreliable deltas, each
21722172
// NetworkTransform will stagger its full transfom synchronization over a 1 second period based on the
@@ -2235,8 +2235,8 @@ private bool CheckForStateChange(ref NetworkTransformState networkState, ref Tra
22352235

22362236

22372237
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
2238-
var position = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetPosition() : InLocalSpace ? transformToUse.localPosition : transformToUse.position;
2239-
var rotation = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetRotation() : InLocalSpace ? transformToUse.localRotation : transformToUse.rotation;
2238+
var position = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetPosition() : InLocalSpace ? CachedTransform.localPosition : CachedTransform.position;
2239+
var rotation = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetRotation() : InLocalSpace ? CachedTransform.localRotation : CachedTransform.rotation;
22402240

22412241
var positionThreshold = Vector3.one * PositionThreshold;
22422242
var rotationThreshold = Vector3.one * RotAngleThreshold;
@@ -2255,7 +2255,7 @@ private bool CheckForStateChange(ref NetworkTransformState networkState, ref Tra
22552255
var rotationThreshold = Vector3.one * RotAngleThreshold;
22562256
#endif
22572257
var rotAngles = rotation.eulerAngles;
2258-
var scale = transformToUse.localScale;
2258+
var scale = CachedTransform.localScale;
22592259
flagStates.IsSynchronizing = isSynchronization;
22602260

22612261
// Check for parenting when synchronizing and/or teleporting
@@ -2636,9 +2636,8 @@ private void OnNetworkTick(bool isCalledFromParent = false)
26362636
}
26372637
#endif
26382638

2639-
// Update any changes to the transform
2640-
var transformSource = transform;
2641-
OnUpdateAuthoritativeState(ref transformSource, isCalledFromParent);
2639+
// Update any changes to the transform based on the current state
2640+
OnUpdateAuthoritativeState(isCalledFromParent);
26422641
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
26432642
m_InternalCurrentPosition = m_LastStateTargetPosition = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetPosition() : GetSpaceRelativePosition();
26442643
m_InternalCurrentRotation = m_UseRigidbodyForMotion ? m_NetworkRigidbodyInternal.GetRotation() : GetSpaceRelativeRotation();
@@ -3497,7 +3496,7 @@ private void AxisChangedDeltaPositionCheck()
34973496
/// Called by authority to check for deltas and update non-authoritative instances
34983497
/// if any are found.
34993498
/// </summary>
3500-
internal void OnUpdateAuthoritativeState(ref Transform transformSource, bool settingState = false)
3499+
internal void OnUpdateAuthoritativeState(bool settingState = false)
35013500
{
35023501
// If our replicated state is not dirty and our local authority state is dirty, clear it.
35033502
if (!m_LocalAuthoritativeNetworkState.ExplicitSet && m_LocalAuthoritativeNetworkState.IsDirty && !m_LocalAuthoritativeNetworkState.IsTeleportingNextFrame)
@@ -3517,7 +3516,7 @@ internal void OnUpdateAuthoritativeState(ref Transform transformSource, bool set
35173516

35183517
AxisChangedDeltaPositionCheck();
35193518

3520-
TryCommitTransform(ref transformSource, settingState: settingState);
3519+
TryCommitTransform(settingState: settingState);
35213520
}
35223521
#endregion
35233522

@@ -3633,6 +3632,8 @@ protected internal override void InternalOnNetworkPostSpawn()
36333632
internal static bool AssignDefaultInterpolationType;
36343633
internal static InterpolationTypes DefaultInterpolationType;
36353634

3635+
internal Transform CachedTransform;
3636+
36363637
/// <summary>
36373638
/// Create interpolators when first instantiated to avoid memory allocations if the
36383639
/// associated NetworkObject persists (i.e. despawned but not destroyed or pools)
@@ -3655,6 +3656,8 @@ protected virtual void Awake()
36553656
{
36563657
InLocalSpace = false;
36573658
}
3659+
3660+
CachedTransform = transform;
36583661
}
36593662

36603663
/// <inheritdoc/>
@@ -4142,16 +4145,14 @@ private void SetStateInternal(Vector3 pos, Quaternion rot, Vector3 scale, bool s
41424145
transform.localScale = scale;
41434146
m_LocalAuthoritativeNetworkState.FlagStates.IsTeleportingNextFrame = shouldTeleport;
41444147

4145-
var transformToCommit = transform;
4146-
41474148
// Explicit set states are cumulative during a fractional tick period of time (i.e. each SetState invocation will
41484149
// update the axial deltas to whatever changes are applied). As such, we need to preserve the dirty and explicit
41494150
// state flags.
41504151
var stateWasDirty = m_LocalAuthoritativeNetworkState.IsDirty;
41514152
var explicitState = m_LocalAuthoritativeNetworkState.ExplicitSet;
41524153

41534154
// Apply any delta states to the m_LocalAuthoritativeNetworkState
4154-
var isDirty = CheckForStateChange(ref m_LocalAuthoritativeNetworkState, ref transformToCommit);
4155+
var isDirty = CheckForStateChange(ref m_LocalAuthoritativeNetworkState);
41554156

41564157
// If we were dirty and the explicit state was set (prior to checking for deltas) or the current explicit state is dirty,
41574158
// then we set the explicit state flag.

com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformGeneral.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public void TestMultipleStateSynchronization([Values] bool isLocal, [Values] boo
5555
var localState = m_NonAuthoritativeTransform.LocalAuthoritativeNetworkState;
5656

5757
// Assure this is not set to avoid a false positive result with teleporting
58-
localState.IsTeleportingNextFrame = false;
58+
localState.FlagStates.IsTeleportingNextFrame = false;
5959

6060
// Simulate a state update
61-
localState.UseInterpolation = false;
61+
localState.FlagStates.UseInterpolation = false;
6262
localState.CurrentPosition = new Vector3(5.0f, 0.0f, 0.0f);
6363
localState.SetHasPosition(NetworkTransform.Axis.X, true);
6464
localState.PositionX = 5.0f;

0 commit comments

Comments
 (0)