我在场景中有一个给定的矢量V.矢量由世界坐标(VWx,VWy,VWz)定义。请注意,它是方向向量而非位置向量。
在场景中我还有一个随机方向(和位置)的Object3D。
我希望找到方向向量V的局部(即相对于Object3D)坐标(VLx,VLy,VLz)。
我尝试了以下代码(从this question中的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(矢量)不适用,因为它仅用于转换位置矢量,而不是方向矢量。
EIDT(2)This jsfiddle说明了一个示例应用程序。绿色圆锥是绿色框的孩子。该应用程序试图保持绿色圆锥指向与白色世界锥相同的世界方向。
答案 0 :(得分:0)
(我之前回答的改进版本)。
This jsfiddle说明了3种对齐儿童视锥的方法。
红色圆锥相对于其父框具有固定的方向。
绿锥对齐,以便它的' z轴与白色世界锥对齐。
蓝锥有它的' XYZ轴与白色世界锥的XYZ轴共同对齐。
CODE(来自init函数): -
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 );