Вы можете применить кватернион к единичному вектору, а затем вычислить угол:
const vec = new THREE.Vector3();
function getCameraAngle() {
// Reset the vector to a unit vector pointing into the scene
vec.set(0, 0, -1);
// Apply the camera quaternion
vec.applyQuaternion(camera.quaternion);
// Calculate the angle based on Y
return Math.asin(vec.y) * THREE.Math.RAD2DEG;
}
const angle = document.getElementById('angle');
function animate() {
angle.textContent = getCameraAngle().toFixed(2);
renderer.render(scene, camera);
};
// Boilerplate
new WebVRPolyfill();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const scene = new THREE.Scene();
const renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.vr.enabled = true;
document.body.appendChild(renderer.domElement);
WEBVR.createButton(renderer);
scene.add(new THREE.AmbientLight('white', 1));
let grid = new THREE.GridHelper();
scene.add(grid);
grid = new THREE.GridHelper();
grid.rotation.x = Math.PI / 2;
grid.position.set(0, 5, -5);
scene.add(grid);
grid = new THREE.GridHelper();
grid.rotation.x = Math.PI / 2;
grid.position.set(0, 5, 5);
scene.add(grid);
grid = new THREE.GridHelper();
grid.rotation.x = Math.PI / 2;
grid.rotation.z = Math.PI / 2;
grid.position.set(5, 5, 0);
scene.add(grid);
grid = new THREE.GridHelper();
grid.rotation.x = Math.PI / 2;
grid.rotation.z = Math.PI / 2;
grid.position.set(-5, 5, 0);
scene.add(grid);
grid = new THREE.GridHelper();
grid.position.set(0, 10, 0);
scene.add(grid);
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});
body {
margin: 0;
font-size: 0;
}
#angle {
position: absolute;
top: 10px;
left: 10px;
background: white;
font-size: 20pt;
font-family: monospace;
padding: 0.2em;
}
<script src="https://unpkg.com/webvr-polyfill@0.10.6/build/webvr-polyfill.min.js"></script>
<script src="https://unpkg.com/three@0.95.0/build/three.min.js"></script>
<script src="https://unpkg.com/three@0.95.0/examples/js/vr/WebVR.js"></script>
<span id="angle"></span>