Three.js:使用骨骼动画获取更新的顶点?

nic*_*c_m 3 graphics animation three.js

与此堆栈溢出问题Three.js:使用变形目标获取更新的顶点中的问题类似,我对如何使用骨架动画获取网格顶点的“实际”位置感兴趣。

我尝试打印出位置值,但它们从未真正更新(据我所知,这是因为它们是在 GPU 上计算的,而不是在 CPU 上计算的)。上述问题的答案提到在 CPU 上执行与 GPU 上相同的计算,以获得变形目标动画的最新顶点位置,但是有没有办法对骨骼动画执行相同的方法?如果是这样,怎么办??

另外,对于变形目标,有人指出该代码已经存在于 Mesh.raycast 函数中(https://github.com/mrdoob/ Three.js/blob/master/src/objects/Mesh.js#L115 )。但是,我不明白光线投射如何与骨骼动画网格体一起工作——它如何知道面部的更新位置?

谢谢你!

Mug*_*n87 5

前段时间在Three.js 论坛上讨论过类似的话题。我在那里提出了一个小提琴,它计算每帧蒙皮网格的 AABB。该代码实际上通过 JavaScript 执行与顶点着色器中相同的顶点位移。例程看起来像这样:

function updateAABB( skinnedMesh, aabb ) {

    var skeleton = skinnedMesh.skeleton;
    var boneMatrices = skeleton.boneMatrices;
    var geometry = skinnedMesh.geometry;

    var index = geometry.index;
    var position = geometry.attributes.position;
    var skinIndex = geometry.attributes.skinIndex;
    var skinWeigth = geometry.attributes.skinWeight;

    var bindMatrix = skinnedMesh.bindMatrix;
    var bindMatrixInverse = skinnedMesh.bindMatrixInverse;

    var i, j, si, sw;

    aabb.makeEmpty();

    // 

    if ( index !== null ) {

        // indexed geometry

        for ( i = 0; i < index.count; i ++ ) {

            vertex.fromBufferAttribute( position, index[ i ] );
            skinIndices.fromBufferAttribute( skinIndex, index[ i ] );
            skinWeights.fromBufferAttribute( skinWeigth, index[ i ] );

            // the following code section is normally implemented in the vertex shader

            vertex.applyMatrix4( bindMatrix ); // transform to bind space
            skinned.set( 0, 0, 0 );

            for ( j = 0; j < 4; j ++ ) {

                si = skinIndices.getComponent( j );
                sw = skinWeights.getComponent( j );
                boneMatrix.fromArray( boneMatrices, si * 16 );

                // weighted vertex transformation

                temp.copy( vertex ).applyMatrix4( boneMatrix ).multiplyScalar( sw );
                skinned.add( temp );

            }

            skinned.applyMatrix4( bindMatrixInverse ); // back to local space

            // expand aabb

            aabb.expandByPoint( skinned );

        }

    } else {

        // non-indexed geometry

        for ( i = 0; i < position.count; i ++ ) {

            vertex.fromBufferAttribute( position, i );
            skinIndices.fromBufferAttribute( skinIndex, i );
            skinWeights.fromBufferAttribute( skinWeigth, i );

            // the following code section is normally implemented in the vertex shader

            vertex.applyMatrix4( bindMatrix ); // transform to bind space
            skinned.set( 0, 0, 0 );

            for ( j = 0; j < 4; j ++ ) {

                si = skinIndices.getComponent( j );
                sw = skinWeights.getComponent( j );
                boneMatrix.fromArray( boneMatrices, si * 16 );

                // weighted vertex transformation

                temp.copy( vertex ).applyMatrix4( boneMatrix ).multiplyScalar( sw );
                skinned.add( temp );

            }

            skinned.applyMatrix4( bindMatrixInverse ); // back to local space

            // expand aabb

            aabb.expandByPoint( skinned );

        }

    }

    aabb.applyMatrix4( skinnedMesh.matrixWorld );

}
Run Code Online (Sandbox Code Playgroud)

另外,对于变形目标,有人指出该代码已经存在于 Mesh.raycast 函数中

是的,您可以针对变形网格进行光线投射。尚不支持针对蒙皮网格物体进行光线投射。里面的代码Mesh.raycast()已经很复杂了。我认为在进一步增强之前需要进行一些认真的重构。同时,您可以使用提供的代码片段自行构建解决方案。顶点位移逻辑实际上是最复杂的部分。

现场演示: https: //jsfiddle.net/fnjkeg9x/1/

three.js R107

  • 更新:在 https://github.com/mrdoob/ Three.js/pull/19178 - r116 中添加了蒙皮网格的光线投射 (2认同)