using System.Runtime.CompilerServices;
using Unity.Mathematics;
using UnityEngine;
namespace Unity.Netcode.Components
{
///
/// Half Precision that can also be used to convert a to half precision.
///
///
/// The Vector4T<ushort> values are half float values returned by for each
/// individual axis and the 16 bits of the half float are stored as values since C# does not have
/// a half float type.
///
public struct HalfVector4 : INetworkSerializable
{
internal const int Length = 4;
///
/// The half float precision value of the x-axis as a .
///
public half X => Axis.x;
///
/// The half float precision value of the y-axis as a .
///
public half Y => Axis.y;
///
/// The half float precision value of the z-axis as a .
///
public half Z => Axis.z;
///
/// The half float precision value of the w-axis as a .
///
public half W => Axis.w;
///
/// Used to store the half float precision values as a
///
public half4 Axis;
private void SerializeWrite(FastBufferWriter writer)
{
for (int i = 0; i < Length; i++)
{
writer.WriteUnmanagedSafe(Axis[i]);
}
}
private void SerializeRead(FastBufferReader reader)
{
for (int i = 0; i < Length; i++)
{
var axisValue = Axis[i];
reader.ReadUnmanagedSafe(out axisValue);
Axis[i] = axisValue;
}
}
///
/// The serialization implementation of .
///
public void NetworkSerialize(BufferSerializer serializer) where T : IReaderWriter
{
if (serializer.IsReader)
{
SerializeRead(serializer.GetFastBufferReader());
}
else
{
SerializeWrite(serializer.GetFastBufferWriter());
}
}
///
/// Converts this instance to a full precision .
///
/// A as the full precision value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToVector4()
{
return math.float4(Axis);
}
///
/// Converts this instance to a full precision .
///
/// A as the full precision value.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Quaternion ToQuaternion()
{
return math.quaternion(Axis);
}
///
/// Converts a full precision to half precision and updates the current instance.
///
/// The to convert and update this instance with.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UpdateFrom(ref Vector4 vector4)
{
Axis = math.half4(vector4);
}
///
/// Converts a full precision to half precision and updates the current instance.
///
/// The to convert and update this instance with.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UpdateFrom(ref Quaternion quaternion)
{
Axis = math.half4(math.half(quaternion.x), math.half(quaternion.y), math.half(quaternion.z), math.half(quaternion.w));
}
///
/// Constructor
///
/// The initial axial values (converted to half floats) when instantiated.
public HalfVector4(Vector4 vector4)
{
Axis = default;
UpdateFrom(ref vector4);
}
///
/// Constructor
///
/// The initial x axis (converted to half float) value when instantiated.
/// The initial y axis (converted to half float) value when instantiated.
/// The initial z axis (converted to half float) value when instantiated.
/// The initial w axis (converted to half float) value when instantiated.
public HalfVector4(float x, float y, float z, float w) : this(new Vector4(x, y, z, w))
{
}
}
}