babylon.oculusOrientedCamera.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.OculusController = function (scene, target) {
  5. BABYLON.inputController.call(this, scene, target);
  6. this._deviceOrientationHandler = this.onOrientationEvent.bind(this);
  7. this._tempOrientation = { yaw: 0.0, pitch: 0.0, roll: 0.0 };
  8. this._relativeOrientation = { yaw: 0.0, pitch: 0.0, roll: 0.0 };
  9. window.addEventListener("deviceorientation", this._deviceOrientationHandler);
  10. };
  11. BABYLON.OculusController.prototype = Object.create(BABYLON.inputController.prototype);
  12. BABYLON.OculusController.prototype.onOrientationEvent = function (ev) {
  13. this._tempOrientation.yaw = ev.alpha / 180 * Math.PI;
  14. this._tempOrientation.pitch = ev.beta / 180 * Math.PI;
  15. this._tempOrientation.roll = ev.gamma / 180 * Math.PI;
  16. if (!this._lastOrientation) {
  17. this._lastOrientation = Object.create(this._tempOrientation);
  18. }
  19. else {
  20. this._relativeOrientation.yaw = this._tempOrientation.yaw - this._lastOrientation.yaw;
  21. this._relativeOrientation.pitch = this._tempOrientation.pitch - this._lastOrientation.pitch;
  22. this._relativeOrientation.roll = this._tempOrientation.roll - this._lastOrientation.roll;
  23. var temp = this._tempOrientation;
  24. this._tempOrientation = this._lastOrientation;
  25. this._lastOrientation = temp;
  26. this.target.rotateRelative(this._relativeOrientation);
  27. }
  28. };
  29. BABYLON.OculusController.prototype.dispose = function () {
  30. window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
  31. };
  32. BABYLON.OculusController.CameraSettings_OculusRiftDevKit2013_Metric = {
  33. HResolution: 1280,
  34. VResolution: 800,
  35. HScreenSize: 0.149759993,
  36. VScreenSize: 0.0935999975,
  37. VScreenCenter: 0.0467999987,
  38. EyeToScreenDistance: 0.0410000011,
  39. LensSeparationDistance: 0.0635000020,
  40. InterpupillaryDistance: 0.0640000030,
  41. DistortionK: [1.0, 0.219999999, 0.239999995, 0.0],
  42. ChromaAbCorrection: [0.995999992, -0.00400000019, 1.01400006, 0.0],
  43. PostProcessScaleFactor: 1.714605507808412,
  44. LensCenterOffset: 0.151976421
  45. };
  46. BABYLON.OculusOrientedCamera = function (name, position, scene, isLeftEye, ovrSettings, neutralOrientation) {
  47. BABYLON.Camera.call(this, name, position, scene);
  48. this._referenceDirection = new BABYLON.Vector3(0, 0, 1);
  49. this._referenceUp = new BABYLON.Vector3(0, 1, 0);
  50. this._actualDirection = new BABYLON.Vector3(1, 0, 0);
  51. this._actualUp = new BABYLON.Vector3(0, 1, 0);
  52. this._currentTargetPoint = new BABYLON.Vector3(0, 0, 0);
  53. this._currentOrientation = neutralOrientation || { yaw: 0.0, pitch: 0.0, roll: 0.0 };
  54. this._currentViewMatrix = new BABYLON.Matrix();
  55. this._currentOrientationMatrix = new BABYLON.Matrix();
  56. this._tempMatrix = new BABYLON.Matrix();
  57. if (isLeftEye) {
  58. this.viewport = new BABYLON.Viewport(0, 0, 0.5, 1.0);
  59. } else {
  60. this.viewport = new BABYLON.Viewport(0.5, 0, 0.5, 1.0);
  61. }
  62. this._aspectRatioAspectRatio = ovrSettings.HResolution / (2 * ovrSettings.VResolution);
  63. this._aspectRatioFov = (2 * Math.atan((ovrSettings.PostProcessScaleFactor * ovrSettings.VScreenSize) / (2 * ovrSettings.EyeToScreenDistance)));
  64. var hMeters = (ovrSettings.HScreenSize / 4) - (ovrSettings.LensSeparationDistance / 2);
  65. var h = (4 * hMeters) / ovrSettings.HScreenSize;
  66. this._hMatrix = BABYLON.Matrix.Translation(isLeftEye ? h : -h, 0, 0);
  67. this._projectionMatrix = new BABYLON.Matrix();
  68. this._preViewMatrix = BABYLON.Matrix.Translation(isLeftEye ? .5 * ovrSettings.InterpupillaryDistance : -.5 * ovrSettings.InterpupillaryDistance, 0, 0);
  69. new BABYLON.oculusDistortionCorrectionPostProcess("Oculus Distortion", this, !isLeftEye, ovrSettings);
  70. this.resetProjectionMatrix();
  71. this.resetViewMatrix();
  72. };
  73. BABYLON.OculusOrientedCamera.buildOculusStereoCamera = function (scene, name, canvas, minZ, maxZ, position, neutralOrientation, useFXAA, ovrSettings) {
  74. position = position || new BABYLON.Vector2(0, 0);
  75. neutralOrientation = neutralOrientation || { yaw: 0.0, pitch: 0.0, roll: 0.0 };
  76. //var controller = new BABYLON.OculusController();
  77. ovrSettings = ovrSettings || BABYLON.OculusController.CameraSettings_OculusRiftDevKit2013_Metric;
  78. var leftCamera = new BABYLON.OculusOrientedCamera(name + "_left", position, scene, true, ovrSettings, neutralOrientation);
  79. leftCamera.minZ = minZ;
  80. leftCamera.maxZ = maxZ;
  81. if (useFXAA) {
  82. new BABYLON.FxaaPostProcess("fxaa_left", 1.0, leftCamera);
  83. }
  84. var rightCamera = new BABYLON.OculusOrientedCamera(name + "_right", position, scene, false, ovrSettings, neutralOrientation);
  85. rightCamera.minZ = minZ;
  86. rightCamera.maxZ = maxZ;
  87. if (useFXAA) {
  88. new BABYLON.FxaaPostProcess("fxaa_right", 1.0, rightCamera);
  89. }
  90. scene.activeCameras = [];
  91. scene.activeCameras.push(leftCamera);
  92. scene.activeCameras.push(rightCamera);
  93. leftCamera.attachControl(canvas);
  94. rightCamera.attachControl(canvas);
  95. var multiTarget = new BABYLON.inputControllerMultiTarget([leftCamera, rightCamera]);
  96. var controller = new BABYLON.OculusController(scene, multiTarget);
  97. var moveController = new BABYLON.keyboardMoveController(scene, multiTarget);
  98. moveController.attachToCanvas(canvas);
  99. var result = {
  100. leftCamera: leftCamera, rightCamera: rightCamera, intermediateControllerTarget: multiTarget,
  101. oculusController: controller,
  102. keyboardController: moveController
  103. };
  104. result.dispose = function () {
  105. this.leftCamera.detachControl(canvas);
  106. this.rightCamera.detachControl(canvas);
  107. this.leftCamera.dispose();
  108. this.rightCamera.dispose();
  109. this.oculusController.dispose();
  110. this.keyboardController.detachFromCanvas(canvas);
  111. this.keyboardController.dispose();
  112. }.bind(result);
  113. return result;
  114. };
  115. BABYLON.OculusOrientedCamera.prototype = Object.create(BABYLON.Camera.prototype);
  116. BABYLON.OculusOrientedCamera.prototype.resetViewMatrix = function () {
  117. BABYLON.Matrix.RotationYawPitchRollToRef(
  118. this._currentOrientation.yaw,
  119. this._currentOrientation.pitch,
  120. -this._currentOrientation.roll
  121. , this._currentOrientationMatrix);
  122. BABYLON.Vector3.TransformCoordinatesToRef(this._referenceDirection, this._currentOrientationMatrix, this._actualDirection);
  123. BABYLON.Vector3.TransformCoordinatesToRef(this._referenceUp, this._currentOrientationMatrix, this._actualUp);
  124. BABYLON.Vector3.FromFloatsToRef(this.position.x + this._actualDirection.x, this.position.y + this._actualDirection.y, this.position.z + this._actualDirection.z, this._currentTargetPoint);
  125. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTargetPoint, this._actualUp, this._tempMatrix);
  126. this._tempMatrix.multiplyToRef(this._preViewMatrix, this._currentViewMatrix);
  127. return this._currentViewMatrix;
  128. };
  129. BABYLON.OculusOrientedCamera.prototype.getViewMatrix = function () {
  130. return this._currentViewMatrix;
  131. };
  132. BABYLON.OculusOrientedCamera.prototype._update = function () {
  133. //if (!this._referenceOculusOrientation) {
  134. // this._referenceOculusOrientation = { yaw: this._controller._currentOrientation.yaw, pitch: this._controller._currentOrientation.pitch, roll: this._controller._currentOrientation.roll };
  135. //}
  136. //else {
  137. // this._currentOrientation.yaw = this._controller._currentOrientation.yaw - this._referenceOculusOrientation.yaw;
  138. // this._currentOrientation.pitch = this._controller._currentOrientation.pitch - this._referenceOculusOrientation.pitch;
  139. // this._currentOrientation.roll = this._controller._currentOrientation.roll - this._referenceOculusOrientation.roll;
  140. //}
  141. if (this.controllers) {
  142. for (var i = 0; i < this.controllers.length; ++i) {
  143. this.controllers[i].update();
  144. }
  145. }
  146. };
  147. BABYLON.OculusOrientedCamera.prototype.resetProjectionMatrix = function () {
  148. BABYLON.Matrix.PerspectiveFovLHToRef(this._aspectRatioFov, this._aspectRatioAspectRatio, this.minZ, this.maxZ, this._tempMatrix);
  149. this._tempMatrix.multiplyToRef(this._hMatrix, this._projectionMatrix);
  150. return this._projectionMatrix;
  151. };
  152. BABYLON.OculusOrientedCamera.prototype.getProjectionMatrix = function (force) {
  153. return this._projectionMatrix;
  154. };
  155. // implementation of inputControllerTarget
  156. BABYLON.OculusOrientedCamera.prototype.getOrientation = function () {
  157. return this._currentOrientation;
  158. };
  159. BABYLON.OculusOrientedCamera.prototype.getPosition = function () {
  160. return this.position;
  161. };
  162. BABYLON.OculusOrientedCamera.prototype.moveRelative = function (movementVector) {
  163. if (!this._tempMoveVector) {
  164. this._tempMoveVector = new BABYLON.Vector3(0, 0, 0);
  165. }
  166. BABYLON.Vector3.TransformCoordinatesToRef(movementVector, this._currentOrientationMatrix, this._tempMoveVector);
  167. this.position.addInPlace(this._tempMoveVector);
  168. this.resetViewMatrix();
  169. };
  170. BABYLON.OculusOrientedCamera.prototype.rotateRelative = function (rotation) {
  171. this._currentOrientation.yaw += rotation.yaw;
  172. this._currentOrientation.pitch += rotation.pitch;
  173. this._currentOrientation.roll += rotation.roll;
  174. this.resetViewMatrix();
  175. };
  176. })();