VRControls.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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;
  8. var standingMatrix = new THREE.Matrix4();
  9. var frameData = null;
  10. if ( 'VRFrameData' in window ) {
  11. frameData = new VRFrameData();
  12. }
  13. // the Rift SDK returns the position in meters
  14. // this scale factor allows the user to define how meters
  15. // are converted to scene units.
  16. this.scale = 1;
  17. // If true will use "standing space" coordinate system where y=0 is the
  18. // floor and x=0, z=0 is the center of the room.
  19. this.standing = false;
  20. // Distance from the users eyes to the floor in meters. Used when
  21. // standing=true but the VRDisplay doesn't provide stageParameters.
  22. this.userHeight = 1.6;
  23. this.setDisplay = function ( display ) {
  24. vrDisplay = display;
  25. };
  26. this.getDisplay = function () {
  27. return vrDisplay;
  28. };
  29. this.getStandingMatrix = function () {
  30. return standingMatrix;
  31. };
  32. this.update = function () {
  33. if ( vrDisplay ) {
  34. var pose;
  35. if ( vrDisplay.getFrameData ) {
  36. vrDisplay.getFrameData( frameData );
  37. pose = frameData.pose;
  38. } else if ( vrDisplay.getPose ) {
  39. pose = vrDisplay.getPose();
  40. }
  41. if ( pose.orientation !== null ) {
  42. object.quaternion.fromArray( pose.orientation );
  43. }
  44. if ( pose.position !== null ) {
  45. object.position.fromArray( pose.position );
  46. } else {
  47. object.position.set( 0, 0, 0 );
  48. }
  49. if ( this.standing ) {
  50. if ( vrDisplay.stageParameters ) {
  51. object.updateMatrix();
  52. standingMatrix.fromArray( vrDisplay.stageParameters.sittingToStandingTransform );
  53. object.applyMatrix( standingMatrix );
  54. } else {
  55. object.position.setY( object.position.y + this.userHeight );
  56. }
  57. }
  58. object.position.multiplyScalar( scope.scale );
  59. }
  60. };
  61. this.resetPose = function () {
  62. if ( vrDisplay ) {
  63. vrDisplay.resetPose();
  64. }
  65. };
  66. this.resetSensor = function () {
  67. console.warn( 'THREE.VRControls: .resetSensor() is now .resetPose().' );
  68. this.resetPose();
  69. };
  70. this.zeroSensor = function () {
  71. console.warn( 'THREE.VRControls: .zeroSensor() is now .resetPose().' );
  72. this.resetPose();
  73. };
  74. this.dispose = function () {
  75. vrDisplay = null;
  76. };
  77. };