rearrange function order in importer

main
Evan Hemsley 2020-07-29 00:33:12 -07:00
parent e670bba1a6
commit c5ecda6b7d
1 changed files with 78 additions and 74 deletions

View File

@ -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<Mesh>();
foreach (var mesh in sharpModel.LogicalMeshes)
{
var meshParts = new List<MeshPart>();
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<Mesh>();
foreach (var mesh in sharpModel.LogicalMeshes)
{
var meshParts = new List<MeshPart>();
foreach (var primitive in mesh.Primitives)
{
meshParts.Add(
ImportMeshPart(graphicsDevice, vertexType, primitive)
);
}
meshes.Add(new Mesh(meshParts.ToArray()));
}
return new Model(meshes.ToArray());
}
}
}