babylon.oculusGamepadCamera.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. module BABYLON {
  2. var OculusRiftDevKit2013_Metric = {
  3. HResolution: 1280,
  4. VResolution: 800,
  5. HScreenSize: 0.149759993,
  6. VScreenSize: 0.0935999975,
  7. VScreenCenter: 0.0467999987,
  8. EyeToScreenDistance: 0.0410000011,
  9. LensSeparationDistance: 0.0635000020,
  10. InterpupillaryDistance: 0.0640000030,
  11. DistortionK: [1.0, 0.219999999, 0.239999995, 0.0],
  12. ChromaAbCorrection: [0.995999992, -0.00400000019, 1.01400006, 0.0],
  13. PostProcessScaleFactor: 1.714605507808412,
  14. LensCenterOffset: 0.151976421
  15. };
  16. class _OculusInnerGamepadCamera extends FreeCamera {
  17. private _aspectRatioAspectRatio: number;
  18. private _aspectRatioFov: number;
  19. private _hMatrix: Matrix;
  20. private _workMatrix = new BABYLON.Matrix();
  21. private _preViewMatrix: Matrix;
  22. private _actualUp = new BABYLON.Vector3(0, 0, 0);
  23. constructor(name: string, position: Vector3, scene: Scene, isLeftEye: boolean) {
  24. super(name, position, scene);
  25. // Constants
  26. this._aspectRatioAspectRatio = OculusRiftDevKit2013_Metric.HResolution / (2 * OculusRiftDevKit2013_Metric.VResolution);
  27. this._aspectRatioFov = (2 * Math.atan((OculusRiftDevKit2013_Metric.PostProcessScaleFactor * OculusRiftDevKit2013_Metric.VScreenSize) / (2 * OculusRiftDevKit2013_Metric.EyeToScreenDistance)));
  28. var hMeters = (OculusRiftDevKit2013_Metric.HScreenSize / 4) - (OculusRiftDevKit2013_Metric.LensSeparationDistance / 2);
  29. var h = (4 * hMeters) / OculusRiftDevKit2013_Metric.HScreenSize;
  30. this._hMatrix = BABYLON.Matrix.Translation(isLeftEye ? h : -h, 0, 0);
  31. this.viewport = new BABYLON.Viewport(isLeftEye ? 0 : 0.5, 0, 0.5, 1.0);
  32. this._preViewMatrix = BABYLON.Matrix.Translation(isLeftEye ? .5 * OculusRiftDevKit2013_Metric.InterpupillaryDistance : -.5 * OculusRiftDevKit2013_Metric.InterpupillaryDistance, 0, 0);
  33. // Postprocess
  34. var postProcess = new BABYLON.OculusDistortionCorrectionPostProcess("Oculus Distortion", this, !isLeftEye, OculusRiftDevKit2013_Metric);
  35. }
  36. public getProjectionMatrix(): Matrix {
  37. BABYLON.Matrix.PerspectiveFovLHToRef(this._aspectRatioFov, this._aspectRatioAspectRatio, this.minZ, this.maxZ, this._workMatrix);
  38. this._workMatrix.multiplyToRef(this._hMatrix, this._projectionMatrix);
  39. return this._projectionMatrix;
  40. }
  41. public _getViewMatrix(): Matrix {
  42. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
  43. BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
  44. BABYLON.Vector3.TransformNormalToRef(this.upVector, this._cameraRotationMatrix, this._actualUp);
  45. // Computing target and final matrix
  46. this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
  47. BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this._actualUp, this._workMatrix);
  48. this._workMatrix.multiplyToRef(this._preViewMatrix, this._viewMatrix);
  49. return this._viewMatrix;
  50. }
  51. }
  52. export class OculusGamepadCamera extends FreeCamera {
  53. private _leftCamera: _OculusInnerGamepadCamera;
  54. private _rightCamera: _OculusInnerGamepadCamera;
  55. private _offsetOrientation: { yaw: number; pitch: number; roll: number };
  56. private _deviceOrientationHandler;
  57. private _gamepad: BABYLON.Gamepad;
  58. private _gamepads: BABYLON.Gamepads;
  59. public angularSensibility = 200;
  60. public moveSensibility = 75;
  61. constructor(name: string, position: Vector3, scene: Scene) {
  62. super(name, position, scene);
  63. this._leftCamera = new _OculusInnerGamepadCamera(name + "_left", position.clone(), scene, true);
  64. this._rightCamera = new _OculusInnerGamepadCamera(name + "_right", position.clone(), scene, false);
  65. this.subCameras.push(this._leftCamera);
  66. this.subCameras.push(this._rightCamera);
  67. this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
  68. this._gamepads = new BABYLON.Gamepads((gamepad: BABYLON.Gamepad) => { this._onNewGameConnected(gamepad); });
  69. }
  70. private _onNewGameConnected(gamepad: BABYLON.Gamepad) {
  71. // Only the first gamepad can control the camera
  72. if (gamepad.index === 0) {
  73. this._gamepad = gamepad;
  74. }
  75. }
  76. public _update(): void {
  77. this._leftCamera.position.copyFrom(this.position);
  78. this._rightCamera.position.copyFrom(this.position);
  79. this._updateCamera(this._leftCamera);
  80. this._updateCamera(this._rightCamera);
  81. super._update();
  82. }
  83. public _checkInputs(): void {
  84. if (!this._gamepad) {
  85. return;
  86. }
  87. var LSValues = this._gamepad.leftStick;
  88. var normalizedLX = LSValues.x / this.moveSensibility;
  89. var normalizedLY = LSValues.y / this.moveSensibility;
  90. LSValues.x = Math.abs(normalizedLX) > 0.005 ? 0 + normalizedLX : 0;
  91. LSValues.y = Math.abs(normalizedLY) > 0.005 ? 0 + normalizedLY : 0;
  92. var cameraTransform = BABYLON.Matrix.RotationYawPitchRoll(this.rotation.y, this.rotation.x, 0);
  93. var deltaTransform = BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(LSValues.x, 0, -LSValues.y), cameraTransform);
  94. this.cameraDirection = this.cameraDirection.add(deltaTransform);
  95. }
  96. public _updateCamera(camera: FreeCamera): void {
  97. camera.minZ = this.minZ;
  98. camera.maxZ = this.maxZ;
  99. camera.rotation.x = this.rotation.x;
  100. camera.rotation.y = this.rotation.y;
  101. camera.rotation.z = this.rotation.z;
  102. }
  103. // Oculus events
  104. public _onOrientationEvent(evt: DeviceOrientationEvent): void {
  105. var yaw = evt.alpha / 180 * Math.PI;
  106. var pitch = evt.beta / 180 * Math.PI;
  107. var roll = evt.gamma / 180 * Math.PI;
  108. if (!this._offsetOrientation) {
  109. this._offsetOrientation = {
  110. yaw: yaw,
  111. pitch: pitch,
  112. roll: roll
  113. };
  114. return;
  115. }
  116. else {
  117. this.rotation.y += yaw - this._offsetOrientation.yaw;
  118. this.rotation.x += pitch - this._offsetOrientation.pitch;
  119. this.rotation.z += this._offsetOrientation.roll - roll;
  120. this._offsetOrientation.yaw = yaw;
  121. this._offsetOrientation.pitch = pitch;
  122. this._offsetOrientation.roll = roll;
  123. }
  124. }
  125. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  126. super.attachControl(element, noPreventDefault);
  127. window.addEventListener("deviceorientation", this._deviceOrientationHandler);
  128. }
  129. public detachControl(element: HTMLElement): void {
  130. super.detachControl(element);
  131. window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
  132. }
  133. public dispose(): void {
  134. this._gamepads.dispose();
  135. super.dispose();
  136. }
  137. }
  138. }