Smuggler/Importer.cs

462 lines
15 KiB
C#
Raw 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
{
2020-07-29 07:33:12 +00:00
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()));
}
2020-07-29 08:35:11 +00:00
var model = new Model(meshes.ToArray());
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
2020-07-29 07:33:12 +00:00
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);
}
2020-07-31 08:55:29 +00:00
var effect = new PBREffect(graphicsDevice);
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)
{
effect.NormalTexture = Texture2D.FromStream(
graphicsDevice,
normalChannel.Value.Texture.PrimaryImage.Content.Open()
);
}
}
//var occlusionChannel = primitive.Material.FindChannel("Occlusion");
//if (occlusionChannel.HasValue)
//{
// if (occlusionChannel.Value.Texture != null)
// {
// effect.OcclusionTexture = Texture2D.FromStream(
// graphicsDevice,
// occlusionChannel.Value.Texture.PrimaryImage.Content.Open()
// );
// }
// effect.OcclusionStrength = occlusionChannel.Value.Parameter.X;
//}
//var emissiveChannel = primitive.Material.FindChannel("Emissive");
//if (emissiveChannel.HasValue)
//{
// if (emissiveChannel.Value.Texture != null)
// {
// effect.EmissionTexture = Texture2D.FromStream(
// graphicsDevice,
// emissiveChannel.Value.Texture.PrimaryImage.Content.Open()
// );
// }
// var parameter = emissiveChannel.Value.Parameter;
// effect.EmissiveFactor = new Vector3(
// parameter.X,
// parameter.Y,
// parameter.Z
// );
//}
var albedoChannel = primitive.Material.FindChannel("BaseColor");
if (albedoChannel.HasValue)
2020-07-31 08:55:29 +00:00
{
if (albedoChannel.Value.Texture != null)
{
effect.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
effect.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)
{
effect.MetallicRoughnessTexture = Texture2D.FromStream(
graphicsDevice,
metallicRoughnessChannel.Value.Texture.PrimaryImage.Content.Open()
);
}
2020-07-31 08:55:29 +00:00
var parameter = metallicRoughnessChannel.Value.Parameter;
effect.Metallic = parameter.X;
effect.Roughness = parameter.Y;
2020-07-31 08:55:29 +00:00
}
}
effect.Albedo = new Vector3(0.5f, 0, 0);
effect.AO = 1f;
effect.Lights[0] = new PBRLight(
2020-08-03 23:27:59 +00:00
new Vector3(-10f, 10f, -5f),
new Vector3(300f, 300f, 300f)
);
2020-08-03 23:27:59 +00:00
effect.Lights[1] = new PBRLight(
new Vector3(10f, 10f, 5f),
new Vector3(300f, 300f, 300f)
);
2020-08-03 23:27:59 +00:00
effect.Lights[2] = new PBRLight(
new Vector3(-10f, -10f, 5f),
new Vector3(300f, 300f, 300f)
);
2020-08-03 23:27:59 +00:00
effect.Lights[3] = new PBRLight(
new Vector3(10f, -10f, 5f),
new Vector3(300f, 300f, 300f)
);
2020-07-29 08:35:11 +00:00
2020-07-31 08:55:29 +00:00
/* FIXME: how to load cube maps from GLTF? */
/*
var diffuseChannel = primitive.Material.FindChannel("Diffuse");
if (diffuseChannel.HasValue)
{
}
var specularChannel = primitive.Material.FindChannel("SpecularGlossiness");
if (specularChannel.HasValue)
{
}
*/
2020-07-29 08:35:11 +00:00
2020-07-29 07:33:12 +00:00
return new MeshPart(
vertexBuffer,
indexBuffer,
positions,
triangles,
2020-07-29 08:35:11 +00:00
effect
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
}
}