这是 3D 计算机图形学中非常常见的问题。欢迎来到云台锁问题!这基本上是您在尝试使用“欧拉”角度设置旋转时遇到的问题,尤其是在以 90 度的倍数旋转时。如果您要像这样旋转,请尝试仅旋转两个轴(例如 x 和 y)而不是全部 3 个(x,y,z)。
更多云台锁信息:https://www.youtube.com/watch?v=zc8b2Jo7mno&feature=youtu.be
一个更好的做法是使用quaternions 来代替旋转。我还将注意到,最佳实践是使用组件来确保 A-Frame 可用于修改,或者不推荐在 a-scene 上使用“已加载”事件侦听器(有关更多信息,请参阅此 SO 问题。How to detect when a scene is loaded in A-Frame?) :
//listen for scene load s0 we know Aframe and Threejs are around to access. A snipped of your code, modified slightly for some direction ...
document.querySelector('a-scene').addEventListener('loaded', function () {
//AFRAME loaded
const scene = document.querySelector('a-scene');
const arrowElem = scene.querySelector(".shape-container");
//now set your click listener
setButton.addEventListener('click',function(){
let rotationValue = `${inputPitch.valueAsNumber} ${inputYaw.valueAsNumber} ${inputRoll.valueAsNumber}`
//arrowElem.setAttribute('rotation', rotationValue); //your way
//this doesn't work well ...
//arrowElem.object3D.rotation.set( THREE.Math.degToRad(inputPitch.valueAsNumber),
THREE.Math.degToRad(inputYaw.valueAsNumber),
THREE.Math.degToRad(inputRoll.valueAsNumber)
);
//consider how you can use quaternions instead
let quaternion = new THREE.Quaternion();
quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 ); //might have to change your logic to better use this functionality ...
})
});