Skip to content

Commit

Permalink
feat: Add built-in serialization for UnityEngine.Pose (Unity-Technolo…
Browse files Browse the repository at this point in the history
  • Loading branch information
PitouGames committed Aug 25, 2023
1 parent b5b8eda commit 6408c04
Show file tree
Hide file tree
Showing 21 changed files with 396 additions and 24 deletions.
2 changes: 2 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
## [Unreleased]
### Added

- Added serializer for `Pose` (#2675)

### Fixed

- Fixed issue where `NetworkAnimator` was not internally tracking changes to layer weights which prevented proper layer weight synchronization back to the original layer weight value. (#2674)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal static class CodeGenHelpers
public static readonly string UnityVector3_FullName = typeof(Vector3).FullName;
public static readonly string UnityVector4_FullName = typeof(Vector4).FullName;
public static readonly string UnityQuaternion_FullName = typeof(Quaternion).FullName;
public static readonly string UnityPose_FullName = typeof(Pose).FullName;
public static readonly string UnityRay_FullName = typeof(Ray).FullName;
public static readonly string UnityRay2D_FullName = typeof(Ray2D).FullName;

Expand Down Expand Up @@ -305,6 +306,11 @@ public static bool IsSerializable(this TypeReference typeReference)
return true;
}

if (typeReference.FullName == UnityPose_FullName)
{
return true;
}

if (typeReference.FullName == UnityRay_FullName)
{
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly)
typeof(Vector3Int),
typeof(Vector4),
typeof(Quaternion),
typeof(Pose),
typeof(Color),
typeof(Color32),
typeof(Ray),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ public FastBufferWriter GetFastBufferWriter()
/// <param name="value">The values to read/write</param>
public void SerializeValue(ref Quaternion[] value) => m_Implementation.SerializeValue(ref value);

/// <summary>
/// Read or write a Pose value
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValue(ref Pose value) => m_Implementation.SerializeValue(ref value);

/// <summary>
/// Read or write an array of Pose values
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValue(ref Pose[] value) => m_Implementation.SerializeValue(ref value);

/// <summary>
/// Read or write a Color value
/// </summary>
Expand Down Expand Up @@ -555,6 +567,24 @@ public bool PreCheck(int amount)
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Quaternion[] value) => m_Implementation.SerializeValuePreChecked(ref value);

/// <summary>
/// Serialize a Pose, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Pose value) => m_Implementation.SerializeValuePreChecked(ref value);

/// <summary>
/// Serialize a Pose array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Pose[] value) => m_Implementation.SerializeValuePreChecked(ref value);

/// <summary>
/// Serialize a Color, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public void SerializeValue<T>(ref T value, FastBufferWriter.ForFixedStrings unus
public void SerializeValue(ref Quaternion value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Quaternion[] value) => m_Reader.ReadValueSafe(out value);

public void SerializeValue(ref Pose value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Pose[] value) => m_Reader.ReadValueSafe(out value);

public void SerializeValue(ref Color value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Color[] value) => m_Reader.ReadValueSafe(out value);

Expand Down Expand Up @@ -127,6 +130,9 @@ public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedSt
public void SerializeValuePreChecked(ref Quaternion value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Quaternion[] value) => m_Reader.ReadValue(out value);

public void SerializeValuePreChecked(ref Pose value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Pose[] value) => m_Reader.ReadValue(out value);

public void SerializeValuePreChecked(ref Color value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Color[] value) => m_Reader.ReadValue(out value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public void SerializeValue<T>(ref T value, FastBufferWriter.ForFixedStrings unus
public void SerializeValue(ref Quaternion value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Quaternion[] value) => m_Writer.WriteValueSafe(value);

public void SerializeValue(ref Pose value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Pose[] value) => m_Writer.WriteValueSafe(value);

public void SerializeValue(ref Color value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Color[] value) => m_Writer.WriteValueSafe(value);

Expand Down Expand Up @@ -128,6 +131,9 @@ public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedSt
public void SerializeValuePreChecked(ref Quaternion value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Quaternion[] value) => m_Writer.WriteValue(value);

public void SerializeValuePreChecked(ref Pose value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Pose[] value) => m_Writer.WriteValue(value);

public void SerializeValuePreChecked(ref Color value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Color[] value) => m_Writer.WriteValue(value);

Expand Down
17 changes: 17 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Serialization/BytePacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,23 @@ public static void WriteValuePacked(FastBufferWriter writer, Quaternion rotation
WriteValuePacked(writer, rotation.w);
}

/// <summary>
/// Writes the pose to the buffer.
/// </summary>
/// <param name="writer">The writer to write to</param>
/// <param name="pose">Pose to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteValuePacked(FastBufferWriter writer, Pose pose)
{
WriteValuePacked(writer, pose.position.x);
WriteValuePacked(writer, pose.position.y);
WriteValuePacked(writer, pose.position.z);
WriteValuePacked(writer, pose.rotation.x);
WriteValuePacked(writer, pose.rotation.y);
WriteValuePacked(writer, pose.rotation.z);
WriteValuePacked(writer, pose.rotation.w);
}

/// <summary>
/// Writes a string in a packed format
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,24 @@ public static void ReadValuePacked(FastBufferReader reader, out Quaternion rotat
ReadValuePacked(reader, out rotation.w);
}

/// <summary>
/// Reads the pose from the stream.
/// </summary>
/// <param name="reader">The reader to read from</param>
/// <param name="pose">Pose to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ReadValuePacked(FastBufferReader reader, out Pose pose)
{
pose = new Pose();
ReadValuePacked(reader, out pose.position.x);
ReadValuePacked(reader, out pose.position.y);
ReadValuePacked(reader, out pose.position.z);
ReadValuePacked(reader, out pose.rotation.x);
ReadValuePacked(reader, out pose.rotation.y);
ReadValuePacked(reader, out pose.rotation.z);
ReadValuePacked(reader, out pose.rotation.w);
}

/// <summary>
/// Reads a string in a packed format
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,20 @@ public void ReadValueSafeInPlace<T>(ref NativeList<T> value, FastBufferWriter.Fo
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Quaternion[] value) => ReadUnmanaged(out value);

/// <summary>
/// Read a Pose
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Pose value) => ReadUnmanaged(out value);

/// <summary>
/// Read a Pose array
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Pose[] value) => ReadUnmanaged(out value);

/// <summary>
/// Read a Color
/// </summary>
Expand Down Expand Up @@ -1426,6 +1440,26 @@ public void ReadValueSafeInPlace<T>(ref NativeList<T> value, FastBufferWriter.Fo
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Quaternion[] value) => ReadUnmanagedSafe(out value);

/// <summary>
/// Read a Pose
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Pose value) => ReadUnmanagedSafe(out value);

/// <summary>
/// Read a Pose array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Pose[] value) => ReadUnmanagedSafe(out value);

/// <summary>
/// Read a Color
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,20 @@ public void WriteValueSafe<T>(NativeList<T> value, ForGeneric unused = default)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(Quaternion[] value) => WriteUnmanaged(value);

/// <summary>
/// Write a Pose
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(in Pose value) => WriteUnmanaged(value);

/// <summary>
/// Write a Pose array
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(Pose[] value) => WriteUnmanaged(value);

/// <summary>
/// Write a Color
/// </summary>
Expand Down Expand Up @@ -1617,6 +1631,26 @@ public void WriteValueSafe<T>(NativeList<T> value, ForGeneric unused = default)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(Quaternion[] value) => WriteUnmanagedSafe(value);

/// <summary>
/// Write a Pose
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(in Pose value) => WriteUnmanagedSafe(value);

/// <summary>
/// Write a Pose array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(Pose[] value) => WriteUnmanagedSafe(value);

/// <summary>
/// Write a Color
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ void SerializeValue<T>(ref NativeList<T> value)
/// <param name="value">The values to read/write</param>
void SerializeValue(ref Quaternion[] value);

/// <summary>
/// Read or write a Pose value
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValue(ref Pose value);

/// <summary>
/// Read or write an array of Pose values
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValue(ref Pose[] value);

/// <summary>
/// Read or write a Color value
/// </summary>
Expand Down Expand Up @@ -529,6 +541,24 @@ void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedStrings u
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Quaternion[] value);

/// <summary>
/// Serialize a Pose, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Pose value);

/// <summary>
/// Serialize a Pose array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Pose[] value);

/// <summary>
/// Serialize a Color, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
Expand Down
Loading

0 comments on commit 6408c04

Please sign in to comment.