Browse Source

more API correctins for easier model authoring

Vicente Penades 6 years ago
parent
commit
0f10efb4d9

+ 24 - 0
src/SharpGLTF.Toolkit/Schema2/MeshExtensions.cs

@@ -45,6 +45,24 @@ namespace SharpGLTF.Schema2
             return primitive;
             return primitive;
         }
         }
 
 
+        public static MeshPrimitive WithVertexAccessor(this MeshPrimitive primitive, string attribute, IReadOnlyList<Vector4> values)
+        {
+            var root = primitive.LogicalParent.LogicalParent;
+
+            // create a vertex buffer and fill it
+            var view = root.UseBufferView(new Byte[16 * values.Count], 0, null, 0, BufferMode.ARRAY_BUFFER);
+            var array = new Vector4Array(view.Content);
+            array.FillFrom(0, values);
+
+            var accessor = root.CreateAccessor();
+
+            accessor.SetVertexData(view, 0, values.Count, DimensionType.VEC4, EncodingType.FLOAT, false);
+
+            primitive.SetVertexAccessor(attribute, accessor);
+
+            return primitive;
+        }
+
         public static MeshPrimitive WithIndicesAutomatic(this MeshPrimitive primitive, PrimitiveType primitiveType)
         public static MeshPrimitive WithIndicesAutomatic(this MeshPrimitive primitive, PrimitiveType primitiveType)
         {
         {
             var root = primitive.LogicalParent.LogicalParent;
             var root = primitive.LogicalParent.LogicalParent;
@@ -73,5 +91,11 @@ namespace SharpGLTF.Schema2
 
 
             return primitive;
             return primitive;
         }
         }
+
+        public static MeshPrimitive WithMaterial(this MeshPrimitive primitive, Material material)
+        {
+            primitive.Material = material;
+            return primitive;
+        }
     }
     }
 }
 }

+ 2 - 2
src/SharpGLTF/Schema2/gltf.Scene.cs

@@ -390,8 +390,8 @@ namespace SharpGLTF.Schema2
         }
         }
 
 
         /// <summary>
         /// <summary>
-        /// Creates or reuses a <see cref="Scene"/> instance
-        /// at <see cref="ModelRoot.LogicalScenes"/>.
+        /// Creates or reuses a <see cref="Scene"/> instance that has the
+        /// same <paramref name="name"/> at <see cref="ModelRoot.LogicalScenes"/>.
         /// </summary>
         /// </summary>
         /// <param name="name">The name of the instance.</param>
         /// <param name="name">The name of the instance.</param>
         /// <returns>A <see cref="Scene"/> instance.</returns>
         /// <returns>A <see cref="Scene"/> instance.</returns>

+ 2 - 2
src/SharpGLTF/Schema2/khr.lights.cs

@@ -199,8 +199,8 @@ namespace SharpGLTF.Schema2
         }
         }
 
 
         /// <summary>
         /// <summary>
-        /// Creates a new <see cref="PunctualLight"/> instance.
-        /// and adds it to <see cref="ModelRoot.LogicalPunctualLights"/>.
+        /// Creates a new <see cref="PunctualLight"/> instance and
+        /// adds it to <see cref="ModelRoot.LogicalPunctualLights"/>.
         /// </summary>
         /// </summary>
         /// <param name="lightType">A value of <see cref="PunctualLightType"/> describing the type of light to create.</param>
         /// <param name="lightType">A value of <see cref="PunctualLightType"/> describing the type of light to create.</param>
         /// <returns>A <see cref="PunctualLight"/> instance.</returns>
         /// <returns>A <see cref="PunctualLight"/> instance.</returns>

+ 38 - 43
tests/SharpGLTF.Tests/Schema2/Authoring/CreateModelTests.cs

@@ -102,11 +102,16 @@ namespace SharpGLTF.Schema2.Authoring
             // create scene
             // create scene
             var scene = model.DefaultScene = model.UseScene("Default");
             var scene = model.DefaultScene = model.UseScene("Default");
             // create node
             // create node
-            var rnode = scene.CreateNode("Triangle Node");
+            var rnode = scene.CreateNode("Triangle Node");            
+            // create material
+            var material = model.CreateMaterial("Default").WithDefault(new Vector4(0, 1, 0, 1));
+            material.DoubleSided = true;
+
             // create mesh
             // create mesh
             var rmesh = rnode.Mesh = model.CreateMesh("Triangle Mesh");
             var rmesh = rnode.Mesh = model.CreateMesh("Triangle Mesh");
 
 
-            var positionsArray = new[]
+            // create the vertex positions
+            var positions = new[]
             {
             {
                 new Vector3(0, 10, 0),
                 new Vector3(0, 10, 0),
                 new Vector3(-10, -10, 0),
                 new Vector3(-10, -10, 0),
@@ -114,15 +119,13 @@ namespace SharpGLTF.Schema2.Authoring
             };
             };
 
 
             // create an index buffer and fill it            
             // create an index buffer and fill it            
-            var indicesArray = new[] { 0, 1, 2 };
-
+            var indices = new[] { 0, 1, 2 };
+            
             // create mesh primitive
             // create mesh primitive
             var primitive = rmesh.CreatePrimitive()
             var primitive = rmesh.CreatePrimitive()
-                .WithVertexAccessor("POSITION", positionsArray)
-                .WithIndicesAccessor(PrimitiveType.TRIANGLES, indicesArray);
-
-            primitive.Material = model.CreateMaterial("Default").WithDefault(new Vector4(0, 1, 0, 1));
-            primitive.Material.DoubleSided = true;
+                .WithVertexAccessor("POSITION", positions)
+                .WithIndicesAccessor(PrimitiveType.TRIANGLES, indices)
+                .WithMaterial(material);
 
 
             model.AttachToCurrentTest("result.glb");
             model.AttachToCurrentTest("result.glb");
             model.AttachToCurrentTest("result.gltf");
             model.AttachToCurrentTest("result.gltf");
@@ -143,6 +146,12 @@ namespace SharpGLTF.Schema2.Authoring
             var rnode = scene.CreateNode("Triangle Node");
             var rnode = scene.CreateNode("Triangle Node");
             var rmesh = rnode.Mesh = model.CreateMesh("Triangle Mesh");
             var rmesh = rnode.Mesh = model.CreateMesh("Triangle Mesh");
 
 
+            var material = model.CreateMaterial("Default")
+                .WithPBRMetallicRoughness();
+
+            material.DoubleSided = true;
+            material.FindChannel("BaseColor").SetTexture(0, model.UseImageWithFile(imagePath));
+
             // define the triangle positions
             // define the triangle positions
             var positions = new[]
             var positions = new[]
             {
             {
@@ -163,20 +172,9 @@ namespace SharpGLTF.Schema2.Authoring
             var primitive = rmesh.CreatePrimitive()
             var primitive = rmesh.CreatePrimitive()
                 .WithVertexAccessor("POSITION", positions)
                 .WithVertexAccessor("POSITION", positions)
                 .WithVertexAccessor("TEXCOORD_0", texCoords)
                 .WithVertexAccessor("TEXCOORD_0", texCoords)
-                .WithIndicesAutomatic(PrimitiveType.TRIANGLES);
-
-            // create and assign a material
-            primitive.Material = model
-                .CreateMaterial("Default")
-                .WithPBRMetallicRoughness();
+                .WithIndicesAutomatic(PrimitiveType.TRIANGLES)
+                .WithMaterial(material);
 
 
-            primitive.Material.DoubleSided = true;
-
-            // PBRMetallicRoughness has a "BaseColor" and a "Metallic" and a "Roughness" channels.
-            primitive.Material
-                .FindChannel("BaseColor")
-                .SetTexture(0, model.UseImageWithFile(imagePath) );
-            
             model.AttachToCurrentTest("result.glb");
             model.AttachToCurrentTest("result.glb");
             model.AttachToCurrentTest("result.gltf");            
             model.AttachToCurrentTest("result.gltf");            
         }
         }
@@ -207,8 +205,8 @@ namespace SharpGLTF.Schema2.Authoring
             // fill our node with the mesh
             // fill our node with the mesh
             meshBuilder.CopyToNode(rnode, createMaterialForColor);
             meshBuilder.CopyToNode(rnode, createMaterialForColor);
 
 
-            model.DeepClone().AttachToCurrentTest("result.gltf");
-            model.DeepClone().AttachToCurrentTest("result.glb");            
+            model.AttachToCurrentTest("result.gltf");
+            model.AttachToCurrentTest("result.glb");            
         }
         }
 
 
 
 
@@ -251,8 +249,23 @@ namespace SharpGLTF.Schema2.Authoring
         public void CreateAnimatedMeshBuilderScene()
         public void CreateAnimatedMeshBuilderScene()
         {
         {
             TestContext.CurrentContext.AttachShowDirLink();
             TestContext.CurrentContext.AttachShowDirLink();
-            TestContext.CurrentContext.AttachGltfValidatorLink();
+            TestContext.CurrentContext.AttachGltfValidatorLink();           
+
+            // create animation sequence with 4 frames
+            var keyframes = new Dictionary<Single, Vector3>()
+            {
+                [1] = new Vector3(0, 0, 0),
+                [2] = new Vector3(50, 0, 0),
+                [3] = new Vector3(0, 50, 0),
+                [4] = new Vector3(0, 0, 0),
+            };
+
+            var model = ModelRoot.CreateModel();
+            var scene = model.UseScene("Default");
+            var rnode = scene.CreateNode("RootNode").WithTranslationAnimation("track1", keyframes);
+            rnode.LocalTransform = new Transforms.AffineTransform(null, null, null, Vector3.Zero);
 
 
+            // create mesh
             var meshBuilder = new InterleavedMeshBuilder<STATICVERTEX, Vector4>();
             var meshBuilder = new InterleavedMeshBuilder<STATICVERTEX, Vector4>();
 
 
             var v1 = new STATICVERTEX(-10, 10, 0, -10, 10, 15);
             var v1 = new STATICVERTEX(-10, 10, 0, -10, 10, 15);
@@ -261,11 +274,6 @@ namespace SharpGLTF.Schema2.Authoring
             var v4 = new STATICVERTEX(-10, -10, 0, -10, -10, 15);
             var v4 = new STATICVERTEX(-10, -10, 0, -10, -10, 15);
             meshBuilder.AddPolygon(new Vector4(1, 1, 1, 1), v1, v2, v3, v4);
             meshBuilder.AddPolygon(new Vector4(1, 1, 1, 1), v1, v2, v3, v4);
 
 
-            var model = ModelRoot.CreateModel();
-            var scene = model.UseScene("Default");
-            var rnode = scene.CreateNode("RootNode");
-            rnode.LocalTransform = new Transforms.AffineTransform(null, null, null, Vector3.Zero);
-
             // setup a lambda function that creates a material for a given color
             // setup a lambda function that creates a material for a given color
             Material createMaterialForColor(Vector4 color)
             Material createMaterialForColor(Vector4 color)
             {
             {
@@ -277,19 +285,6 @@ namespace SharpGLTF.Schema2.Authoring
             // fill our node with the mesh
             // fill our node with the mesh
             meshBuilder.CopyToNode(rnode, createMaterialForColor);
             meshBuilder.CopyToNode(rnode, createMaterialForColor);
 
 
-            // create animation sequence with 4 frames
-            var keyframes = new Dictionary<Single, Vector3>()
-            {
-                [1] = new Vector3(0, 0, 0),
-                [2] = new Vector3(50, 0, 0),
-                [3] = new Vector3(0, 50, 0),
-                [4] = new Vector3(0, 0, 0),
-            };
-
-            var animation = model.CreateAnimation("Animation");
-            animation.CreateTranslationChannel(rnode, keyframes);
-            
-
             model.AttachToCurrentTest("result.glb");
             model.AttachToCurrentTest("result.glb");
             model.AttachToCurrentTest("result.gltf");
             model.AttachToCurrentTest("result.gltf");
         }
         }