|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using UnityEngine; |
| 3 | + |
| 4 | +namespace Unity.Netcode.Components |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Half float precision <see cref="Vector3"/>. |
| 8 | + /// </summary> |
| 9 | + /// <remarks> |
| 10 | + /// The Vector3T<ushort> values are half float values returned by <see cref="Mathf.FloatToHalf(float)"/> for each |
| 11 | + /// individual axis and the 16 bits of the half float are stored as <see cref="ushort"/> values since C# does not have |
| 12 | + /// a half float type. |
| 13 | + /// </remarks> |
| 14 | + public struct HalfVector3 : INetworkSerializable |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// The half float precision value of the x-axis as a <see cref="ushort"/>. |
| 18 | + /// </summary> |
| 19 | + public ushort X => Axis.X; |
| 20 | + /// <summary> |
| 21 | + /// The half float precision value of the y-axis as a <see cref="ushort"/>. |
| 22 | + /// </summary> |
| 23 | + public ushort Y => Axis.Y; |
| 24 | + /// <summary> |
| 25 | + /// The half float precision value of the z-axis as a <see cref="ushort"/>. |
| 26 | + /// </summary> |
| 27 | + public ushort Z => Axis.Z; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Used to store the half float precision value as a <see cref="ushort"/>. |
| 31 | + /// </summary> |
| 32 | + public Vector3T<ushort> Axis; |
| 33 | + |
| 34 | + internal Vector3AxisToSynchronize AxisToSynchronize; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// The serialization implementation of <see cref="INetworkSerializable"/>. |
| 38 | + /// </summary> |
| 39 | + public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter |
| 40 | + { |
| 41 | + for (int i = 0; i < Axis.Length; i++) |
| 42 | + { |
| 43 | + if (AxisToSynchronize.SyncAxis[i]) |
| 44 | + { |
| 45 | + var axisValue = Axis[i]; |
| 46 | + serializer.SerializeValue(ref axisValue); |
| 47 | + if (serializer.IsReader) |
| 48 | + { |
| 49 | + Axis[i] = axisValue; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Gets the full precision value as a <see cref="Vector3"/>. |
| 57 | + /// </summary> |
| 58 | + /// <returns>a <see cref="Vector3"/> as the full precision value.</returns> |
| 59 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 60 | + public Vector3 ToVector3() |
| 61 | + { |
| 62 | + Vector3 fullPrecision = Vector3.zero; |
| 63 | + for (int i = 0; i < Axis.Length; i++) |
| 64 | + { |
| 65 | + if (AxisToSynchronize.SyncAxis[i]) |
| 66 | + { |
| 67 | + fullPrecision[i] = Mathf.HalfToFloat(Axis[i]); |
| 68 | + } |
| 69 | + } |
| 70 | + return fullPrecision; |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Converts a full precision <see cref="Vector3"/> to half precision and updates the current instance. |
| 75 | + /// </summary> |
| 76 | + /// <param name="vector3">The <see cref="Vector3"/> to convert.</param> |
| 77 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 78 | + public void UpdateFrom(ref Vector3 vector3) |
| 79 | + { |
| 80 | + for (int i = 0; i < Axis.Length; i++) |
| 81 | + { |
| 82 | + if (AxisToSynchronize.SyncAxis[i]) |
| 83 | + { |
| 84 | + Axis[i] = Mathf.FloatToHalf(vector3[i]); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Constructor |
| 91 | + /// </summary> |
| 92 | + /// <param name="vector3">The initial axial values (converted to half floats) when instantiated.</param> |
| 93 | + /// <param name="vector3AxisToSynchronize">The axis to synchronize.</param> |
| 94 | + public HalfVector3(Vector3 vector3, Vector3AxisToSynchronize vector3AxisToSynchronize) |
| 95 | + { |
| 96 | + Axis = default; |
| 97 | + AxisToSynchronize = vector3AxisToSynchronize; |
| 98 | + UpdateFrom(ref vector3); |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Constructor that defaults to all axis being synchronized. |
| 103 | + /// </summary> |
| 104 | + /// <param name="vector3">The initial axial values (converted to half floats) when instantiated.</param> |
| 105 | + public HalfVector3(Vector3 vector3) : this(vector3, Vector3AxisToSynchronize.AllAxis) |
| 106 | + { |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Constructor |
| 112 | + /// </summary> |
| 113 | + /// <param name="x">The initial x axis (converted to half float) value when instantiated.</param> |
| 114 | + /// <param name="y">The initial y axis (converted to half float) value when instantiated.</param> |
| 115 | + /// <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) |
| 118 | + { |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Constructor that defaults to all axis being synchronized. |
| 123 | + /// </summary> |
| 124 | + /// <param name="x">The initial x axis (converted to half float) value when instantiated.</param> |
| 125 | + /// <param name="y">The initial y axis (converted to half float) value when instantiated.</param> |
| 126 | + /// <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) |
| 128 | + { |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments