VRControls.js 954 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @author dmarcos / https://github.com/dmarcos
  3. * @author mrdoob / http://mrdoob.com
  4. */
  5. THREE.VRControls = function ( object, callback ) {
  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 ( callback !== undefined ) {
  16. callback( 'HMD not available' );
  17. }
  18. };
  19. if ( navigator.getVRDevices !== undefined ) {
  20. navigator.getVRDevices().then( onVRDevices );
  21. } else if ( callback !== undefined ) {
  22. callback( 'Your browser is not VR Ready' );
  23. }
  24. this.update = function () {
  25. if ( vrInput === undefined ) return;
  26. var orientation = vrInput.getState().orientation;
  27. if ( orientation !== null ) {
  28. object.quaternion.set( orientation.x, orientation.y, orientation.z, orientation.w );
  29. }
  30. };
  31. };