babylon.webVRCamera.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. var WebVRFreeCamera = (function (_super) {
  9. __extends(WebVRFreeCamera, _super);
  10. function WebVRFreeCamera(name, position, scene, compensateDistortion) {
  11. if (compensateDistortion === void 0) { compensateDistortion = true; }
  12. _super.call(this, name, position, scene);
  13. this._hmdDevice = null;
  14. this._sensorDevice = null;
  15. this._cacheState = null;
  16. this._vrEnabled = false;
  17. var metrics = BABYLON.VRCameraMetrics.GetDefault();
  18. metrics.compensateDistortion = compensateDistortion;
  19. this.setCameraRigMode(BABYLON.Camera.RIG_MODE_VR, { vrCameraMetrics: metrics });
  20. this._getWebVRDevices = this._getWebVRDevices.bind(this);
  21. this.rotationQuaternion = new BABYLON.Quaternion();
  22. }
  23. WebVRFreeCamera.prototype._getWebVRDevices = function (devices) {
  24. var size = devices.length;
  25. var i = 0;
  26. // Reset devices.
  27. this._sensorDevice = null;
  28. this._hmdDevice = null;
  29. // Search for a HmdDevice.
  30. while (i < size && this._hmdDevice === null) {
  31. if (devices[i] instanceof HMDVRDevice) {
  32. this._hmdDevice = devices[i];
  33. }
  34. i++;
  35. }
  36. i = 0;
  37. while (i < size && this._sensorDevice === null) {
  38. if (devices[i] instanceof PositionSensorVRDevice && (!this._hmdDevice || devices[i].hardwareUnitId === this._hmdDevice.hardwareUnitId)) {
  39. this._sensorDevice = devices[i];
  40. }
  41. i++;
  42. }
  43. this._vrEnabled = this._sensorDevice && this._hmdDevice ? true : false;
  44. };
  45. WebVRFreeCamera.prototype._checkInputs = function () {
  46. if (this._vrEnabled) {
  47. this._cacheState = this._sensorDevice.getState();
  48. this.rotationQuaternion.copyFrom(this._cacheState.orientation);
  49. //Flip in XY plane
  50. this.rotationQuaternion.z *= -1;
  51. this.rotationQuaternion.w *= -1;
  52. }
  53. _super.prototype._checkInputs.call(this);
  54. };
  55. WebVRFreeCamera.prototype.attachControl = function (element, noPreventDefault) {
  56. _super.prototype.attachControl.call(this, element, noPreventDefault);
  57. noPreventDefault = BABYLON.Camera.ForceAttachControlToAlwaysPreventDefault ? false : noPreventDefault;
  58. if (navigator.getVRDevices) {
  59. navigator.getVRDevices().then(this._getWebVRDevices);
  60. }
  61. else if (navigator.mozGetVRDevices) {
  62. navigator.mozGetVRDevices(this._getWebVRDevices);
  63. }
  64. };
  65. WebVRFreeCamera.prototype.detachControl = function (element) {
  66. _super.prototype.detachControl.call(this, element);
  67. this._vrEnabled = false;
  68. };
  69. WebVRFreeCamera.prototype.requestVRFullscreen = function (requestPointerlock) {
  70. if (!this._hmdDevice)
  71. return;
  72. this.getEngine().switchFullscreen(requestPointerlock, { vrDisplay: this._hmdDevice });
  73. };
  74. WebVRFreeCamera.prototype.getTypeName = function () {
  75. return "WebVRFreeCamera";
  76. };
  77. return WebVRFreeCamera;
  78. }(BABYLON.FreeCamera));
  79. BABYLON.WebVRFreeCamera = WebVRFreeCamera;
  80. })(BABYLON || (BABYLON = {}));