VRControls.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @author dmarcos / https://github.com/dmarcos
  3. * @author mrdoob / http://mrdoob.com
  4. */
  5. THREE.VRControls = function ( object, onError ) {
  6. var scope = this;
  7. var vrDisplay, vrDisplays;
  8. var standingMatrix = new THREE.Matrix4();
  9. var frameData = null;
  10. if ( 'VRFrameData' in window ) {
  11. frameData = new VRFrameData();
  12. }
  13. function gotVRDisplays( displays ) {
  14. vrDisplays = displays;
  15. if ( displays.length > 0 ) {
  16. vrDisplay = displays[ 0 ];
  17. } else {
  18. if ( onError ) onError( 'VR input not available.' );
  19. }
  20. }
  21. if ( navigator.getVRDisplays ) {
  22. navigator.getVRDisplays().then( gotVRDisplays ).catch ( function () {
  23. console.warn( 'THREE.VRControls: Unable to get VR Displays' );
  24. } );
  25. }
  26. // the Rift SDK returns the position in meters
  27. // this scale factor allows the user to define how meters
  28. // are converted to scene units.
  29. this.scale = 1;
  30. // If true will use "standing space" coordinate system where y=0 is the
  31. // floor and x=0, z=0 is the center of the room.
  32. this.standing = false;
  33. // Distance from the users eyes to the floor in meters. Used when
  34. // standing=true but the VRDisplay doesn't provide stageParameters.
  35. this.userHeight = 1.6;
  36. this.getVRDisplay = function () {
  37. return vrDisplay;
  38. };
  39. this.setVRDisplay = function ( value ) {
  40. vrDisplay = value;
  41. };
  42. this.getVRDisplays = function () {
  43. console.warn( 'THREE.VRControls: getVRDisplays() is being deprecated.' );
  44. return vrDisplays;
  45. };
  46. this.getStandingMatrix = function () {
  47. return standingMatrix;
  48. };
  49. this.update = function () {
  50. if ( vrDisplay ) {
  51. var pose;
  52. if ( vrDisplay.getFrameData ) {
  53. vrDisplay.getFrameData( frameData );
  54. pose = frameData.pose;
  55. } else if ( vrDisplay.getPose ) {
  56. pose = vrDisplay.getPose();
  57. }
  58. if ( pose.orientation !== null ) {
  59. object.quaternion.fromArray( pose.orientation );
  60. }
  61. if ( pose.position !== null ) {
  62. object.position.fromArray( pose.position );
  63. } else {
  64. object.position.set( 0, 0, 0 );
  65. }
  66. if ( this.standing ) {
  67. if ( vrDisplay.stageParameters ) {
  68. object.updateMatrix();
  69. standingMatrix.fromArray( vrDisplay.stageParameters.sittingToStandingTransform );
  70. object.applyMatrix( standingMatrix );
  71. } else {
  72. object.position.setY( object.position.y + this.userHeight );
  73. }
  74. }
  75. object.position.multiplyScalar( scope.scale );
  76. }
  77. };
  78. this.resetPose = function () {
  79. if ( vrDisplay ) {
  80. vrDisplay.resetPose();
  81. }
  82. };
  83. this.resetSensor = function () {
  84. console.warn( 'THREE.VRControls: .resetSensor() is now .resetPose().' );
  85. this.resetPose();
  86. };
  87. this.zeroSensor = function () {
  88. console.warn( 'THREE.VRControls: .zeroSensor() is now .resetPose().' );
  89. this.resetPose();
  90. };
  91. this.dispose = function () {
  92. vrDisplay = null;
  93. };
  94. };