IAM
IAM

Reputation: 55

In A-Frame: How do I get the VR camera

In this example:
https://glitch.com/~query-aframe-cameras I have registered a component which launches a projectile in the direction the user is looking (with a little boost for elevation)

Spacebar or Screen Tap to launch - be sure to be looking above the horizon!

It fails in mobile vr (stereo camera) mode:
Projectiles continue to fire, but from the default orientation of the mono, not the stereo camera

I'm using:
var cam = document.querySelector('a-scene').camera.el.object3D; var camVec = new THREE.Vector3(); var camDir = cam.getWorldDirection(camVec); to get the camera information and spit the projectiles back

QUERY: HOW DO I GET THE STEREO CAMERA INFORMATION

Upvotes: 2

Views: 894

Answers (1)

Diarmid Mackenzie
Diarmid Mackenzie

Reputation: 826

The problem will be due to this bug in THREE.js.

https://github.com/mrdoob/three.js/issues/18448

Following the documented workaround, the code here works with correct direction tracking on my Quest 2.

https://cuddly-endurable-soap.glitch.me

(there's a couple of other changes in here: to test this on my VR headset I also had to upgrade to A-Frame 1.1.0, and make the code fire the balls on a timer, since I don't have any keys to press when using it! Neither of these was enough to fix the problem, though).

This code will set up CamDir as you need it.

var camQ = new THREE.Quaternion();
cam.updateMatrixWorld();
camQ.setFromRotationMatrix(cam.matrixWorld);
var camDir = new THREE.Vector3(0, 0, 1);
camDir.applyQuaternion(camQ);

The following code also works (and is more efficient), but is less easy to understand.

var camDir = new THREE.Vector3();
var e = cam.matrixWorld.elements;
camDir.set(e[ 8 ], e[ 9 ], e[ 10 ]).normalize();

I picked it up from here: https://lace-fern-ghost.glitch.me/

Which I found a link to from here: https://github.com/aframevr/aframe/issues/4412

(but I can't find any reference to explain why these components matrixWorld.elements are the right ones to use here to get the direction).

Upvotes: 1

Related Questions