enable reading texture from GLB

main
Evan Hemsley 2020-07-29 01:35:11 -07:00
parent c5ecda6b7d
commit 05bc0341a8
2 changed files with 32 additions and 3 deletions

View File

@ -30,7 +30,17 @@ namespace Smuggler
meshes.Add(new Mesh(meshParts.ToArray())); 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) private static System.Type InferVertexType(SharpGLTF.Schema2.ModelRoot model)
@ -135,17 +145,23 @@ namespace Smuggler
ImportVertexPositionTexture(primitive, vertexBuffer, indices, positions); 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( return new MeshPart(
vertexBuffer, vertexBuffer,
indexBuffer, indexBuffer,
positions, positions,
triangles, triangles,
new BasicEffect(graphicsDevice) effect
); );
} }
/* Attribute Getters */ /* Attribute Getters */
private static Vector3[] Positions(SharpGLTF.Schema2.MeshPrimitive primitive) private static Vector3[] Positions(SharpGLTF.Schema2.MeshPrimitive primitive)
{ {
var positionAccessor = primitive.GetVertexAccessor("POSITION").AsVector3Array(); var positionAccessor = primitive.GetVertexAccessor("POSITION").AsVector3Array();

View File

@ -6,12 +6,25 @@ namespace Smuggler
public class Model public class Model
{ {
public Mesh[] Meshes { get; } public Mesh[] Meshes { get; }
public Texture2D[] Textures { get; }
public Model(Mesh[] meshes) public Model(Mesh[] meshes)
{ {
Meshes = 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) public void Draw(GraphicsDevice graphicsDevice, Matrix world, Matrix view, Matrix projection)
{ {
foreach (var mesh in Meshes) foreach (var mesh in Meshes)