From c5ecda6b7d195fbd9ecbb25d935f486a24a7dbc2 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Wed, 29 Jul 2020 00:33:12 -0700 Subject: [PATCH] rearrange function order in importer --- Importer.cs | 152 +++++++++++++++++++++++++++------------------------- 1 file changed, 78 insertions(+), 74 deletions(-) diff --git a/Importer.cs b/Importer.cs index 0a36596..394b3e1 100644 --- a/Importer.cs +++ b/Importer.cs @@ -7,6 +7,32 @@ namespace Smuggler { public static class Importer { + public static Model ImportGLB(GraphicsDevice graphicsDevice, Stream stream) + { + var sharpModel = SharpGLTF.Schema2.ModelRoot.ReadGLB(stream); + var vertexType = InferVertexType(sharpModel); + + if (vertexType == null) { return null; } + + var meshes = new List(); + + foreach (var mesh in sharpModel.LogicalMeshes) + { + var meshParts = new List(); + + foreach (var primitive in mesh.Primitives) + { + meshParts.Add( + ImportMeshPart(graphicsDevice, vertexType, primitive) + ); + } + + meshes.Add(new Mesh(meshParts.ToArray())); + } + + return new Model(meshes.ToArray()); + } + private static System.Type InferVertexType(SharpGLTF.Schema2.ModelRoot model) { bool hasColor = false; @@ -70,6 +96,56 @@ namespace Smuggler return null; } + private static MeshPart ImportMeshPart(GraphicsDevice graphicsDevice, System.Type vertexType, SharpGLTF.Schema2.MeshPrimitive primitive) + { + var indices = Indices(primitive); + var positions = Positions(primitive); + var triangles = Triangles(primitive); + + var vertexBuffer = new VertexBuffer( + graphicsDevice, + vertexType, + positions.Length, + BufferUsage.None + ); + + var indexBuffer = new IndexBuffer( + graphicsDevice, + IndexElementSize.ThirtyTwoBits, + indices.Length, + BufferUsage.None + ); + + indexBuffer.SetData(indices); + + if (vertexType == typeof(VertexPositionColor)) + { + ImportVertexPositionColor(primitive, vertexBuffer, indices, positions); + } + else if (vertexType == typeof(VertexPositionColorTexture)) + { + ImportVertexPositionColorTexture(primitive, vertexBuffer, indices, positions); + } + else if (vertexType == typeof(VertexPositionNormalTexture)) + { + ImportVertexPositionNormalTexture(primitive, vertexBuffer, indices, positions); + } + else if (vertexType == typeof(VertexPositionTexture)) + { + ImportVertexPositionTexture(primitive, vertexBuffer, indices, positions); + } + + return new MeshPart( + vertexBuffer, + indexBuffer, + positions, + triangles, + new BasicEffect(graphicsDevice) + ); + } + + /* Attribute Getters */ + private static Vector3[] Positions(SharpGLTF.Schema2.MeshPrimitive primitive) { var positionAccessor = primitive.GetVertexAccessor("POSITION").AsVector3Array(); @@ -151,6 +227,8 @@ namespace Smuggler return indices; } + /* Vertex Builders */ + private static void ImportVertexPositionColor( SharpGLTF.Schema2.MeshPrimitive primitive, VertexBuffer vertexBuffer, @@ -252,79 +330,5 @@ namespace Smuggler vertexBuffer.SetData(vertices); } - - private static MeshPart ImportMeshPart(GraphicsDevice graphicsDevice, System.Type vertexType, SharpGLTF.Schema2.MeshPrimitive primitive) - { - var indices = Indices(primitive); - var positions = Positions(primitive); - var triangles = Triangles(primitive); - - var vertexBuffer = new VertexBuffer( - graphicsDevice, - vertexType, - positions.Length, - BufferUsage.None - ); - - var indexBuffer = new IndexBuffer( - graphicsDevice, - IndexElementSize.ThirtyTwoBits, - indices.Length, - BufferUsage.None - ); - - indexBuffer.SetData(indices); - - if (vertexType == typeof(VertexPositionColor)) - { - ImportVertexPositionColor(primitive, vertexBuffer, indices, positions); - } - else if (vertexType == typeof(VertexPositionColorTexture)) - { - ImportVertexPositionColorTexture(primitive, vertexBuffer, indices, positions); - } - else if (vertexType == typeof(VertexPositionNormalTexture)) - { - ImportVertexPositionNormalTexture(primitive, vertexBuffer, indices, positions); - } - else if (vertexType == typeof(VertexPositionTexture)) - { - ImportVertexPositionTexture(primitive, vertexBuffer, indices, positions); - } - - return new MeshPart( - vertexBuffer, - indexBuffer, - positions, - triangles, - new BasicEffect(graphicsDevice) - ); - } - - public static Model ImportGLB(GraphicsDevice graphicsDevice, Stream stream) - { - var sharpModel = SharpGLTF.Schema2.ModelRoot.ReadGLB(stream); - var vertexType = InferVertexType(sharpModel); - - if (vertexType == null) { return null; } - - var meshes = new List(); - - foreach (var mesh in sharpModel.LogicalMeshes) - { - var meshParts = new List(); - - foreach (var primitive in mesh.Primitives) - { - meshParts.Add( - ImportMeshPart(graphicsDevice, vertexType, primitive) - ); - } - - meshes.Add(new Mesh(meshParts.ToArray())); - } - - return new Model(meshes.ToArray()); - } } }