Skip to content

Commit 39f9a1c

Browse files
refactor: use unity.mathematics.half in place of added types (#2473)
1 parent f3a4021 commit 39f9a1c

20 files changed

+191
-342
lines changed

com.unity.netcode.gameobjects/Components/HalfVector3.cs

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Runtime.CompilerServices;
22
using UnityEngine;
3+
using Unity.Mathematics;
34

45
namespace Unity.Netcode.Components
56
{
@@ -13,45 +14,70 @@ namespace Unity.Netcode.Components
1314
/// </remarks>
1415
public struct HalfVector3 : INetworkSerializable
1516
{
17+
internal const int Length = 3;
18+
1619
/// <summary>
17-
/// The half float precision value of the x-axis as a <see cref="ushort"/>.
20+
/// The half float precision value of the x-axis as a <see cref="half"/>.
1821
/// </summary>
19-
public ushort X => Axis.X;
22+
public half X => Axis.x;
2023
/// <summary>
21-
/// The half float precision value of the y-axis as a <see cref="ushort"/>.
24+
/// The half float precision value of the y-axis as a <see cref="half"/>.
2225
/// </summary>
23-
public ushort Y => Axis.Y;
26+
public half Y => Axis.y;
2427
/// <summary>
25-
/// The half float precision value of the z-axis as a <see cref="ushort"/>.
28+
/// The half float precision value of the z-axis as a <see cref="half"/>.
2629
/// </summary>
27-
public ushort Z => Axis.Z;
30+
public half Z => Axis.x;
2831

2932
/// <summary>
30-
/// Used to store the half float precision value as a <see cref="ushort"/>.
33+
/// Used to store the half float precision values as a <see cref="half3"/>
3134
/// </summary>
32-
public Vector3T<ushort> Axis;
33-
34-
internal Vector3AxisToSynchronize AxisToSynchronize;
35+
public half3 Axis;
3536

3637
/// <summary>
37-
/// The serialization implementation of <see cref="INetworkSerializable"/>.
38+
/// Determine which axis will be synchronized during serialization
3839
/// </summary>
39-
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
40+
public bool3 AxisToSynchronize;
41+
42+
private void SerializeWrite(FastBufferWriter writer)
43+
{
44+
for (int i = 0; i < Length; i++)
45+
{
46+
if (AxisToSynchronize[i])
47+
{
48+
writer.WriteUnmanagedSafe(Axis[i]);
49+
}
50+
}
51+
}
52+
53+
private void SerializeRead(FastBufferReader reader)
4054
{
41-
for (int i = 0; i < Axis.Length; i++)
55+
for (int i = 0; i < Length; i++)
4256
{
43-
if (AxisToSynchronize.SyncAxis[i])
57+
if (AxisToSynchronize[i])
4458
{
4559
var axisValue = Axis[i];
46-
serializer.SerializeValue(ref axisValue);
47-
if (serializer.IsReader)
48-
{
49-
Axis[i] = axisValue;
50-
}
60+
reader.ReadUnmanagedSafe(out axisValue);
61+
Axis[i] = axisValue;
5162
}
5263
}
5364
}
5465

66+
/// <summary>
67+
/// The serialization implementation of <see cref="INetworkSerializable"/>.
68+
/// </summary>
69+
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
70+
{
71+
if (serializer.IsReader)
72+
{
73+
SerializeRead(serializer.GetFastBufferReader());
74+
}
75+
else
76+
{
77+
SerializeWrite(serializer.GetFastBufferWriter());
78+
}
79+
}
80+
5581
/// <summary>
5682
/// Gets the full precision value as a <see cref="Vector3"/>.
5783
/// </summary>
@@ -60,11 +86,12 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
6086
public Vector3 ToVector3()
6187
{
6288
Vector3 fullPrecision = Vector3.zero;
63-
for (int i = 0; i < Axis.Length; i++)
89+
Vector3 fullConversion = math.float3(Axis);
90+
for (int i = 0; i < Length; i++)
6491
{
65-
if (AxisToSynchronize.SyncAxis[i])
92+
if (AxisToSynchronize[i])
6693
{
67-
fullPrecision[i] = Mathf.HalfToFloat(Axis[i]);
94+
fullPrecision[i] = fullConversion[i];
6895
}
6996
}
7097
return fullPrecision;
@@ -77,11 +104,12 @@ public Vector3 ToVector3()
77104
[MethodImpl(MethodImplOptions.AggressiveInlining)]
78105
public void UpdateFrom(ref Vector3 vector3)
79106
{
80-
for (int i = 0; i < Axis.Length; i++)
107+
var half3Full = math.half3(vector3);
108+
for (int i = 0; i < Length; i++)
81109
{
82-
if (AxisToSynchronize.SyncAxis[i])
110+
if (AxisToSynchronize[i])
83111
{
84-
Axis[i] = Mathf.FloatToHalf(vector3[i]);
112+
Axis[i] = half3Full[i];
85113
}
86114
}
87115
}
@@ -91,18 +119,18 @@ public void UpdateFrom(ref Vector3 vector3)
91119
/// </summary>
92120
/// <param name="vector3">The initial axial values (converted to half floats) when instantiated.</param>
93121
/// <param name="vector3AxisToSynchronize">The axis to synchronize.</param>
94-
public HalfVector3(Vector3 vector3, Vector3AxisToSynchronize vector3AxisToSynchronize)
122+
public HalfVector3(Vector3 vector3, bool3 axisToSynchronize)
95123
{
96-
Axis = default;
97-
AxisToSynchronize = vector3AxisToSynchronize;
124+
Axis = half3.zero;
125+
AxisToSynchronize = axisToSynchronize;
98126
UpdateFrom(ref vector3);
99127
}
100128

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

108136
}
@@ -113,8 +141,8 @@ public HalfVector3(Vector3 vector3) : this(vector3, Vector3AxisToSynchronize.All
113141
/// <param name="x">The initial x axis (converted to half float) value when instantiated.</param>
114142
/// <param name="y">The initial y axis (converted to half float) value when instantiated.</param>
115143
/// <param name="z">The initial z axis (converted to half float) value when instantiated.</param>
116-
/// <param name="vector3AxisToSynchronize">The axis to synchronize.</param>
117-
public HalfVector3(float x, float y, float z, Vector3AxisToSynchronize vector3AxisToSynchronize) : this(new Vector3(x, y, z), vector3AxisToSynchronize)
144+
/// <param name="axisToSynchronize">The axis to synchronize.</param>
145+
public HalfVector3(float x, float y, float z, bool3 axisToSynchronize) : this(new Vector3(x, y, z), axisToSynchronize)
118146
{
119147
}
120148

@@ -124,7 +152,7 @@ public HalfVector3(float x, float y, float z, Vector3AxisToSynchronize vector3Ax
124152
/// <param name="x">The initial x axis (converted to half float) value when instantiated.</param>
125153
/// <param name="y">The initial y axis (converted to half float) value when instantiated.</param>
126154
/// <param name="z">The initial z axis (converted to half float) value when instantiated.</param>
127-
public HalfVector3(float x, float y, float z) : this(new Vector3(x, y, z), Vector3AxisToSynchronize.AllAxis)
155+
public HalfVector3(float x, float y, float z) : this(new Vector3(x, y, z), math.bool3(true))
128156
{
129157
}
130158
}

com.unity.netcode.gameobjects/Components/HalfVector4.cs

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Runtime.CompilerServices;
2+
using Unity.Mathematics;
23
using UnityEngine;
34

45
namespace Unity.Netcode.Components
@@ -13,44 +14,62 @@ namespace Unity.Netcode.Components
1314
/// </remarks>
1415
public struct HalfVector4 : INetworkSerializable
1516
{
17+
internal const int Length = 4;
1618
/// <summary>
17-
/// The half float precision value of the x-axis as a <see cref="ushort"/>.
19+
/// The half float precision value of the x-axis as a <see cref="half"/>.
1820
/// </summary>
19-
public ushort X => Axis.X;
21+
public half X => Axis.x;
2022

2123
/// <summary>
22-
/// The half float precision value of the y-axis as a <see cref="ushort"/>.
24+
/// The half float precision value of the y-axis as a <see cref="half"/>.
2325
/// </summary>
24-
public ushort Y => Axis.Y;
26+
public half Y => Axis.y;
2527

2628
/// <summary>
27-
/// The half float precision value of the z-axis as a <see cref="ushort"/>.
29+
/// The half float precision value of the z-axis as a <see cref="half"/>.
2830
/// </summary>
29-
public ushort Z => Axis.Z;
31+
public half Z => Axis.z;
3032

3133
/// <summary>
32-
/// The half float precision value of the w-axis as a <see cref="ushort"/>.
34+
/// The half float precision value of the w-axis as a <see cref="half"/>.
3335
/// </summary>
34-
public ushort W => Axis.W;
36+
public half W => Axis.w;
3537

3638
/// <summary>
37-
/// Used to store the half float precision value as a <see cref="ushort"/>
39+
/// Used to store the half float precision values as a <see cref="half4"/>
3840
/// </summary>
39-
public Vector4T<ushort> Axis;
41+
public half4 Axis;
42+
43+
private void SerializeWrite(FastBufferWriter writer)
44+
{
45+
for (int i = 0; i < Length; i++)
46+
{
47+
writer.WriteUnmanagedSafe(Axis[i]);
48+
}
49+
}
50+
51+
private void SerializeRead(FastBufferReader reader)
52+
{
53+
for (int i = 0; i < Length; i++)
54+
{
55+
var axisValue = Axis[i];
56+
reader.ReadUnmanagedSafe(out axisValue);
57+
Axis[i] = axisValue;
58+
}
59+
}
4060

4161
/// <summary>
42-
/// The serialization implementation of <see cref="INetworkSerializable"/>
62+
/// The serialization implementation of <see cref="INetworkSerializable"/>.
4363
/// </summary>
4464
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
4565
{
46-
for (int i = 0; i < Axis.Length; i++)
66+
if (serializer.IsReader)
4767
{
48-
var axisValue = Axis[i];
49-
serializer.SerializeValue(ref axisValue);
50-
if (serializer.IsReader)
51-
{
52-
Axis[i] = axisValue;
53-
}
68+
SerializeRead(serializer.GetFastBufferReader());
69+
}
70+
else
71+
{
72+
SerializeWrite(serializer.GetFastBufferWriter());
5473
}
5574
}
5675

@@ -61,12 +80,7 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
6180
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6281
public Vector4 ToVector4()
6382
{
64-
Vector4 fullPrecision = Vector4.zero;
65-
for (int i = 0; i < Axis.Length; i++)
66-
{
67-
fullPrecision[i] = Mathf.HalfToFloat(Axis[i]);
68-
}
69-
return fullPrecision;
83+
return math.float4(Axis);
7084
}
7185

7286
/// <summary>
@@ -76,12 +90,7 @@ public Vector4 ToVector4()
7690
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7791
public Quaternion ToQuaternion()
7892
{
79-
var quaternion = Quaternion.identity;
80-
for (int i = 0; i < Axis.Length; i++)
81-
{
82-
quaternion[i] = Mathf.HalfToFloat(Axis[i]);
83-
}
84-
return quaternion;
93+
return math.quaternion(Axis);
8594
}
8695

8796
/// <summary>
@@ -91,10 +100,7 @@ public Quaternion ToQuaternion()
91100
[MethodImpl(MethodImplOptions.AggressiveInlining)]
92101
public void UpdateFrom(ref Vector4 vector4)
93102
{
94-
for (int i = 0; i < Axis.Length; i++)
95-
{
96-
Axis[i] = Mathf.FloatToHalf(vector4[i]);
97-
}
103+
Axis = math.half4(vector4);
98104
}
99105

100106
/// <summary>
@@ -104,10 +110,7 @@ public void UpdateFrom(ref Vector4 vector4)
104110
[MethodImpl(MethodImplOptions.AggressiveInlining)]
105111
public void UpdateFrom(ref Quaternion quaternion)
106112
{
107-
for (int i = 0; i < Axis.Length; i++)
108-
{
109-
Axis[i] = Mathf.FloatToHalf(quaternion[i]);
110-
}
113+
Axis = math.half4(math.half(quaternion.x), math.half(quaternion.y), math.half(quaternion.z), math.half(quaternion.w));
111114
}
112115

113116
/// <summary>

0 commit comments

Comments
 (0)