三.js获取具有给定世界方向坐标的场景矢量的对象相对局部方向

Three.js Get Object-relative Local orientation of a scene vector with given World orientation coordinates

本文关键字:方向 对象 相对局 js 坐标 世界 获取      更新时间:2023-09-26

我在场景中有一个给定的向量V。 矢量由世界坐标(VWx,VWy,VWz)定义。 请注意,它是一个方向向量,而不是位置向量。

在场景中,我还有一个具有随机方向(和位置)的Object3D。

我希望找到方向向量 V 的局部(即相对于 Object3D)坐标(VLx、VLy、VLz)。

我已经尝试了以下代码(从WestLangley在这个问题中的答案推断),但它似乎没有给出正确的结果。

var VW = new THREE.Vector3(10,20,30); 
var VL = new THREE.Vector3(0,0,0); 
givenObject.updateMatrixWorld();
var ob_WorldQuaternion = new THREE.Quaternion(); 
ob_WorldQuaternion  = givenObject.getWorldQuaternion();
var ob_InvWorldQuaternion = new THREE.Quaternion(); 
ob_InvWorldQuaternion = ob_WorldQuaternion.inverse();
VL = VW.applyQuaternion( ob_InvWorldQuaternion);

编辑(1) 请注意,THREE.js Object3D 方法 .worldToLocal(vector) 不适合,因为它仅用于转换位置向量,而不是方向向量。

EIDT(2) 这个 jsfiddle 说明了一个示例应用程序。 绿色圆锥体是绿色盒子的子项。 该应用程序试图使绿色圆锥指向与白色世界圆锥相同的世界方向。

(我之前答案的改进版本)。

此 jsfiddle 说明了对齐子锥体的 3 种方法。

红锥体相对于其父框具有固定的方向。

绿色圆锥对齐,以便其 z 轴与白色世界圆锥对齐。

蓝色圆锥体的XYZ轴

与白色世界锥体的XYZ轴共同对齐。

代码(来自初始化函数):-

cone_geometry = new THREE.CylinderGeometry(3, 40, 120, 40, 10, false);
//... The following mod is as recommended by WestLangley's answer at:-
//... http://stackoverflow.com/questions/13757483/three-js-lookat-seems-to-be-flipped
//... cone.LookAt(PosX) will then point the cone Z-axis towards PosX
//... (assuming cone.position and PosX are relative to the same coordinate system).
cone_geometry.applyMatrix( new THREE.Matrix4().makeRotationX( Math.PI / 2 ) );

核心代码(来自更新/动画功能):-

    worldCone.lookAt(target.position); 
    var worldVec =  new THREE.Vector3();
    worldVec.copy(target.position).sub(worldCone.position);
    worldBox.rotation.x += 0.01;
    worldBox.rotation.y += 0.03;
    worldBox.rotation.z += 0.02;
    var localVec =  new THREE.Vector3();
    localVec = F_get_LocalDirectionVector_relativeTo_Object3D_of_WorldDirectionVector 
( worldBox, worldVec, localVec) ;
    //=================================================================
    function F_get_LocalDirectionVector_relativeTo_Object3D_of_WorldDirectionVector 
( givenObject, WDV, LDV)
          {
              givenObject.updateMatrixWorld(); 
             //FLABBY method: - works but verbose and excessive object creation
             /*
              var ob_InvWorldQuaternion = new THREE.Quaternion(); 
              ob_InvWorldQuaternion = ob_WorldQuaternion.inverse(); 
              LDV.copy( WDV ).applyQuaternion( ob_InvWorldQuaternion );        
              */
              //LEAN method: copies WDV value into LDV 
              //then applies inverse world quaternion of givenObject.
              LDV.copy( WDV )
              .applyQuaternion( 
              givenObject.getWorldQuaternion().inverse() 
              );  
              return LDV;
      } 
      //=================================================================
//... Apply localVec 
//... Get the localCone_G to point in the direction of the LocalVec
 var localCone_G_target_pos = new THREE.Vector3();
 //... LHS becomes sum of two RHS vectors
 localCone_G_target_pos.addVectors( localCone_G.position, localVec );
//... ORIENT THE CHILD CONES
//...Cone_R has fixed orientation relative to parent cube
localCone_R.lookAt(1,0,0); 
//... Cone_G has z-vector pointing in the same direction as world cone
localCone_G.lookAt(localCone_G_target_pos); 
//... Cone_B has its' xyz axes aligned with the world cone
localCone_B.lookAt(1,0,0);
localCone_B.quaternion.multiply( worldBox.getWorldQuaternion().inverse()  ); 
localCone_B.quaternion.multiply( worldCone.quaternion );