Smuggler/Importer.cs

394 lines
12 KiB
C#
Raw Permalink Normal View History

2020-07-28 09:08:13 +00:00
using System.Collections.Generic;
using System.IO;
2020-07-28 09:36:20 +00:00
using Microsoft.Xna.Framework;
2020-07-28 09:08:13 +00:00
using Microsoft.Xna.Framework.Graphics;
namespace Smuggler
{
public static class Importer
{
public static ModelData ImportGLB(GraphicsDevice graphicsDevice, Stream stream)
2020-07-29 07:33:12 +00:00
{
var sharpModel = SharpGLTF.Schema2.ModelRoot.ReadGLB(stream);
var vertexType = InferVertexType(sharpModel);
if (vertexType == null) { return null; }
var meshes = new List<MeshData>();
2020-07-29 07:33:12 +00:00
foreach (var mesh in sharpModel.LogicalMeshes)
{
var meshParts = new List<MeshPartData>();
2020-07-29 07:33:12 +00:00
foreach (var primitive in mesh.Primitives)
{
meshParts.Add(
ImportMeshPart(graphicsDevice, vertexType, primitive)
);
}
meshes.Add(new MeshData(meshParts.ToArray()));
2020-07-29 07:33:12 +00:00
}
var model = new ModelData(meshes.ToArray());
2020-07-29 08:35:11 +00:00
return model;
2020-07-29 07:33:12 +00:00
}
private static System.Type InferVertexType(SharpGLTF.Schema2.ModelRoot model)
2020-07-28 09:08:13 +00:00
{
bool hasColor = false;
bool hasNormal = false;
bool hasTexture = false;
2020-07-28 09:08:13 +00:00
if (model.LogicalMeshes.Count == 0) { return null; }
if (model.LogicalMeshes[0].Primitives.Count == 0) { return null; }
2020-07-28 09:08:13 +00:00
var samplePrimitive = model.LogicalMeshes[0].Primitives[0];
if (samplePrimitive.GetVertexAccessor("POSITION") == null)
2020-07-28 09:08:13 +00:00
{
System.Console.WriteLine("There is no Position attribute on this model's vertices. That seems weird!");
return null;
}
2020-07-28 09:08:13 +00:00
if (samplePrimitive.GetVertexAccessor("COLOR") != null)
{
hasColor = true;
}
2020-07-28 09:08:13 +00:00
if (samplePrimitive.GetVertexAccessor("NORMAL") != null)
{
hasNormal = true;
}
if (samplePrimitive.GetVertexAccessor("TEXCOORD_0") != null)
{
hasTexture = true;
}
if (hasColor && hasNormal && hasTexture)
{
// FIXME: Implement VertexPositionColorNormalTexture
return null;
}
if (hasColor && hasTexture)
{
return typeof(VertexPositionColorTexture);
}
if (hasNormal && hasTexture)
{
return typeof(VertexPositionNormalTexture);
}
if (hasColor)
{
return typeof(VertexPositionColor);
}
if (hasTexture)
{
return typeof(VertexPositionTexture);
}
System.Console.WriteLine("Could not infer vertex type");
2020-07-28 09:08:13 +00:00
return null;
}
2020-07-28 09:08:13 +00:00
private static MeshPartData ImportMeshPart(GraphicsDevice graphicsDevice, System.Type vertexType, SharpGLTF.Schema2.MeshPrimitive primitive)
2020-07-29 07:33:12 +00:00
{
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);
}
var meshPartData = new MeshPartData(
vertexBuffer,
indexBuffer,
positions,
triangles
);
2020-07-31 08:55:29 +00:00
if (primitive.Material != null)
{
2020-08-03 23:47:08 +00:00
var normalChannel = primitive.Material.FindChannel("Normal");
if (normalChannel.HasValue)
{
if (normalChannel.Value.Texture != null)
{
meshPartData.NormalTexture = Texture2D.FromStream(
graphicsDevice,
normalChannel.Value.Texture.PrimaryImage.Content.Open()
);
}
2020-08-03 23:47:08 +00:00
}
var albedoChannel = primitive.Material.FindChannel("BaseColor");
if (albedoChannel.HasValue)
2020-07-31 08:55:29 +00:00
{
if (albedoChannel.Value.Texture != null)
{
meshPartData.AlbedoTexture = Texture2D.FromStream(
graphicsDevice,
albedoChannel.Value.Texture.PrimaryImage.Content.Open()
);
}
2020-07-31 08:55:29 +00:00
var parameter = albedoChannel.Value.Parameter;
2020-07-31 08:55:29 +00:00
meshPartData.Albedo = new Vector3(
2020-07-31 08:55:29 +00:00
parameter.X,
parameter.Y,
parameter.Z
);
}
var metallicRoughnessChannel = primitive.Material.FindChannel("MetallicRoughness");
if (metallicRoughnessChannel.HasValue)
{
if (metallicRoughnessChannel.Value.Texture != null)
{
meshPartData.MetallicRoughnessTexture = Texture2D.FromStream(
graphicsDevice,
metallicRoughnessChannel.Value.Texture.PrimaryImage.Content.Open()
);
}
2020-07-31 08:55:29 +00:00
var parameter = metallicRoughnessChannel.Value.Parameter;
meshPartData.Metallic = parameter.X;
meshPartData.Roughness = parameter.Y;
2020-07-31 08:55:29 +00:00
}
}
return meshPartData;
2020-07-29 07:33:12 +00:00
}
/* Attribute Getters */
2020-07-29 08:35:11 +00:00
private static Vector3[] Positions(SharpGLTF.Schema2.MeshPrimitive primitive)
{
var positionAccessor = primitive.GetVertexAccessor("POSITION").AsVector3Array();
var positions = new Vector3[positionAccessor.Count];
2020-07-28 09:08:13 +00:00
for (int i = 0; i < positions.Length; i++)
{
var position = positionAccessor[i];
positions[i] = new Vector3(-position.X, -position.Z, position.Y);
}
2020-07-28 09:08:13 +00:00
return positions;
}
private static Color[] Colors(SharpGLTF.Schema2.MeshPrimitive primitive)
{
var colorAccessor = primitive.GetVertexAccessor("COLOR").AsColorArray();
var colors = new Color[colorAccessor.Count];
for (int i = 0; i < colors.Length; i++)
{
var color = colorAccessor[i];
colors[i] = new Color(color.X, color.Y, color.Z, color.W);
}
return colors;
}
private static Vector3[] Normals(SharpGLTF.Schema2.MeshPrimitive primitive)
{
var normalAccessor = primitive.GetVertexAccessor("NORMAL").AsVector3Array();
var normals = new Vector3[normalAccessor.Count];
for (int i = 0; i < normals.Length; i++)
{
var normal = normalAccessor[i];
2020-08-03 23:27:59 +00:00
normals[i] = new Vector3(-normal.X, -normal.Z, normal.Y);
}
2020-07-28 09:08:13 +00:00
return normals;
}
private static Vector2[] TexCoords(SharpGLTF.Schema2.MeshPrimitive primitive)
{
var texcoordAccessor = primitive.GetVertexAccessor("TEXCOORD_0").AsVector2Array();
var texcoords = new Vector2[texcoordAccessor.Count];
for (int i = 0; i < texcoords.Length; i++)
{
var texcoord = texcoordAccessor[i];
texcoords[i] = new Vector2(texcoord.X, texcoord.Y);
}
return texcoords;
}
private static Triangle[] Triangles(SharpGLTF.Schema2.MeshPrimitive primitive)
{
var triangles = new List<Triangle>();
foreach (var (a, b, c) in primitive.GetTriangleIndices())
{
triangles.Add(new Triangle(a, b, c));
}
return triangles.ToArray();
}
private static uint[] Indices(SharpGLTF.Schema2.MeshPrimitive primitive)
{
var indexAccessor = primitive.GetIndexAccessor().AsIndicesArray();
var indices = new uint[indexAccessor.Count];
2020-07-28 09:08:13 +00:00
for (int i = 0; i < indices.Length; i++)
{
indices[i] = indexAccessor[i];
}
return indices;
}
2020-07-29 07:33:12 +00:00
/* Vertex Builders */
private static void ImportVertexPositionColor(
SharpGLTF.Schema2.MeshPrimitive primitive,
VertexBuffer vertexBuffer,
uint[] indices,
Vector3[] positions
)
{
var colors = Colors(primitive);
var vertices = new VertexPositionColor[positions.Length];
foreach (var index in indices)
{
var position = positions[index];
var color = colors[index];
vertices[index] = new VertexPositionColor(
position,
color
);
}
vertexBuffer.SetData(vertices);
}
private static void ImportVertexPositionColorTexture(
SharpGLTF.Schema2.MeshPrimitive primitive,
VertexBuffer vertexBuffer,
uint[] indices,
Vector3[] positions
)
{
var colors = Colors(primitive);
var texcoords = TexCoords(primitive);
var vertices = new VertexPositionColorTexture[positions.Length];
foreach (var index in indices)
{
var position = positions[index];
var color = colors[index];
var texcoord = texcoords[index];
vertices[index] = new VertexPositionColorTexture(
position,
color,
texcoord
);
}
vertexBuffer.SetData(vertices);
}
private static void ImportVertexPositionNormalTexture(
SharpGLTF.Schema2.MeshPrimitive primitive,
VertexBuffer vertexBuffer,
uint[] indices,
Vector3[] positions
)
{
var normals = Normals(primitive);
var texcoords = TexCoords(primitive);
var vertices = new VertexPositionNormalTexture[positions.Length];
foreach (var index in indices)
{
var position = positions[index];
var normal = normals[index];
var texcoord = texcoords[index];
vertices[index] = new VertexPositionNormalTexture(
position,
normal,
texcoord
);
}
vertexBuffer.SetData(vertices);
}
private static void ImportVertexPositionTexture(
SharpGLTF.Schema2.MeshPrimitive primitive,
VertexBuffer vertexBuffer,
uint[] indices,
Vector3[] positions
)
{
var texcoords = TexCoords(primitive);
var vertices = new VertexPositionTexture[positions.Length];
foreach (var index in indices)
{
var position = positions[index];
var texcoord = texcoords[index];
vertices[index] = new VertexPositionTexture(
position,
texcoord
);
}
vertexBuffer.SetData(vertices);
}
2020-07-28 09:08:13 +00:00
}
}