From 05bc0341a8386149e09ed0b849c6ef40d53d9076 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Wed, 29 Jul 2020 01:35:11 -0700 Subject: [PATCH] enable reading texture from GLB --- Importer.cs | 22 +++++++++++++++++++--- Model.cs | 13 +++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Importer.cs b/Importer.cs index 394b3e1..1369802 100644 --- a/Importer.cs +++ b/Importer.cs @@ -30,7 +30,17 @@ namespace Smuggler meshes.Add(new Mesh(meshParts.ToArray())); } - return new Model(meshes.ToArray()); + var model = new Model(meshes.ToArray()); + + /* TODO: BasicEffect only supports one texture but GLTF supports several */ + + if (sharpModel.LogicalTextures.Count > 0) + { + var fnaTexture = Texture2D.FromStream(graphicsDevice, sharpModel.LogicalTextures[0].PrimaryImage.Content.Open()); + model.SetTexture(fnaTexture); + } + + return model; } private static System.Type InferVertexType(SharpGLTF.Schema2.ModelRoot model) @@ -135,17 +145,23 @@ namespace Smuggler ImportVertexPositionTexture(primitive, vertexBuffer, indices, positions); } + /* TODO: We need a new Effect subclass to support some GLTF features */ + + var effect = new BasicEffect(graphicsDevice); + effect.EnableDefaultLighting(); + effect.PreferPerPixelLighting = true; + return new MeshPart( vertexBuffer, indexBuffer, positions, triangles, - new BasicEffect(graphicsDevice) + effect ); } /* Attribute Getters */ - + private static Vector3[] Positions(SharpGLTF.Schema2.MeshPrimitive primitive) { var positionAccessor = primitive.GetVertexAccessor("POSITION").AsVector3Array(); diff --git a/Model.cs b/Model.cs index abdbf3e..ed3ec25 100644 --- a/Model.cs +++ b/Model.cs @@ -6,12 +6,25 @@ namespace Smuggler public class Model { public Mesh[] Meshes { get; } + public Texture2D[] Textures { get; } public Model(Mesh[] meshes) { Meshes = meshes; } + public void SetTexture(Texture2D texture) + { + foreach (var mesh in Meshes) + { + foreach (var meshPart in mesh.MeshParts) + { + meshPart.Effect.TextureEnabled = true; + meshPart.Effect.Texture = texture; + } + } + } + public void Draw(GraphicsDevice graphicsDevice, Matrix world, Matrix view, Matrix projection) { foreach (var mesh in Meshes)