_Extensions.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. namespace SharpGLTF.Runtime
  6. {
  7. static class _Extensions
  8. {
  9. public static Vector2 ToXna(this System.Numerics.Vector2 v)
  10. {
  11. return new Vector2(v.X, v.Y);
  12. }
  13. public static Vector3 ToXna(this System.Numerics.Vector3 v)
  14. {
  15. return new Vector3(v.X, v.Y, v.Z);
  16. }
  17. public static Vector4 ToXna(this System.Numerics.Vector4 v)
  18. {
  19. return new Vector4(v.X, v.Y, v.Z, v.W);
  20. }
  21. public static Matrix ToXna(this System.Numerics.Matrix4x4 m)
  22. {
  23. return new Matrix
  24. (
  25. m.M11, m.M12, m.M13, m.M14,
  26. m.M21, m.M22, m.M23, m.M24,
  27. m.M31, m.M32, m.M33, m.M34,
  28. m.M41, m.M42, m.M43, m.M44
  29. );
  30. }
  31. public static void FixTextureSampler(this SharpGLTF.Schema2.ModelRoot root)
  32. {
  33. // SharpGLTF 1.0.0-Alpha10 has an issue with TextureSamplers, it's fixed in newer versions
  34. foreach(var t in root.LogicalTextures)
  35. {
  36. if (t.Sampler == null)
  37. {
  38. var sampler = root.UseTextureSampler
  39. (
  40. Schema2.TextureWrapMode.REPEAT,
  41. Schema2.TextureWrapMode.REPEAT,
  42. Schema2.TextureMipMapFilter.DEFAULT,
  43. Schema2.TextureInterpolationFilter.LINEAR
  44. );
  45. t.Sampler = sampler;
  46. }
  47. }
  48. }
  49. public static BoundingSphere CreateBoundingSphere(this Schema2.Mesh mesh)
  50. {
  51. var points = mesh
  52. .Primitives
  53. .Select(item => item.GetVertexAccessor("POSITION"))
  54. .Where(item => item != null)
  55. .SelectMany(item => item.AsVector3Array())
  56. .Select(item => item.ToXna());
  57. return BoundingSphere.CreateFromPoints(points);
  58. }
  59. public static int GetPrimitiveVertexSize(this Schema2.PrimitiveType ptype)
  60. {
  61. switch (ptype)
  62. {
  63. case Schema2.PrimitiveType.POINTS: return 1;
  64. case Schema2.PrimitiveType.LINES: return 2;
  65. case Schema2.PrimitiveType.LINE_LOOP: return 2;
  66. case Schema2.PrimitiveType.LINE_STRIP: return 2;
  67. case Schema2.PrimitiveType.TRIANGLES: return 3;
  68. case Schema2.PrimitiveType.TRIANGLE_FAN: return 3;
  69. case Schema2.PrimitiveType.TRIANGLE_STRIP: return 3;
  70. default: throw new NotImplementedException();
  71. }
  72. }
  73. public static IEnumerable<(int, int, int)> GetTriangleIndices(this Schema2.MeshPrimitive primitive)
  74. {
  75. if (primitive == null || primitive.DrawPrimitiveType.GetPrimitiveVertexSize() != 3) return Enumerable.Empty<(int, int, int)>();
  76. if (primitive.IndexAccessor == null) return primitive.DrawPrimitiveType.GetTrianglesIndices(primitive.GetVertexAccessor("POSITION").Count);
  77. return primitive.DrawPrimitiveType.GetTrianglesIndices(primitive.IndexAccessor.AsIndicesArray());
  78. }
  79. public static IEnumerable<(int, int, int)> GetTrianglesIndices(this Schema2.PrimitiveType ptype, int vertexCount)
  80. {
  81. return ptype.GetTrianglesIndices(Enumerable.Range(0, vertexCount).Select(item => (UInt32)item));
  82. }
  83. public static IEnumerable<(int, int, int)> GetTrianglesIndices(this Schema2.PrimitiveType ptype, IEnumerable<UInt32> sourceIndices)
  84. {
  85. switch (ptype)
  86. {
  87. case Schema2.PrimitiveType.TRIANGLES:
  88. {
  89. using (var ptr = sourceIndices.GetEnumerator())
  90. {
  91. while (true)
  92. {
  93. if (!ptr.MoveNext()) break;
  94. var a = ptr.Current;
  95. if (!ptr.MoveNext()) break;
  96. var b = ptr.Current;
  97. if (!ptr.MoveNext()) break;
  98. var c = ptr.Current;
  99. if (!_IsDegenerated(a, b, c)) yield return ((int)a, (int)b, (int)c);
  100. }
  101. }
  102. break;
  103. }
  104. case Schema2.PrimitiveType.TRIANGLE_FAN:
  105. {
  106. using (var ptr = sourceIndices.GetEnumerator())
  107. {
  108. if (!ptr.MoveNext()) break;
  109. var a = ptr.Current;
  110. if (!ptr.MoveNext()) break;
  111. var b = ptr.Current;
  112. while (true)
  113. {
  114. if (!ptr.MoveNext()) break;
  115. var c = ptr.Current;
  116. if (!_IsDegenerated(a, b, c)) yield return ((int)a, (int)b, (int)c);
  117. b = c;
  118. }
  119. }
  120. break;
  121. }
  122. case Schema2.PrimitiveType.TRIANGLE_STRIP:
  123. {
  124. using (var ptr = sourceIndices.GetEnumerator())
  125. {
  126. if (!ptr.MoveNext()) break;
  127. var a = ptr.Current;
  128. if (!ptr.MoveNext()) break;
  129. var b = ptr.Current;
  130. bool reversed = false;
  131. while (true)
  132. {
  133. if (!ptr.MoveNext()) break;
  134. var c = ptr.Current;
  135. if (!_IsDegenerated(a, b, c))
  136. {
  137. if (reversed) yield return ((int)b, (int)a, (int)c);
  138. else yield return ((int)a, (int)b, (int)c);
  139. }
  140. a = b;
  141. b = c;
  142. reversed = !reversed;
  143. }
  144. }
  145. break;
  146. }
  147. default: throw new NotImplementedException();
  148. }
  149. }
  150. static bool _IsDegenerated(uint a, uint b, uint c)
  151. {
  152. if (a == b) return true;
  153. if (a == c) return true;
  154. if (b == c) return true;
  155. return false;
  156. }
  157. }
  158. }