NUnitGltfUtils.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5. using System.Text;
  6. using NUnit.Framework;
  7. using SharpGLTF.Schema2;
  8. namespace SharpGLTF
  9. {
  10. public static class NUnitGltfUtils
  11. {
  12. public static void AttachGltfValidatorLinks(this TestContext context)
  13. {
  14. // new AttachmentInfo(context, "🌍 Khronos Validator.lnk").WriteLink("http://github.khronos.org/glTF-Validator/");
  15. // new AttachmentInfo(context, "🌍 BabylonJS Sandbox.lnk").WriteLink("https://sandbox.babylonjs.com/");
  16. // new AttachmentInfo(context, "🌍 Don McCurdy Sandbox.lnk").WriteLink("https://gltf-viewer.donmccurdy.com/");
  17. // new AttachmentInfo(context, "🌍 VirtualGIS Cesium Sandbox.lnk").WriteLink("https://www.virtualgis.io/gltfviewer/");
  18. }
  19. public static void AttachToCurrentTest(this Scenes.SceneBuilder scene, string fileName)
  20. {
  21. var model = scene.ToGltf2();
  22. model.AttachToCurrentTest(fileName);
  23. }
  24. public static void AttachToCurrentTest(this ModelRoot model, string fileName, Animation animation, float time)
  25. {
  26. // wavefront does't like files paths with spaces because
  27. // some implementations would not find the material file
  28. fileName = fileName.Replace(" ", "_");
  29. AttachmentInfo
  30. .From(fileName)
  31. .WriteObject(f => model.SaveAsWavefront(f, animation, time));
  32. }
  33. public static string AttachToCurrentTest<TvG, TvM, TvS>(this Geometry.MeshBuilder<TvG, TvM, TvS> mesh, string fileName)
  34. where TvG : struct, Geometry.VertexTypes.IVertexGeometry
  35. where TvM : struct, Geometry.VertexTypes.IVertexMaterial
  36. where TvS : struct, Geometry.VertexTypes.IVertexSkinning
  37. {
  38. var gl2model = ModelRoot.CreateModel();
  39. var gl2mesh = gl2model.CreateMeshes(mesh)[0];
  40. var node = gl2model.UseScene(0).CreateNode();
  41. node.Mesh = gl2mesh;
  42. return gl2model.AttachToCurrentTest(fileName);
  43. }
  44. public static string AttachToCurrentTest(this ModelRoot model, string fileName, WriteSettings settings = null)
  45. {
  46. string validationPath = null;
  47. if (fileName.ToUpperInvariant().EndsWith(".GLB"))
  48. {
  49. validationPath = fileName = AttachmentInfo
  50. .From(fileName)
  51. .WriteObject(f => model.SaveGLB(f, settings))
  52. .FullName;
  53. }
  54. else if (fileName.ToUpperInvariant().EndsWith(".GLTF"))
  55. {
  56. if (settings == null) settings = new WriteSettings { JsonIndented = true };
  57. validationPath = fileName = AttachmentInfo
  58. .From(fileName)
  59. .WriteObject(f => model.Save(f, settings))
  60. .FullName;
  61. }
  62. else if (fileName.ToUpperInvariant().EndsWith(".OBJ"))
  63. {
  64. // skip exporting to obj if gpu instancing is there
  65. if (Node.Flatten(model.DefaultScene).Any(n => n.GetGpuInstancing() != null)) return fileName;
  66. fileName = fileName.Replace(" ", "_");
  67. fileName = AttachmentInfo
  68. .From(fileName)
  69. .WriteObject(f => model.SaveAsWavefront(f))
  70. .FullName;
  71. }
  72. else if (fileName.ToUpperInvariant().EndsWith(".PLOTLY"))
  73. {
  74. fileName = fileName.Replace(".plotly", ".html");
  75. var html = model.DefaultScene
  76. .ToPlotly()
  77. .ToHtml();
  78. fileName = AttachmentInfo
  79. .From(fileName)
  80. .WriteAllText(html)
  81. .FullName;
  82. }
  83. if (validationPath != null)
  84. {
  85. var report = GltfValidator.ValidationReport.ValidateAsync(fileName, System.Threading.CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult();
  86. if (report == null) return fileName;
  87. if (report.Severity == GltfValidator.Severity.Error || report.Severity == GltfValidator.Severity.Warning)
  88. {
  89. TestContext.WriteLine(report.ToString());
  90. }
  91. Assert.That(report.Severity, Is.Not.EqualTo(GltfValidator.Severity.Error));
  92. }
  93. return fileName;
  94. }
  95. }
  96. }