Three.js - 以**绝对**角度绕轴旋转
Three.js - Rotate around Axis at an **absoulte** angle
我正在使用此函数(来自 )围绕 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;
提前致谢!
对象的变换存储在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
如果你想 return 对象在旋转之前进行一些特定的转换,你可以。
- 在应用新旋转之前反转之前的旋转:
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
我正在使用此函数(来自
// 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;
提前致谢!
对象的变换存储在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
如果你想 return 对象在旋转之前进行一些特定的转换,你可以。
- 在应用新旋转之前反转之前的旋转:
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