Skip to content

Commit 7f2a809

Browse files
feat: synchronize NetworkTransform as one batched message per tick
Last of five PRs that replace #4123. With NetworkConfig.TransformSyncMode set to Batched, NetworkTransformStateManager detects changes for every registered instance in one Burst job, the server sends one NetworkTransformBatchMessage per observing client per tick, and non-authority instances interpolate in a job. Rigidbody driven and nested instances stay on the per-instance path. NetworkTransformSyncModeParityTests runs both modes side by side.
1 parent 6ad2698 commit 7f2a809

17 files changed

Lines changed: 1999 additions & 6 deletions

‎com.unity.netcode.gameobjects/Editor/NetworkTransformEditor.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ private void DisplayNetworkTransformProperties()
218218
EditorGUILayout.Space();
219219
EditorGUILayout.LabelField("Delivery", EditorStyles.boldLabel);
220220
EditorGUILayout.PropertyField(m_TickSyncChildren);
221+
222+
// UseUnreliableDeltas only applies to per instance synchronization mode, but the mode is authored on
223+
// the NetworkManager that will run this instance, which a prefab cannot know. So it is always drawn
224+
// and the runtime ignores it under the batched mode, where delivery is determined per state update
225+
// as opposed to per component.
221226
// If both are set from a previous configuration, then SwitchTransformSpaceWhenParented takes
222227
// precedence.
223228
if (networkTransform.UseUnreliableDeltas && networkTransform.SwitchTransformSpaceWhenParented)
@@ -242,6 +247,7 @@ private void DisplayNetworkTransformProperties()
242247
EditorGUILayout.Space();
243248
EditorGUILayout.LabelField("Configurations", EditorStyles.boldLabel);
244249

250+
// SwitchTransformSpaceWhenParented is only constrained by UseUnreliableDeltas while the latter applies.
245251
SetGUIActive(!networkTransform.UseUnreliableDeltas);
246252
if (networkTransform.UseUnreliableDeltas)
247253
{
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Unity.Burst;
2+
using Unity.Collections;
3+
using UnityEngine.Jobs;
4+
using static Unity.Netcode.Components.NetworkTransform;
5+
6+
namespace Unity.Netcode.Components
7+
{
8+
/// <summary>
9+
/// Motion Authority Only:
10+
/// Detects <see cref="NetworkTransform"/> state changes for every registered instance in a parallel job.
11+
/// </summary>
12+
/// <remarks>
13+
/// <see cref="CheckForStateChange(ref NetworkTransformState, ref NetworkDeltaPosition, ref TransformDeltaConfig, in TransformSample, bool, bool, bool)"/>
14+
/// is the common method used for both per instance, runs on the main thread, and batched modes. This assures both paths detect changes in state identically.
15+
/// </remarks>
16+
[BurstCompile]
17+
internal struct DetectTransformDeltaJob : IJobParallelForTransform
18+
{
19+
/// <summary>
20+
/// The per instance input and output, parallel to the job's scheduled transforms.
21+
/// </summary>
22+
public NativeArray<TransformDeltaEntry> Entries;
23+
24+
/// <summary>
25+
/// This job's primary entry point.
26+
/// </summary>
27+
/// <remarks>
28+
/// TODO: Investigate ways to work around the fact that a Rigidbody's position and rotation cannot
29+
/// be sampled from a job. As such, any NetworkTransform that is using Rigidbody for motion will
30+
/// use the <see cref="UnityEngine.GameObject.transform"/> to detect changes in position and rotation
31+
/// states on the authority side.
32+
/// </remarks>
33+
/// <param name="index">Index for the transform in question.</param>
34+
/// <param name="transform">The job safe <see cref="TransformAccess"/></param>
35+
public void Execute(int index, TransformAccess transform)
36+
{
37+
if (!transform.isValid)
38+
{
39+
return;
40+
}
41+
42+
var entry = Entries[index];
43+
var flagStates = entry.State.FlagStates;
44+
// Only ResolveTransformSpace can raise this on the batched path. The one caller that forces a full
45+
// state update does so from CommitDetectedState on the main thread, after this job has run.
46+
var forceState = false;
47+
48+
// Resolve the transform space before sampling, otherwise the wrong set of values gets compared.
49+
var transformSpaceChanged = ResolveTransformSpace(ref entry.Config, ref flagStates, entry.TransformHasParent, false, ref forceState);
50+
entry.State.FlagStates = flagStates;
51+
52+
var rotation = entry.Config.InLocalSpace ? transform.localRotation : transform.rotation;
53+
entry.Sample.Position = entry.Config.InLocalSpace ? transform.localPosition : transform.position;
54+
entry.Sample.Rotation = rotation;
55+
entry.Sample.RotAngles = NetworkTransformMath.EulerAngles(rotation);
56+
entry.Sample.Scale = transform.localScale;
57+
58+
entry.IsDirty = CheckForStateChange(ref entry.State, ref entry.HalfPositionState, ref entry.Config,
59+
entry.Sample, false, forceState, transformSpaceChanged);
60+
61+
Entries[index] = entry;
62+
}
63+
}
64+
}

‎com.unity.netcode.gameobjects/Runtime/Components/DetectTransformDeltaJob.cs.meta‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎com.unity.netcode.gameobjects/Runtime/Components/Interpolator/NativeInterpolator.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ internal struct BufferedItemNative
4646
/// <br />
4747
/// The smooth parenting transition flag, <see cref="NetworkTransform.SwitchTransformSpaceWhenParented"/>,
4848
/// is excluded from this state as it is handled differently.<br />
49-
/// - <see cref="NativeInterpolator.ConvertSpace"/> provides additional details on this.
49+
/// - <see cref="NativeInterpolator.ConvertSpace"/> provides additional details on this. <br />
50+
/// - <see cref="NetworkTransformStateManager.ConvertInterpolationSpace"/> is where this happens.
5051
/// </remarks>
5152
internal struct NativeInterpolatorState
5253
{

0 commit comments

Comments
 (0)