我正在使用這個函式(來自Three JS Pivot point)來圍繞 Three.js 中的一個軸旋轉一個物件:
// obj - your object (THREE.Object3D or derived)
// point - the point of rotation (THREE.Vector3)
// axis - the axis of rotation (normalized THREE.Vector3)
// theta - radian value of rotation
// pointIsWorld - boolean indicating the point is in world coordinates (default = false)
function rotateAboutPoint(obj, point, axis, theta, pointIsWorld){
pointIsWorld = (pointIsWorld === undefined)? false : pointIsWorld;
if(pointIsWorld){
obj.parent.localToWorld(obj.position); // compensate for world coordinate
}
obj.position.sub(point); // remove the offset
obj.position.applyAxisAngle(axis, theta); // rotate the POSITION
obj.position.add(point); // re-add the offset
if(pointIsWorld){
obj.parent.worldToLocal(obj.position); // undo world coordinates compensation
}
obj.rotateOnAxis(axis, theta); // rotate the OBJECT
}
正如答案中所指出的,我遇到的問題是轉換是持久的。因此,當我再次呼叫該函式時,將從停止的位置應用轉換。如何“重置”轉換,使提供的角度是絕對值?
以下不起作用:
obj.rotation.y = 0;
提前致謝!
uj5u.com熱心網友回復:
物件的變換存盤在 obj.matrix 下
如果您想將物件重置為具有默認變換(即沒有縮放、旋轉或平移),您可以這樣做:
obj.matrix.identity();
obj.matrix.decompose(obj.position, obj.quaternion, obj.scale);
或者如果您只想將旋轉重置為默認值,您可以這樣做
obj.quaternion.identity();
https://threejs.org/docs/index.html?q=matrix4#api/en/math/Matrix4.identity
如果您想將物件回傳到它在旋轉之前進行的某些特定轉換,您也可以。
- 在應用新旋轉之前反轉之前的旋轉:
obj.rotateOnAxis(oldAxis, -oldTheta);
或者...
- 存盤舊的變換矩陣并在應用新變換之前恢復它。需要存盤在某個持久的地方(所以不是例如區域變數)。您可以在各種地方存盤它,但絕對可以使用的地方是 object3D 用戶資料。
obj.userData.oldMatrix = obj.matrix.clone();
然后像這樣重新設定它:
obj.matrix.copy(obj.userData.oldMatrix);
obj.matrix.decompose(obj.position, obj.quaternion, obj.scale);
https://threejs.org/docs/index.html?q=object3D#api/en/core/Object3D.userData
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/362858.html
標籤:javascript 数学 三.js 3d
