VRControls.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. this.zeroSensor = function () {
  32. if ( vrInput === undefined ) return;
  33. vrInput.zeroSensor();
  34. };
  35. };