【问题标题】:Three JS - Access ArrowHelper direction?三 JS - 访问 ArrowHelper 方向?
【发布时间】:2022-01-30 20:39:37
【问题描述】:

假设我创建了一个箭头助手,例如类似:

        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);

创建后如何获取其方向?

【问题讨论】:

  • 这能回答你的问题吗? Three JS - Rotate arrow in 3D to track mouse
  • 不,该页面上的答案没有显示创建后如何访问箭头的方向。
  • 您可以将方向写入助手的.userData以便稍后获取。

标签: three.js


【解决方案1】:

简而言之,我不确定是否有一种简单的方法可以访问该方向,至少以 vector3 格式。在ArrowHelper 构造函数中使用的值dir(如in the docs 所述)从未直接分配给ArrowHelperObject3D 父类的向量属性,而是用于设置ArrowHelper,继承自父类。

the helper source code下面的代码sn-p:

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)。 on this unity forum 可能会找到关于数学的更好讨论,但要获得矢量方向,您需要使用 quaternion 字段

希望这会有所帮助,如果有人可以改进这个解释,请做,我对欧拉和四元数的经验是有限的

【讨论】:

    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 2017-02-09
    相关资源
    最近更新 更多