babylon.oculusOrientedCamera.js 7.8 KB

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