#region License
/* MoonWorks - Game Development Framework
* Copyright 2021 Evan Hemsley
*/
/* Derived from code by Ethan Lee (Copyright 2009-2021).
* Released under the Microsoft Public License.
* See fna.LICENSE for details.
* Derived from code by the Mono.Xna Team (Copyright 2006).
* Released under the MIT License. See monoxna.LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using System.Numerics;
#endregion
namespace Nerfed.Runtime.Graphics.PackedVector;
///
/// Packed vector type containing unsigned normalized values ranging from 0 to 1.
/// The x and z components use 5 bits, and the y component uses 6 bits.
///
public struct Rgba1010102 : IPackedVector, IEquatable, IPackedVector
{
#region Public Properties
///
/// Gets and sets the packed value.
///
public uint PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private uint packedValue;
#endregion
#region Public Constructors
///
/// Creates a new instance of Rgba1010102.
///
/// The x component
/// The y component
/// The z component
/// The w component
public Rgba1010102(float x, float y, float z, float w)
{
packedValue = Pack(x, y, z, w);
}
///
/// Creates a new instance of Rgba1010102.
///
///
/// Vector containing the components for the packed vector.
///
public Rgba1010102(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Methods
///
/// Gets the packed vector in Vector4 format.
///
/// The packed vector in Vector4 format
public Vector4 ToVector4()
{
return new Vector4(
(packedValue & 0x03FF) / 1023.0f,
((packedValue >> 10) & 0x03FF) / 1023.0f,
((packedValue >> 20) & 0x03FF) / 1023.0f,
(packedValue >> 30) / 3.0f
);
}
#endregion
#region IPackedVector Methods
///
/// Sets the packed vector from a Vector4.
///
/// Vector containing the components.
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Public Static Operators and Override Methods
///
/// Compares an object with the packed vector.
///
/// The object to compare.
/// True if the object is equal to the packed vector.
public override bool Equals(object obj)
{
return (obj is Rgba1010102) && Equals((Rgba1010102) obj);
}
///
/// Compares another Rgba1010102 packed vector with the packed vector.
///
/// The Rgba1010102 packed vector to compare.
/// True if the packed vectors are equal.
public bool Equals(Rgba1010102 other)
{
return packedValue == other.packedValue;
}
///
/// Gets a string representation of the packed vector.
///
/// A string representation of the packed vector.
public override string ToString()
{
return packedValue.ToString("X");
}
///
/// Gets a hash code of the packed vector.
///
/// The hash code for the packed vector.
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public static bool operator ==(Rgba1010102 lhs, Rgba1010102 rhs)
{
return lhs.packedValue == rhs.packedValue;
}
public static bool operator !=(Rgba1010102 lhs, Rgba1010102 rhs)
{
return lhs.packedValue != rhs.packedValue;
}
#endregion
#region Private Static Pack Method
private static uint Pack(float x, float y, float z, float w)
{
return (uint) (
((uint) System.Math.Round(Math.Clamp(x, 0, 1) * 1023.0f)) |
((uint) System.Math.Round(Math.Clamp(y, 0, 1) * 1023.0f) << 10) |
((uint) System.Math.Round(Math.Clamp(z, 0, 1) * 1023.0f) << 20) |
((uint) System.Math.Round(Math.Clamp(w, 0, 1) * 3.0f) << 30)
);
}
#endregion
}