Repository navigation
fix: apply the base of a late half float base sync #4251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NoelStephensUnity
wants to merge
3
commits into
develop-2.0.0
Choose a base branch
from
fix/half-float-late-base-sync
base: develop-2.0.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...code.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformHalfFloatLateBaseTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| using System.Collections; | ||
| using NUnit.Framework; | ||
| using Unity.Netcode.Components; | ||
| using Unity.Netcode.TestHelpers.Runtime; | ||
| using UnityEngine; | ||
| using UnityEngine.TestTools; | ||
|
|
||
| namespace Unity.Netcode.RuntimeTests | ||
| { | ||
| /// <summary> | ||
| /// Validates that a half float base sync arriving after the unreliable deltas that depend on it still updates | ||
| /// the non-authority's base position, unless a newer base has already been applied. | ||
| /// </summary> | ||
| [TestFixture(HostOrServer.Host)] | ||
| [TestFixture(HostOrServer.DAHost)] | ||
| internal class NetworkTransformHalfFloatLateBaseTests : IntegrationTestWithApproximation | ||
| { | ||
| protected override int NumberOfClients => 1; | ||
|
|
||
| private const float k_DeltaStep = 0.5f; | ||
|
|
||
| private GameObject m_TestPrefab; | ||
| private NetworkTransform m_NonAuthority; | ||
| private int m_NetworkTick; | ||
|
|
||
| public NetworkTransformHalfFloatLateBaseTests(HostOrServer hostOrServer) : base(hostOrServer) | ||
| { | ||
| } | ||
|
|
||
| protected override void OnServerAndClientsCreated() | ||
| { | ||
| m_TestPrefab = CreateNetworkObjectPrefab("HalfFloatLateBase"); | ||
| var networkTransform = m_TestPrefab.AddComponent<NetworkTransform>(); | ||
| networkTransform.UseHalfFloatPrecision = true; | ||
| networkTransform.UseUnreliableDeltas = true; | ||
| networkTransform.Interpolate = false; | ||
|
|
||
| base.OnServerAndClientsCreated(); | ||
| } | ||
|
|
||
| private IEnumerator SpawnTestObject() | ||
| { | ||
| var authority = GetAuthorityNetworkManager(); | ||
| var authorityInstance = SpawnObject(m_TestPrefab, authority).GetComponent<NetworkObject>(); | ||
| yield return WaitForSpawnedOnAllOrTimeOut(authorityInstance); | ||
| AssertOnTimeout($"Timed out waiting for {authorityInstance.name} to spawn on all clients!"); | ||
|
|
||
| m_NonAuthority = GetNonAuthorityNetworkManager().SpawnManager.SpawnedObjects[authorityInstance.NetworkObjectId].GetComponent<NetworkTransform>(); | ||
| m_NetworkTick = authority.ServerTime.Tick + 10; | ||
| } | ||
|
|
||
| private static NetworkTransform.NetworkTransformState CreateState(NetworkDeltaPosition networkDeltaPosition, int networkTick) | ||
| { | ||
| return new NetworkTransform.NetworkTransformState() | ||
| { | ||
| NetworkTick = networkTick, | ||
| NetworkDeltaPosition = networkDeltaPosition, | ||
| FlagStates = | ||
| { | ||
| HasPositionX = true, | ||
| HasPositionChange = true, | ||
| UseHalfFloatPrecision = true, | ||
| UseUnreliableDeltas = true, | ||
| SynchronizeBaseHalfFloat = networkDeltaPosition.CollapsedDeltaIntoBase, | ||
| ReliableSequenced = networkDeltaPosition.CollapsedDeltaIntoBase, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Encodes the way the authority does: crossing MaxDeltaBeforeAdjustment collapses the delta into a new base. | ||
| /// </summary> | ||
| private NetworkTransform.NetworkTransformState CreateBaseSync(ref NetworkDeltaPosition networkDeltaPosition, ref Vector3 position) | ||
| { | ||
| position = new Vector3(NetworkDeltaPosition.MaxDeltaBeforeAdjustment + k_DeltaStep, 0.0f, 0.0f); | ||
| networkDeltaPosition.UpdateFrom(ref position, ++m_NetworkTick); | ||
| Assert.IsTrue(networkDeltaPosition.CollapsedDeltaIntoBase, "The update did not collapse into a new base!"); | ||
| return CreateState(networkDeltaPosition, m_NetworkTick); | ||
| } | ||
|
|
||
| private void Receive(NetworkTransform.NetworkTransformState state) | ||
| { | ||
| m_NonAuthority.InboundState = state; | ||
| m_NonAuthority.TransformStateUpdate(); | ||
| } | ||
|
|
||
| private void AssertNonAuthorityPosition(Vector3 position, string reason) | ||
| { | ||
| Assert.IsTrue(Approximately(m_NonAuthority.transform.position, position), | ||
| $"{m_NonAuthority.NetworkManager.name} is at {m_NonAuthority.transform.position} but the authority is at {position}. {reason}"); | ||
| } | ||
|
|
||
| [UnityTest] | ||
| public IEnumerator LateBaseSyncStillUpdatesBase() | ||
| { | ||
| yield return SpawnTestObject(); | ||
|
|
||
| var authorityDeltaPosition = new NetworkDeltaPosition(Vector3.zero, m_NetworkTick); | ||
| var position = Vector3.zero; | ||
| var baseSync = CreateBaseSync(ref authorityDeltaPosition, ref position); | ||
|
|
||
| position.x += k_DeltaStep; | ||
| authorityDeltaPosition.UpdateFrom(ref position, ++m_NetworkTick); | ||
| var firstDelta = CreateState(authorityDeltaPosition, m_NetworkTick); | ||
|
|
||
| position.x += k_DeltaStep; | ||
| authorityDeltaPosition.UpdateFrom(ref position, ++m_NetworkTick); | ||
| var secondDelta = CreateState(authorityDeltaPosition, m_NetworkTick); | ||
|
|
||
| // The unreliable delta overtakes the reliable base sync it depends on. | ||
| Receive(firstDelta); | ||
| Receive(baseSync); | ||
| Receive(secondDelta); | ||
|
|
||
| AssertNonAuthorityPosition(position, "The late base sync was not applied, so the delta was decoded against the previous base."); | ||
| } | ||
|
|
||
| [UnityTest] | ||
| public IEnumerator BaseSyncOlderThanAppliedBaseIsIgnored() | ||
| { | ||
| yield return SpawnTestObject(); | ||
|
|
||
| // The former owner's base sync, delayed past an ownership change. | ||
| var formerOwnerDeltaPosition = new NetworkDeltaPosition(Vector3.zero, m_NetworkTick); | ||
| var position = Vector3.zero; | ||
| var staleBaseSync = CreateBaseSync(ref formerOwnerDeltaPosition, ref position); | ||
|
|
||
| // The new owner teleports to its current position when it gains ownership, which sets a new base. | ||
| m_NetworkTick += 10; | ||
| position = new Vector3(10.0f, 0.0f, 0.0f); | ||
| var teleport = new NetworkTransform.NetworkTransformState() | ||
| { | ||
| NetworkTick = m_NetworkTick, | ||
| CurrentPosition = position, | ||
| FlagStates = | ||
| { | ||
| HasPositionX = true, | ||
| HasPositionChange = true, | ||
| UseHalfFloatPrecision = true, | ||
| UseUnreliableDeltas = true, | ||
| IsTeleportingNextFrame = true, | ||
| ReliableSequenced = true, | ||
| }, | ||
| }; | ||
|
|
||
| var newOwnerDeltaPosition = new NetworkDeltaPosition(position, m_NetworkTick); | ||
| position.x += k_DeltaStep; | ||
| newOwnerDeltaPosition.UpdateFrom(ref position, ++m_NetworkTick); | ||
| var newOwnerDelta = CreateState(newOwnerDeltaPosition, m_NetworkTick); | ||
|
|
||
| Receive(teleport); | ||
| Receive(staleBaseSync); | ||
| Receive(newOwnerDelta); | ||
|
|
||
| AssertNonAuthorityPosition(position, "The former owner's base sync replaced the new owner's base."); | ||
| } | ||
| } | ||
| } |
2 changes: 2 additions & 0 deletions
2
...gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformHalfFloatLateBaseTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.