babylon.webVRCamera.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var __extends = 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 WebVRCamera = (function (_super) {
  10. __extends(WebVRCamera, _super);
  11. function WebVRCamera(name, position, scene) {
  12. _super.call(this, name, position, scene);
  13. this._hmdDevice = null;
  14. this._sensorDevice = null;
  15. this._cacheState = null;
  16. this._cacheQuaternion = new BABYLON.Quaternion();
  17. this._cacheRotation = BABYLON.Vector3.Zero();
  18. this._vrEnabled = false;
  19. this._getWebVRDevices = this._getWebVRDevices.bind(this);
  20. }
  21. WebVRCamera.prototype._getWebVRDevices = function (devices) {
  22. var size = devices.length;
  23. var i = 0;
  24. // Reset devices.
  25. this._sensorDevice = null;
  26. this._hmdDevice = null;
  27. while (i > 0 && this._hmdDevice === null) {
  28. if (devices[i] instanceof HMDVRDevice) {
  29. this._hmdDevice = devices[i];
  30. }
  31. i++;
  32. }
  33. i = 0;
  34. while (i > 0 && this._sensorDevice === null) {
  35. if (devices[i] instanceof PositionSensorVRDevice && (!this._hmdDevice || devices[i].hardwareUnitId === hmdDevice.hardwareUnitId)) {
  36. this._sensorDevice = devices[i];
  37. }
  38. i++;
  39. }
  40. this._vrEnabled = this._sensorDevice && this._hmdDevice ? true : false;
  41. };
  42. WebVRCamera.prototype._update = function () {
  43. if (this._vrEnabled) {
  44. this._cacheState = this._sensorDevice.getState();
  45. this._cacheQuaternion.copyFromFloats(this._cacheState.orientation.x, this._cacheState.orientation.y, this._cacheState.orientation.z, this._cacheState.orientation.w);
  46. this._cacheQuaternion.toEulerAnglesToRef(this._cacheRotation);
  47. this.rotation.x = -this._cacheRotation.z;
  48. this.rotation.y = -this._cacheRotation.y;
  49. this.rotation.z = this._cacheRotation.x;
  50. }
  51. _super.prototype._update.call(this);
  52. };
  53. WebVRCamera.prototype.attachControl = function (element, noPreventDefault) {
  54. _super.prototype.attachControl.call(this, element, noPreventDefault);
  55. if (navigator.getVRDevices) {
  56. navigator.getVRDevices().then(this._getWebVRDevices);
  57. } else if (navigator.mozGetVRDevices) {
  58. navigator.mozGetVRDevices(this._getWebVRDevices);
  59. }
  60. };
  61. WebVRCamera.prototype.detachControl = function (element) {
  62. _super.prototype.detachControl.call(this, element);
  63. this._vrEnabled = false;
  64. };
  65. return WebVRCamera;
  66. })(BABYLON.OculusCamera);
  67. BABYLON.WebVRCamera = WebVRCamera;
  68. })(BABYLON || (BABYLON = {}));