babylon.webVRCamera.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var WebVRFreeCamera = (function (_super) {
  10. __extends(WebVRFreeCamera, _super);
  11. function WebVRFreeCamera(name, position, scene, compensateDistorsion) {
  12. if (compensateDistorsion === void 0) { compensateDistorsion = true; }
  13. _super.call(this, name, position, scene);
  14. this._hmdDevice = null;
  15. this._sensorDevice = null;
  16. this._cacheState = null;
  17. this._cacheQuaternion = new BABYLON.Quaternion();
  18. this._cacheRotation = BABYLON.Vector3.Zero();
  19. this._vrEnabled = false;
  20. var metrics = BABYLON.VRCameraMetrics.GetDefault();
  21. metrics.compensateDistorsion = compensateDistorsion;
  22. this.setCameraRigMode(BABYLON.Camera.RIG_MODE_VR, { vrCameraMetrics: metrics });
  23. this._getWebVRDevices = this._getWebVRDevices.bind(this);
  24. }
  25. WebVRFreeCamera.prototype._getWebVRDevices = function (devices) {
  26. var size = devices.length;
  27. var i = 0;
  28. // Reset devices.
  29. this._sensorDevice = null;
  30. this._hmdDevice = null;
  31. // Search for a HmdDevice.
  32. while (i < size && this._hmdDevice === null) {
  33. if (devices[i] instanceof HMDVRDevice) {
  34. this._hmdDevice = devices[i];
  35. }
  36. i++;
  37. }
  38. i = 0;
  39. while (i < size && this._sensorDevice === null) {
  40. if (devices[i] instanceof PositionSensorVRDevice && (!this._hmdDevice || devices[i].hardwareUnitId === this._hmdDevice.hardwareUnitId)) {
  41. this._sensorDevice = devices[i];
  42. }
  43. i++;
  44. }
  45. this._vrEnabled = this._sensorDevice && this._hmdDevice ? true : false;
  46. };
  47. WebVRFreeCamera.prototype._checkInputs = function () {
  48. if (this._vrEnabled) {
  49. this._cacheState = this._sensorDevice.getState();
  50. this._cacheQuaternion.copyFromFloats(this._cacheState.orientation.x, this._cacheState.orientation.y, this._cacheState.orientation.z, this._cacheState.orientation.w);
  51. this._cacheQuaternion.toEulerAnglesToRef(this._cacheRotation);
  52. this.rotation.x = -this._cacheRotation.z;
  53. this.rotation.y = -this._cacheRotation.y;
  54. this.rotation.z = this._cacheRotation.x;
  55. }
  56. _super.prototype._checkInputs.call(this);
  57. };
  58. WebVRFreeCamera.prototype.attachControl = function (element, noPreventDefault) {
  59. _super.prototype.attachControl.call(this, element, noPreventDefault);
  60. if (navigator.getVRDevices) {
  61. navigator.getVRDevices().then(this._getWebVRDevices);
  62. }
  63. else if (navigator.mozGetVRDevices) {
  64. navigator.mozGetVRDevices(this._getWebVRDevices);
  65. }
  66. };
  67. WebVRFreeCamera.prototype.detachControl = function (element) {
  68. _super.prototype.detachControl.call(this, element);
  69. this._vrEnabled = false;
  70. };
  71. return WebVRFreeCamera;
  72. })(BABYLON.FreeCamera);
  73. BABYLON.WebVRFreeCamera = WebVRFreeCamera;
  74. })(BABYLON || (BABYLON = {}));
  75. //# sourceMappingURL=babylon.webVRCamera.js.map