LoadSpecialModelsTest.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. [Test(Description = "Example of traversing the visual tree all the way to individual vertices and indices")]
  23. public void LoadPollyModel()
  24. {
  25. TestContext.CurrentContext.AttachShowDirLink();
  26. // load Polly model
  27. var model = ModelRoot.Load(TestFiles.GetPollyFileModelPath());
  28. Assert.NotNull(model);
  29. var triangles = model.DefaultScene
  30. .EvaluateTriangles<Geometry.VertexTypes.VertexPosition, Geometry.VertexTypes.VertexTexture1>(model.LogicalAnimations[0], 0.5f)
  31. .ToList();
  32. // Save as GLB, and also evaluate all triangles and save as Wavefront OBJ
  33. model.AttachToCurrentTest("polly_out.glb");
  34. model.AttachToCurrentTest("polly_out.obj");
  35. // hierarchically browse some elements of the model:
  36. var scene = model.DefaultScene;
  37. var pollyNode = scene.FindNode(n => n.Name == "Polly_Display");
  38. var pollyPrimitive = pollyNode.Mesh.Primitives[0];
  39. var pollyIndices = pollyPrimitive.GetIndices();
  40. var pollyPositions = pollyPrimitive.GetVertices("POSITION").AsVector3Array();
  41. var pollyNormals = pollyPrimitive.GetVertices("NORMAL").AsVector3Array();
  42. for (int i = 0; i < pollyIndices.Count; i += 3)
  43. {
  44. var a = (int)pollyIndices[i + 0];
  45. var b = (int)pollyIndices[i + 1];
  46. var c = (int)pollyIndices[i + 2];
  47. var ap = pollyPositions[a];
  48. var bp = pollyPositions[b];
  49. var cp = pollyPositions[c];
  50. var an = pollyNormals[a];
  51. var bn = pollyNormals[b];
  52. var cn = pollyNormals[c];
  53. TestContext.WriteLine($"Triangle {ap} {an} {bp} {bn} {cp} {cn}");
  54. }
  55. }
  56. [Test]
  57. public void LoadUniVRM()
  58. {
  59. TestContext.CurrentContext.AttachShowDirLink();
  60. var path = TestFiles.GetUniVRMModelPath();
  61. var model = ModelRoot.Load(path);
  62. Assert.NotNull(model);
  63. model.AttachToCurrentTest("AliceModel.glb");
  64. }
  65. [Test]
  66. public void LoadShrekshaoModel()
  67. {
  68. TestContext.CurrentContext.AttachShowDirLink();
  69. var path = "Assets\\SpecialCases\\shrekshao.glb";
  70. var model = ModelRoot.Load(path);
  71. Assert.NotNull(model);
  72. }
  73. }
  74. }