_Extensions.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 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. }
  60. }