VRControls.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @author dmarcos / https://github.com/dmarcos
  3. */
  4. THREE.VRControls = function ( camera, done ) {
  5. this._camera = camera;
  6. this._init = function () {
  7. var self = this;
  8. this._renderer = renderer;
  9. if ( !navigator.mozGetVRDevices ) {
  10. if ( done ) {
  11. done("Your browser is not VR Ready");
  12. }
  13. return;
  14. }
  15. navigator.mozGetVRDevices( gotVRDevices );
  16. function gotVRDevices( devices ) {
  17. var vrInput;
  18. var error;
  19. for ( var i = 0; i < devices.length; ++i ) {
  20. if ( devices[i] instanceof PositionSensorVRDevice ) {
  21. vrInput = devices[i]
  22. self._vrInput = vrInput;
  23. break; // We keep the first we encounter
  24. }
  25. }
  26. if ( done ) {
  27. if ( !vrInput ) {
  28. error = 'HMD not available';
  29. }
  30. done( error );
  31. }
  32. }
  33. };
  34. this._init();
  35. this.update = function() {
  36. var camera = this._camera;
  37. var quat;
  38. var vrState = this.getVRState();
  39. if ( !vrState ) {
  40. return;
  41. }
  42. // Applies head rotation from sensors data.
  43. if ( camera ) {
  44. quat = new THREE.Quaternion(
  45. vrState.hmd.rotation[0],
  46. vrState.hmd.rotation[1],
  47. vrState.hmd.rotation[2],
  48. vrState.hmd.rotation[3]
  49. );
  50. camera.setRotationFromQuaternion( quat );
  51. }
  52. };
  53. this.getVRState = function() {
  54. var vrInput = this._vrInput;
  55. var orientation;
  56. var vrState;
  57. if ( !vrInput ) {
  58. return null;
  59. }
  60. orientation = vrInput.getState().orientation;
  61. vrState = {
  62. hmd : {
  63. rotation : [
  64. orientation.x,
  65. orientation.y,
  66. orientation.z,
  67. orientation.w
  68. ]
  69. }
  70. };
  71. return vrState;
  72. };
  73. };