CreateMeshTests.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Numerics;
  4. using System.Text;
  5. using NUnit.Framework;
  6. namespace SharpGLTF.Geometry
  7. {
  8. using Schema2;
  9. [TestFixture]
  10. public class CreateMeshTests
  11. {
  12. [Test]
  13. public void CreateTriangleScene()
  14. {
  15. // define the data of a triangle:
  16. var positions = new[]
  17. {
  18. new Vector3(10,-10,0),
  19. new Vector3(0,10,0),
  20. new Vector3(-10,-10,0)
  21. };
  22. var normals = new[]
  23. {
  24. Vector3.UnitZ,
  25. Vector3.UnitZ,
  26. Vector3.UnitZ
  27. };
  28. var indices = new UInt32[] { 0, 1, 2 };
  29. // create a new mesh:
  30. var srcMesh = new Mesh<int?>();
  31. // setup a mesh primitive
  32. var srcPrimitive = srcMesh.CreatePrimitive();
  33. srcPrimitive.AllocateVertices(positions.Length, "POSITION", "NORMAL"); // (#1)
  34. srcPrimitive.AllocateIndices(indices.Length, PrimitiveType.TRIANGLES); // (#2)
  35. // assign vertices and indices
  36. srcPrimitive.Vertices[0].SetValues(0, positions);
  37. srcPrimitive.Vertices[1].SetValues(0, normals);
  38. srcPrimitive.Indices.SetValues(0, indices);
  39. srcPrimitive.Material = 0; // using material with index 0, which will be created later
  40. // check the values we've set match the input data
  41. CollectionAssert.AreEqual(positions, srcPrimitive.Vertices[0].AsVector3Array());
  42. CollectionAssert.AreEqual(normals, srcPrimitive.Vertices[1].AsVector3Array());
  43. CollectionAssert.AreEqual(indices, srcPrimitive.Indices.AsIntegerArray());
  44. // Notice that until now, we've been working with objects in the .Geometry namespace.
  45. // Now we switch to the .Schema2 namespace and we create a new scene:
  46. var model = ModelRoot.CreateModel();
  47. var scene = model.UseScene("default");
  48. var rnode = scene.CreateNode("main scene");
  49. var material = model.CreateMaterial("DefaultMaterial")
  50. .WithDefault(new Vector4(1, 0, 0, 1))
  51. .WithDoubleSide(true);
  52. rnode.Mesh = model.CreateMesh();
  53. // this assigns the mesh we've created before to this schema mesh.
  54. // Notice that the schema buffers being created will be using the
  55. // memory allocated by (#1) and (#2)
  56. srcMesh.AssignToSchema(rnode.Mesh);
  57. model.AttachToCurrentTest("Triangle.gltf");
  58. model.AttachToCurrentTest("Triangle.glb");
  59. TestContext.CurrentContext.AttachShowDirLink();
  60. }
  61. }
  62. }