将向量的长度等于three.js中的某个值

时间:2021-10-29 06:37:26

I've been reading the documentation & source to try and find out a way to compute the length of a vector and then set it to a defined value.

我一直在阅读文档和源代码,试图找出计算向量长度的方法,然后将其设置为定义的值。

my code:

if (viewerPosition !== null && placementPosition !== null) {
    var vectorDiff = placementPosition.sub(viewerPosition);
    placementPosition.setY(0);
    placementPosition.multiplyScalar(100000);
    console.log(placementPosition)

the log returns:

日志返回:

Object { x: -23.658385352970377, y: 0, z: 9.121675219786463 }

Ive tried adding the line:

我试过添加该行:

placementPosition.length (32);

to see if that would change the length of the vector to 32m, but unfortunately this did not work.

看看是否会将矢量的长度改为32米,但不幸的是,这不起作用。

Any advice would be greatly appreciated. Thanks

任何建议将不胜感激。谢谢

1 个解决方案

#1


// make the length of the vector 1.0
placementPosition.normalize();

// make the length of the vector 32
placementPosition.multiplyScalar(32);

#1


// make the length of the vector 1.0
placementPosition.normalize();

// make the length of the vector 32
placementPosition.multiplyScalar(32);