三个JS——访问ArrowHelper的方向?
Three JS - Access ArrowHelper direction?
假设我创建了一个箭头助手,例如类似于:
const arrowPosition = new THREE.Vector3(0, 0, 0);
const arrowPosition = new THREE.Vector3(2, -2, 0);
arrowDirection.subVectors( scene.position, new THREE.Vector3(1, 1, 1)).normalize();
arrow = new THREE.ArrowHelper( arrowDirection, arrowPosition, arrowLength, 0xffff00, arrowHeadLength, arrowHeadWidth);
创建后,如何访问它的方向?
简而言之,我不确定是否有一种简单的方法来访问方向,至少在 vector3 格式中是这样。 ArrowHelper
构造函数中使用的值 dir
(如 in the docs 所述)从未直接分配给 ArrowHelper
或 Object3D
的向量 属性父 class,而是用于设置 ArrowHelper
的四元数 属性,继承自父 class。
请参阅下面来自 the helper source code 的代码片段:
setDirection( dir ) {
// dir is assumed to be normalized
if ( dir.y > 0.99999 ) {
this.quaternion.set( 0, 0, 0, 1 );
} else if ( dir.y < - 0.99999 ) {
this.quaternion.set( 1, 0, 0, 0 );
} else {
_axis.set( dir.z, 0, - dir.x ).normalize();
const radians = Math.acos( dir.y );
this.quaternion.setFromAxisAngle( _axis, radians );
}
}
如 Object3D 文档中所列,quaternion
描述了助手的旋转(quaternion docs). A better discussion on the math might be found on this unity forum,但要获得矢量方向,您需要使用 quaternion
字段
希望这会有所帮助,如果有人可以改进此解释,请这样做,我在欧拉和四元数方面的经验有限
假设我创建了一个箭头助手,例如类似于:
const arrowPosition = new THREE.Vector3(0, 0, 0);
const arrowPosition = new THREE.Vector3(2, -2, 0);
arrowDirection.subVectors( scene.position, new THREE.Vector3(1, 1, 1)).normalize();
arrow = new THREE.ArrowHelper( arrowDirection, arrowPosition, arrowLength, 0xffff00, arrowHeadLength, arrowHeadWidth);
创建后,如何访问它的方向?
简而言之,我不确定是否有一种简单的方法来访问方向,至少在 vector3 格式中是这样。 ArrowHelper
构造函数中使用的值 dir
(如 in the docs 所述)从未直接分配给 ArrowHelper
或 Object3D
的向量 属性父 class,而是用于设置 ArrowHelper
的四元数 属性,继承自父 class。
请参阅下面来自 the helper source code 的代码片段:
setDirection( dir ) {
// dir is assumed to be normalized
if ( dir.y > 0.99999 ) {
this.quaternion.set( 0, 0, 0, 1 );
} else if ( dir.y < - 0.99999 ) {
this.quaternion.set( 1, 0, 0, 0 );
} else {
_axis.set( dir.z, 0, - dir.x ).normalize();
const radians = Math.acos( dir.y );
this.quaternion.setFromAxisAngle( _axis, radians );
}
}
如 Object3D 文档中所列,quaternion
描述了助手的旋转(quaternion docs). A better discussion on the math might be found on this unity forum,但要获得矢量方向,您需要使用 quaternion
字段
希望这会有所帮助,如果有人可以改进此解释,请这样做,我在欧拉和四元数方面的经验有限