CreateModelTests.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using NUnit.Framework;
  5. namespace SharpGLTF.Schema2
  6. {
  7. [TestFixture]
  8. public class CreateModelTests
  9. {
  10. [Test(Description = "Creates an empty model")]
  11. public void CreateEmptyScene()
  12. {
  13. var root = ModelRoot.CreateModel();
  14. var scene = root.UseScene("Empty Scene");
  15. Assert.NotNull(scene);
  16. Assert.AreEqual("Empty Scene", root.DefaultScene.Name);
  17. }
  18. [Test(Description ="Creates a model with a triangle mesh")]
  19. public void CreateTriangleScene()
  20. {
  21. TestContext.CurrentContext.AttachShowDirLink();
  22. // Although this is a valid way of creating a gltf mesh, it will be extremely GPU inefficient.
  23. var root = ModelRoot.CreateModel();
  24. // create a vertex buffer with positions and fill it
  25. var positionsView = root.CreateBufferView(root.CreateBuffer(12 * 3), null, null, null, BufferMode.ARRAY_BUFFER);
  26. var positionsArray = new Memory.Vector3Array(positionsView.Content);
  27. positionsArray[0] = new System.Numerics.Vector3(0, 10, 0);
  28. positionsArray[1] = new System.Numerics.Vector3(-10, -10, 0);
  29. positionsArray[2] = new System.Numerics.Vector3(10, -10, 0);
  30. // create an index buffer and fill it
  31. var indicesView = root.CreateBufferView(root.CreateBuffer(4 * 3), null, null, null, BufferMode.ELEMENT_ARRAY_BUFFER);
  32. var indicesArray = new Memory.IntegerArray(indicesView.Content);
  33. indicesArray[0] = 0;
  34. indicesArray[1] = 1;
  35. indicesArray[2] = 2;
  36. // create a positions accessor
  37. var positionsAccessor = root.CreateAccessor();
  38. positionsAccessor.SetVertexData(positionsView, 0, ElementType.VEC3, ComponentType.FLOAT, false, 3);
  39. // create an indices accessor
  40. var indicesAccessor = root.CreateAccessor();
  41. indicesAccessor.SetIndexData(indicesView, 0, IndexType.UNSIGNED_INT, 3);
  42. // create a mesh and a mesh primitive
  43. var mesh = root.CreateMesh();
  44. var primitive = mesh.CreatePrimitive();
  45. primitive.DrawPrimitiveType = PrimitiveType.TRIANGLES;
  46. primitive.SetVertexAccessor("POSITION", positionsAccessor);
  47. primitive.IndexAccessor = indicesAccessor;
  48. // create a scene
  49. var scene = root.UseScene("Empty Scene");
  50. // create a node
  51. var node = scene.AddVisualNode("Triangle");
  52. // assign the mesh we previously created
  53. node.Mesh = mesh;
  54. // save
  55. root.AttachToCurrentTest("result.glb");
  56. root.AttachToCurrentTest("result.gltf");
  57. }
  58. }
  59. }