UV.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. namespace OpenVIII.Battle.Dat
  6. {
  7. [StructLayout(LayoutKind.Explicit, Pack = 1, Size = 2)]
  8. public struct UV
  9. {
  10. #region Fields
  11. [field: FieldOffset(0)]
  12. public readonly byte U;
  13. [field: FieldOffset(1)]
  14. public readonly byte V;
  15. #endregion Fields
  16. #region Constructors
  17. private UV(byte u, byte v) => (U, V) = (u, v);
  18. private UV(BinaryReader br)
  19. => (U, V) = (br.ReadByte(), br.ReadByte());
  20. #endregion Constructors
  21. #region Methods
  22. public static UV CreateInstance(BinaryReader br) => new UV(br);
  23. public static UV CreateInstance(byte u, byte v) => new UV(u, v);
  24. public override string ToString() => $"{U};{U1()};{V};{V1()}";
  25. public Vector2 ToVector2(float width, float height) => new Vector2(U1(width), V1(height));
  26. public float U1(float height = 128f) => U / height;
  27. public float V1(float width = 128f)
  28. {
  29. if (width <= 0) throw new ArgumentOutOfRangeException(nameof(width));
  30. return V > 128 ? //if bigger than 128, then multi texture index to odd
  31. (V - 128f) / width
  32. : Math.Abs(width - 32) < float.Epsilon ? //if equals 32, then it's weapon texture and should be in range of 96-128
  33. (V - 96) / width
  34. : V / width;
  35. }
  36. #endregion Methods
  37. }
  38. }