Browse Source

Added more VertexAccessor creation utilities

Vicente Penades 6 năm trước cách đây
mục cha
commit
6a5ec1ab32

+ 1 - 23
src/SharpGLTF.Toolkit/Geometry/InterleavedMeshBuilder.cs

@@ -79,30 +79,8 @@ namespace SharpGLTF.Geometry
         {
             var root = dstMesh.LogicalParent;
 
-            // get vertex attributes from TVertex type using reflection
-            var attributes = VertexUtils.GetVertexAttributes(typeof(TVertex), _Vertices.Count);
-
-            // create vertex buffer
-            int byteStride = attributes[0].ByteStride;
-            var vbytes = new Byte[byteStride * _Vertices.Count];
-
-            var vbuffer = root.UseBufferView(new ArraySegment<byte>(vbytes), byteStride, BufferMode.ARRAY_BUFFER);
-
             // create vertex accessors
-            var vertexAccessors = new Dictionary<String, Accessor>();
-
-            foreach (var attribute in attributes)
-            {
-                var accessor = root.CreateAccessor(attribute.Name);
-
-                var field = VertexUtils.GetVertexField(typeof(TVertex), attribute.Name);
-
-                if (field.FieldType == typeof(Vector2)) accessor.SetVertexData(vbuffer, attribute.ByteOffset, field.GetVector2Column(_Vertices), attribute.Encoding, attribute.Normalized);
-                if (field.FieldType == typeof(Vector3)) accessor.SetVertexData(vbuffer, attribute.ByteOffset, field.GetVector3Column(_Vertices), attribute.Encoding, attribute.Normalized);
-                if (field.FieldType == typeof(Vector4)) accessor.SetVertexData(vbuffer, attribute.ByteOffset, field.GetVector4Column(_Vertices), attribute.Encoding, attribute.Normalized);
-
-                vertexAccessors[attribute.Name] = accessor;
-            }
+            var vertexAccessors = root.CreateInterleavedVertexAccessors(_Vertices);
 
             foreach (var kvp in _Indices)
             {

+ 32 - 0
src/SharpGLTF.Toolkit/Schema2/AccessorExtensions.cs

@@ -1,10 +1,42 @@
 using System;
 using System.Collections.Generic;
 using System.Text;
+using System.Numerics;
 
 namespace SharpGLTF.Schema2
 {
+    using SharpGLTF.Geometry.VertexTypes;
+
     public static partial class Toolkit
     {
+        public static IReadOnlyDictionary<string, Accessor> CreateInterleavedVertexAccessors<TVertex>(this ModelRoot root, IReadOnlyList<TVertex> vertices)
+        {
+            // get vertex attributes from TVertex type using reflection
+            var attributes = VertexUtils.GetVertexAttributes(typeof(TVertex), vertices.Count);
+
+            // create vertex buffer
+            int byteStride = attributes[0].ByteStride;
+            var vbytes = new Byte[byteStride * vertices.Count];
+
+            var vbuffer = root.UseBufferView(new ArraySegment<byte>(vbytes), byteStride, BufferMode.ARRAY_BUFFER);
+
+            // create vertex accessors
+            var vertexAccessors = new Dictionary<String, Accessor>();
+
+            foreach (var attribute in attributes)
+            {
+                var accessor = root.CreateAccessor(attribute.Name);
+
+                var field = VertexUtils.GetVertexField(typeof(TVertex), attribute.Name);
+
+                if (field.FieldType == typeof(Vector2)) accessor.SetVertexData(vbuffer, attribute.ByteOffset, field.GetVector2Column(vertices), attribute.Encoding, attribute.Normalized);
+                if (field.FieldType == typeof(Vector3)) accessor.SetVertexData(vbuffer, attribute.ByteOffset, field.GetVector3Column(vertices), attribute.Encoding, attribute.Normalized);
+                if (field.FieldType == typeof(Vector4)) accessor.SetVertexData(vbuffer, attribute.ByteOffset, field.GetVector4Column(vertices), attribute.Encoding, attribute.Normalized);
+
+                vertexAccessors[attribute.Name] = accessor;
+            }
+
+            return vertexAccessors;
+        }
     }
 }

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

@@ -9,6 +9,15 @@ namespace SharpGLTF.Schema2
 
     public static partial class Toolkit
     {
+        public static MeshPrimitive WithVertexAccessors<TVertex>(this MeshPrimitive primitive, IReadOnlyList<TVertex> vertices)
+        {
+            var accessors = primitive.LogicalParent.LogicalParent.CreateInterleavedVertexAccessors(vertices);
+
+            foreach (var va in accessors) primitive.SetVertexAccessor(va.Key, va.Value);
+
+            return primitive;
+        }
+
         public static MeshPrimitive WithVertexAccessor(this MeshPrimitive primitive, string attribute, IReadOnlyList<Vector2> values)
         {
             var root = primitive.LogicalParent.LogicalParent;

+ 6 - 0
src/SharpGLTF.Toolkit/Schema2/SceneExtensions.cs

@@ -33,5 +33,11 @@ namespace SharpGLTF.Schema2
 
             return node;
         }
+
+        public static Node WithMesh(this Node node, Mesh mesh)
+        {
+            node.Mesh = mesh;
+            return node;
+        }
     }
 }

+ 30 - 3
tests/SharpGLTF.Tests/Schema2/Authoring/CreateModelTests.cs

@@ -210,7 +210,35 @@ namespace SharpGLTF.Schema2.Authoring
         }
 
 
-        
+        [Test(Description = "Creates an interleaved scene using a toolkit utilities")]
+        public void CreateInterleavedQuadScene()
+        {
+            TestContext.CurrentContext.AttachShowDirLink();
+            TestContext.CurrentContext.AttachGltfValidatorLink();
+
+            var vertices = new[]
+            {
+                new STATICVERTEX(-10,  10, 0, -10,  10, 15),
+                new STATICVERTEX( 10,  10, 0,  10,  10, 15),
+                new STATICVERTEX( 10, -10, 0,  10, -10, 15),
+                new STATICVERTEX(-10, -10, 0, -10, -10, 15)
+            };
+
+            var model = ModelRoot.CreateModel();
+
+            var mesh = model.CreateMesh("mesh1");
+
+            mesh.CreatePrimitive()
+                .WithMaterial(model.CreateMaterial("Default").WithDefault(Vector4.One))
+                .WithVertexAccessors(vertices)
+                .WithIndicesAccessor(PrimitiveType.TRIANGLES, new int[] { 0, 1, 2, 0, 2, 3 });
+
+            var scene = model.UseScene("Default");
+            var rnode = scene.CreateNode("RootNode").WithMesh(mesh);
+
+            model.AttachToCurrentTest("result.glb");
+            model.AttachToCurrentTest("result.gltf");
+        }
 
         [Test(Description = "Creates an interleaved scene using a mesh builder helper class")]
         public void CreateInterleavedMeshBuilderScene()
@@ -262,8 +290,7 @@ namespace SharpGLTF.Schema2.Authoring
 
             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);
+            var rnode = scene.CreateNode("RootNode").WithTranslationAnimation("track1", keyframes);            
 
             // create mesh
             var meshBuilder = new InterleavedMeshBuilder<STATICVERTEX, Vector4>();