1
1
using System . Runtime . CompilerServices ;
2
2
using UnityEngine ;
3
+ using Unity . Mathematics ;
3
4
4
5
namespace Unity . Netcode . Components
5
6
{
@@ -13,45 +14,70 @@ namespace Unity.Netcode.Components
13
14
/// </remarks>
14
15
public struct HalfVector3 : INetworkSerializable
15
16
{
17
+ internal const int Length = 3 ;
18
+
16
19
/// <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 "/>.
18
21
/// </summary>
19
- public ushort X => Axis . X ;
22
+ public half X => Axis . x ;
20
23
/// <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 "/>.
22
25
/// </summary>
23
- public ushort Y => Axis . Y ;
26
+ public half Y => Axis . y ;
24
27
/// <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 "/>.
26
29
/// </summary>
27
- public ushort Z => Axis . Z ;
30
+ public half Z => Axis . x ;
28
31
29
32
/// <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 "/>
31
34
/// </summary>
32
- public Vector3T < ushort > Axis ;
33
-
34
- internal Vector3AxisToSynchronize AxisToSynchronize ;
35
+ public half3 Axis ;
35
36
36
37
/// <summary>
37
- /// The serialization implementation of <see cref="INetworkSerializable"/>.
38
+ /// Determine which axis will be synchronized during serialization
38
39
/// </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 )
40
54
{
41
- for ( int i = 0 ; i < Axis . Length ; i ++ )
55
+ for ( int i = 0 ; i < Length ; i ++ )
42
56
{
43
- if ( AxisToSynchronize . SyncAxis [ i ] )
57
+ if ( AxisToSynchronize [ i ] )
44
58
{
45
59
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 ;
51
62
}
52
63
}
53
64
}
54
65
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
+
55
81
/// <summary>
56
82
/// Gets the full precision value as a <see cref="Vector3"/>.
57
83
/// </summary>
@@ -60,11 +86,12 @@ public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReade
60
86
public Vector3 ToVector3 ( )
61
87
{
62
88
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 ++ )
64
91
{
65
- if ( AxisToSynchronize . SyncAxis [ i ] )
92
+ if ( AxisToSynchronize [ i ] )
66
93
{
67
- fullPrecision [ i ] = Mathf . HalfToFloat ( Axis [ i ] ) ;
94
+ fullPrecision [ i ] = fullConversion [ i ] ;
68
95
}
69
96
}
70
97
return fullPrecision ;
@@ -77,11 +104,12 @@ public Vector3 ToVector3()
77
104
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
78
105
public void UpdateFrom ( ref Vector3 vector3 )
79
106
{
80
- for ( int i = 0 ; i < Axis . Length ; i ++ )
107
+ var half3Full = math . half3 ( vector3 ) ;
108
+ for ( int i = 0 ; i < Length ; i ++ )
81
109
{
82
- if ( AxisToSynchronize . SyncAxis [ i ] )
110
+ if ( AxisToSynchronize [ i ] )
83
111
{
84
- Axis [ i ] = Mathf . FloatToHalf ( vector3 [ i ] ) ;
112
+ Axis [ i ] = half3Full [ i ] ;
85
113
}
86
114
}
87
115
}
@@ -91,18 +119,18 @@ public void UpdateFrom(ref Vector3 vector3)
91
119
/// </summary>
92
120
/// <param name="vector3">The initial axial values (converted to half floats) when instantiated.</param>
93
121
/// <param name="vector3AxisToSynchronize">The axis to synchronize.</param>
94
- public HalfVector3 ( Vector3 vector3 , Vector3AxisToSynchronize vector3AxisToSynchronize )
122
+ public HalfVector3 ( Vector3 vector3 , bool3 axisToSynchronize )
95
123
{
96
- Axis = default ;
97
- AxisToSynchronize = vector3AxisToSynchronize ;
124
+ Axis = half3 . zero ;
125
+ AxisToSynchronize = axisToSynchronize ;
98
126
UpdateFrom ( ref vector3 ) ;
99
127
}
100
128
101
129
/// <summary>
102
130
/// Constructor that defaults to all axis being synchronized.
103
131
/// </summary>
104
132
/// <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 ) )
106
134
{
107
135
108
136
}
@@ -113,8 +141,8 @@ public HalfVector3(Vector3 vector3) : this(vector3, Vector3AxisToSynchronize.All
113
141
/// <param name="x">The initial x axis (converted to half float) value when instantiated.</param>
114
142
/// <param name="y">The initial y axis (converted to half float) value when instantiated.</param>
115
143
/// <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 )
118
146
{
119
147
}
120
148
@@ -124,7 +152,7 @@ public HalfVector3(float x, float y, float z, Vector3AxisToSynchronize vector3Ax
124
152
/// <param name="x">The initial x axis (converted to half float) value when instantiated.</param>
125
153
/// <param name="y">The initial y axis (converted to half float) value when instantiated.</param>
126
154
/// <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 ) )
128
156
{
129
157
}
130
158
}
0 commit comments