vpenades / SharpGLTF

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

Adding custom vertex attributes to existing model? #58

Closed bertt closed 3 years ago

bertt commented 4 years ago

Hi, I know glTF is not intended for editing existing models, but maybe its doable to:

Is there a way to do this?

vpenades commented 4 years ago

Assuming you convert the source glTF to a SceneBuilder, and you traverse the instances identifying individual MeshBuilders...

The process of converting one MeshBuilder to another one with a different vertex type is this:

// pseudo code
var src = MeshBuilder<srcVertexType>();
var dst = MeshBuilder<dstVertexType>();
dst.AddMesh(src, materialTransformFunction, vertexTransformFunction);

So you create a new MeshBuilder with the destination vertex type, and you add the source mesh to the destination mesh with the AddMesh method, which provides callbacks to modify the vertices and the materials.

If you need something more specific when converting the vertices you can try to follow the source code of the AddMesh method. It's a bit more tricky than it should be because it also takes into account morph targets.

vpenades commented 4 years ago

Sorry, Just had time to review the API, and AddMesh does NOT have an API to add a mesh from a different mesh type, it requires both the source and destination vertex types to be the same.

But the procedure is still valid; changing the vertex type boils down to copying the contents of one mesh to another, and transforming the vertices during the process.

I'll look into adding helpers to do so... but it'll take a while...

bertt commented 4 years ago

ok thanks, I'll wait till there is support for changing vertex types

vpenades commented 3 years ago

A while ago, I've added this method to the MeshBuilder API:

void AddMesh<TSourceMaterial>
    ( IMeshBuilder<TSourceMaterial> mesh
    , Func<TSourceMaterial, TMaterial> materialTransform
    , Converter<IVertexBuilder, VertexBuilder<TvG, TvM, TvS>> vertexTransform
    )

It allows adding one mesh to another mesh, with different material and vertex definitions, assuming you provide the appropiate conversion functions.

bertt commented 3 years ago

Ok! Will try it next week.