babylon.oculusController.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.OculusOrientedCamera = function (name, position, scene, isLeftEye, ovrSettings, neutralOrientation) {
  5. BABYLON.Camera.call(this, name, position, scene);
  6. this._referenceDirection = new BABYLON.Vector3(0, 0, 1);
  7. this._referenceUp = new BABYLON.Vector3(0, 1, 0);
  8. this._actualDirection = new BABYLON.Vector3(1, 0, 0);
  9. this._actualUp = new BABYLON.Vector3(0, 1, 0);
  10. this._currentTargetPoint = new BABYLON.Vector3(0, 0, 0);
  11. this._currentOrientation = neutralOrientation || { yaw: 0.0, pitch: 0.0, roll: 0.0 };
  12. this._currentViewMatrix = new BABYLON.Matrix();
  13. this._currentOrientationMatrix = new BABYLON.Matrix();
  14. this._tempMatrix = new BABYLON.Matrix();
  15. if (isLeftEye) {
  16. this.viewport = new BABYLON.Viewport(0, 0, 0.5, 1.0);
  17. } else {
  18. this.viewport = new BABYLON.Viewport(0.5, 0, 0.5, 1.0);
  19. }
  20. this._aspectRatioAspectRatio = ovrSettings.HResolution / (2 * ovrSettings.VResolution);
  21. this._aspectRatioFov = (2 * Math.atan((ovrSettings.PostProcessScaleFactor * ovrSettings.VScreenSize) / (2 * ovrSettings.EyeToScreenDistance)));
  22. var hMeters = (ovrSettings.HScreenSize / 4) - (ovrSettings.LensSeparationDistance / 2);
  23. var h = (4 * hMeters) / ovrSettings.HScreenSize;
  24. this._hMatrix = BABYLON.Matrix.Translation(isLeftEye ? h : -h, 0, 0);
  25. this._projectionMatrix = new BABYLON.Matrix();
  26. this._preViewMatrix = BABYLON.Matrix.Translation(isLeftEye ? .5 * ovrSettings.InterpupillaryDistance : -.5 * ovrSettings.InterpupillaryDistance, 0, 0);
  27. new BABYLON.oculusDistortionCorrectionPostProcess("Oculus Distortion", this, !isLeftEye, ovrSettings);
  28. this.resetProjectionMatrix();
  29. this.resetViewMatrix();
  30. };
  31. BABYLON.OculusOrientedCamera.buildOculusStereoCamera = function (scene, name, canvas, minZ, maxZ, position, neutralOrientation, useFXAA, ovrSettings) {
  32. position = position || new BABYLON.Vector2(0, 0);
  33. neutralOrientation = neutralOrientation || { yaw: 0.0, pitch: 0.0, roll: 0.0 };
  34. //var controller = new BABYLON.OculusController();
  35. ovrSettings = ovrSettings || BABYLON.OculusController.CameraSettings_OculusRiftDevKit2013_Metric;
  36. var leftCamera = new BABYLON.OculusOrientedCamera(name + "_left", position, scene, true, ovrSettings, neutralOrientation);
  37. leftCamera.minZ = minZ;
  38. leftCamera.maxZ = maxZ;
  39. if (useFXAA) {
  40. new BABYLON.FxaaPostProcess("fxaa_left", 1.0, leftCamera);
  41. }
  42. var rightCamera = new BABYLON.OculusOrientedCamera(name + "_right", position, scene, false, ovrSettings, neutralOrientation);
  43. rightCamera.minZ = minZ;
  44. rightCamera.maxZ = maxZ;
  45. if (useFXAA) {
  46. new BABYLON.FxaaPostProcess("fxaa_right", 1.0, rightCamera);
  47. }
  48. scene.activeCameras = [];
  49. scene.activeCameras.push(leftCamera);
  50. scene.activeCameras.push(rightCamera);
  51. leftCamera.attachControl(canvas);
  52. rightCamera.attachControl(canvas);
  53. var multiTarget = new BABYLON.inputControllerMultiTarget([leftCamera, rightCamera]);
  54. var controller = new BABYLON.OculusController(scene, multiTarget);
  55. var moveController = new BABYLON.keyboardMoveController(scene, multiTarget);
  56. moveController.attachToCanvas(canvas);
  57. var result = {
  58. leftCamera: leftCamera, rightCamera: rightCamera, intermediateControllerTarget: multiTarget,
  59. oculusController: controller,
  60. keyboardController: moveController
  61. };
  62. result.dispose = function () {
  63. this.leftCamera.detachControl(canvas);
  64. this.rightCamera.detachControl(canvas);
  65. this.leftCamera.dispose();
  66. this.rightCamera.dispose();
  67. this.oculusController.dispose();
  68. this.keyboardController.detachFromCanvas(canvas);
  69. this.keyboardController.dispose();
  70. }.bind(result);
  71. return result;
  72. };
  73. BABYLON.OculusOrientedCamera.prototype = Object.create(BABYLON.Camera.prototype);
  74. BABYLON.OculusOrientedCamera.prototype.resetViewMatrix = function () {
  75. BABYLON.Matrix.RotationYawPitchRollToRef(
  76. this._currentOrientation.yaw,
  77. this._currentOrientation.pitch,
  78. -this._currentOrientation.roll
  79. , this._currentOrientationMatrix);
  80. BABYLON.Vector3.TransformCoordinatesToRef(this._referenceDirection, this._currentOrientationMatrix, this._actualDirection);
  81. BABYLON.Vector3.TransformCoordinatesToRef(this._referenceUp, this._currentOrientationMatrix, this._actualUp);
  82. BABYLON.Vector3.FromFloatsToRef(this.position.x + this._actualDirection.x, this.position.y + this._actualDirection.y, this.position.z + this._actualDirection.z, this._currentTargetPoint);
  83. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTargetPoint, this._actualUp, this._tempMatrix);
  84. this._tempMatrix.multiplyToRef(this._preViewMatrix, this._currentViewMatrix);
  85. return this._currentViewMatrix;
  86. };
  87. BABYLON.OculusOrientedCamera.prototype.getViewMatrix = function () {
  88. return this._currentViewMatrix;
  89. };
  90. BABYLON.OculusOrientedCamera.prototype._update = function () {
  91. //if (!this._referenceOculusOrientation) {
  92. // this._referenceOculusOrientation = { yaw: this._controller._currentOrientation.yaw, pitch: this._controller._currentOrientation.pitch, roll: this._controller._currentOrientation.roll };
  93. //}
  94. //else {
  95. // this._currentOrientation.yaw = this._controller._currentOrientation.yaw - this._referenceOculusOrientation.yaw;
  96. // this._currentOrientation.pitch = this._controller._currentOrientation.pitch - this._referenceOculusOrientation.pitch;
  97. // this._currentOrientation.roll = this._controller._currentOrientation.roll - this._referenceOculusOrientation.roll;
  98. //}
  99. if (this.controllers) {
  100. for (var i = 0; i < this.controllers.length; ++i) {
  101. this.controllers[i].update();
  102. }
  103. }
  104. };
  105. BABYLON.OculusOrientedCamera.prototype.resetProjectionMatrix = function () {
  106. BABYLON.Matrix.PerspectiveFovLHToRef(this._aspectRatioFov, this._aspectRatioAspectRatio, this.minZ, this.maxZ, this._tempMatrix);
  107. this._tempMatrix.multiplyToRef(this._hMatrix, this._projectionMatrix);
  108. return this._projectionMatrix;
  109. };
  110. BABYLON.OculusOrientedCamera.prototype.getProjectionMatrix = function (force) {
  111. return this._projectionMatrix;
  112. };
  113. // implementation of inputControllerTarget
  114. BABYLON.OculusOrientedCamera.prototype.getOrientation = function () {
  115. return this._currentOrientation;
  116. };
  117. BABYLON.OculusOrientedCamera.prototype.getPosition = function () {
  118. return this.position;
  119. };
  120. BABYLON.OculusOrientedCamera.prototype.moveRelative = function (movementVector) {
  121. if (!this._tempMoveVector) {
  122. this._tempMoveVector = new BABYLON.Vector3(0, 0, 0);
  123. }
  124. BABYLON.Vector3.TransformCoordinatesToRef(movementVector, this._currentOrientationMatrix, this._tempMoveVector);
  125. this.position.addInPlace(this._tempMoveVector);
  126. this.resetViewMatrix();
  127. };
  128. BABYLON.OculusOrientedCamera.prototype.rotateRelative = function (rotation) {
  129. this._currentOrientation.yaw += rotation.yaw;
  130. this._currentOrientation.pitch += rotation.pitch;
  131. this._currentOrientation.roll += rotation.roll;
  132. this.resetViewMatrix();
  133. };
  134. })();