内部是.matrixWorld吗?

时间:2018-06-01 14:10:19

标签: three.js

我有两个函数,区别在于使用了originObj.matrixWorld的克隆来乘以' transform'论点。 第一个不起作用,但第二个起作用。 据我所知,'矩阵'和' matrixWorld' object3d的属性是在每个帧中新计算的,更具体地说,在renderer.render()中。 所以我认为将任意矩阵分配给.matrix或.matrixWorld是没有价值的,因为它会在rederer.render()中被覆盖。 如果我的想法是正确的,那么以下两个功能也可以正常运行。 但只有第二个确实有效。 我有什么误解?

applyTransform(originObj, target, transform) {
    const newTransform = originObj.matrixWorld.multiply(transform);
        // decompose newTransformMatrix as position, rotation and scale
    const position = new THREE.Vector3();
    const quaternion = new THREE.Quaternion();
    const scale = new THREE.Vector3();
    newTransform.decompose(position, quaternion, scale);

    target.position.copy(position);
    target.quaternion.copy(quaternion);
    target.scale.copy(scale);
}



applyTransform(originObj, target, transform) {
    const newTransform = originObj.matrixWorld.clone().multiply(transform);
        // decompose newTransformMatrix as position, rotation and scale
    const position = new THREE.Vector3();
    const quaternion = new THREE.Quaternion();
    const scale = new THREE.Vector3();
    newTransform.decompose(position, quaternion, scale);

    target.position.copy(position);
    target.quaternion.copy(quaternion);
    target.scale.copy(scale);
}

我使用的是threejs r90

1 个答案:

答案 0 :(得分:0)

有一个内置函数可以将矩阵应用于对象。 Object3D.applyMatrix方法与第二个函数的作用完全相同。

  

将矩阵变换应用于对象并更新对象的位置,旋转和缩放。

applyMatrix: function ( matrix ) {

    this.matrix.multiplyMatrices( matrix, this.matrix );

    this.matrix.decompose( this.position, this.quaternion, this.scale );

}

所以,请致电:

target.applyMatrix(originObj.matrixWorld);

编辑:哦,我忘记了变换;

var matrix = new THREE.Matrix4().multiplyMatrices(originObj.matrixWorld, transform);
target.applyMatrix(matrix);