babylon.oculusOrientedCamera.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 = Object.create(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, disableGravity,disableCollisions, 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 moveTarget = multiTarget;
  56. if (!disableCollisions) {
  57. var collisionFilter = new BABYLON.inputCollisionFilter(scene, multiTarget);
  58. moveTarget = collisionFilter;
  59. }
  60. if (!disableGravity) {
  61. var globalAxisFactorFilter = new BABYLON.globalAxisFactorsFilter(scene, moveTarget, 1, 0, 1);
  62. var gravityController = new BABYLON.GravityInputController(scene, moveTarget);
  63. moveTarget = globalAxisFactorFilter;
  64. }
  65. var moveController = new BABYLON.keyboardMoveController(scene, moveTarget);
  66. moveController.attachToCanvas(canvas);
  67. var result = {
  68. leftCamera: leftCamera, rightCamera: rightCamera, intermediateControllerTarget: multiTarget,
  69. oculusController: controller,
  70. keyboardController: moveController
  71. };
  72. result.dispose = function () {
  73. this.leftCamera.detachControl(canvas);
  74. this.rightCamera.detachControl(canvas);
  75. this.leftCamera.dispose();
  76. this.rightCamera.dispose();
  77. this.oculusController.dispose();
  78. this.keyboardController.detachFromCanvas(canvas);
  79. this.keyboardController.dispose();
  80. }.bind(result);
  81. return result;
  82. };
  83. BABYLON.OculusOrientedCamera.prototype = Object.create(BABYLON.Camera.prototype);
  84. BABYLON.OculusOrientedCamera.prototype.resetViewMatrix = function () {
  85. BABYLON.Matrix.RotationYawPitchRollToRef(
  86. this._currentOrientation.yaw,
  87. this._currentOrientation.pitch,
  88. -this._currentOrientation.roll
  89. , this._currentOrientationMatrix);
  90. BABYLON.Vector3.TransformNormalToRef(this._referenceDirection, this._currentOrientationMatrix, this._actualDirection);
  91. BABYLON.Vector3.TransformNormalToRef(this._referenceUp, this._currentOrientationMatrix, this._actualUp);
  92. BABYLON.Vector3.FromFloatsToRef(this.position.x + this._actualDirection.x, this.position.y + this._actualDirection.y, this.position.z + this._actualDirection.z, this._currentTargetPoint);
  93. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTargetPoint, this._actualUp, this._tempMatrix);
  94. this._tempMatrix.multiplyToRef(this._preViewMatrix, this._currentViewMatrix);
  95. return this._currentViewMatrix;
  96. };
  97. BABYLON.OculusOrientedCamera.prototype.getViewMatrix = function () {
  98. return this._currentViewMatrix;
  99. };
  100. BABYLON.OculusOrientedCamera.prototype._update = function () {
  101. if (this.controllers) {
  102. for (var i = 0; i < this.controllers.length; ++i) {
  103. this.controllers[i].update();
  104. }
  105. }
  106. };
  107. BABYLON.OculusOrientedCamera.prototype.resetProjectionMatrix = function () {
  108. BABYLON.Matrix.PerspectiveFovLHToRef(this._aspectRatioFov, this._aspectRatioAspectRatio, this.minZ, this.maxZ, this._tempMatrix);
  109. this._tempMatrix.multiplyToRef(this._hMatrix, this._projectionMatrix);
  110. return this._projectionMatrix;
  111. };
  112. BABYLON.OculusOrientedCamera.prototype.getProjectionMatrix = function (force) {
  113. return this._projectionMatrix;
  114. };
  115. // implementation of inputControllerTarget
  116. BABYLON.OculusOrientedCamera.prototype.getOrientation = function () {
  117. return this._currentOrientation;
  118. };
  119. BABYLON.OculusOrientedCamera.prototype.getPosition = function () {
  120. return this.position;
  121. };
  122. BABYLON.OculusOrientedCamera.prototype.moveRelative = function (movementVector) {
  123. if (!this._tempMoveVector) {
  124. this._tempMoveVector = new BABYLON.Vector3(0, 0, 0);
  125. }
  126. BABYLON.Vector3.TransformNormalToRef(movementVector, this._currentOrientationMatrix, this._tempMoveVector);
  127. this.position.addInPlace(this._tempMoveVector);
  128. this.resetViewMatrix();
  129. };
  130. BABYLON.OculusOrientedCamera.prototype.rotateRelative = function (rotation) {
  131. this._currentOrientation.yaw += rotation.yaw;
  132. this._currentOrientation.pitch += rotation.pitch;
  133. this._currentOrientation.roll += rotation.roll;
  134. this.resetViewMatrix();
  135. };
  136. })();