vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
467 stars 75 forks source link

How to pass indices to a Mesh Primitive #64

Closed Crauzer closed 4 years ago

Crauzer commented 4 years ago

I'm converting a model to the glTF format but I can't seem to find how to pass vertex indices to a PrimitiveBuilder. The AddXXXXX functions return the indices of the added triangle/point/w.e which seems like a very weird design choice to me.

vpenades commented 4 years ago

You don't, Indices are generated internally by Mesh and Primitive builders.

Given you have sourceVertices[] and sourceIndices[], you just do:

var a = sourceVertices[ sourceIndices[ idx0 ] ];
var b = sourceVertices[ sourceIndices[ idx1 ] ];
var c = sourceVertices[ sourceIndices[ idx2 ] ];
Builder.AddTriangle( a, b, c);

And the builder handles creating an internal collection of unique vertices and the corresponding index list.

Crauzer commented 4 years ago

Thanks for the help, I managed to convert the model geometry properly. 👍🏼