Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions com.unity.netcode.gameobjects/Components/HalfVector3.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using UnityEngine;
using Unity.Mathematics;

namespace Unity.Netcode.Components
{
Expand All @@ -13,34 +14,39 @@ namespace Unity.Netcode.Components
/// </remarks>
public struct HalfVector3 : INetworkSerializable
{
public const int Size = 3;

/// <summary>
/// The half float precision value of the x-axis as a <see cref="ushort"/>.
/// The half float precision value of the x-axis as a <see cref="half"/>.
/// </summary>
public ushort X => Axis.X;
public half X => Axis.x;
/// <summary>
/// The half float precision value of the y-axis as a <see cref="ushort"/>.
/// The half float precision value of the y-axis as a <see cref="half"/>.
/// </summary>
public ushort Y => Axis.Y;
public half Y => Axis.y;
/// <summary>
/// The half float precision value of the z-axis as a <see cref="ushort"/>.
/// The half float precision value of the z-axis as a <see cref="half"/>.
/// </summary>
public ushort Z => Axis.Z;
public half Z => Axis.x;

/// <summary>
/// Used to store the half float precision value as a <see cref="ushort"/>.
/// Used to store the half float precision values as a <see cref="half3"/>
/// </summary>
public Vector3T<ushort> Axis;
public half3 Axis;

internal Vector3AxisToSynchronize AxisToSynchronize;
/// <summary>
/// Determine which axis will be synchronized during serialization
/// </summary>
public bool3 AxisToSynchronize;

/// <summary>
/// The serialization implementation of <see cref="INetworkSerializable"/>.
/// </summary>
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
for (int i = 0; i < Axis.Length; i++)
for (int i = 0; i < Size; i++)
{
if (AxisToSynchronize.SyncAxis[i])
if (AxisToSynchronize[i])
{
var axisValue = Axis[i];
serializer.SerializeValue(ref axisValue);
Expand All @@ -60,11 +66,12 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
public Vector3 ToVector3()
{
Vector3 fullPrecision = Vector3.zero;
for (int i = 0; i < Axis.Length; i++)
Vector3 fullConversion = math.float3(Axis);
for (int i = 0; i < Size; i++)
{
if (AxisToSynchronize.SyncAxis[i])
if (AxisToSynchronize[i])
{
fullPrecision[i] = Mathf.HalfToFloat(Axis[i]);
fullPrecision[i] = fullConversion[i];
}
}
return fullPrecision;
Expand All @@ -77,11 +84,12 @@ public Vector3 ToVector3()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UpdateFrom(ref Vector3 vector3)
{
for (int i = 0; i < Axis.Length; i++)
var half3Full = math.half3(vector3);
for (int i = 0; i < Size; i++)
{
if (AxisToSynchronize.SyncAxis[i])
if (AxisToSynchronize[i])
{
Axis[i] = Mathf.FloatToHalf(vector3[i]);
Axis[i] = half3Full[i];
}
}
}
Expand All @@ -91,18 +99,18 @@ public void UpdateFrom(ref Vector3 vector3)
/// </summary>
/// <param name="vector3">The initial axial values (converted to half floats) when instantiated.</param>
/// <param name="vector3AxisToSynchronize">The axis to synchronize.</param>
public HalfVector3(Vector3 vector3, Vector3AxisToSynchronize vector3AxisToSynchronize)
public HalfVector3(Vector3 vector3, bool3 axisToSynchronize)
{
Axis = default;
AxisToSynchronize = vector3AxisToSynchronize;
Axis = half3.zero;
AxisToSynchronize = axisToSynchronize;
UpdateFrom(ref vector3);
}

/// <summary>
/// Constructor that defaults to all axis being synchronized.
/// </summary>
/// <param name="vector3">The initial axial values (converted to half floats) when instantiated.</param>
public HalfVector3(Vector3 vector3) : this(vector3, Vector3AxisToSynchronize.AllAxis)
public HalfVector3(Vector3 vector3) : this(vector3, math.bool3(true))
{

}
Expand All @@ -113,8 +121,8 @@ public HalfVector3(Vector3 vector3) : this(vector3, Vector3AxisToSynchronize.All
/// <param name="x">The initial x axis (converted to half float) value when instantiated.</param>
/// <param name="y">The initial y axis (converted to half float) value when instantiated.</param>
/// <param name="z">The initial z axis (converted to half float) value when instantiated.</param>
/// <param name="vector3AxisToSynchronize">The axis to synchronize.</param>
public HalfVector3(float x, float y, float z, Vector3AxisToSynchronize vector3AxisToSynchronize) : this(new Vector3(x, y, z), vector3AxisToSynchronize)
/// <param name="axisToSynchronize">The axis to synchronize.</param>
public HalfVector3(float x, float y, float z, bool3 axisToSynchronize) : this(new Vector3(x, y, z), axisToSynchronize)
{
}

Expand All @@ -124,7 +132,7 @@ public HalfVector3(float x, float y, float z, Vector3AxisToSynchronize vector3Ax
/// <param name="x">The initial x axis (converted to half float) value when instantiated.</param>
/// <param name="y">The initial y axis (converted to half float) value when instantiated.</param>
/// <param name="z">The initial z axis (converted to half float) value when instantiated.</param>
public HalfVector3(float x, float y, float z) : this(new Vector3(x, y, z), Vector3AxisToSynchronize.AllAxis)
public HalfVector3(float x, float y, float z) : this(new Vector3(x, y, z), math.bool3(true))
{
}
}
Expand Down
48 changes: 17 additions & 31 deletions com.unity.netcode.gameobjects/Components/HalfVector4.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using Unity.Mathematics;
using UnityEngine;

namespace Unity.Netcode.Components
Expand All @@ -13,37 +14,38 @@ namespace Unity.Netcode.Components
/// </remarks>
public struct HalfVector4 : INetworkSerializable
{
private const int k_Size = 4;
/// <summary>
/// The half float precision value of the x-axis as a <see cref="ushort"/>.
/// The half float precision value of the x-axis as a <see cref="half"/>.
/// </summary>
public ushort X => Axis.X;
public half X => Axis.x;

/// <summary>
/// The half float precision value of the y-axis as a <see cref="ushort"/>.
/// The half float precision value of the y-axis as a <see cref="half"/>.
/// </summary>
public ushort Y => Axis.Y;
public half Y => Axis.y;

/// <summary>
/// The half float precision value of the z-axis as a <see cref="ushort"/>.
/// The half float precision value of the z-axis as a <see cref="half"/>.
/// </summary>
public ushort Z => Axis.Z;
public half Z => Axis.z;

/// <summary>
/// The half float precision value of the w-axis as a <see cref="ushort"/>.
/// The half float precision value of the w-axis as a <see cref="half"/>.
/// </summary>
public ushort W => Axis.W;
public half W => Axis.w;

/// <summary>
/// Used to store the half float precision value as a <see cref="ushort"/>
/// Used to store the half float precision values as a <see cref="half4"/>
/// </summary>
public Vector4T<ushort> Axis;
public half4 Axis;

/// <summary>
/// The serialization implementation of <see cref="INetworkSerializable"/>
/// </summary>
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
for (int i = 0; i < Axis.Length; i++)
for (int i = 0; i < k_Size; i++)
{
var axisValue = Axis[i];
serializer.SerializeValue(ref axisValue);
Expand All @@ -61,12 +63,7 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToVector4()
{
Vector4 fullPrecision = Vector4.zero;
for (int i = 0; i < Axis.Length; i++)
{
fullPrecision[i] = Mathf.HalfToFloat(Axis[i]);
}
return fullPrecision;
return math.float4(Axis);
}

/// <summary>
Expand All @@ -76,12 +73,7 @@ public Vector4 ToVector4()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Quaternion ToQuaternion()
{
var quaternion = Quaternion.identity;
for (int i = 0; i < Axis.Length; i++)
{
quaternion[i] = Mathf.HalfToFloat(Axis[i]);
}
return quaternion;
return math.quaternion(Axis);
}

/// <summary>
Expand All @@ -91,10 +83,7 @@ public Quaternion ToQuaternion()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UpdateFrom(ref Vector4 vector4)
{
for (int i = 0; i < Axis.Length; i++)
{
Axis[i] = Mathf.FloatToHalf(vector4[i]);
}
Axis = math.half4(vector4);
}

/// <summary>
Expand All @@ -104,10 +93,7 @@ public void UpdateFrom(ref Vector4 vector4)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UpdateFrom(ref Quaternion quaternion)
{
for (int i = 0; i < Axis.Length; i++)
{
Axis[i] = Mathf.FloatToHalf(quaternion[i]);
}
Axis = math.half4(math.half(quaternion.x), math.half(quaternion.y), math.half(quaternion.z), math.half(quaternion.w));
}

/// <summary>
Expand Down
Loading