LoadModelTests.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using NUnit.Framework;
  5. namespace SharpGLTF.Schema2.LoadAndSave
  6. {
  7. [TestFixture]
  8. public class LoadModelTests
  9. {
  10. #region setup
  11. [OneTimeSetUp]
  12. public void Setup()
  13. {
  14. TestFiles.CheckoutDataDirectories();
  15. }
  16. #endregion
  17. #region testing models of https://github.com/bghgary/glTF-Asset-Generator.git
  18. [Test]
  19. public void TestLoadReferenceModels()
  20. {
  21. TestContext.CurrentContext.AttachShowDirLink();
  22. foreach (var f in TestFiles.GetGeneratedFilePaths())
  23. {
  24. var model = GltfUtils.LoadModel(f);
  25. Assert.NotNull(model);
  26. model.AttachToCurrentTest(System.IO.Path.ChangeExtension(System.IO.Path.GetFileName(f), ".obj"));
  27. }
  28. }
  29. [TestCase(0)]
  30. [TestCase(6)]
  31. public void TestLoadCompatibleModels(int idx)
  32. {
  33. var filePath = TestFiles.GetCompatibilityFilePath(idx);
  34. var model = GltfUtils.LoadModel(filePath);
  35. Assert.NotNull(model);
  36. }
  37. [TestCase(1)]
  38. [TestCase(2)]
  39. [TestCase(3)]
  40. [TestCase(4)]
  41. [TestCase(5)]
  42. public void TestLoadInvalidModels(int idx)
  43. {
  44. var filePath = TestFiles.GetCompatibilityFilePath(idx);
  45. try
  46. {
  47. ModelRoot.Load(filePath);
  48. Assert.Fail("Did not throw!");
  49. }
  50. catch(IO.ModelException ex)
  51. {
  52. TestContext.WriteLine($"{filePath} threw {ex.Message}");
  53. }
  54. }
  55. #endregion
  56. #region testing models of https://github.com/KhronosGroup/glTF-Sample-Models.git
  57. [TestCase("\\glTF\\")]
  58. // [TestCase("\\glTF-Draco\\")] // Not supported
  59. [TestCase("\\glTF-Binary\\")]
  60. [TestCase("\\glTF-Embedded\\")]
  61. [TestCase("\\glTF-pbrSpecularGlossiness\\")]
  62. public void TestLoadSampleModels(string section)
  63. {
  64. TestContext.CurrentContext.AttachShowDirLink();
  65. foreach (var f in TestFiles.GetSampleFilePaths())
  66. {
  67. if (!f.Contains(section)) continue;
  68. var model = GltfUtils.LoadModel(f);
  69. Assert.NotNull(model);
  70. // evaluate and save all the triangles to a Wavefront Object
  71. model.AttachToCurrentTest(System.IO.Path.ChangeExtension(System.IO.Path.GetFileName(f), ".obj"));
  72. // attempt clone
  73. var xclone = model.DeepClone();
  74. // do a model roundtrip
  75. model.MergeImages();
  76. model.MergeBuffers();
  77. var bytes = model.WriteGLB();
  78. var modelBis = ModelRoot.ParseGLB(bytes);
  79. }
  80. }
  81. [Test]
  82. public void TestLoadSampleModelsWithMaterialSpecularGlossiness()
  83. {
  84. foreach (var f in TestFiles.GetFilePathsWithSpecularGlossinessPBR())
  85. {
  86. var root = GltfUtils.LoadModel(f);
  87. Assert.NotNull(root);
  88. }
  89. }
  90. #endregion
  91. #region testing polly model
  92. [Test(Description ="Example of traversing the visual tree all the way to individual vertices and indices")]
  93. public void TestLoadPolly()
  94. {
  95. TestContext.CurrentContext.AttachShowDirLink();
  96. // load Polly model
  97. var model = GltfUtils.LoadModel( TestFiles.GetPollyFilePath() );
  98. Assert.NotNull(model);
  99. // Save as GLB, and also evaluate all triangles and save as Wavefront OBJ
  100. model.AttachToCurrentTest("polly_out.glb");
  101. model.AttachToCurrentTest("polly_out.obj");
  102. // hierarchically browse some elements of the model:
  103. var scene = model.DefaultScene;
  104. var pollyNode = scene.FindNode("Polly_Display");
  105. var pollyPrimitive = pollyNode.Mesh.Primitives[0];
  106. var pollyIndices = pollyPrimitive.GetIndices();
  107. var pollyPositions = pollyPrimitive.GetVertices("POSITION").AsVector3Array();
  108. var pollyNormals = pollyPrimitive.GetVertices("NORMAL").AsVector3Array();
  109. for (int i=0; i < pollyIndices.Count; i+=3)
  110. {
  111. var a = (int)pollyIndices[i + 0];
  112. var b = (int)pollyIndices[i + 1];
  113. var c = (int)pollyIndices[i + 2];
  114. var ap = pollyPositions[a];
  115. var bp = pollyPositions[b];
  116. var cp = pollyPositions[c];
  117. var an = pollyNormals[a];
  118. var bn = pollyNormals[b];
  119. var cn = pollyNormals[c];
  120. TestContext.WriteLine($"Triangle {ap} {an} {bp} {bn} {cp} {cn}");
  121. }
  122. }
  123. #endregion
  124. }
  125. }