babylon.oculusCamera.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.OculusController = function () {
  5. this._currentOrientation = { yaw: 0, pitch: 0, roll: 0 };
  6. this._deviceOrientationHandler = this.onOrientationEvent.bind(this);
  7. window.addEventListener("deviceorientation", this._deviceOrientationHandler);
  8. };
  9. BABYLON.OculusController.prototype.onOrientationEvent = function (ev) {
  10. var yaw = ev.alpha / 180 * Math.PI;
  11. if(!this._referenceYaw){
  12. this._referenceYaw= yaw;
  13. }
  14. this._currentOrientation.yaw = yaw - this._referenceYaw;
  15. this._currentOrientation.pitch = ev.beta / 180 * Math.PI;
  16. this._currentOrientation.roll = ev.gamma / 180 * Math.PI;
  17. };
  18. BABYLON.OculusController.prototype.dispose = function () {
  19. window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
  20. };
  21. BABYLON.OculusController.prototype.getCurrentOrientation = function () {
  22. return this._currentOrientation;
  23. };
  24. BABYLON.OculusController.CameraSettings_OculusRiftDevKit2013_Metric = {
  25. HResolution: 1280,
  26. VResolution: 800,
  27. HScreenSize: 0.149759993,
  28. VScreenSize: 0.0935999975,
  29. VScreenCenter: 0.0467999987,
  30. EyeToScreenDistance: 0.0410000011,
  31. LensSeparationDistance: 0.0635000020,
  32. InterpupillaryDistance: 0.0640000030,
  33. DistortionK: [1.0, 0.219999999, 0.239999995, 0.0],
  34. ChromaAbCorrection: [0.995999992, -0.00400000019, 1.01400006, 0.0],
  35. PostProcessScaleFactor: 1.714605507808412,
  36. LensCenterOffset: 0.151976421
  37. };
  38. BABYLON.OculusOrientedCamera = function (name, position, scene, controller, isLeftEye, ovrSettings, neutralOrientation) {
  39. BABYLON.Camera.call(this, name, position, scene);
  40. this._controller = controller;
  41. this._referenceDirection = new BABYLON.Vector3(0, 0, 1);
  42. this._referenceUp = new BABYLON.Vector3(0, 1, 0);
  43. this._actualDirection = new BABYLON.Vector3(1, 0, 0);
  44. this._actualUp = new BABYLON.Vector3(0, 1, 0);
  45. this._currentTargetPoint = new BABYLON.Vector3(0, 0, 0);
  46. this._currentOculusOrientation = { yaw: 0.0, pitch: 0.0, roll: 0.0 };
  47. this._currentViewMatrix = new BABYLON.Matrix();
  48. this._currentOculusOrientationMatrix = new BABYLON.Matrix();
  49. this._tempMatrix = new BABYLON.Matrix();
  50. neutralOrientation = neutralOrientation || { yaw: 0.0, pitch: 0.0, roll: 0.0 };
  51. this._neutralOrientation = neutralOrientation;
  52. if (isLeftEye) {
  53. this.viewport = new BABYLON.Viewport(0, 0, 0.5, 1.0);
  54. } else {
  55. this.viewport = new BABYLON.Viewport(0.5, 0, 0.5, 1.0);
  56. }
  57. this._aspectRatioAspectRatio = ovrSettings.HResolution / (2 * ovrSettings.VResolution);
  58. this._aspectRatioFov = (2 * Math.atan((ovrSettings.PostProcessScaleFactor * ovrSettings.VScreenSize) / (2 * ovrSettings.EyeToScreenDistance))) ;
  59. var hMeters = (ovrSettings.HScreenSize / 4) - (ovrSettings.LensSeparationDistance / 2);
  60. var h = (4 * hMeters) / ovrSettings.HScreenSize;
  61. this._hMatrix = BABYLON.Matrix.Translation(isLeftEye ? h : -h, 0, 0);
  62. this._projectionMatrix = new BABYLON.Matrix();
  63. this._preViewMatrix = BABYLON.Matrix.Translation(isLeftEye ? .5 * ovrSettings.InterpupillaryDistance : -.5 * ovrSettings.InterpupillaryDistance, 0, 0);
  64. new BABYLON.oculusDistortionCorrectionPostProcess("Oculus Distortion", this, !isLeftEye, ovrSettings);
  65. };
  66. BABYLON.OculusOrientedCamera.buildOculusStereoCamera = function (scene, name, canvas, minZ, maxZ, position, neutralOrientation, useFXAA, controller, ovrSettings) {
  67. position = position || new BABYLON.Vector2(0, 0);
  68. neutralOrientation = neutralOrientation || { yaw: 0.0, pitch: 0.0, roll: 0.0 };
  69. controller = controller || new BABYLON.OculusController();
  70. ovrSettings = ovrSettings || BABYLON.OculusController.CameraSettings_OculusRiftDevKit2013_Metric;
  71. var leftCamera = new BABYLON.OculusOrientedCamera(name + "_left", position, scene, controller, true, ovrSettings, neutralOrientation);
  72. leftCamera.minZ = minZ;
  73. leftCamera.maxZ = maxZ;
  74. if (useFXAA) {
  75. new BABYLON.FxaaPostProcess("fxaa_left", 1.0, leftCamera);
  76. }
  77. var rightCamera = new BABYLON.OculusOrientedCamera(name + "_right", position, scene, controller, false, ovrSettings, neutralOrientation);
  78. rightCamera.minZ = minZ;
  79. rightCamera.maxZ = maxZ;
  80. if (useFXAA) {
  81. new BABYLON.FxaaPostProcess("fxaa_right", 1.0, rightCamera);
  82. }
  83. scene.activeCameras = [];
  84. scene.activeCameras.push(leftCamera);
  85. scene.activeCameras.push(rightCamera);
  86. leftCamera.attachControl(canvas);
  87. rightCamera.attachControl(canvas);
  88. };
  89. BABYLON.OculusOrientedCamera.prototype = Object.create(BABYLON.Camera.prototype);
  90. BABYLON.OculusOrientedCamera.prototype.getViewMatrix = function () {
  91. BABYLON.Matrix.RotationYawPitchRollToRef(
  92. this._currentOculusOrientation.yaw + this._neutralOrientation.yaw,
  93. this._currentOculusOrientation.pitch + this._neutralOrientation.pitch,
  94. -this._currentOculusOrientation.roll + this._neutralOrientation.roll
  95. , this._currentOculusOrientationMatrix);
  96. BABYLON.Vector3.TransformCoordinatesToRef(this._referenceDirection, this._currentOculusOrientationMatrix, this._actualDirection);
  97. BABYLON.Vector3.TransformCoordinatesToRef(this._referenceUp, this._currentOculusOrientationMatrix, this._actualUp);
  98. BABYLON.Vector3.FromFloatsToRef(this.position.x + this._actualDirection.x, this.position.y + this._actualDirection.y, this.position.z + this._actualDirection.z, this._currentTargetPoint);
  99. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTargetPoint, this._actualUp, this._tempMatrix);
  100. this._tempMatrix.multiplyToRef(this._preViewMatrix, this._currentViewMatrix);
  101. return this._currentViewMatrix;
  102. };
  103. BABYLON.OculusOrientedCamera.prototype._update = function () {
  104. if (!this._referenceOculusOrientation) {
  105. this._referenceOculusOrientation = { yaw: this._controller._currentOrientation.yaw, pitch: this._controller._currentOrientation.pitch, roll: this._controller._currentOrientation.roll };
  106. }
  107. else {
  108. this._currentOculusOrientation.yaw = this._controller._currentOrientation.yaw - this._referenceOculusOrientation.yaw;
  109. this._currentOculusOrientation.pitch = this._controller._currentOrientation.pitch - this._referenceOculusOrientation.pitch;
  110. this._currentOculusOrientation.roll = this._controller._currentOrientation.roll - this._referenceOculusOrientation.roll;
  111. }
  112. };
  113. BABYLON.OculusOrientedCamera.prototype.getProjectionMatrix = function (force) {
  114. BABYLON.Matrix.PerspectiveFovLHToRef(this._aspectRatioFov, this._aspectRatioAspectRatio, this.minZ, this.maxZ, this._tempMatrix);
  115. this._tempMatrix.multiplyToRef(this._hMatrix, this._projectionMatrix);
  116. return this._projectionMatrix;
  117. };
  118. })();