This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
InterpolateTransformSystem.cs
193 lines (162 loc) · 7.58 KB
/
InterpolateTransformSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using Improbable.Gdk.Core;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
namespace Improbable.Gdk.TransformSynchronization
{
[DisableAutoCreation]
[UpdateInGroup(typeof(SpatialOSUpdateGroup))]
public class InterpolateTransformSystem : ComponentSystem
{
private ComponentUpdateSystem updateSystem;
private EntityQuery interpolationGroup;
protected override void OnCreate()
{
base.OnCreate();
updateSystem = World.GetExistingSystem<ComponentUpdateSystem>();
interpolationGroup = GetEntityQuery(
ComponentType.ReadWrite<BufferedTransform>(),
ComponentType.ReadWrite<DeferredUpdateTransform>(),
ComponentType.ReadOnly<TransformInternal.Component>(),
ComponentType.ReadOnly<SpatialEntityId>(),
ComponentType.ReadOnly<InterpolationConfig>(),
ComponentType.ReadOnly<TransformInternal.ComponentAuthority>()
);
interpolationGroup.SetFilter(TransformInternal.ComponentAuthority.NotAuthoritative);
}
protected override void OnUpdate()
{
Entities.With(interpolationGroup).ForEach(
(Entity entity,
InterpolationConfig config,
ref SpatialEntityId spatialEntityId,
ref TransformInternal.Component transformInternal,
ref DeferredUpdateTransform deferredUpdateTransform) =>
{
var transformBuffer = EntityManager.GetBuffer<BufferedTransform>(entity);
var lastTransformApplied = deferredUpdateTransform.Transform;
if (transformBuffer.Length >= config.MaxLoadMatchedBufferSize)
{
transformBuffer.Clear();
}
if (transformBuffer.Length == 0)
{
var currentTransformComponent = transformInternal;
if (currentTransformComponent.PhysicsTick <= lastTransformApplied.PhysicsTick)
{
return;
}
deferredUpdateTransform = new DeferredUpdateTransform
{
Transform = currentTransformComponent
};
var transformToInterpolateTo = ToBufferedTransform(currentTransformComponent);
var ticksToFill = math.max((uint) config.TargetBufferSize, 1);
if (ticksToFill > 1)
{
var transformToInterpolateFrom = ToBufferedTransformAtTick(lastTransformApplied,
transformToInterpolateTo.PhysicsTick - ticksToFill + 1);
transformBuffer.Add(transformToInterpolateFrom);
for (uint j = 0; j < ticksToFill - 2; ++j)
{
transformBuffer.Add(InterpolateValues(transformToInterpolateFrom,
transformToInterpolateTo,
j + 1));
}
}
transformBuffer.Add(transformToInterpolateTo);
return;
}
var updates =
updateSystem
.GetEntityComponentUpdatesReceived<TransformInternal.Update>(spatialEntityId.EntityId);
for (var j = 0; j < updates.Count; ++j)
{
var update = updates[j].Update;
UpdateLastTransform(ref lastTransformApplied, update);
deferredUpdateTransform = new DeferredUpdateTransform
{
Transform = lastTransformApplied
};
if (!update.PhysicsTick.HasValue)
{
continue;
}
var transformToInterpolateTo = ToBufferedTransform(lastTransformApplied);
var transformToInterpolateFrom = transformBuffer[transformBuffer.Length - 1];
var lastTickId = transformToInterpolateFrom.PhysicsTick;
// This could go backwards if authority changes quickly between two workers with different loads
if (lastTickId >= transformToInterpolateTo.PhysicsTick)
{
continue;
}
var ticksToFill = math.max(transformToInterpolateTo.PhysicsTick - lastTickId, 1);
for (uint k = 0; k < ticksToFill - 1; ++k)
{
transformBuffer.Add(InterpolateValues(
transformToInterpolateFrom,
transformToInterpolateTo,
k + 1));
}
transformBuffer.Add(transformToInterpolateTo);
}
});
}
private static void UpdateLastTransform(ref TransformInternal.Component lastTransform,
TransformInternal.Update update)
{
if (update.Location.HasValue)
{
lastTransform.Location = update.Location.Value;
}
if (update.Rotation.HasValue)
{
lastTransform.Rotation = update.Rotation.Value;
}
if (update.Velocity.HasValue)
{
lastTransform.Velocity = update.Velocity.Value;
}
if (update.TicksPerSecond.HasValue)
{
lastTransform.TicksPerSecond = update.TicksPerSecond.Value;
}
if (update.PhysicsTick.HasValue)
{
lastTransform.PhysicsTick = update.PhysicsTick.Value;
}
}
private static BufferedTransform ToBufferedTransform(TransformInternal.Component transform)
{
return new BufferedTransform
{
Position = transform.Location.ToUnityVector(),
Velocity = transform.Velocity.ToUnityVector(),
Orientation = transform.Rotation.ToUnityQuaternion(),
PhysicsTick = transform.PhysicsTick
};
}
private static BufferedTransform ToBufferedTransformAtTick(TransformInternal.Component component, uint tick)
{
return new BufferedTransform
{
Position = component.Location.ToUnityVector(),
Velocity = component.Velocity.ToUnityVector(),
Orientation = component.Rotation.ToUnityQuaternion(),
PhysicsTick = tick
};
}
private static BufferedTransform InterpolateValues(BufferedTransform first, BufferedTransform second,
uint ticksAfterFirst)
{
var t = (float) ticksAfterFirst / (float) (second.PhysicsTick - first.PhysicsTick);
return new BufferedTransform
{
Position = Vector3.Lerp(first.Position, second.Position, t),
Velocity = Vector3.Lerp(first.Velocity, second.Velocity, t),
Orientation = UnityEngine.Quaternion.Slerp(first.Orientation, second.Orientation, t),
PhysicsTick = first.PhysicsTick + ticksAfterFirst
};
}
}
}