BasicSceneCreationTests.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System.Collections.Generic;
  2. using System.Numerics;
  3. using NUnit.Framework;
  4. using SharpGLTF.IO;
  5. using JSONEXTRAS = System.Text.Json.Nodes.JsonNode;
  6. namespace SharpGLTF.Schema2.Authoring
  7. {
  8. using VPOSNRM = Geometry.VertexBuilder<Geometry.VertexTypes.VertexPositionNormal,Geometry.VertexTypes.VertexEmpty,Geometry.VertexTypes.VertexEmpty>;
  9. [TestFixture]
  10. [Category("Model Authoring")]
  11. public class BasicSceneCreationTests
  12. {
  13. [Test(Description = "Creates an empty model")]
  14. public void CreateEmptyScene()
  15. {
  16. var root = ModelRoot.CreateModel();
  17. var scene = root.UseScene("Empty Scene");
  18. Assert.That(scene, Is.Not.Null);
  19. Assert.That(root.DefaultScene.Name, Is.EqualTo("Empty Scene"));
  20. }
  21. [Test(Description = "Creates an empty model")]
  22. public void CreateSceneWithExtras()
  23. {
  24. var root = ModelRoot.CreateModel();
  25. var scene = root.UseScene("Empty Scene");
  26. var dict = new Dictionary<string, object>();
  27. dict["author"] = "me";
  28. dict["string_null"] = (string)null;
  29. dict["value1"] = 17;
  30. dict["array1"] = new List<int> { 1, 2, 3 };
  31. dict["array_empty"] = new List<int>();
  32. dict["dict1"] = new Dictionary<string, object>
  33. {
  34. ["A"] = 16,
  35. ["B"] = "delta",
  36. ["C"] = new List<int> { 4, 6, 7 },
  37. ["D"] = new Dictionary<string, int> { ["S"]= 1, ["T"] = 2 }
  38. };
  39. dict["dict2"] = new Dictionary<string, int> { ["2"] = 2, ["3"] = 3 };
  40. var extras = JSONEXTRAS.Parse(System.Text.Json.JsonSerializer.Serialize(dict));
  41. root.Extras = extras;
  42. var bytes = root.WriteGLB();
  43. var rootBis = ModelRoot.ParseGLB(bytes);
  44. var a = root.Extras;
  45. var b = rootBis.Extras;
  46. var json = rootBis.Extras.ToJsonString();
  47. var c = JSONEXTRAS.Parse(json);
  48. Assert.That(JsonContentTests.AreEqual(a, b), Is.True);
  49. Assert.That(JsonContentTests.AreEqual(a, extras), Is.True);
  50. Assert.That(JsonContentTests.AreEqual(b, extras), Is.True);
  51. Assert.That(JsonContentTests.AreEqual(c, extras), Is.True);
  52. // Assert.AreEqual(2, c.GetValue<int>("dict1","D","T"));
  53. }
  54. [Test(Description = "Creates a model with a triangle mesh")]
  55. public void CreateSceneWithSolidTriangle()
  56. {
  57. TestContext.CurrentContext.AttachGltfValidatorLinks();
  58. // create model
  59. var model = ModelRoot.CreateModel();
  60. // create scene
  61. var scene = model.DefaultScene = model.UseScene("Default");
  62. // create node
  63. var rnode = scene.CreateNode("Triangle Node");
  64. // create material
  65. var material = model.CreateMaterial("Default")
  66. .WithDefault(new Vector4(0, 1, 0, 1))
  67. .WithDoubleSide(true);
  68. // create mesh
  69. var rmesh = rnode.Mesh = model.CreateMesh("Triangle Mesh");
  70. // create the vertex positions
  71. var positions = new[]
  72. {
  73. new Vector3(0, 10, 0),
  74. new Vector3(-10, -10, 0),
  75. new Vector3(10, -10, 0),
  76. };
  77. // create an index buffer and fill it
  78. var indices = new[] { 0, 1, 2 };
  79. // create mesh primitive
  80. var primitive = rmesh.CreatePrimitive()
  81. .WithVertexAccessor("POSITION", positions)
  82. .WithIndicesAccessor(PrimitiveType.TRIANGLES, indices)
  83. .WithMaterial(material);
  84. model.AttachToCurrentTest("result.glb");
  85. model.AttachToCurrentTest("result.gltf");
  86. }
  87. [Test(Description = "Creates a model with a textured triangle mesh")]
  88. public void CreateSceneWithTexturedTriangle()
  89. {
  90. TestContext.CurrentContext.AttachGltfValidatorLinks();
  91. // we'll use our icon as the source texture
  92. var imagePath = System.IO.Path.Combine(TestContext.CurrentContext.WorkDirectory, "..\\..\\..\\..\\..\\build\\Icons\\glTF2Sharp.png");
  93. // create a basic scene
  94. var model = ModelRoot.CreateModel();
  95. var scene = model.UseScene("Default");
  96. var rnode = scene.CreateNode("Triangle Node");
  97. var rmesh = rnode.Mesh = model.CreateMesh("Triangle Mesh");
  98. var material = model.CreateMaterial("Default")
  99. .WithPBRMetallicRoughness(Vector4.One, imagePath)
  100. .WithDoubleSide(true);
  101. // define the triangle positions
  102. var positions = new[]
  103. {
  104. new Vector3(0, 10, 0),
  105. new Vector3(-10, -10, 0),
  106. new Vector3(10, -10, 0)
  107. };
  108. // define the triangle UV coordinates
  109. var texCoords = new[]
  110. {
  111. new Vector2(0.5f, -0.8f),
  112. new Vector2(-0.5f, 1.2f),
  113. new Vector2(1.5f, 1.2f)
  114. };
  115. // create a mesh primitive and assgin the accessors and other properties
  116. var primitive = rmesh.CreatePrimitive()
  117. .WithVertexAccessor("POSITION", positions)
  118. .WithVertexAccessor("TEXCOORD_0", texCoords)
  119. .WithIndicesAutomatic(PrimitiveType.TRIANGLES)
  120. .WithMaterial(material);
  121. model.AttachToCurrentTest("result.glb");
  122. model.AttachToCurrentTest("result.obj");
  123. model.AttachToCurrentTest("result.gltf");
  124. }
  125. [Test(Description = "Creates an interleaved scene using a toolkit utilities")]
  126. public void CreateSceneWithInterleavedQuadMesh()
  127. {
  128. TestContext.CurrentContext.AttachGltfValidatorLinks();
  129. var vertices = new[]
  130. {
  131. VPOSNRM.Create(new Vector3(-10, 10, 0), Vector3.UnitZ),
  132. VPOSNRM.Create(new Vector3( 10, 10, 0), Vector3.UnitZ),
  133. VPOSNRM.Create(new Vector3( 10, -10, 0), Vector3.UnitZ),
  134. VPOSNRM.Create(new Vector3(-10, -10, 0), Vector3.UnitZ)
  135. };
  136. var model = ModelRoot.CreateModel();
  137. var mesh = model.CreateMesh("mesh1");
  138. mesh.CreatePrimitive()
  139. .WithMaterial(model.CreateMaterial("Default").WithDefault(Vector4.One).WithDoubleSide(true))
  140. .WithVertexAccessors(vertices)
  141. .WithIndicesAccessor(PrimitiveType.TRIANGLES, new int[] { 0, 1, 2, 0, 2, 3 });
  142. var scene = model.UseScene("Default");
  143. var rnode = scene.CreateNode("RootNode").WithMesh(mesh);
  144. model.AttachToCurrentTest("result.glb");
  145. model.AttachToCurrentTest("result.gltf");
  146. }
  147. [Test(Description = "Creates a scene with a perspective camera")]
  148. public void CreateSceneWithCamera()
  149. {
  150. TestContext.CurrentContext.AttachGltfValidatorLinks();
  151. var model = ModelRoot.CreateModel();
  152. model.UseScene(0)
  153. .CreateNode()
  154. .WithLocalTranslation(new Vector3(0, 3, 10))
  155. .WithPerspectiveCamera(null, 1, 0.1f);
  156. model.AttachToCurrentTest("result.glb");
  157. model.AttachToCurrentTest("result.gltf");
  158. }
  159. }
  160. }