server->client position as Half

This commit is contained in:
mischa 2024-10-24 22:42:00 +02:00
parent 4a7960a52d
commit 60106c6e06

View File

@ -247,8 +247,11 @@ void CmdClientToServerBaseline_Rotation(byte baselineTick, Quaternion rotation)
// cmd delta /////////////////////////////////////////////////////////// // cmd delta ///////////////////////////////////////////////////////////
[Command(channel = Channels.Unreliable)] // unreliable delta [Command(channel = Channels.Unreliable)] // unreliable delta
void CmdClientToServerDelta_Position(byte baselineTick, Vector3 position) void CmdClientToServerDelta_Position(byte baselineTick, Half x, Half y, Half z)
{ {
// Half: +-65k with 0.0001 precision is enough for deltas
Vector3 position = new Vector3((float)x, (float)y, (float)z);
// Debug.Log($"[{name}] server received delta for baseline #{lastDeserializedBaselineTick}"); // Debug.Log($"[{name}] server received delta for baseline #{lastDeserializedBaselineTick}");
OnClientToServerDeltaSync(baselineTick, position, Quaternion.identity);//, scale); OnClientToServerDeltaSync(baselineTick, position, Quaternion.identity);//, scale);
} }
@ -261,8 +264,11 @@ void CmdClientToServerDelta_Rotation(byte baselineTick, Quaternion rotation)
} }
[Command(channel = Channels.Unreliable)] // unreliable delta [Command(channel = Channels.Unreliable)] // unreliable delta
void CmdClientToServerDelta_PositionRotation(byte baselineTick, Vector3 position, Quaternion rotation) void CmdClientToServerDelta_PositionRotation(byte baselineTick, Half x, Half y, Half z, Quaternion rotation)
{ {
// Half: +-65k with 0.0001 precision is enough for deltas
Vector3 position = new Vector3((float)x, (float)y, (float)z);
// Debug.Log($"[{name}] server received delta for baseline #{lastDeserializedBaselineTick}"); // Debug.Log($"[{name}] server received delta for baseline #{lastDeserializedBaselineTick}");
OnClientToServerDeltaSync(baselineTick, position, rotation);//, scale); OnClientToServerDeltaSync(baselineTick, position, rotation);//, scale);
} }
@ -368,23 +374,29 @@ void RpcServerToClientBaseline_Rotation(byte baselineTick, Quaternion rotation)
// rpc delta /////////////////////////////////////////////////////////// // rpc delta ///////////////////////////////////////////////////////////
[ClientRpc(channel = Channels.Unreliable)] // unreliable delta [ClientRpc(channel = Channels.Unreliable)] // unreliable delta
void RpcServerToClientDelta_PositionRotation(byte baselineTick, Vector3 position, Quaternion rotation) void RpcServerToClientDelta_PositionRotation(byte baselineTick, Half x, Half y, Half z, Quaternion rotation)
{ {
// delta is broadcast to all clients. // delta is broadcast to all clients.
// ignore if this object is owned by this client. // ignore if this object is owned by this client.
if (IsClientWithAuthority) return; if (IsClientWithAuthority) return;
// Half: +-65k with 0.0001 precision is enough for deltas
Vector3 position = new Vector3((float)x, (float)y, (float)z);
OnServerToClientDeltaSync(baselineTick, position, rotation);//, scale); OnServerToClientDeltaSync(baselineTick, position, rotation);//, scale);
} }
[ClientRpc(channel = Channels.Unreliable)] // unreliable delta [ClientRpc(channel = Channels.Unreliable)] // unreliable delta
void RpcServerToClientDelta_Position(byte baselineTick, Vector3 position) void RpcServerToClientDelta_Position(byte baselineTick, Half x, Half y, Half z)
{ {
// delta is broadcast to all clients. // delta is broadcast to all clients.
// ignore if this object is owned by this client. // ignore if this object is owned by this client.
if (IsClientWithAuthority) return; if (IsClientWithAuthority) return;
// Half: +-65k with 0.0001 precision is enough for deltas
Vector3 position = new Vector3((float)x, (float)y, (float)z);
OnServerToClientDeltaSync(baselineTick, position, Quaternion.identity);//, scale); OnServerToClientDeltaSync(baselineTick, position, Quaternion.identity);//, scale);
} }
@ -570,26 +582,33 @@ void UpdateServerDelta(double localTime)
// TransformSnapshot snapshot = ConstructSnapshot(); // TransformSnapshot snapshot = ConstructSnapshot();
target.GetLocalPositionAndRotation(out Vector3 position, out Quaternion rotation); target.GetLocalPositionAndRotation(out Vector3 position, out Quaternion rotation);
// Half: +-65k with 0.0001 precision is enough for deltas.
// and this cuts position sync bandwidth in half.
Half x = (Half)position.x;
Half y = (Half)position.y;
Half z = (Half)position.z;
// save bandwidth by only transmitting what is needed. // save bandwidth by only transmitting what is needed.
// -> ArraySegment with random data is slower since byte[] copying // -> ArraySegment with random data is slower since byte[] copying
// -> Vector3? and Quaternion? nullables takes more bandwidth // -> Vector3? and Quaternion? nullables takes more bandwidth
if (syncPosition && syncRotation) if (syncPosition && syncRotation)
{ {
// send snapshot without timestamp. // send snapshot without timestamp.
// receiver gets it from batch timestamp to save bandwidth. // receiver gets it from batch timestamp to save bandwidth.
// unreliable redundancy to make up for potential message drops // unreliable redundancy to make up for potential message drops
RpcServerToClientDelta_PositionRotation(lastSerializedBaselineTick, position, rotation); RpcServerToClientDelta_PositionRotation(lastSerializedBaselineTick, x,y,z, rotation);
if (unreliableRedundancy) if (unreliableRedundancy)
RpcServerToClientDelta_PositionRotation(lastSerializedBaselineTick, position, rotation); RpcServerToClientDelta_PositionRotation(lastSerializedBaselineTick, x,y,z, rotation);
} }
else if (syncPosition) else if (syncPosition)
{ {
// send snapshot without timestamp. // send snapshot without timestamp.
// receiver gets it from batch timestamp to save bandwidth. // receiver gets it from batch timestamp to save bandwidth.
// unreliable redundancy to make up for potential message drops // unreliable redundancy to make up for potential message drops
RpcServerToClientDelta_Position(lastSerializedBaselineTick, position); RpcServerToClientDelta_Position(lastSerializedBaselineTick, x,y,z);
if (unreliableRedundancy) if (unreliableRedundancy)
RpcServerToClientDelta_Position(lastSerializedBaselineTick, position); RpcServerToClientDelta_Position(lastSerializedBaselineTick, x,y,z);
} }
else if (syncRotation) else if (syncRotation)
{ {
@ -756,6 +775,12 @@ void UpdateClientDelta(double localTime)
// TransformSnapshot snapshot = ConstructSnapshot(); // TransformSnapshot snapshot = ConstructSnapshot();
target.GetLocalPositionAndRotation(out Vector3 position, out Quaternion rotation); target.GetLocalPositionAndRotation(out Vector3 position, out Quaternion rotation);
// Half: +-65k with 0.0001 precision is enough for deltas.
// and this cuts position sync bandwidth in half.
Half x = (Half)position.x;
Half y = (Half)position.y;
Half z = (Half)position.z;
// save bandwidth by only transmitting what is needed. // save bandwidth by only transmitting what is needed.
// -> ArraySegment with random data is slower since byte[] copying // -> ArraySegment with random data is slower since byte[] copying
// -> Vector3? and Quaternion? nullables takes more bandwidth // -> Vector3? and Quaternion? nullables takes more bandwidth
@ -764,9 +789,9 @@ void UpdateClientDelta(double localTime)
// send snapshot without timestamp. // send snapshot without timestamp.
// receiver gets it from batch timestamp to save bandwidth. // receiver gets it from batch timestamp to save bandwidth.
// unreliable redundancy to make up for potential message drops // unreliable redundancy to make up for potential message drops
CmdClientToServerDelta_PositionRotation(lastSerializedBaselineTick, position, rotation); CmdClientToServerDelta_PositionRotation(lastSerializedBaselineTick, x,y,z, rotation);
if (unreliableRedundancy) if (unreliableRedundancy)
CmdClientToServerDelta_PositionRotation(lastSerializedBaselineTick, position, rotation); CmdClientToServerDelta_PositionRotation(lastSerializedBaselineTick, x,y,z, rotation);
} }
else if (syncPosition) else if (syncPosition)
@ -774,9 +799,9 @@ void UpdateClientDelta(double localTime)
// send snapshot without timestamp. // send snapshot without timestamp.
// receiver gets it from batch timestamp to save bandwidth. // receiver gets it from batch timestamp to save bandwidth.
// unreliable redundancy to make up for potential message drops // unreliable redundancy to make up for potential message drops
CmdClientToServerDelta_Position(lastSerializedBaselineTick, position); CmdClientToServerDelta_Position(lastSerializedBaselineTick, x,y,z);
if (unreliableRedundancy) if (unreliableRedundancy)
CmdClientToServerDelta_Position(lastSerializedBaselineTick, position); CmdClientToServerDelta_Position(lastSerializedBaselineTick, x,y,z);
} }
else if (syncRotation) else if (syncRotation)
{ {