Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit b4dda7f

Browse files
author
Paul Balaji
authored
Transform sync bandwidth optimisations (#990)
1 parent 72df3d6 commit b4dda7f

25 files changed

+431
-144
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
### Breaking Changes
66

77
- The constructor for the `ForwardingDispatcher` now accepts a `UnityEngine.LogType` instead of `Improbable.Worker.CInterop.LogLevel` as its parameter. [#987](https://github.com/spatialos/gdk-for-unity/pull/987)
8+
- The schema for the Transform Synchronization feature module has been optimised to reduce bandwidth. If you use this module, please make use of the updated helper methods and regenerate your snapshot. [#990](https://github.com/spatialos/gdk-for-unity/pull/990)
9+
- The `Location` and `Velocity` schema types have been replaced by the `FixedPointVector3` type.
10+
- The location and velocity fields are now [`Q21.10`](https://en.wikipedia.org/wiki/Q_(number_format)) fixed point values.
11+
- The `Quaternion` schema type has been replaced by the `CompressedQuaternion` type.
12+
- Rotation is now compressed from 4 floats to a single uint32.
813

914
### Added
1015

UPGRADE_GUIDE.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Upgrade Guide
2+
3+
## From `0.2.3` to `Unreleased`
4+
5+
### General changes
6+
7+
When constructing a `ForwardingDispatcher`, you must now provide a `UnityEngine.LogType` instead of a `Improbable.Worker.CInterop.LogLevel`.
8+
9+
### Transform Synchronization changes
10+
11+
Several utility methods in the `TransformUtils` class have been made `internal` to the Transform Synchronization package. We highly recommend that **if you were using the `TransformInternal` component in your logic, to use `UnityEngine.Transform` instead.**
12+
13+
You must no longer use the `TransformInternal.Snapshot` constructor.
14+
15+
```csharp
16+
// Sample of INVALID code - the `Location` and `Quaternion` schema types have been removed.
17+
var transform = new TransformInternal.Snapshot
18+
{
19+
Location = new Location((float) coords.X, (float) coords.Y, (float) coords.Z),
20+
Rotation = new Quaternion(1, 0, 0, 0),
21+
TicksPerSecond = 1f / Time.fixedDeltaTime
22+
};
23+
```
24+
25+
Instead, you must use the `TransformUtils.CreateTransformSnapshot` static method. This is to ensure that the contents of an entity's `TransformInternal` component are compressed using the Transform Synchronization module's new compression scheme.
26+
27+
```csharp
28+
// This is the correct way of initialising a `TransformInternal` snapshot manually.
29+
var transform = TransformUtils.CreateTransformSnapshot(Coordinates.Zero, Quaternion.identity, Vector3.forward)
30+
```
31+
32+
If you use the `AddTransformSynchronizationComponents` method in the `TransformSynchronizationHelper` class, you do not need to update code.
33+
34+
**You must regenerate any snapshots which contain entities that make use of the Transform Synchronization feature module.**

snapshots/default.snapshot

-17 Bytes
Binary file not shown.

workers/unity/Assets/Playground/Editor/Improbable.Playground.Editor.asmdef

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
"references": [
44
"Improbable.Playground",
55
"Improbable.Gdk.Core",
6-
"Improbable.Gdk.Generated"
6+
"Improbable.Gdk.Generated",
7+
"Improbable.Gdk.TransformSynchronization"
78
],
89
"optionalUnityReferences": [],
910
"includePlatforms": [
1011
"Editor"
1112
],
1213
"excludePlatforms": [],
13-
"allowUnsafeCode": false
14+
"allowUnsafeCode": false,
15+
"overrideReferences": false,
16+
"precompiledReferences": [],
17+
"autoReferenced": true,
18+
"defineConstraints": [],
19+
"versionDefines": []
1420
}

workers/unity/Assets/Playground/Editor/SnapshotGenerator/SnapshotGenerator.cs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Improbable.Gdk.PlayerLifecycle;
55
using Improbable.Gdk.TransformSynchronization;
66
using UnityEngine;
7-
using Quaternion = Improbable.Gdk.TransformSynchronization.Quaternion;
87

98
namespace Playground.Editor.SnapshotGenerator
109
{
@@ -84,16 +83,9 @@ private static void AddCubeGrid(Snapshot snapshot, int cubeCount)
8483
return;
8584
}
8685

87-
var positionSnapshot = new Position.Snapshot
88-
{
89-
Coords = new Coordinates(x, 1, z)
90-
};
91-
var transformSnapshot = new TransformInternal.Snapshot
92-
{
93-
Location = new Location(x, 1, z),
94-
Rotation = new Quaternion(1, 0, 0, 0),
95-
TicksPerSecond = 1f / Time.fixedDeltaTime
96-
};
86+
var location = new Vector3(x, 1, z);
87+
var positionSnapshot = new Position.Snapshot(location.ToCoordinates());
88+
var transformSnapshot = TransformUtils.CreateTransformSnapshot(location, Quaternion.identity);
9789

9890
cubeTemplate.SetComponent(positionSnapshot);
9991
cubeTemplate.SetComponent(transformSnapshot);
@@ -106,26 +98,20 @@ private static void CreateSpinner(Snapshot snapshot, Coordinates coords)
10698
{
10799
const string entityType = "Spinner";
108100

109-
var transform = new TransformInternal.Snapshot
110-
{
111-
Location = new Location((float) coords.X, (float) coords.Y, (float) coords.Z),
112-
Rotation = new Quaternion(1, 0, 0, 0),
113-
TicksPerSecond = 1f / Time.fixedDeltaTime
114-
};
101+
var transform = TransformUtils.CreateTransformSnapshot(coords, Quaternion.identity);
115102

116103
var template = new EntityTemplate();
117-
template.AddComponent(new Position.Snapshot { Coords = coords }, WorkerUtils.UnityGameLogic);
118-
template.AddComponent(new Metadata.Snapshot { EntityType = entityType }, WorkerUtils.UnityGameLogic);
104+
template.AddComponent(new Position.Snapshot(coords), WorkerUtils.UnityGameLogic);
105+
template.AddComponent(new Metadata.Snapshot(entityType), WorkerUtils.UnityGameLogic);
119106
template.AddComponent(transform, WorkerUtils.UnityGameLogic);
120107
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
121108
template.AddComponent(new Collisions.Snapshot(), WorkerUtils.UnityGameLogic);
122-
template.AddComponent(new SpinnerColor.Snapshot { Color = Color.BLUE }, WorkerUtils.UnityGameLogic);
109+
template.AddComponent(new SpinnerColor.Snapshot(Color.BLUE), WorkerUtils.UnityGameLogic);
123110
template.AddComponent(new SpinnerRotation.Snapshot(), WorkerUtils.UnityGameLogic);
124111

125112
template.SetReadAccess(WorkerUtils.UnityGameLogic, WorkerUtils.UnityClient, WorkerUtils.MobileClient);
126113
template.SetComponentWriteAccess(EntityAcl.ComponentId, WorkerUtils.UnityGameLogic);
127114

128-
129115
snapshot.AddEntity(template);
130116
}
131117
}

workers/unity/Assets/Playground/Scripts/DisconnectSystem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ protected override void OnUpdate()
2323
{
2424
Entities.With(group).ForEach((OnDisconnected data) =>
2525
{
26-
Debug.LogWarningFormat("Disconnected from SpatialOS with reason: \"{0}\"",
27-
data.ReasonForDisconnect);
26+
Debug.LogWarning($"Disconnected from SpatialOS with reason: {data.ReasonForDisconnect}");
2827
});
2928
}
3029
}

workers/unity/Assets/Playground/Scripts/MonoBehaviours/CheckForPlayerCollision.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class CheckForPlayerCollision : MonoBehaviour
1010
[Require] private CollisionsWriter collisionWriter;
1111
#pragma warning restore 649
1212

13-
void OnTriggerEnter(Collider other)
13+
private void OnTriggerEnter(Collider other)
1414
{
1515
collisionWriter?.SendPlayerCollidedEvent(new Empty());
1616
}

workers/unity/Assets/Playground/Scripts/MonoBehaviours/RotationBehaviour.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Improbable.Gdk.Subscriptions;
22
using Improbable.Gdk.TransformSynchronization;
33
using UnityEngine;
4-
using SpatialQuaternion = Improbable.Gdk.TransformSynchronization.Quaternion;
4+
using SpatialQuaternion = Improbable.Gdk.TransformSynchronization.CompressedQuaternion;
55
using Quaternion = UnityEngine.Quaternion;
66

77
public class RotationBehaviour : MonoBehaviour
@@ -14,6 +14,6 @@ public class RotationBehaviour : MonoBehaviour
1414
private void Update()
1515
{
1616
transform.rotation *=
17-
Quaternion.Euler((RotatingClockWise ? Vector3.up : Vector3.down) * Time.deltaTime * 20.0f);
17+
Quaternion.Euler(20.0f * Time.deltaTime * (RotatingClockWise ? Vector3.up : Vector3.down));
1818
}
1919
}

workers/unity/Assets/Playground/Scripts/MonoBehaviours/SpawnCubeCommandReceiver.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Improbable.Gdk.TransformSynchronization;
77
using Improbable.Worker.CInterop;
88
using UnityEngine;
9-
using Quaternion = Improbable.Gdk.TransformSynchronization.Quaternion;
109

1110
namespace Playground.MonoBehaviours
1211
{
@@ -49,17 +48,14 @@ private void OnEntityIdsReserved(WorldCommands.ReserveEntityIds.ReceivedResponse
4948
return;
5049
}
5150

52-
var location = transformReader.Data.Location;
51+
var location = gameObject.transform.position;
52+
location.y += 2;
53+
5354
var cubeEntityTemplate = CubeTemplate.CreateCubeEntityTemplate();
54-
cubeEntityTemplate.SetComponent(new Position.Snapshot
55-
{
56-
Coords = new Coordinates(location.X, location.Y + 2, location.Z)
57-
});
58-
cubeEntityTemplate.SetComponent(new TransformInternal.Snapshot
59-
{
60-
Location = new Location(location.X, location.Y + 2, location.Z),
61-
Rotation = new Quaternion(1, 0, 0, 0)
62-
});
55+
56+
cubeEntityTemplate.SetComponent(new Position.Snapshot(location.ToCoordinates()));
57+
cubeEntityTemplate.SetComponent(TransformUtils.CreateTransformSnapshot(location, Quaternion.identity));
58+
6359
var expectedEntityId = response.FirstEntityId.Value;
6460

6561
worldCommandRequestSender.SendCreateEntityCommand(

workers/unity/Assets/Playground/Scripts/Player/ProcessLaunchCommandSystem.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ private void HandleLaunchMeRequests()
9898
var info = request.Payload;
9999

100100
rigidbody.AddForceAtPosition(
101-
new Vector3(info.LaunchDirection.X, info.LaunchDirection.Y, info.LaunchDirection.Z) *
102-
info.LaunchEnergy * 100.0f,
101+
info.LaunchEnergy * 100.0f * new Vector3(info.LaunchDirection.X, info.LaunchDirection.Y, info.LaunchDirection.Z),
103102
new Vector3(info.ImpactPoint.X, info.ImpactPoint.Y, info.ImpactPoint.Z)
104103
);
105104

0 commit comments

Comments
 (0)