babylon.oculusGamepadCamera.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 OculusRiftDevKit2013_Metric = {
  10. HResolution: 1280,
  11. VResolution: 800,
  12. HScreenSize: 0.149759993,
  13. VScreenSize: 0.0935999975,
  14. VScreenCenter: 0.0467999987,
  15. EyeToScreenDistance: 0.0410000011,
  16. LensSeparationDistance: 0.0635000020,
  17. InterpupillaryDistance: 0.0640000030,
  18. DistortionK: [1.0, 0.219999999, 0.239999995, 0.0],
  19. ChromaAbCorrection: [0.995999992, -0.00400000019, 1.01400006, 0.0],
  20. PostProcessScaleFactor: 1.714605507808412,
  21. LensCenterOffset: 0.151976421
  22. };
  23. var _OculusInnerGamepadCamera = (function (_super) {
  24. __extends(_OculusInnerGamepadCamera, _super);
  25. function _OculusInnerGamepadCamera(name, position, scene, isLeftEye) {
  26. _super.call(this, name, position, scene);
  27. this._workMatrix = new BABYLON.Matrix();
  28. this._actualUp = new BABYLON.Vector3(0, 0, 0);
  29. // Constants
  30. this._aspectRatioAspectRatio = OculusRiftDevKit2013_Metric.HResolution / (2 * OculusRiftDevKit2013_Metric.VResolution);
  31. this._aspectRatioFov = (2 * Math.atan((OculusRiftDevKit2013_Metric.PostProcessScaleFactor * OculusRiftDevKit2013_Metric.VScreenSize) / (2 * OculusRiftDevKit2013_Metric.EyeToScreenDistance)));
  32. var hMeters = (OculusRiftDevKit2013_Metric.HScreenSize / 4) - (OculusRiftDevKit2013_Metric.LensSeparationDistance / 2);
  33. var h = (4 * hMeters) / OculusRiftDevKit2013_Metric.HScreenSize;
  34. this._hMatrix = BABYLON.Matrix.Translation(isLeftEye ? h : -h, 0, 0);
  35. this.viewport = new BABYLON.Viewport(isLeftEye ? 0 : 0.5, 0, 0.5, 1.0);
  36. this._preViewMatrix = BABYLON.Matrix.Translation(isLeftEye ? .5 * OculusRiftDevKit2013_Metric.InterpupillaryDistance : -.5 * OculusRiftDevKit2013_Metric.InterpupillaryDistance, 0, 0);
  37. // Postprocess
  38. var postProcess = new BABYLON.OculusDistortionCorrectionPostProcess("Oculus Distortion", this, !isLeftEye, OculusRiftDevKit2013_Metric);
  39. }
  40. _OculusInnerGamepadCamera.prototype.getProjectionMatrix = function () {
  41. BABYLON.Matrix.PerspectiveFovLHToRef(this._aspectRatioFov, this._aspectRatioAspectRatio, this.minZ, this.maxZ, this._workMatrix);
  42. this._workMatrix.multiplyToRef(this._hMatrix, this._projectionMatrix);
  43. return this._projectionMatrix;
  44. };
  45. _OculusInnerGamepadCamera.prototype._getViewMatrix = function () {
  46. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  47. BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
  48. BABYLON.Vector3.TransformNormalToRef(this.upVector, this._cameraRotationMatrix, this._actualUp);
  49. // Computing target and final matrix
  50. this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
  51. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this._actualUp, this._workMatrix);
  52. this._workMatrix.multiplyToRef(this._preViewMatrix, this._viewMatrix);
  53. return this._viewMatrix;
  54. };
  55. return _OculusInnerGamepadCamera;
  56. })(BABYLON.FreeCamera);
  57. var OculusGamepadCamera = (function (_super) {
  58. __extends(OculusGamepadCamera, _super);
  59. function OculusGamepadCamera(name, position, scene) {
  60. var _this = this;
  61. _super.call(this, name, position, scene);
  62. this.angularSensibility = 200;
  63. this.moveSensibility = 75;
  64. this._leftCamera = new _OculusInnerGamepadCamera(name + "_left", position.clone(), scene, true);
  65. this._rightCamera = new _OculusInnerGamepadCamera(name + "_right", position.clone(), scene, false);
  66. this.subCameras.push(this._leftCamera);
  67. this.subCameras.push(this._rightCamera);
  68. this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
  69. this._gamepads = new BABYLON.Gamepads(function (gamepad) {
  70. _this._onNewGameConnected(gamepad);
  71. });
  72. }
  73. OculusGamepadCamera.prototype._onNewGameConnected = function (gamepad) {
  74. // Only the first gamepad can control the camera
  75. if (gamepad.index === 0) {
  76. this._gamepad = gamepad;
  77. }
  78. };
  79. OculusGamepadCamera.prototype._update = function () {
  80. this._leftCamera.position.copyFrom(this.position);
  81. this._rightCamera.position.copyFrom(this.position);
  82. this._updateCamera(this._leftCamera);
  83. this._updateCamera(this._rightCamera);
  84. _super.prototype._update.call(this);
  85. };
  86. OculusGamepadCamera.prototype._checkInputs = function () {
  87. if (!this._gamepad) {
  88. return;
  89. }
  90. var LSValues = this._gamepad.leftStick;
  91. var normalizedLX = LSValues.x / this.moveSensibility;
  92. var normalizedLY = LSValues.y / this.moveSensibility;
  93. LSValues.x = Math.abs(normalizedLX) > 0.005 ? 0 + normalizedLX : 0;
  94. LSValues.y = Math.abs(normalizedLY) > 0.005 ? 0 + normalizedLY : 0;
  95. var cameraTransform = BABYLON.Matrix.RotationYawPitchRoll(this.rotation.y, this.rotation.x, 0);
  96. var deltaTransform = BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(LSValues.x, 0, -LSValues.y), cameraTransform);
  97. this.cameraDirection = this.cameraDirection.add(deltaTransform);
  98. };
  99. OculusGamepadCamera.prototype._updateCamera = function (camera) {
  100. camera.minZ = this.minZ;
  101. camera.maxZ = this.maxZ;
  102. camera.rotation.x = this.rotation.x;
  103. camera.rotation.y = this.rotation.y;
  104. camera.rotation.z = this.rotation.z;
  105. };
  106. // Oculus events
  107. OculusGamepadCamera.prototype._onOrientationEvent = function (evt) {
  108. var yaw = evt.alpha / 180 * Math.PI;
  109. var pitch = evt.beta / 180 * Math.PI;
  110. var roll = evt.gamma / 180 * Math.PI;
  111. if (!this._offsetOrientation) {
  112. this._offsetOrientation = {
  113. yaw: yaw,
  114. pitch: pitch,
  115. roll: roll
  116. };
  117. return;
  118. } else {
  119. this.rotation.y += yaw - this._offsetOrientation.yaw;
  120. this.rotation.x += pitch - this._offsetOrientation.pitch;
  121. this.rotation.z += this._offsetOrientation.roll - roll;
  122. this._offsetOrientation.yaw = yaw;
  123. this._offsetOrientation.pitch = pitch;
  124. this._offsetOrientation.roll = roll;
  125. }
  126. };
  127. OculusGamepadCamera.prototype.attachControl = function (element, noPreventDefault) {
  128. _super.prototype.attachControl.call(this, element, noPreventDefault);
  129. window.addEventListener("deviceorientation", this._deviceOrientationHandler);
  130. };
  131. OculusGamepadCamera.prototype.detachControl = function (element) {
  132. _super.prototype.detachControl.call(this, element);
  133. window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
  134. };
  135. OculusGamepadCamera.prototype.dispose = function () {
  136. this._gamepads.dispose();
  137. _super.prototype.dispose.call(this);
  138. };
  139. return OculusGamepadCamera;
  140. })(BABYLON.FreeCamera);
  141. BABYLON.OculusGamepadCamera = OculusGamepadCamera;
  142. })(BABYLON || (BABYLON = {}));
  143. //# sourceMappingURL=babylon.oculusGamepadCamera.js.map