LoadPollyTest.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using NUnit.Framework;
  5. namespace SharpGLTF.Schema2.LoadAndSave
  6. {
  7. /// <summary>
  8. /// Test cases for models found in <see href="https://github.com/KhronosGroup/glTF-Blender-Exporter"/>
  9. /// </summary>
  10. [TestFixture]
  11. public class LoadPollyTest
  12. {
  13. #region setup
  14. [OneTimeSetUp]
  15. public void Setup()
  16. {
  17. TestFiles.DownloadReferenceModels();
  18. }
  19. #endregion
  20. [Test(Description = "Example of traversing the visual tree all the way to individual vertices and indices")]
  21. public void TestLoadPolly()
  22. {
  23. TestContext.CurrentContext.AttachShowDirLink();
  24. // load Polly model
  25. var model = GltfUtils.LoadModel(TestFiles.GetPollyFileModelPath());
  26. Assert.NotNull(model);
  27. // Save as GLB, and also evaluate all triangles and save as Wavefront OBJ
  28. model.AttachToCurrentTest("polly_out.glb");
  29. model.AttachToCurrentTest("polly_out.obj");
  30. // hierarchically browse some elements of the model:
  31. var scene = model.DefaultScene;
  32. var pollyNode = scene.FindNode(n => n.Name == "Polly_Display");
  33. var pollyPrimitive = pollyNode.Mesh.Primitives[0];
  34. var pollyIndices = pollyPrimitive.GetIndices();
  35. var pollyPositions = pollyPrimitive.GetVertices("POSITION").AsVector3Array();
  36. var pollyNormals = pollyPrimitive.GetVertices("NORMAL").AsVector3Array();
  37. for (int i = 0; i < pollyIndices.Count; i += 3)
  38. {
  39. var a = (int)pollyIndices[i + 0];
  40. var b = (int)pollyIndices[i + 1];
  41. var c = (int)pollyIndices[i + 2];
  42. var ap = pollyPositions[a];
  43. var bp = pollyPositions[b];
  44. var cp = pollyPositions[c];
  45. var an = pollyNormals[a];
  46. var bn = pollyNormals[b];
  47. var cn = pollyNormals[c];
  48. TestContext.WriteLine($"Triangle {ap} {an} {bp} {bn} {cp} {cn}");
  49. }
  50. }
  51. }
  52. }