Three.js - Bake skeleton transform into geometry

2019-06-13 19:10发布

What is the proper way of applying bone matrices to vertices so that the actual geometry represents the transformed shape without using GPU?

I have a character model I am applying a skeleton to that I want to 3D print in different poses, so I want to use the STLExporter to export a 3D printable file, but it only exports the original geometry.

If I can scene.traverse() over each SkinnedMesh and geometry.applyMatrix(???) with the correct bone matrix in the right order, I should be able to export an STL with the transform applied, right?

var objects = [];
var triangles = 0;
scene.traverse( function ( object ) {
    if ( object.isMesh ) {
        var geometry = object.geometry;
        if ( geometry.isBufferGeometry ) {
            geometry = new THREE.Geometry().fromBufferGeometry( geometry );
        }
        // TODO: Apply skeleton transform
        if ( geometry.isGeometry ) {
            triangles += geometry.faces.length;
            objects.push( {
                geometry: geometry,
                matrixWorld: object.matrixWorld
            } );
        }
    }
} );

标签: three.js 3d
1条回答
一夜七次
2楼-- · 2019-06-13 20:11

have a look at this pull request: https://github.com/mrdoob/three.js/pull/8953/files

Especially this part:

THREE.SkinnedMesh.prototype.boneTransform = 
...snip...
for ( var i = 0; i < 4; i ++ ) {
+
+           var skinWeight = skinWeights[ properties[ i ] ];
+
+           if ( skinWeight != 0 ) {
+
+               var boneIndex = skinIndices[ properties[ i ] ];
+               tempMatrix.multiplyMatrices( this.skeleton.bones[ boneIndex ].matrixWorld, this.skeleton.boneInverses[ boneIndex ] );
+               result.add( temp.copy( clone ).applyMatrix4( tempMatrix ).multiplyScalar( skinWeight ) );
+
+           }
+
+       }

So to bake the deformations you have to do the following steps:

  1. Go over all vertices
  2. Get their corresponding skinIndices and skinWeights
  3. Get the associated bones for each vertex and their weights
  4. Go over all bones
  5. Get the transform matrix of the bone and translate it to the coordinate system of the Geometry
  6. Weight the matrix via the associated skinWeights[boneIdx]
  7. Apply the final matrix
查看更多
登录 后发表回答