Skip to content

Commit b8795fe

Browse files
feat: add the native interpolator and the transform sync mode setting
Third of five PRs that replace #4123. NativeInterpolator and InterpolateTransformJob interpolate NetworkTransform values over native data in a Burst job. NetworkConfig.TransformSyncMode selects per-instance or batched synchronization. Neither is used until the batched path is switched on.
1 parent bb25a9c commit b8795fe

9 files changed

Lines changed: 1304 additions & 2 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using Unity.Netcode.Components;
56
using Unity.Netcode.GameObjects.Editor.Configuration;
67
using Unity.Netcode.Logging;
78
using UnityEditor;
@@ -54,6 +55,7 @@ public class NetworkManagerEditor : NetcodeEditorBase<NetworkManager>
5455
private SerializedProperty m_SpawnTimeOutProperty;
5556
private SerializedProperty m_RpcHashSizeProperty;
5657
private SerializedProperty m_LoadSceneTimeOutProperty;
58+
private SerializedProperty m_TransformSyncModeProperty;
5759
private SerializedProperty m_PrefabsList;
5860

5961
private SerializedProperty m_NetworkProfileMetrics;
@@ -144,6 +146,7 @@ private void Initialize()
144146
m_NetworkMessageMetrics = m_NetworkConfigProperty.FindPropertyRelative("NetworkMessageMetrics");
145147
#endif
146148
m_RpcHashSizeProperty = m_NetworkConfigProperty.FindPropertyRelative("RpcHashSize");
149+
m_TransformSyncModeProperty = m_NetworkConfigProperty.FindPropertyRelative(nameof(NetworkConfig.TransformSyncMode));
147150
m_PrefabsList = m_NetworkConfigProperty
148151
.FindPropertyRelative(nameof(NetworkConfig.Prefabs))
149152
.FindPropertyRelative(nameof(NetworkPrefabs.NetworkPrefabsLists));
@@ -192,6 +195,7 @@ private void CheckNullProperties()
192195
#endif
193196

194197
m_RpcHashSizeProperty = m_NetworkConfigProperty.FindPropertyRelative("RpcHashSize");
198+
m_TransformSyncModeProperty = m_NetworkConfigProperty.FindPropertyRelative(nameof(NetworkConfig.TransformSyncMode));
195199
m_PrefabsList = m_NetworkConfigProperty
196200
.FindPropertyRelative(nameof(NetworkConfig.Prefabs))
197201
.FindPropertyRelative(nameof(NetworkPrefabs.NetworkPrefabsLists));
@@ -335,6 +339,17 @@ private void DisplayNetworkManagerProperties()
335339
EditorGUILayout.PropertyField(m_PrefabsList);
336340
}
337341

342+
EditorGUILayout.Space();
343+
EditorGUILayout.LabelField("Transform Synchronization", EditorStyles.boldLabel);
344+
EditorGUILayout.PropertyField(m_TransformSyncModeProperty, new GUIContent("Mode"));
345+
if (m_NetworkManager.NetworkConfig.TransformSyncMode == TransformSyncModes.Batched)
346+
{
347+
EditorGUILayout.HelpBox(
348+
$"{nameof(NetworkTransform.UseUnreliableDeltas)} does not apply in this mode. Delivery is determined per state " +
349+
"update as opposed to per component. Every peer in a session has to use the same mode.",
350+
MessageType.Info);
351+
}
352+
338353
EditorGUILayout.Space();
339354
EditorGUILayout.LabelField("Scene Management Settings", EditorStyles.boldLabel);
340355
EditorGUILayout.PropertyField(m_EnableSceneManagementProperty);

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,13 @@ public void Clear()
275275
/// Resets the current interpolator to the target value.
276276
/// </summary>
277277
/// <remarks>
278-
/// This is used when first synchronizing/initializing and when telporting an object.
278+
/// This is used when first synchronizing/initializing and when teleporting an object.<br />
279+
/// No baseline measurement is recorded.<br />
280+
/// A baseline is stamped with the local ServerTime.Time.<br />
281+
/// The measurements that follow carry the older tick they were authored on.<br />
282+
/// AddMeasurement would drop every one of those.<br />
283+
/// The interpolator is left the way a freshly spawned one is instead.<br />
284+
/// <paramref name="serverTime"/> is not used. Mark this obsolete and deprecate it at a later date.
279285
/// </remarks>
280286
/// <param name="targetValue">The target value to reset the interpolator to</param>
281287
/// <param name="serverTime">The current server time</param>
@@ -288,7 +294,9 @@ internal void ResetTo(Transform parent, T targetValue, double serverTime)
288294
{
289295
// Clear the interpolator
290296
Clear();
291-
InternalReset(parent, targetValue, serverTime);
297+
298+
// No baseline measurement. See the ResetTo remarks above.
299+
InternalReset(parent, targetValue, serverTime, false);
292300
}
293301

294302
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using Unity.Burst;
2+
using Unity.Collections;
3+
using Unity.Jobs;
4+
using Unity.Mathematics;
5+
6+
namespace Unity.Netcode.Components
7+
{
8+
/// <summary>
9+
/// The NGO non-authority instance's transform state used by <see cref="InterpolateTransformJob"/>.
10+
/// See also:
11+
/// - <see cref="NativeInterpolator"/>
12+
/// - <see cref="NativeInterpolatorState"/>
13+
/// </summary>
14+
internal struct InterpolationEntry
15+
{
16+
internal NativeInterpolatorState Position;
17+
internal NativeInterpolatorState Rotation;
18+
internal NativeInterpolatorState Scale;
19+
20+
/// <summary>
21+
/// The delta frame time whether fixed or standard delta.
22+
/// </summary>
23+
internal float DeltaTime;
24+
25+
/// <summary>
26+
/// The "ticks ago" time used to decide which buffered measurements are ready to consume.
27+
/// </summary>
28+
internal double TickLatencyAsTime;
29+
30+
/// <summary>
31+
/// The render time used by <see cref="NetworkTransform.InterpolationTypes.LegacyLerp"/> only.
32+
/// </summary>
33+
internal double LegacyRenderTime;
34+
35+
internal double CurrentTime;
36+
internal double MinDeltaTime;
37+
internal double MaxDeltaTime;
38+
39+
internal NetworkTransform.InterpolationTypes PositionInterpolationType;
40+
internal NetworkTransform.InterpolationTypes RotationInterpolationType;
41+
internal NetworkTransform.InterpolationTypes ScaleInterpolationType;
42+
43+
internal bool SynchronizePosition;
44+
internal bool SynchronizeRotation;
45+
internal bool SynchronizeScale;
46+
47+
// Results, read back on the main thread and applied to the transform there.
48+
internal float4 InterpolatedPosition;
49+
internal float4 InterpolatedRotation;
50+
internal float4 InterpolatedScale;
51+
}
52+
53+
/// <summary>
54+
/// Non-Authority Only:
55+
/// Handles interpolation for every registered non-authority <see cref="NetworkTransform"/> in
56+
/// a parallel job.
57+
/// </summary>
58+
/// <remarks>
59+
/// This performs the buffer consumption and interpolation between two state updates only.<br />
60+
/// Applying the results to the transforms stays on the main thread, which keeps this job free
61+
/// of hierarchy write ordering.<br />
62+
/// Each entry owns its own slice of <see cref="BufferedItems"/>, so no two indices address the same items.<br />
63+
/// The whole array can be written without aliasing (access is to a distinct, independent memory region).
64+
/// </remarks>
65+
[BurstCompile]
66+
internal struct InterpolateTransformJob : IJobParallelFor
67+
{
68+
public NativeArray<InterpolationEntry> Entries;
69+
70+
/// <summary>
71+
/// The shared state measurement storage. Disabling the safety restriction is what allows each index to write
72+
/// into its own slice of one array; <see cref="NativeInterpolatorState.BufferOffset"/> keeps those
73+
/// slices disjoint.
74+
/// </summary>
75+
[NativeDisableParallelForRestriction]
76+
public NativeArray<BufferedItemNative> BufferedItems;
77+
78+
public void Execute(int index)
79+
{
80+
var entry = Entries[index];
81+
82+
if (entry.SynchronizePosition)
83+
{
84+
entry.InterpolatedPosition = Advance(ref entry.Position, ref entry, entry.PositionInterpolationType);
85+
}
86+
87+
if (entry.SynchronizeRotation)
88+
{
89+
entry.InterpolatedRotation = Advance(ref entry.Rotation, ref entry, entry.RotationInterpolationType);
90+
}
91+
92+
if (entry.SynchronizeScale)
93+
{
94+
entry.InterpolatedScale = Advance(ref entry.Scale, ref entry, entry.ScaleInterpolationType);
95+
}
96+
97+
Entries[index] = entry;
98+
}
99+
100+
private float4 Advance(ref NativeInterpolatorState state, ref InterpolationEntry entry, NetworkTransform.InterpolationTypes interpolationType)
101+
{
102+
if (interpolationType == NetworkTransform.InterpolationTypes.LegacyLerp)
103+
{
104+
return NativeInterpolator.UpdateLegacy(ref state, ref BufferedItems, entry.DeltaTime, entry.LegacyRenderTime, entry.CurrentTime);
105+
}
106+
107+
return NativeInterpolator.Update(ref state, ref BufferedItems, entry.DeltaTime, entry.TickLatencyAsTime,
108+
entry.MinDeltaTime, entry.MaxDeltaTime, interpolationType == NetworkTransform.InterpolationTypes.Lerp);
109+
}
110+
}
111+
}

‎com.unity.netcode.gameobjects/Runtime/Components/Interpolator/InterpolateTransformJob.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.

0 commit comments

Comments
 (0)