babylon.oculusOrientedCamera.js 7.9 KB

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