Port previous premature rounding fix to V1

This commit is contained in:
ninjakickja 2024-01-20 22:54:44 +08:00
parent 006fdcc3b7
commit f785d70bf4
2 changed files with 24 additions and 24 deletions

View File

@ -42,9 +42,9 @@ public class HybridNetworkTransform : NetworkBehaviour
double lastFullSendIntervalTime = double.MinValue; double lastFullSendIntervalTime = double.MinValue;
private byte lastSentFullSyncIndex = 0; private byte lastSentFullSyncIndex = 0;
private SyncDataFull lastSentFullSyncData; private SyncDataFull lastSentFullSyncData;
private QuantizedSnapshot lastSentFullQuantized; //private QuantizedSnapshot lastSentFullQuantized;
private SyncDataFull lastReceivedFullSyncData; private SyncDataFull lastReceivedFullSyncData;
private QuantizedSnapshot lastReceivedFullQuantized; //private QuantizedSnapshot lastReceivedFullQuantized;
/*[Header("Delta Send Interval Multiplier")] /*[Header("Delta Send Interval Multiplier")]
[Tooltip("Check/Sync every multiple of Network Manager send interval (= 1 / NM Send Rate), instead of every send interval.\n(30 NM send rate, and 3 interval, is a send every 0.1 seconds)\nA larger interval means less network sends, which has a variety of upsides. The drawbacks are delays and lower accuracy, you should find a nice balance between not sending too much, but the results looking good for your particular scenario.")] [Tooltip("Check/Sync every multiple of Network Manager send interval (= 1 / NM Send Rate), instead of every send interval.\n(30 NM send rate, and 3 interval, is a send every 0.1 seconds)\nA larger interval means less network sends, which has a variety of upsides. The drawbacks are delays and lower accuracy, you should find a nice balance between not sending too much, but the results looking good for your particular scenario.")]
@ -219,7 +219,7 @@ public override void OnSerialize(NetworkWriter writer, bool initialState)
if (syncRotation) writer.WriteQuaternion(lastSentFullSyncData.rotation); if (syncRotation) writer.WriteQuaternion(lastSentFullSyncData.rotation);
if (syncScale) writer.WriteVector3(lastSentFullSyncData.scale); if (syncScale) writer.WriteVector3(lastSentFullSyncData.scale);
lastSentFullQuantized = ConstructQuantizedSnapshot(lastSentFullSyncData.position, lastSentFullSyncData.rotation, lastSentFullSyncData.scale); //lastSentFullQuantized = ConstructQuantizedSnapshot(lastSentFullSyncData.position, lastSentFullSyncData.rotation, lastSentFullSyncData.scale);
} }
} }
@ -342,7 +342,7 @@ protected virtual void ServerBroadcastFull()
{ {
lastSentFullSyncData = ConstructFullSyncData(true); lastSentFullSyncData = ConstructFullSyncData(true);
lastSentFullQuantized = ConstructQuantizedSnapshot(lastSentFullSyncData.position, lastSentFullSyncData.rotation, lastSentFullSyncData.scale); //lastSentFullQuantized = ConstructQuantizedSnapshot(lastSentFullSyncData.position, lastSentFullSyncData.rotation, lastSentFullSyncData.scale);
RpcServerToClientSyncFull(lastSentFullSyncData); RpcServerToClientSyncFull(lastSentFullSyncData);
} }
@ -378,7 +378,7 @@ protected virtual void OnServerToClientSyncFull(SyncDataFull syncData)
// use current non-synced axis instead of giving it a 0. // use current non-synced axis instead of giving it a 0.
lastReceivedFullSyncData = syncData; lastReceivedFullSyncData = syncData;
CleanUpFullSyncDataPositionSync(ref lastReceivedFullSyncData); CleanUpFullSyncDataPositionSync(ref lastReceivedFullSyncData);
lastReceivedFullQuantized = ConstructQuantizedSnapshot(lastReceivedFullSyncData.position, lastReceivedFullSyncData.rotation, lastReceivedFullSyncData.scale); //lastReceivedFullQuantized = ConstructQuantizedSnapshot(lastReceivedFullSyncData.position, lastReceivedFullSyncData.rotation, lastReceivedFullSyncData.scale);
// We don't care if we are adding 'default' to any field because // We don't care if we are adding 'default' to any field because
// syncing is checked again in Apply before applying the changes. // syncing is checked again in Apply before applying the changes.
@ -394,9 +394,9 @@ protected virtual void ServerBroadcastDelta()
if (lastSentFullSyncIndex == 0) return; if (lastSentFullSyncIndex == 0) return;
SyncDataFull currentFull = ConstructFullSyncData(false); SyncDataFull currentFull = ConstructFullSyncData(false);
QuantizedSnapshot currentQuantized = ConstructQuantizedSnapshot(currentFull.position, currentFull.rotation, currentFull.scale); //QuantizedSnapshot currentQuantized = ConstructQuantizedSnapshot(currentFull.position, currentFull.rotation, currentFull.scale);
SyncDataDelta syncDataDelta = DeriveDelta(currentQuantized); SyncDataDelta syncDataDelta = DeriveDelta(currentFull);
RpcServerToClientSyncDelta(syncDataDelta); RpcServerToClientSyncDelta(syncDataDelta);
} }
@ -438,7 +438,7 @@ protected virtual void ClientBroadcastFull()
{ {
lastSentFullSyncData = ConstructFullSyncData(true); lastSentFullSyncData = ConstructFullSyncData(true);
lastSentFullQuantized = ConstructQuantizedSnapshot(lastSentFullSyncData.position, lastSentFullSyncData.rotation, lastSentFullSyncData.scale); //lastSentFullQuantized = ConstructQuantizedSnapshot(lastSentFullSyncData.position, lastSentFullSyncData.rotation, lastSentFullSyncData.scale);
CmdClientToServerSyncFull(lastSentFullSyncData); CmdClientToServerSyncFull(lastSentFullSyncData);
} }
@ -467,7 +467,7 @@ protected virtual void OnClientToServerSyncFull(SyncDataFull syncData)
// See Server's issue // See Server's issue
lastReceivedFullSyncData = syncData; lastReceivedFullSyncData = syncData;
CleanUpFullSyncDataPositionSync(ref lastReceivedFullSyncData); CleanUpFullSyncDataPositionSync(ref lastReceivedFullSyncData);
lastReceivedFullQuantized = ConstructQuantizedSnapshot(lastReceivedFullSyncData.position, lastReceivedFullSyncData.rotation, lastReceivedFullSyncData.scale); //lastReceivedFullQuantized = ConstructQuantizedSnapshot(lastReceivedFullSyncData.position, lastReceivedFullSyncData.rotation, lastReceivedFullSyncData.scale);
// We don't care if we are adding 'default' to any field because // We don't care if we are adding 'default' to any field because
// syncing is checked again in Apply before applying the changes. // syncing is checked again in Apply before applying the changes.
@ -482,9 +482,9 @@ protected virtual void ClientBroadcastDelta()
if (lastSentFullSyncIndex == 0) return; if (lastSentFullSyncIndex == 0) return;
SyncDataFull currentFull = ConstructFullSyncData(false); SyncDataFull currentFull = ConstructFullSyncData(false);
QuantizedSnapshot currentQuantized = ConstructQuantizedSnapshot(currentFull.position, currentFull.rotation, currentFull.scale); //QuantizedSnapshot currentQuantized = ConstructQuantizedSnapshot(currentFull.position, currentFull.rotation, currentFull.scale);
SyncDataDelta syncDataDelta = DeriveDelta(currentQuantized); SyncDataDelta syncDataDelta = DeriveDelta(currentFull);
CmdClientToServerSyncDelta(syncDataDelta); CmdClientToServerSyncDelta(syncDataDelta);
} }
@ -529,7 +529,7 @@ protected virtual SyncDataFull ConstructFullSyncData(bool updateIndex)
); );
} }
protected virtual QuantizedSnapshot ConstructQuantizedSnapshot(Vector3 position, Quaternion rotation, Vector3 scale) /*protected virtual QuantizedSnapshot ConstructQuantizedSnapshot(Vector3 position, Quaternion rotation, Vector3 scale)
{ {
Compression.ScaleToLong(position, positionPrecision, out Vector3Long positionQuantized); Compression.ScaleToLong(position, positionPrecision, out Vector3Long positionQuantized);
Compression.ScaleToLong(rotation.eulerAngles, rotationSensitivity, out Vector3Long eulRotation); Compression.ScaleToLong(rotation.eulerAngles, rotationSensitivity, out Vector3Long eulRotation);
@ -540,15 +540,15 @@ protected virtual QuantizedSnapshot ConstructQuantizedSnapshot(Vector3 position,
eulRotation, eulRotation,
scaleQuantized scaleQuantized
); );
} }*/
protected virtual SyncDataDelta DeriveDelta(QuantizedSnapshot current) protected virtual SyncDataDelta DeriveDelta(SyncDataFull current)
{ {
SyncDataDelta syncDataDelta = new SyncDataDelta(); SyncDataDelta syncDataDelta = new SyncDataDelta();
syncDataDelta.fullSyncDataIndex = lastSentFullSyncIndex; syncDataDelta.fullSyncDataIndex = lastSentFullSyncIndex;
syncDataDelta.deltaHeader = DeltaHeader.None; syncDataDelta.deltaHeader = DeltaHeader.None;
syncDataDelta.position = current.position - lastSentFullQuantized.position; Compression.ScaleToLong(current.position - lastSentFullSyncData.position, positionPrecision, out syncDataDelta.position);
if ((fullHeader & FullHeader.SyncPosX) > 0 && syncDataDelta.position.x != 0) if ((fullHeader & FullHeader.SyncPosX) > 0 && syncDataDelta.position.x != 0)
syncDataDelta.deltaHeader |= DeltaHeader.PosX; syncDataDelta.deltaHeader |= DeltaHeader.PosX;
@ -578,7 +578,7 @@ protected virtual SyncDataDelta DeriveDelta(QuantizedSnapshot current)
{ {
if ((fullHeader & FullHeader.UseEulerAngles) > 0) if ((fullHeader & FullHeader.UseEulerAngles) > 0)
{ {
Compression.ScaleToLong(lastSentFullQuantized.rotation.eulerAngles, rotationSensitivity, out Vector3Long lastRotationEuler); Compression.ScaleToLong(lastSentFullSyncData.rotation.eulerAngles, rotationSensitivity, out Vector3Long lastRotationEuler);
Compression.ScaleToLong(current.rotation.eulerAngles, rotationSensitivity, out Vector3Long currentRotationEuler); Compression.ScaleToLong(current.rotation.eulerAngles, rotationSensitivity, out Vector3Long currentRotationEuler);
syncDataDelta.eulRotation = currentRotationEuler - lastRotationEuler; syncDataDelta.eulRotation = currentRotationEuler - lastRotationEuler;
@ -589,7 +589,7 @@ protected virtual SyncDataDelta DeriveDelta(QuantizedSnapshot current)
} }
else else
{ {
if (Quaternion.Angle(lastSentFullQuantized.rotation, current.rotation) > rotationSensitivity) if (Quaternion.Angle(lastSentFullSyncData.rotation, current.rotation) > rotationSensitivity)
{ {
syncDataDelta.quatRotation = current.rotation; syncDataDelta.quatRotation = current.rotation;
syncDataDelta.deltaHeader |= DeltaHeader.SendQuat; syncDataDelta.deltaHeader |= DeltaHeader.SendQuat;
@ -603,7 +603,7 @@ protected virtual SyncDataDelta DeriveDelta(QuantizedSnapshot current)
if ((fullHeader & FullHeader.SyncScale) > 0) if ((fullHeader & FullHeader.SyncScale) > 0)
{ {
syncDataDelta.scale = current.scale - lastSentFullQuantized.scale; Compression.ScaleToLong(current.scale - lastSentFullSyncData.scale, positionPrecision, out syncDataDelta.scale);
if (syncDataDelta.scale != Vector3Long.zero) if (syncDataDelta.scale != Vector3Long.zero)
{ {
syncDataDelta.deltaHeader |= DeltaHeader.Scale; syncDataDelta.deltaHeader |= DeltaHeader.Scale;
@ -615,11 +615,11 @@ protected virtual SyncDataDelta DeriveDelta(QuantizedSnapshot current)
protected virtual void ApplyDelta(SyncDataDelta delta, out Vector3 position, out Quaternion rotation, out Vector3 scale) protected virtual void ApplyDelta(SyncDataDelta delta, out Vector3 position, out Quaternion rotation, out Vector3 scale)
{ {
position = Compression.ScaleToFloat(lastReceivedFullQuantized.position + delta.position, positionPrecision); position = lastReceivedFullSyncData.position + Compression.ScaleToFloat(delta.position, positionPrecision);
if ((lastReceivedFullSyncData.fullHeader & FullHeader.UseEulerAngles) > 0) if ((lastReceivedFullSyncData.fullHeader & FullHeader.UseEulerAngles) > 0)
{ {
Vector3 eulRotation = Compression.ScaleToFloat(lastReceivedFullQuantized.rotationEuler + delta.eulRotation, rotationSensitivity); Vector3 eulRotation = lastReceivedFullSyncData.rotation.eulerAngles + Compression.ScaleToFloat(delta.eulRotation, rotationSensitivity);
rotation = Quaternion.Euler(eulRotation); rotation = Quaternion.Euler(eulRotation);
} }
@ -631,7 +631,7 @@ protected virtual void ApplyDelta(SyncDataDelta delta, out Vector3 position, out
rotation = lastReceivedFullSyncData.rotation; rotation = lastReceivedFullSyncData.rotation;
} }
scale = Compression.ScaleToFloat(lastReceivedFullQuantized.scale + delta.scale, scalePrecision); scale = lastReceivedFullSyncData.scale + Compression.ScaleToFloat(delta.scale, scalePrecision);
} }
protected void AddSnapshot(SortedList<double, TransformSnapshot> snapshots, double timeStamp, Vector3? position, Quaternion? rotation, Vector3? scale) protected void AddSnapshot(SortedList<double, TransformSnapshot> snapshots, double timeStamp, Vector3? position, Quaternion? rotation, Vector3? scale)

View File

@ -625,7 +625,7 @@ protected virtual SyncDataFull ConstructFullSyncData(bool updateIndex)
); );
} }
protected virtual QuantizedSnapshot ConstructQuantizedSnapshot(Vector3 position, Quaternion rotation, Vector3 scale) /*protected virtual QuantizedSnapshot ConstructQuantizedSnapshot(Vector3 position, Quaternion rotation, Vector3 scale)
{ {
Compression.ScaleToLong(position, positionPrecision, out Vector3Long positionQuantized); Compression.ScaleToLong(position, positionPrecision, out Vector3Long positionQuantized);
Compression.ScaleToLong(rotation.eulerAngles, rotationSensitivity, out Vector3Long eulRotation); Compression.ScaleToLong(rotation.eulerAngles, rotationSensitivity, out Vector3Long eulRotation);
@ -636,7 +636,7 @@ protected virtual QuantizedSnapshot ConstructQuantizedSnapshot(Vector3 position,
eulRotation, eulRotation,
scaleQuantized scaleQuantized
); );
} }*/
protected virtual SyncDataDelta DeriveDelta(SyncDataFull current) protected virtual SyncDataDelta DeriveDelta(SyncDataFull current)
{ {