AttributeFormat.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace SharpGLTF.Memory
  5. {
  6. using DIMENSIONS = SharpGLTF.Schema2.DimensionType;
  7. using ENCODING = SharpGLTF.Schema2.EncodingType;
  8. /// <summary>
  9. /// Defines the formatting in which a byte sequence can be encoded/decoded to attribute elements.
  10. /// </summary>
  11. [System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
  12. public readonly struct AttributeFormat : IEquatable<AttributeFormat>
  13. {
  14. #region debug
  15. internal string _GetDebuggerDisplay()
  16. {
  17. var txt = $"{Encoding}";
  18. switch (Dimensions)
  19. {
  20. case DIMENSIONS.SCALAR: break;
  21. case DIMENSIONS.VEC2: txt += "2"; break;
  22. case DIMENSIONS.VEC3: txt += "3"; break;
  23. case DIMENSIONS.VEC4: txt += "4"; break;
  24. case DIMENSIONS.MAT2: txt += "2x2"; break;
  25. case DIMENSIONS.MAT3: txt += "3x3"; break;
  26. case DIMENSIONS.MAT4: txt += "4x4"; break;
  27. default: txt += "?"; break;
  28. }
  29. if (Normalized) txt += " Normalized";
  30. return txt;
  31. }
  32. #endregion
  33. #region constructors
  34. public static implicit operator AttributeFormat(ENCODING enc) { return new AttributeFormat(enc); }
  35. public static implicit operator AttributeFormat(DIMENSIONS dim) { return new AttributeFormat(dim); }
  36. public static implicit operator AttributeFormat((DIMENSIONS dim, ENCODING enc) fmt) { return new AttributeFormat(fmt.dim, fmt.enc); }
  37. public static implicit operator AttributeFormat((DIMENSIONS dim, ENCODING enc, Boolean nrm) fmt) { return new AttributeFormat(fmt.dim, fmt.enc, fmt.nrm); }
  38. public AttributeFormat(ENCODING enc)
  39. {
  40. Dimensions = DIMENSIONS.SCALAR;
  41. Encoding = enc;
  42. Normalized = false;
  43. ByteSize = Dimensions.DimCount() * Encoding.ByteLength();
  44. }
  45. public AttributeFormat(DIMENSIONS dim)
  46. {
  47. Dimensions = dim;
  48. Encoding = ENCODING.FLOAT;
  49. Normalized = false;
  50. ByteSize = Dimensions.DimCount() * Encoding.ByteLength();
  51. }
  52. public AttributeFormat(DIMENSIONS dim, ENCODING enc)
  53. {
  54. Dimensions = dim;
  55. Encoding = enc;
  56. Normalized = false;
  57. ByteSize = Dimensions.DimCount() * Encoding.ByteLength();
  58. }
  59. public AttributeFormat(DIMENSIONS dim, ENCODING enc, Boolean nrm)
  60. {
  61. Dimensions = dim;
  62. Encoding = enc;
  63. Normalized = nrm;
  64. ByteSize = Dimensions.DimCount() * Encoding.ByteLength();
  65. }
  66. #endregion
  67. #region data
  68. public readonly ENCODING Encoding;
  69. public readonly DIMENSIONS Dimensions;
  70. public readonly Boolean Normalized;
  71. public readonly Int32 ByteSize;
  72. public override int GetHashCode()
  73. {
  74. return Dimensions.GetHashCode()
  75. ^ Encoding.GetHashCode()
  76. ^ Normalized.GetHashCode();
  77. }
  78. public static bool AreEqual(AttributeFormat a, AttributeFormat b)
  79. {
  80. if (a.Encoding != b.Encoding) return false;
  81. if (a.Dimensions != b.Dimensions) return false;
  82. if (a.Normalized != b.Normalized) return false;
  83. return true;
  84. }
  85. public Int32 ByteSizePadded => ByteSize.WordPadded();
  86. #endregion
  87. #region API
  88. public override bool Equals(object obj) { return obj is AttributeFormat other ? AreEqual(this, other) : false; }
  89. public bool Equals(AttributeFormat other) { return AreEqual(this, other); }
  90. public static bool operator ==(AttributeFormat a, AttributeFormat b) { return AreEqual(a, b); }
  91. public static bool operator !=(AttributeFormat a, AttributeFormat b) { return !AreEqual(a, b); }
  92. #endregion
  93. }
  94. }