2
0

LoadModelTests.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. model.AttachToCurrentTest(System.IO.Path.ChangeExtension(System.IO.Path.GetFileName(f), ".glb"));
  73. // attempt clone
  74. var xclone = model.DeepClone();
  75. // do a model roundtrip
  76. model.MergeImages();
  77. model.MergeBuffers();
  78. var bytes = model.WriteGLB();
  79. var modelBis = ModelRoot.ParseGLB(bytes);
  80. }
  81. }
  82. [Test]
  83. public void TestLoadSampleModelsWithMaterialSpecularGlossiness()
  84. {
  85. foreach (var f in TestFiles.GetFilePathsWithSpecularGlossinessPBR())
  86. {
  87. var root = GltfUtils.LoadModel(f);
  88. Assert.NotNull(root);
  89. }
  90. }
  91. #endregion
  92. #region testing polly model
  93. [Test(Description ="Example of traversing the visual tree all the way to individual vertices and indices")]
  94. public void TestLoadPolly()
  95. {
  96. TestContext.CurrentContext.AttachShowDirLink();
  97. // load Polly model
  98. var model = GltfUtils.LoadModel( TestFiles.GetPollyFilePath() );
  99. Assert.NotNull(model);
  100. // Save as GLB, and also evaluate all triangles and save as Wavefront OBJ
  101. model.AttachToCurrentTest("polly_out.glb");
  102. model.AttachToCurrentTest("polly_out.obj");
  103. // hierarchically browse some elements of the model:
  104. var scene = model.DefaultScene;
  105. var pollyNode = scene.FindNode("Polly_Display");
  106. var pollyPrimitive = pollyNode.Mesh.Primitives[0];
  107. var pollyIndices = pollyPrimitive.GetIndices();
  108. var pollyPositions = pollyPrimitive.GetVertices("POSITION").AsVector3Array();
  109. var pollyNormals = pollyPrimitive.GetVertices("NORMAL").AsVector3Array();
  110. for (int i=0; i < pollyIndices.Count; i+=3)
  111. {
  112. var a = (int)pollyIndices[i + 0];
  113. var b = (int)pollyIndices[i + 1];
  114. var c = (int)pollyIndices[i + 2];
  115. var ap = pollyPositions[a];
  116. var bp = pollyPositions[b];
  117. var cp = pollyPositions[c];
  118. var an = pollyNormals[a];
  119. var bn = pollyNormals[b];
  120. var cn = pollyNormals[c];
  121. TestContext.WriteLine($"Triangle {ap} {an} {bp} {bn} {cp} {cn}");
  122. }
  123. }
  124. #endregion
  125. }
  126. }