LoadSpecialModelsTest.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NUnit.Framework;
  6. namespace SharpGLTF.Schema2.LoadAndSave
  7. {
  8. /// <summary>
  9. /// Test cases for models found in <see href="https://github.com/KhronosGroup/glTF-Blender-Exporter"/>
  10. /// </summary>
  11. [TestFixture]
  12. [Category("Model Load and Save")]
  13. public class LoadSpecialModelsTest
  14. {
  15. #region setup
  16. [OneTimeSetUp]
  17. public void Setup()
  18. {
  19. // TestFiles.DownloadReferenceModels();
  20. }
  21. #endregion
  22. public void LoadWithCustomImageLoader()
  23. {
  24. TestContext.CurrentContext.AttachShowDirLink();
  25. // load Polly model
  26. var model = ModelRoot.Load(TestFiles.GetPollyFileModelPath());
  27. }
  28. [Test(Description = "Example of traversing the visual tree all the way to individual vertices and indices")]
  29. public void LoadPollyModel()
  30. {
  31. TestContext.CurrentContext.AttachShowDirLink();
  32. // load Polly model
  33. var model = ModelRoot.Load(TestFiles.GetPollyFileModelPath());
  34. Assert.NotNull(model);
  35. var triangles = model.DefaultScene
  36. .EvaluateTriangles<Geometry.VertexTypes.VertexPosition, Geometry.VertexTypes.VertexTexture1>(model.LogicalAnimations[0], 0.5f)
  37. .ToList();
  38. // Save as GLB, and also evaluate all triangles and save as Wavefront OBJ
  39. model.AttachToCurrentTest("polly_out.glb");
  40. model.AttachToCurrentTest("polly_out.obj");
  41. // hierarchically browse some elements of the model:
  42. var scene = model.DefaultScene;
  43. var pollyNode = scene.FindNode(n => n.Name == "Polly_Display");
  44. var pollyPrimitive = pollyNode.Mesh.Primitives[0];
  45. var pollyIndices = pollyPrimitive.GetIndices();
  46. var pollyPositions = pollyPrimitive.GetVertices("POSITION").AsVector3Array();
  47. var pollyNormals = pollyPrimitive.GetVertices("NORMAL").AsVector3Array();
  48. for (int i = 0; i < pollyIndices.Count; i += 3)
  49. {
  50. var a = (int)pollyIndices[i + 0];
  51. var b = (int)pollyIndices[i + 1];
  52. var c = (int)pollyIndices[i + 2];
  53. var ap = pollyPositions[a];
  54. var bp = pollyPositions[b];
  55. var cp = pollyPositions[c];
  56. var an = pollyNormals[a];
  57. var bn = pollyNormals[b];
  58. var cn = pollyNormals[c];
  59. TestContext.WriteLine($"Triangle {ap} {an} {bp} {bn} {cp} {cn}");
  60. }
  61. }
  62. // [Test]
  63. public void LoadUniVRM()
  64. {
  65. TestContext.CurrentContext.AttachShowDirLink();
  66. var path = TestFiles.GetUniVRMModelPath();
  67. var model = ModelRoot.Load(path);
  68. Assert.NotNull(model);
  69. model.AttachToCurrentTest("AliceModel.glb");
  70. }
  71. // [Test]
  72. public void LoadShrekshaoModel()
  73. {
  74. TestContext.CurrentContext.AttachShowDirLink();
  75. var path = "Assets\\SpecialCases\\shrekshao.glb";
  76. var model = ModelRoot.Load(path);
  77. Assert.NotNull(model);
  78. }
  79. [Test]
  80. public void LoadMouseModel()
  81. {
  82. // this model has several nodes with curve animations containing a single animation key,
  83. // which is causing some problems to the interpolator.
  84. TestContext.CurrentContext.AttachShowDirLink();
  85. var path = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Assets\\SpecialCases\\mouse.glb");
  86. var model = ModelRoot.Load(path);
  87. var channel = model.LogicalAnimations[1].FindRotationSampler(model.LogicalNodes[5]);
  88. var node5_R_00 = channel.CreateCurveSampler(true).GetPoint(0);
  89. var node5_R_01 = channel.CreateCurveSampler(true).GetPoint(1);
  90. Assert.AreEqual(node5_R_00, node5_R_01);
  91. model.AttachToCurrentTest("mouse_00.obj", model.LogicalAnimations[1], 0f);
  92. model.AttachToCurrentTest("mouse_01.obj", model.LogicalAnimations[1], 1f);
  93. }
  94. // these models show normal mapping but lack tangents, which are expected to be
  95. // generated at runtime; These tests generate the tangents and check them against the baseline.
  96. [TestCase("NormalTangentTest.glb")]
  97. [TestCase("NormalTangentMirrorTest.glb")]
  98. public void LoadGeneratedTangetsTest(string fileName)
  99. {
  100. TestContext.CurrentContext.AttachShowDirLink();
  101. var path = TestFiles.GetSampleModelsPaths().FirstOrDefault(item => item.EndsWith(fileName));
  102. var model = ModelRoot.Load(path);
  103. var mesh = model.DefaultScene
  104. .EvaluateTriangles<Geometry.VertexTypes.VertexPositionNormalTangent, Geometry.VertexTypes.VertexTexture1>()
  105. .ToMeshBuilder( m => m.ToMaterialBuilder() );
  106. var editableScene = new Scenes.SceneBuilder();
  107. editableScene.AddRigidMesh(mesh, System.Numerics.Matrix4x4.Identity);
  108. model.AttachToCurrentTest("original.glb");
  109. editableScene.ToGltf2().AttachToCurrentTest("WithTangents.glb");
  110. }
  111. }
  112. }