|
| 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 | +} |
0 commit comments