-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathNetworkTransformBase.cs
More file actions
1027 lines (906 loc) · 42.5 KB
/
NetworkTransformBase.cs
File metadata and controls
1027 lines (906 loc) · 42.5 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Unity.Netcode.Components;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
namespace Unity.Netcode.RuntimeTests
{
internal class NetworkTransformBase : IntegrationTestWithApproximation
{
// The number of iterations to change position, rotation, and scale for NetworkTransformMultipleChangesOverTime
protected const int k_PositionRotationScaleIterations = 3;
protected const int k_PositionRotationScaleIterations3Axis = 8;
protected float m_CurrentHalfPrecision = 0.0f;
protected const float k_HalfPrecisionPosScale = 0.1256f;
protected const float k_HalfPrecisionRot = 0.725f;
protected NetworkObject m_AuthoritativePlayer;
protected NetworkObject m_NonAuthoritativePlayer;
protected NetworkObject m_ChildObject;
protected NetworkObject m_SubChildObject;
protected NetworkObject m_ParentObject;
protected NetworkTransformTestComponent m_AuthoritativeTransform;
protected NetworkTransformTestComponent m_NonAuthoritativeTransform;
protected NetworkTransformTestComponent m_OwnerTransform;
protected int m_OriginalTargetFrameRate;
protected Axis m_CurrentAxis;
protected bool m_AxisExcluded;
protected float m_DetectedPotentialInterpolatedTeleport;
protected StringBuilder m_InfoMessage = new StringBuilder();
protected Rotation m_Rotation = Rotation.Euler;
protected Precision m_Precision = Precision.Full;
protected RotationCompression m_RotationCompression = RotationCompression.None;
protected Authority m_Authority;
// To test that local position, rotation, and scale remain the same when parented.
protected Vector3 m_ChildObjectLocalPosition = new Vector3(5.0f, 0.0f, -5.0f);
protected Vector3 m_ChildObjectLocalRotation = new Vector3(-35.0f, 90.0f, 270.0f);
protected Vector3 m_ChildObjectLocalScale = new Vector3(0.1f, 0.5f, 0.4f);
protected Vector3 m_SubChildObjectLocalPosition = new Vector3(2.0f, 1.0f, -1.0f);
protected Vector3 m_SubChildObjectLocalRotation = new Vector3(5.0f, 15.0f, 124.0f);
protected Vector3 m_SubChildObjectLocalScale = new Vector3(1.0f, 0.15f, 0.75f);
protected NetworkObject m_AuthorityParentObject;
protected NetworkTransformTestComponent m_AuthorityParentNetworkTransform;
protected NetworkObject m_AuthorityChildObject;
protected NetworkObject m_AuthoritySubChildObject;
protected ChildObjectComponent m_AuthorityChildNetworkTransform;
protected ChildObjectComponent m_AuthoritySubChildNetworkTransform;
public enum Authority
{
ServerAuthority,
OwnerAuthority
}
public enum Interpolation
{
DisableInterpolate,
EnableInterpolate
}
public enum Precision
{
Half,
Full
}
public enum Rotation
{
Euler,
Quaternion
}
public enum RotationCompression
{
None,
QuaternionCompress
}
public enum TransformSpace
{
World,
Local
}
public enum OverrideState
{
Update,
SetState
}
public enum Axis
{
X,
Y,
Z,
XY,
XZ,
YZ,
XYZ
}
protected enum ChildrenTransformCheckType
{
ConnectedClients,
LateJoinClient
}
protected override int NumberOfClients => OnNumberOfClients();
protected override float GetDeltaVarianceThreshold()
{
if (m_Precision == Precision.Half || m_RotationCompression == RotationCompression.QuaternionCompress)
{
return m_CurrentHalfPrecision;
}
return 0.055f;
}
/// <summary>
/// Override to provide the number of clients
/// </summary>
/// <returns></returns>
protected virtual int OnNumberOfClients()
{
return 1;
}
/// <summary>
/// Determines whether the test will use unreliable delivery for implicit state updates or not
/// </summary>
protected virtual bool UseUnreliableDeltas()
{
return false;
}
protected virtual void Setup()
{
NetworkTransformTestComponent.AuthorityInstance = null;
m_Precision = Precision.Full;
ChildObjectComponent.Reset();
}
protected virtual void Teardown()
{
m_EnableVerboseDebug = false;
Object.DestroyImmediate(m_PlayerPrefab);
}
/// <summary>
/// Handles the Setup for time travel enabled child derived tests
/// </summary>
protected override void OnInlineSetup()
{
Setup();
base.OnInlineSetup();
}
/// <summary>
/// Handles the Teardown for time travel enabled child derived tests
/// </summary>
protected override void OnInlineTearDown()
{
Teardown();
base.OnInlineTearDown();
}
/// <summary>
/// Handles the Setup for coroutine based derived tests
/// </summary>
protected override IEnumerator OnSetup()
{
Setup();
return base.OnSetup();
}
/// <summary>
/// Handles the Teardown for coroutine based derived tests
/// </summary>
protected override IEnumerator OnTearDown()
{
Teardown();
return base.OnTearDown();
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="testWithHost">Determines if we are running as a server or host</param>
/// <param name="authority">Determines if we are using server or owner authority</param>
public NetworkTransformBase(HostOrServer testWithHost, Authority authority, RotationCompression rotationCompression, Rotation rotation, Precision precision) : base(testWithHost)
{
m_Authority = authority;
m_Precision = precision;
m_RotationCompression = rotationCompression;
m_Rotation = rotation;
}
protected virtual int TargetFrameRate()
{
return 120;
}
protected override void OnOneTimeSetup()
{
m_OriginalTargetFrameRate = Application.targetFrameRate;
Application.targetFrameRate = TargetFrameRate();
base.OnOneTimeSetup();
}
protected override void OnOneTimeTearDown()
{
Application.targetFrameRate = m_OriginalTargetFrameRate;
base.OnOneTimeTearDown();
}
protected override void OnCreatePlayerPrefab()
{
var networkTransformTestComponent = m_PlayerPrefab.AddComponent<NetworkTransformTestComponent>();
networkTransformTestComponent.ServerAuthority = m_Authority == Authority.ServerAuthority;
// Handle setting up additional transform settings for the current test here.
networkTransformTestComponent.UseUnreliableDeltas = UseUnreliableDeltas();
networkTransformTestComponent.UseHalfFloatPrecision = m_Precision == Precision.Half;
networkTransformTestComponent.UseQuaternionSynchronization = m_Rotation == Rotation.Quaternion;
networkTransformTestComponent.UseQuaternionCompression = m_RotationCompression == RotationCompression.QuaternionCompress;
}
protected override void OnServerAndClientsCreated()
{
var subChildObject = CreateNetworkObjectPrefab("SubChildObject");
var subChildNetworkTransform = subChildObject.AddComponent<SubChildObjectComponent>();
subChildNetworkTransform.ServerAuthority = m_Authority == Authority.ServerAuthority;
m_SubChildObject = subChildObject.GetComponent<NetworkObject>();
var childObject = CreateNetworkObjectPrefab("ChildObject");
var childNetworkTransform = childObject.AddComponent<ChildObjectComponent>();
childNetworkTransform.ServerAuthority = m_Authority == Authority.ServerAuthority;
m_ChildObject = childObject.GetComponent<NetworkObject>();
var parentObject = CreateNetworkObjectPrefab("ParentObject");
var parentNetworkTransform = parentObject.AddComponent<NetworkTransformTestComponent>();
parentNetworkTransform.ServerAuthority = m_Authority == Authority.ServerAuthority;
m_ParentObject = parentObject.GetComponent<NetworkObject>();
// Now apply local transform values
m_ChildObject.transform.position = m_ChildObjectLocalPosition;
var childRotation = m_ChildObject.transform.rotation;
childRotation.eulerAngles = m_ChildObjectLocalRotation;
m_ChildObject.transform.rotation = childRotation;
m_ChildObject.transform.localScale = m_ChildObjectLocalScale;
m_SubChildObject.transform.position = m_SubChildObjectLocalPosition;
var subChildRotation = m_SubChildObject.transform.rotation;
subChildRotation.eulerAngles = m_SubChildObjectLocalRotation;
m_SubChildObject.transform.rotation = childRotation;
m_SubChildObject.transform.localScale = m_SubChildObjectLocalScale;
if (m_EnableVerboseDebug)
{
m_ServerNetworkManager.LogLevel = LogLevel.Developer;
foreach (var clientNetworkManager in m_ClientNetworkManagers)
{
clientNetworkManager.LogLevel = LogLevel.Developer;
}
}
m_ServerNetworkManager.NetworkConfig.TickRate = GetTickRate();
foreach (var clientNetworkManager in m_ClientNetworkManagers)
{
clientNetworkManager.NetworkConfig.TickRate = GetTickRate();
}
}
protected virtual void OnClientsAndServerConnectedSetup()
{
// Get the client player representation on both the server and the client side
var serverSideClientPlayer = m_PlayerNetworkObjects[0][m_ClientNetworkManagers[0].LocalClientId];
var clientSideClientPlayer = m_PlayerNetworkObjects[m_ClientNetworkManagers[0].LocalClientId][m_ClientNetworkManagers[0].LocalClientId];
m_AuthoritativePlayer = m_Authority == Authority.ServerAuthority ? serverSideClientPlayer : clientSideClientPlayer;
m_NonAuthoritativePlayer = m_Authority == Authority.ServerAuthority ? clientSideClientPlayer : serverSideClientPlayer;
// Get the NetworkTransformTestComponent to make sure the client side is ready before starting test
m_AuthoritativeTransform = m_AuthoritativePlayer.GetComponent<NetworkTransformTestComponent>();
m_NonAuthoritativeTransform = m_NonAuthoritativePlayer.GetComponent<NetworkTransformTestComponent>();
m_OwnerTransform = m_AuthoritativeTransform.IsOwner ? m_AuthoritativeTransform : m_NonAuthoritativeTransform;
}
protected override void OnTimeTravelServerAndClientsConnected()
{
OnClientsAndServerConnectedSetup();
// Wait for the client-side to notify it is finished initializing and spawning.
var success = WaitForConditionOrTimeOutWithTimeTravel(() => m_NonAuthoritativeTransform.ReadyToReceivePositionUpdate);
Assert.True(success, "Timed out waiting for client-side to notify it is ready!");
Assert.True(m_AuthoritativeTransform.CanCommitToTransform);
Assert.False(m_NonAuthoritativeTransform.CanCommitToTransform);
// Just wait for at least one tick for NetworkTransforms to finish synchronization
TimeTravelAdvanceTick();
}
/// <summary>
/// Handles the OnServerAndClientsConnected for coroutine based derived tests
/// </summary>
protected override IEnumerator OnServerAndClientsConnected()
{
// Wait for the client-side to notify it is finished initializing and spawning.
yield return WaitForClientsConnectedOrTimeOut();
AssertOnTimeout("Timed out waiting for client-side to notify it is ready!");
OnClientsAndServerConnectedSetup();
yield return base.OnServerAndClientsConnected();
}
/// <summary>
/// Handles setting a new client being connected
/// </summary>
protected override void OnNewClientCreated(NetworkManager networkManager)
{
networkManager.NetworkConfig.TickRate = GetTickRate();
if (m_EnableVerboseDebug)
{
networkManager.LogLevel = LogLevel.Developer;
}
base.OnNewClientCreated(networkManager);
}
/// <summary>
/// Returns true when the server-host and all clients have
/// instantiated the child object to be used in <see cref="NetworkTransformParentingLocalSpaceOffsetTests"/>
/// </summary>
/// <returns></returns>
protected bool AllChildObjectInstancesAreSpawned()
{
if (ChildObjectComponent.AuthorityInstance == null)
{
return false;
}
if (ChildObjectComponent.HasSubChild && ChildObjectComponent.AuthoritySubInstance == null)
{
return false;
}
foreach (var clientNetworkManager in m_ClientNetworkManagers)
{
if (!ChildObjectComponent.ClientInstances.ContainsKey(clientNetworkManager.LocalClientId))
{
return false;
}
}
return true;
}
protected bool AllFirstLevelChildObjectInstancesHaveChild()
{
foreach (var instance in ChildObjectComponent.ClientInstances.Values)
{
if (instance.transform.parent == null)
{
return false;
}
}
return true;
}
protected bool AllChildObjectInstancesHaveChild()
{
foreach (var instance in ChildObjectComponent.ClientInstances.Values)
{
if (instance.transform.parent == null)
{
return false;
}
}
if (ChildObjectComponent.HasSubChild)
{
foreach (var instance in ChildObjectComponent.ClientSubChildInstances.Values)
{
if (instance.transform.parent == null)
{
return false;
}
}
}
return true;
}
protected bool AllFirstLevelChildObjectInstancesHaveNoParent()
{
foreach (var instance in ChildObjectComponent.ClientInstances.Values)
{
if (instance.transform.parent != null)
{
return false;
}
}
return true;
}
protected bool AllSubChildObjectInstancesHaveNoParent()
{
if (ChildObjectComponent.HasSubChild)
{
foreach (var instance in ChildObjectComponent.ClientSubChildInstances.Values)
{
if (instance.transform.parent != null)
{
return false;
}
}
}
return true;
}
/// <summary>
/// A wait condition specific method that assures the local space coordinates
/// are not impacted by NetworkTransform when parented.
/// </summary>
protected bool AllInstancesKeptLocalTransformValues(bool useSubChild)
{
var authorityObjectLocalPosition = useSubChild ? m_AuthoritySubChildObject.transform.localPosition : m_AuthorityChildObject.transform.localPosition;
var authorityObjectLocalRotation = useSubChild ? m_AuthoritySubChildObject.transform.localRotation.eulerAngles : m_AuthorityChildObject.transform.localRotation.eulerAngles;
var authorityObjectLocalScale = useSubChild ? m_AuthoritySubChildObject.transform.localScale : m_AuthorityChildObject.transform.localScale;
var instances = useSubChild ? ChildObjectComponent.SubInstances : ChildObjectComponent.Instances;
foreach (var childInstance in instances)
{
var childLocalPosition = childInstance.transform.localPosition;
var childLocalRotation = childInstance.transform.localRotation.eulerAngles;
var childLocalScale = childInstance.transform.localScale;
// Adjust approximation based on precision
if (m_Precision == Precision.Half)
{
m_CurrentHalfPrecision = k_HalfPrecisionPosScale;
}
if (!Approximately(childLocalPosition, authorityObjectLocalPosition))
{
return false;
}
if (!Approximately(childLocalScale, authorityObjectLocalScale))
{
return false;
}
// Adjust approximation based on precision
if (m_Precision == Precision.Half || m_RotationCompression == RotationCompression.QuaternionCompress)
{
m_CurrentHalfPrecision = k_HalfPrecisionRot;
}
if (!ApproximatelyEuler(childLocalRotation, authorityObjectLocalRotation))
{
return false;
}
}
return true;
}
protected bool PostAllChildrenLocalTransformValuesMatch(bool useSubChild)
{
var success = !s_GlobalTimeoutHelper.TimedOut;
var authorityObjectLocalPosition = useSubChild ? m_AuthoritySubChildObject.transform.localPosition : m_AuthorityChildObject.transform.localPosition;
var authorityObjectLocalRotation = useSubChild ? m_AuthoritySubChildObject.transform.localRotation.eulerAngles : m_AuthorityChildObject.transform.localRotation.eulerAngles;
var authorityObjectLocalScale = useSubChild ? m_AuthoritySubChildObject.transform.localScale : m_AuthorityChildObject.transform.localScale;
if (s_GlobalTimeoutHelper.TimedOut)
{
// If we timed out, then wait for a full range of ticks (plus 1) to assure it sent synchronization data.
for (int j = 0; j < m_ServerNetworkManager.NetworkConfig.TickRate; j++)
{
var instances = useSubChild ? ChildObjectComponent.SubInstances : ChildObjectComponent.Instances;
foreach (var childInstance in instances)
{
var childParentName = "invalid";
try
{
childParentName = useSubChild ? childInstance.transform.parent.parent.name : childInstance.transform.name;
}
catch (System.Exception ex)
{
Debug.Log(ex.Message);
}
var childLocalPosition = childInstance.transform.localPosition;
var childLocalRotation = childInstance.transform.localRotation.eulerAngles;
var childLocalScale = childInstance.transform.localScale;
// Adjust approximation based on precision
if (m_Precision == Precision.Half || m_RotationCompression == RotationCompression.QuaternionCompress)
{
m_CurrentHalfPrecision = k_HalfPrecisionPosScale;
}
if (!Approximately(childLocalPosition, authorityObjectLocalPosition))
{
m_InfoMessage.AppendLine($"[{childParentName}][{childInstance.name}] Child's Local Position ({GetVector3Values(childLocalPosition)}) | Authority Local Position ({GetVector3Values(authorityObjectLocalPosition)})");
success = false;
}
if (!Approximately(childLocalScale, authorityObjectLocalScale))
{
m_InfoMessage.AppendLine($"[{childParentName}][{childInstance.name}] Child's Local Scale ({GetVector3Values(childLocalScale)}) | Authority Local Scale ({GetVector3Values(authorityObjectLocalScale)})");
success = false;
}
// Adjust approximation based on precision
if (m_Precision == Precision.Half || m_RotationCompression == RotationCompression.QuaternionCompress)
{
m_CurrentHalfPrecision = k_HalfPrecisionRot;
}
if (!ApproximatelyEuler(childLocalRotation, authorityObjectLocalRotation))
{
m_InfoMessage.AppendLine($"[{childParentName}][{childInstance.name}] Child's Local Rotation ({GetVector3Values(childLocalRotation)}) | Authority Local Rotation ({GetVector3Values(authorityObjectLocalRotation)})");
success = false;
}
}
}
}
return success;
}
/// <summary>
/// Validates that moving, rotating, and scaling the authority side with a single
/// tick will properly synchronize the non-authoritative side with the same values.
/// </summary>
protected void MoveRotateAndScaleAuthority(Vector3 position, Vector3 rotation, Vector3 scale, OverrideState overrideState)
{
switch (overrideState)
{
case OverrideState.SetState:
{
var authoritativeRotation = m_AuthoritativeTransform.GetSpaceRelativeRotation();
authoritativeRotation.eulerAngles = rotation;
if (m_Authority == Authority.OwnerAuthority)
{
// Under the scenario where the owner is not the server, and non-auth is the server we set the state from the server
// to be updated to the owner.
if (m_AuthoritativeTransform.IsOwner && !m_AuthoritativeTransform.IsServer && m_NonAuthoritativeTransform.IsServer)
{
m_NonAuthoritativeTransform.SetState(position, authoritativeRotation, scale);
}
else
{
m_AuthoritativeTransform.SetState(position, authoritativeRotation, scale);
}
}
else
{
m_AuthoritativeTransform.SetState(position, authoritativeRotation, scale);
}
break;
}
case OverrideState.Update:
default:
{
m_AuthoritativeTransform.transform.position = position;
var authoritativeRotation = m_AuthoritativeTransform.GetSpaceRelativeRotation();
authoritativeRotation.eulerAngles = rotation;
m_AuthoritativeTransform.transform.rotation = authoritativeRotation;
m_AuthoritativeTransform.transform.localScale = scale;
break;
}
}
}
/// <summary>
/// Randomly determine if an axis should be excluded.
/// If so, then randomly pick one of the axis to be excluded.
/// </summary>
protected Vector3 RandomlyExcludeAxis(Vector3 delta)
{
if (Random.Range(0.0f, 1.0f) >= 0.5f)
{
m_AxisExcluded = true;
var axisToIgnore = Random.Range(0, 2);
switch (axisToIgnore)
{
case 0:
{
delta.x = 0;
break;
}
case 1:
{
delta.y = 0;
break;
}
case 2:
{
delta.z = 0;
break;
}
}
}
return delta;
}
protected bool PositionRotationScaleMatches()
{
return RotationsMatch() && PositionsMatch() && ScaleValuesMatch();
}
protected bool PositionRotationScaleMatches(Vector3 position, Vector3 eulerRotation, Vector3 scale)
{
return PositionsMatchesValue(position) && RotationMatchesValue(eulerRotation) && ScaleMatchesValue(scale);
}
protected bool PositionsMatchesValue(Vector3 positionToMatch)
{
var authorityPosition = m_AuthoritativeTransform.transform.position;
var nonAuthorityPosition = m_NonAuthoritativeTransform.transform.position;
var auhtorityIsEqual = Approximately(authorityPosition, positionToMatch);
var nonauthorityIsEqual = Approximately(nonAuthorityPosition, positionToMatch);
if (!auhtorityIsEqual)
{
VerboseDebug($"Authority ({m_AuthoritativeTransform.name}) position {authorityPosition} != position to match: {positionToMatch}!");
}
if (!nonauthorityIsEqual)
{
VerboseDebug($"NonAuthority ({m_NonAuthoritativeTransform.name}) position {nonAuthorityPosition} != position to match: {positionToMatch}!");
}
return auhtorityIsEqual && nonauthorityIsEqual;
}
protected bool RotationMatchesValue(Vector3 rotationEulerToMatch)
{
var authorityRotationEuler = m_AuthoritativeTransform.transform.rotation.eulerAngles;
var nonAuthorityRotationEuler = m_NonAuthoritativeTransform.transform.rotation.eulerAngles;
var auhtorityIsEqual = Approximately(authorityRotationEuler, rotationEulerToMatch);
var nonauthorityIsEqual = Approximately(nonAuthorityRotationEuler, rotationEulerToMatch);
if (!auhtorityIsEqual)
{
VerboseDebug($"Authority rotation {authorityRotationEuler} != rotation to match: {rotationEulerToMatch}!");
}
if (!nonauthorityIsEqual)
{
VerboseDebug($"NonAuthority rotation {nonAuthorityRotationEuler} != rotation to match: {rotationEulerToMatch}!");
}
return auhtorityIsEqual && nonauthorityIsEqual;
}
protected bool ScaleMatchesValue(Vector3 scaleToMatch)
{
var authorityScale = m_AuthoritativeTransform.transform.localScale;
var nonAuthorityScale = m_NonAuthoritativeTransform.transform.localScale;
var auhtorityIsEqual = Approximately(authorityScale, scaleToMatch);
var nonauthorityIsEqual = Approximately(nonAuthorityScale, scaleToMatch);
if (!auhtorityIsEqual)
{
VerboseDebug($"Authority scale {authorityScale} != scale to match: {scaleToMatch}!");
}
if (!nonauthorityIsEqual)
{
VerboseDebug($"NonAuthority scale {nonAuthorityScale} != scale to match: {scaleToMatch}!");
}
return auhtorityIsEqual && nonauthorityIsEqual;
}
protected bool TeleportPositionMatches(Vector3 nonAuthorityOriginalPosition)
{
var nonAuthorityPosition = m_NonAuthoritativeTransform.transform.position;
var authorityPosition = m_AuthoritativeTransform.transform.position;
var targetDistance = Mathf.Abs(Vector3.Distance(nonAuthorityOriginalPosition, authorityPosition));
var nonAuthorityCurrentDistance = Mathf.Abs(Vector3.Distance(nonAuthorityPosition, nonAuthorityOriginalPosition));
// If we are not within our target distance range
if (!Approximately(targetDistance, nonAuthorityCurrentDistance))
{
// Apply the non-authority's distance that is checked at the end of the teleport test
m_DetectedPotentialInterpolatedTeleport = nonAuthorityCurrentDistance;
return false;
}
else
{
// Otherwise, if we are within our target distance range then reset any already set value
m_DetectedPotentialInterpolatedTeleport = 0.0f;
}
var xIsEqual = Approximately(authorityPosition.x, nonAuthorityPosition.x);
var yIsEqual = Approximately(authorityPosition.y, nonAuthorityPosition.y);
var zIsEqual = Approximately(authorityPosition.z, nonAuthorityPosition.z);
if (!xIsEqual || !yIsEqual || !zIsEqual)
{
VerboseDebug($"[{m_AuthoritativeTransform.gameObject.name}] Authority position {authorityPosition} != [{m_NonAuthoritativeTransform.gameObject.name}] NonAuthority position {nonAuthorityPosition}");
}
return xIsEqual && yIsEqual && zIsEqual;
}
protected bool RotationsMatch(bool printDeltas = false)
{
m_CurrentHalfPrecision = k_HalfPrecisionRot;
var authorityEulerRotation = m_AuthoritativeTransform.GetSpaceRelativeRotation().eulerAngles;
var nonAuthorityEulerRotation = m_NonAuthoritativeTransform.GetSpaceRelativeRotation().eulerAngles;
var xIsEqual = ApproximatelyEuler(authorityEulerRotation.x, nonAuthorityEulerRotation.x) || !m_AuthoritativeTransform.SyncRotAngleX;
var yIsEqual = ApproximatelyEuler(authorityEulerRotation.y, nonAuthorityEulerRotation.y) || !m_AuthoritativeTransform.SyncRotAngleY;
var zIsEqual = ApproximatelyEuler(authorityEulerRotation.z, nonAuthorityEulerRotation.z) || !m_AuthoritativeTransform.SyncRotAngleZ;
if (!xIsEqual || !yIsEqual || !zIsEqual)
{
VerboseDebug($"[{m_AuthoritativeTransform.gameObject.name}][X-{xIsEqual} | Y-{yIsEqual} | Z-{zIsEqual}][{m_CurrentAxis}]" +
$"[Sync: X-{m_AuthoritativeTransform.SyncRotAngleX} | Y-{m_AuthoritativeTransform.SyncRotAngleY} | Z-{m_AuthoritativeTransform.SyncRotAngleZ}] Authority rotation {authorityEulerRotation} != [{m_NonAuthoritativeTransform.gameObject.name}] NonAuthority rotation {nonAuthorityEulerRotation}");
}
if (printDeltas)
{
Debug.Log($"[Rotation Match] Euler Delta {EulerDelta(authorityEulerRotation, nonAuthorityEulerRotation)}");
}
return xIsEqual && yIsEqual && zIsEqual;
}
protected bool PositionsMatch()
{
m_CurrentHalfPrecision = k_HalfPrecisionPosScale;
var authorityPosition = m_AuthoritativeTransform.GetSpaceRelativePosition();
var nonAuthorityPosition = m_NonAuthoritativeTransform.GetSpaceRelativePosition();
var xIsEqual = Approximately(authorityPosition.x, nonAuthorityPosition.x) || !m_AuthoritativeTransform.SyncPositionX;
var yIsEqual = Approximately(authorityPosition.y, nonAuthorityPosition.y) || !m_AuthoritativeTransform.SyncPositionY;
var zIsEqual = Approximately(authorityPosition.z, nonAuthorityPosition.z) || !m_AuthoritativeTransform.SyncPositionZ;
if (!xIsEqual || !yIsEqual || !zIsEqual)
{
VerboseDebug($"[{m_AuthoritativeTransform.gameObject.name}] Authority position {authorityPosition} != [{m_NonAuthoritativeTransform.gameObject.name}] NonAuthority position {nonAuthorityPosition}");
}
return xIsEqual && yIsEqual && zIsEqual;
}
protected bool ScaleValuesMatch()
{
m_CurrentHalfPrecision = k_HalfPrecisionPosScale;
var authorityScale = m_AuthoritativeTransform.transform.localScale;
var nonAuthorityScale = m_NonAuthoritativeTransform.transform.localScale;
var xIsEqual = Approximately(authorityScale.x, nonAuthorityScale.x) || !m_AuthoritativeTransform.SyncScaleX;
var yIsEqual = Approximately(authorityScale.y, nonAuthorityScale.y) || !m_AuthoritativeTransform.SyncScaleY;
var zIsEqual = Approximately(authorityScale.z, nonAuthorityScale.z) || !m_AuthoritativeTransform.SyncScaleZ;
if (!xIsEqual || !yIsEqual || !zIsEqual)
{
VerboseDebug($"[{m_AuthoritativeTransform.gameObject.name}] Authority scale {authorityScale} != [{m_NonAuthoritativeTransform.gameObject.name}] NonAuthority scale {nonAuthorityScale}");
}
return xIsEqual && yIsEqual && zIsEqual;
}
}
/// <summary>
/// Helper component for all NetworkTransformTests
/// </summary>
internal class NetworkTransformTestComponent : NetworkTransform
{
public bool ServerAuthority;
public bool ReadyToReceivePositionUpdate = false;
internal NetworkTransformState AuthorityLastSentState;
public bool StatePushed { get; internal set; }
public delegate void AuthorityPushedTransformStateDelegateHandler(ref NetworkTransformState networkTransformState);
public event AuthorityPushedTransformStateDelegateHandler AuthorityPushedTransformState;
protected override void OnAuthorityPushTransformState(ref NetworkTransformState networkTransformState)
{
StatePushed = true;
AuthorityLastSentState = networkTransformState;
AuthorityPushedTransformState?.Invoke(ref networkTransformState);
base.OnAuthorityPushTransformState(ref networkTransformState);
}
public bool StateUpdated { get; internal set; }
protected override void OnNetworkTransformStateUpdated(ref NetworkTransformState oldState, ref NetworkTransformState newState)
{
StateUpdated = true;
base.OnNetworkTransformStateUpdated(ref oldState, ref newState);
}
protected string GetVector3Values(ref Vector3 vector3)
{
return $"({vector3.x:F6},{vector3.y:F6},{vector3.z:F6})";
}
protected string GetVector3Values(Vector3 vector3)
{
return GetVector3Values(ref vector3);
}
protected override bool OnIsServerAuthoritative()
{
return ServerAuthority;
}
public static NetworkTransformTestComponent AuthorityInstance;
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
if (CanCommitToTransform)
{
AuthorityInstance = this;
}
ReadyToReceivePositionUpdate = true;
}
public (bool isDirty, bool isPositionDirty, bool isRotationDirty, bool isScaleDirty) ApplyState()
{
var transformState = ApplyLocalNetworkState();
return (transformState.FlagStates.IsDirty, transformState.FlagStates.HasPositionChange, transformState.FlagStates.HasRotAngleChange, transformState.FlagStates.HasScaleChange);
}
}
/// <summary>
/// Helper component for NetworkTransform parenting tests when
/// a child is a parent of another child (i.e. "sub child")
/// </summary>
internal class SubChildObjectComponent : ChildObjectComponent
{
protected override bool IsSubChild()
{
return true;
}
}
/// <summary>
/// Helper component for NetworkTransform parenting tests
/// </summary>
internal class ChildObjectComponent : NetworkTransform
{
public static int TestCount;
public static bool EnableChildLog;
public static readonly List<ChildObjectComponent> Instances = new List<ChildObjectComponent>();
public static readonly List<ChildObjectComponent> SubInstances = new List<ChildObjectComponent>();
public static ChildObjectComponent AuthorityInstance { get; internal set; }
public static ChildObjectComponent AuthoritySubInstance { get; internal set; }
public static readonly Dictionary<ulong, NetworkObject> ClientInstances = new Dictionary<ulong, NetworkObject>();
public static readonly Dictionary<ulong, NetworkObject> ClientSubChildInstances = new Dictionary<ulong, NetworkObject>();
public static readonly List<ChildObjectComponent> InstancesWithLogging = new List<ChildObjectComponent>();
public static bool HasSubChild;
private StringBuilder m_ChildTransformLog = new StringBuilder();
private StringBuilder m_ChildStateLog = new StringBuilder();
public static void Reset()
{
AuthorityInstance = null;
AuthoritySubInstance = null;
HasSubChild = false;
ClientInstances.Clear();
ClientSubChildInstances.Clear();
Instances.Clear();
SubInstances.Clear();
}
public bool ServerAuthority;
protected virtual bool IsSubChild()
{
return false;
}
protected override bool OnIsServerAuthoritative()
{
return ServerAuthority;
}
public override void OnNetworkSpawn()
{
LogTransform();
base.OnNetworkSpawn();
LogTransform();
if (CanCommitToTransform)
{
if (!IsSubChild())
{
AuthorityInstance = this;
}
else
{
AuthoritySubInstance = this;
}
}
else
{
if (!IsSubChild())
{
Instances.Add(this);
}
else
{
SubInstances.Add(this);
}
}
if (HasSubChild && IsSubChild())
{
ClientSubChildInstances.Add(NetworkManager.LocalClientId, NetworkObject);
}
else
{
ClientInstances.Add(NetworkManager.LocalClientId, NetworkObject);
}
}
public override void OnNetworkDespawn()
{
LogToConsole();
base.OnNetworkDespawn();
}
public override void OnNetworkObjectParentChanged(NetworkObject parentNetworkObject)
{
base.OnNetworkObjectParentChanged(parentNetworkObject);
LogTransform();
}
protected override void OnAuthorityPushTransformState(ref NetworkTransformState networkTransformState)
{
base.OnAuthorityPushTransformState(ref networkTransformState);
LogState(ref networkTransformState);
}
protected override void OnNetworkTransformStateUpdated(ref NetworkTransformState oldState, ref NetworkTransformState newState)
{
base.OnNetworkTransformStateUpdated(ref oldState, ref newState);
LogState(ref newState);
}
protected override void OnSynchronize<T>(ref BufferSerializer<T> serializer)
{
base.OnSynchronize(ref serializer);
var localState = SynchronizeState;
LogState(ref localState);
}
private void LogTransform()
{
if (!EnableChildLog)
{
return;
}
if (m_ChildTransformLog.Length == 0)
{
m_ChildTransformLog.AppendLine($"[{TestCount}][{name}] Begin Child Transform Log (Authority: {CanCommitToTransform})-------------->");
}
m_ChildTransformLog.AppendLine($"POS-SR:{GetSpaceRelativePosition()} POS-W: {transform.position} POS-L: {transform.position}");
m_ChildTransformLog.AppendLine($"SCA-SR:{GetScale()} SCA-LS: {transform.lossyScale} SCA-L: {transform.localScale}");
}
private void LogState(ref NetworkTransformState state)
{
if (!EnableChildLog)
{
return;
}
if (m_ChildStateLog.Length == 0)
{
m_ChildStateLog.AppendLine($"[{TestCount}][{name}] Begin Child State Log (Authority: {CanCommitToTransform})-------------->");
}
var tick = 0;
if (NetworkManager != null && !NetworkManager.ShutdownInProgress)
{
tick = NetworkManager.ServerTime.Tick;
}
m_ChildStateLog.AppendLine($"[{state.NetworkTick}][{tick}] Tele:{state.FlagStates.IsTeleportingNextFrame} Sync: {state.FlagStates.IsSynchronizing} Reliable: {state.IsReliableStateUpdate()} IsParented: {state.FlagStates.IsParented} HasPos: {state.FlagStates.HasPositionChange} Pos: {state.GetPosition()}");
m_ChildStateLog.AppendLine($"Lossy:{state.LossyScale} Scale: {state.GetScale()} Rotation: {state.GetRotation()}");
}
private void LogToConsole()
{
if (!EnableChildLog)
{
return;
}
LogBuilder(m_ChildTransformLog);
LogBuilder(m_ChildStateLog);
}
private void LogBuilder(StringBuilder builder)
{
if (builder.Length == 0)
{
return;
}
var contents = builder.ToString();
var lines = contents.Split('\n');
if (lines.Length > 45)
{
var count = 0;
var tempBuilder = new StringBuilder();