babylon.oculusOrientedCamera.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.OculusOrientedCamera = function (name, position, scene, isLeftEye, ovrSettings, neutralOrientation) {
  5. // OculusOrientedCamera is usually built 2 times using the same position
  6. // So I duplicate the position to avoid errors from physics system (which could manipulate it twice per frame)
  7. position = position ? new BABYLON.Vector3(position.x, position.y, position.z) : null;
  8. BABYLON.Camera.call(this, name, position, scene);
  9. this._referenceDirection = new BABYLON.Vector3(0, 0, 1);
  10. this._referenceUp = new BABYLON.Vector3(0, 1, 0);
  11. this._actualDirection = new BABYLON.Vector3(1, 0, 0);
  12. this._actualUp = new BABYLON.Vector3(0, 1, 0);
  13. this._currentTargetPoint = new BABYLON.Vector3(0, 0, 0);
  14. this._currentOrientation = Object.create(neutralOrientation || { yaw: 0.0, pitch: 0.0, roll: 0.0 });
  15. this._currentViewMatrix = new BABYLON.Matrix();
  16. this._currentOrientationMatrix = new BABYLON.Matrix();
  17. this._currentInvertOrientationMatrix = new BABYLON.Matrix();
  18. this._tempMatrix = new BABYLON.Matrix();
  19. if (isLeftEye) {
  20. this.viewport = new BABYLON.Viewport(0, 0, 0.5, 1.0);
  21. } else {
  22. this.viewport = new BABYLON.Viewport(0.5, 0, 0.5, 1.0);
  23. }
  24. this._aspectRatioAspectRatio = ovrSettings.HResolution / (2 * ovrSettings.VResolution);
  25. this._aspectRatioFov = (2 * Math.atan((ovrSettings.PostProcessScaleFactor * ovrSettings.VScreenSize) / (2 * ovrSettings.EyeToScreenDistance)));
  26. var hMeters = (ovrSettings.HScreenSize / 4) - (ovrSettings.LensSeparationDistance / 2);
  27. var h = (4 * hMeters) / ovrSettings.HScreenSize;
  28. this._hMatrix = BABYLON.Matrix.Translation(isLeftEye ? h : -h, 0, 0);
  29. this._projectionMatrix = new BABYLON.Matrix();
  30. this._preViewMatrix = BABYLON.Matrix.Translation(isLeftEye ? .5 * ovrSettings.InterpupillaryDistance : -.5 * ovrSettings.InterpupillaryDistance, 0, 0);
  31. new BABYLON.OculusDistortionCorrectionPostProcess("Oculus Distortion", this, !isLeftEye, ovrSettings);
  32. this.resetProjectionMatrix();
  33. this.resetViewMatrix();
  34. };
  35. BABYLON.OculusOrientedCamera.BuildOculusStereoCamera = function (scene, name, minZ, maxZ, position, neutralOrientation, useFXAA, disableGravity, disableCollisions, collisionEllipsoid, ovrSettings) {
  36. var canvas = scene.getEngine().getRenderingCanvas();
  37. position = position || BABYLON.Vector3.Zero(0, 0, 0);
  38. neutralOrientation = neutralOrientation || { yaw: 0.0, pitch: 0.0, roll: 0.0 };
  39. //var controller = new BABYLON.OculusController();
  40. ovrSettings = ovrSettings || BABYLON.OculusController.CameraSettings_OculusRiftDevKit2013_Metric;
  41. var leftCamera = new BABYLON.OculusOrientedCamera(name + "_left", position, scene, true, ovrSettings, neutralOrientation);
  42. leftCamera.minZ = minZ;
  43. leftCamera.maxZ = maxZ;
  44. if (useFXAA) {
  45. new BABYLON.FxaaPostProcess("fxaa_left", 1.0, leftCamera);
  46. }
  47. var rightCamera = new BABYLON.OculusOrientedCamera(name + "_right", position.clone(), scene, false, ovrSettings, neutralOrientation);
  48. rightCamera.minZ = minZ;
  49. rightCamera.maxZ = maxZ;
  50. if (useFXAA) {
  51. new BABYLON.FxaaPostProcess("fxaa_right", 1.0, rightCamera);
  52. }
  53. scene.activeCameras = [];
  54. scene.activeCameras.push(leftCamera);
  55. scene.activeCameras.push(rightCamera);
  56. leftCamera.attachControl(canvas);
  57. rightCamera.attachControl(canvas);
  58. var multiTarget = new BABYLON.InputControllerMultiTarget([leftCamera, rightCamera]);
  59. var controller = new BABYLON.OculusController(scene, multiTarget);
  60. var moveTarget = multiTarget;
  61. if (!disableCollisions) {
  62. var collisionFilter = new BABYLON.InputCollisionFilter(scene, multiTarget, collisionEllipsoid);
  63. moveTarget = collisionFilter;
  64. }
  65. if (!disableGravity) {
  66. var globalAxisFactorFilter = new BABYLON.GlobalAxisFactorsFilter(scene, moveTarget, 1, 0, 1);
  67. var gravityController = new BABYLON.GravityInputController(scene, moveTarget);
  68. moveTarget = globalAxisFactorFilter;
  69. }
  70. var moveController = new BABYLON.KeyboardMoveController(scene, moveTarget);
  71. moveController.attachToCanvas(canvas);
  72. var result = {
  73. leftCamera: leftCamera, rightCamera: rightCamera, intermediateControllerTarget: multiTarget,
  74. oculusController: controller,
  75. keyboardController: moveController
  76. };
  77. result.dispose = function () {
  78. this.leftCamera.detachControl(canvas);
  79. this.rightCamera.detachControl(canvas);
  80. this.leftCamera.dispose();
  81. this.rightCamera.dispose();
  82. this.oculusController.dispose();
  83. this.keyboardController.detachFromCanvas(canvas);
  84. this.keyboardController.dispose();
  85. }.bind(result);
  86. return result;
  87. };
  88. BABYLON.OculusOrientedCamera.prototype = Object.create(BABYLON.Camera.prototype);
  89. BABYLON.OculusOrientedCamera.prototype.resetViewMatrix = function () {
  90. BABYLON.Matrix.RotationYawPitchRollToRef(
  91. this._currentOrientation.yaw,
  92. this._currentOrientation.pitch,
  93. -this._currentOrientation.roll
  94. , this._currentOrientationMatrix);
  95. this._currentOrientationMatrix.invertToRef(this._currentInvertOrientationMatrix);
  96. BABYLON.Vector3.TransformNormalToRef(this._referenceDirection, this._currentOrientationMatrix, this._actualDirection);
  97. BABYLON.Vector3.TransformNormalToRef(this._referenceUp, this._currentOrientationMatrix, 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.getViewMatrix = function () {
  104. return this._currentViewMatrix;
  105. };
  106. BABYLON.OculusOrientedCamera.prototype._update = function () {
  107. if (this.controllers) {
  108. for (var i = 0; i < this.controllers.length; ++i) {
  109. this.controllers[i].update();
  110. }
  111. }
  112. };
  113. BABYLON.OculusOrientedCamera.prototype.getOrientationMatrix = function () {
  114. return this._currentOrientationMatrix;
  115. };
  116. BABYLON.OculusOrientedCamera.prototype.getInvertOrientationMatrix = function () {
  117. return this._currentInvertOrientationMatrix;
  118. };
  119. BABYLON.OculusOrientedCamera.prototype.resetProjectionMatrix = function () {
  120. BABYLON.Matrix.PerspectiveFovLHToRef(this._aspectRatioFov, this._aspectRatioAspectRatio, this.minZ, this.maxZ, this._tempMatrix);
  121. this._tempMatrix.multiplyToRef(this._hMatrix, this._projectionMatrix);
  122. return this._projectionMatrix;
  123. };
  124. BABYLON.OculusOrientedCamera.prototype.getProjectionMatrix = function (force) {
  125. return this._projectionMatrix;
  126. };
  127. // implementation of InputControllerTarget
  128. BABYLON.OculusOrientedCamera.prototype.getOrientation = function () {
  129. return this._currentOrientation;
  130. };
  131. BABYLON.OculusOrientedCamera.prototype.getPosition = function () {
  132. return this.position;
  133. };
  134. BABYLON.OculusOrientedCamera.prototype.moveRelative = function (movementVector) {
  135. if (!this._tempMoveVector) {
  136. this._tempMoveVector = new BABYLON.Vector3(0, 0, 0);
  137. }
  138. BABYLON.Vector3.TransformNormalToRef(movementVector, this._currentOrientationMatrix, this._tempMoveVector);
  139. this.position.addInPlace(this._tempMoveVector);
  140. this.resetViewMatrix();
  141. };
  142. BABYLON.OculusOrientedCamera.prototype.rotateRelative = function (rotation) {
  143. this._currentOrientation.yaw += rotation.yaw;
  144. this._currentOrientation.pitch += rotation.pitch;
  145. this._currentOrientation.roll += rotation.roll;
  146. this.resetViewMatrix();
  147. };
  148. })();