babylon.oculusCamera.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 _OculusInnerCamera = (function (_super) {
  24. __extends(_OculusInnerCamera, _super);
  25. function _OculusInnerCamera(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. _OculusInnerCamera.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. _OculusInnerCamera.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 _OculusInnerCamera;
  56. })(BABYLON.FreeCamera);
  57. var OculusCamera = (function (_super) {
  58. __extends(OculusCamera, _super);
  59. function OculusCamera(name, position, scene) {
  60. _super.call(this, name, position, scene);
  61. this._leftCamera = new _OculusInnerCamera(name + "_left", position.clone(), scene, true);
  62. this._rightCamera = new _OculusInnerCamera(name + "_right", position.clone(), scene, false);
  63. this.subCameras.push(this._leftCamera);
  64. this.subCameras.push(this._rightCamera);
  65. this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
  66. }
  67. OculusCamera.prototype._update = function () {
  68. this._leftCamera.position.copyFrom(this.position);
  69. this._rightCamera.position.copyFrom(this.position);
  70. this._updateCamera(this._leftCamera);
  71. this._updateCamera(this._rightCamera);
  72. _super.prototype._update.call(this);
  73. };
  74. OculusCamera.prototype._updateCamera = function (camera) {
  75. camera.minZ = this.minZ;
  76. camera.maxZ = this.maxZ;
  77. camera.rotation.x = this.rotation.x;
  78. camera.rotation.y = this.rotation.y;
  79. camera.rotation.z = this.rotation.z;
  80. };
  81. // Oculus events
  82. OculusCamera.prototype._onOrientationEvent = function (evt) {
  83. var yaw = evt.alpha / 180 * Math.PI;
  84. var pitch = evt.beta / 180 * Math.PI;
  85. var roll = evt.gamma / 180 * Math.PI;
  86. if (!this._offsetOrientation) {
  87. this._offsetOrientation = {
  88. yaw: yaw,
  89. pitch: pitch,
  90. roll: roll
  91. };
  92. return;
  93. }
  94. else {
  95. this.rotation.y += yaw - this._offsetOrientation.yaw;
  96. this.rotation.x += pitch - this._offsetOrientation.pitch;
  97. this.rotation.z += this._offsetOrientation.roll - roll;
  98. this._offsetOrientation.yaw = yaw;
  99. this._offsetOrientation.pitch = pitch;
  100. this._offsetOrientation.roll = roll;
  101. }
  102. };
  103. OculusCamera.prototype.attachControl = function (element, noPreventDefault) {
  104. _super.prototype.attachControl.call(this, element, noPreventDefault);
  105. window.addEventListener("deviceorientation", this._deviceOrientationHandler);
  106. };
  107. OculusCamera.prototype.detachControl = function (element) {
  108. _super.prototype.detachControl.call(this, element);
  109. window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
  110. };
  111. return OculusCamera;
  112. })(BABYLON.FreeCamera);
  113. BABYLON.OculusCamera = OculusCamera;
  114. })(BABYLON || (BABYLON = {}));
  115. //# sourceMappingURL=babylon.oculusCamera.js.map