LoadPollyTest.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. [Category("Model Load and Save")]
  12. public class LoadPollyTest
  13. {
  14. #region setup
  15. [OneTimeSetUp]
  16. public void Setup()
  17. {
  18. TestFiles.DownloadReferenceModels();
  19. }
  20. #endregion
  21. [Test(Description = "Example of traversing the visual tree all the way to individual vertices and indices")]
  22. public void LoadPollyModel()
  23. {
  24. TestContext.CurrentContext.AttachShowDirLink();
  25. // load Polly model
  26. var model = ModelRoot.Load(TestFiles.GetPollyFileModelPath());
  27. Assert.NotNull(model);
  28. // Save as GLB, and also evaluate all triangles and save as Wavefront OBJ
  29. model.AttachToCurrentTest("polly_out.glb");
  30. model.AttachToCurrentTest("polly_out.obj");
  31. // hierarchically browse some elements of the model:
  32. var scene = model.DefaultScene;
  33. var pollyNode = scene.FindNode(n => n.Name == "Polly_Display");
  34. var pollyPrimitive = pollyNode.Mesh.Primitives[0];
  35. var pollyIndices = pollyPrimitive.GetIndices();
  36. var pollyPositions = pollyPrimitive.GetVertices("POSITION").AsVector3Array();
  37. var pollyNormals = pollyPrimitive.GetVertices("NORMAL").AsVector3Array();
  38. for (int i = 0; i < pollyIndices.Count; i += 3)
  39. {
  40. var a = (int)pollyIndices[i + 0];
  41. var b = (int)pollyIndices[i + 1];
  42. var c = (int)pollyIndices[i + 2];
  43. var ap = pollyPositions[a];
  44. var bp = pollyPositions[b];
  45. var cp = pollyPositions[c];
  46. var an = pollyNormals[a];
  47. var bn = pollyNormals[b];
  48. var cn = pollyNormals[c];
  49. TestContext.WriteLine($"Triangle {ap} {an} {bp} {bn} {cp} {cn}");
  50. }
  51. }
  52. }
  53. }