如何在 three.js 上添加两个向量?

How to add two vectors on three.js?

找不到我错过了什么....

调试示例代码我发现了这些行并将其转储到控制台以了解出了什么问题:

        var intPosition = new THREE.Vector3( Math.floor(result.point.x), 0, Math.floor(result.point.z) );
        console.log(intPosition);
        brush.position = intPosition.clone().add(new THREE.Vector3(0.5, 0.5, 0.5) );
        console.log(brush.position);

但是…………输出如下(来自 firebug 控制台):

Object { x=0, y=0, z=10, more... }
Object { x=0, y=0, z=0, more... }

来自 three.js docs:

.add ( v ) this

将 v 添加到此向量。

here:

add: function ( v1, v2 ) {

    this.x = v1.x + v2.x;
    this.y = v1.y + v2.y;
    this.z = v1.z + v2.z;

    return this;

},

怎么了?是不是整数+浮点数在js上失败了?

编辑说明:

我不知道它是否相关,但这个问题出现在我当前版本(修订版 71)的 Three.js 上,在旧版本(r60 或更低版本)上工作正常。

你必须使用 Vector3 的 .set() 方法 class。

var brushPosition = intPosition.clone().add(new THREE.Vector3(0.5, 0.5, 0.5) );
brush.position.set(brushPosition.x, brushPosition.y, brushPosition.z);

您也许还可以使用 Vector3 的复制功能 class。

brush.position.copy(intPosition.clone().add(new THREE.Vector3(0.5, 0.5, 0.5));

编辑: 原因是object3D的位置属性是不可写的。它不能用赋值运算符设置。