11using System . Runtime . CompilerServices ;
22using UnityEngine ;
3+ using Unity . Mathematics ;
34
45namespace 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 }
0 commit comments