VRControls.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @author dmarcos / https://github.com/dmarcos
  3. * @author mrdoob / http://mrdoob.com
  4. */
  5. THREE.VRControls = function ( object, done ) {
  6. var vrInput;
  7. var onVRDevices = function ( devices ) {
  8. for ( var i = 0; i < devices.length; i ++ ) {
  9. var device = devices[ i ];
  10. if ( device instanceof PositionSensorVRDevice ) {
  11. vrInput = devices[ i ];
  12. return; // We keep the first we encounter
  13. }
  14. }
  15. if ( done !== undefined ) {
  16. done( 'HMD not available' );
  17. }
  18. };
  19. if ( navigator.getVRDevices !== undefined ) {
  20. navigator.getVRDevices().then( onVRDevices );
  21. } else if ( navigator.getVRDevices !== undefined ) {
  22. navigator.mozGetVRDevices( onVRDevices );
  23. } else if ( done !== undefined ) {
  24. done( 'Your browser is not VR Ready' );
  25. }
  26. this.update = function () {
  27. if ( vrInput === undefined ) return;
  28. var orientation = vrInput.getState().orientation;
  29. if ( orientation !== null ) {
  30. object.quaternion.set( orientation.x, orientation.y, orientation.z, orientation.w );
  31. }
  32. };
  33. };