2
0

VRControls.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * @author dmarcos / https://github.com/dmarcos
  3. * @author mrdoob / http://mrdoob.com
  4. */
  5. THREE.VRControls = function ( object, callback ) {
  6. var scope = this;
  7. // Allow for multiple VR input devices.
  8. var vrInputs = [];
  9. var onVRDevices = function ( devices ) {
  10. for ( var i = 0; i < devices.length; i ++ ) {
  11. var device = devices[ i ];
  12. if ( device instanceof PositionSensorVRDevice ) {
  13. vrInputs.push( devices[ i ] );
  14. }
  15. }
  16. if ( callback !== undefined ) {
  17. callback( 'HMD not available' );
  18. }
  19. };
  20. if ( navigator.getVRDevices !== undefined ) {
  21. navigator.getVRDevices().then( onVRDevices );
  22. } else if ( callback !== undefined ) {
  23. callback( 'Your browser is not VR Ready' );
  24. }
  25. // the Rift SDK returns the position in meters
  26. // this scale factor allows the user to define how meters
  27. // are converted to scene units.
  28. this.scale = 1;
  29. this.update = function () {
  30. for ( var i = 0; i < vrInputs.length; i++ ) {
  31. var vrInput = vrInputs[ i ];
  32. var state = vrInput.getState();
  33. if ( state.orientation !== null ) {
  34. object.quaternion.copy( state.orientation );
  35. }
  36. if ( state.position !== null ) {
  37. object.position.copy( state.position ).multiplyScalar( scope.scale );
  38. }
  39. }
  40. };
  41. this.zeroSensor = function () {
  42. for ( var i = 0; i < vrInputs.length; i++ ) {
  43. var vrInput = vrInputs[ i ];
  44. vrInput.zeroSensor();
  45. }
  46. };
  47. };