babylon.oculusCamera.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 _OculusInnerCamera 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 OculusCamera extends FreeCamera {
  53. private _leftCamera: _OculusInnerCamera;
  54. private _rightCamera: _OculusInnerCamera;
  55. private _offsetOrientation: { yaw: number; pitch: number; roll: number };
  56. private _deviceOrientationHandler;
  57. constructor(name: string, position: Vector3, scene: Scene) {
  58. super(name, position, scene);
  59. this._leftCamera = new _OculusInnerCamera(name + "_left", position.clone(), scene, true);
  60. this._rightCamera = new _OculusInnerCamera(name + "_right", position.clone(), scene, false);
  61. this.subCameras.push(this._leftCamera);
  62. this.subCameras.push(this._rightCamera);
  63. this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
  64. }
  65. public _update(): void {
  66. this._leftCamera.position.copyFrom(this.position);
  67. this._rightCamera.position.copyFrom(this.position);
  68. this._updateCamera(this._leftCamera);
  69. this._updateCamera(this._rightCamera);
  70. super._update();
  71. }
  72. public _updateCamera(camera: FreeCamera): void {
  73. camera.minZ = this.minZ;
  74. camera.maxZ = this.maxZ;
  75. camera.rotation.x = this.rotation.x;
  76. camera.rotation.y = this.rotation.y;
  77. camera.rotation.z = this.rotation.z;
  78. }
  79. // Oculus events
  80. public _onOrientationEvent(evt: DeviceOrientationEvent): void {
  81. var yaw = evt.alpha / 180 * Math.PI;
  82. var pitch = evt.beta / 180 * Math.PI;
  83. var roll = evt.gamma / 180 * Math.PI;
  84. if (!this._offsetOrientation) {
  85. this._offsetOrientation = {
  86. yaw: yaw,
  87. pitch: pitch,
  88. roll: roll
  89. };
  90. return;
  91. }
  92. else {
  93. this.rotation.y += yaw - this._offsetOrientation.yaw;
  94. this.rotation.x += pitch - this._offsetOrientation.pitch;
  95. this.rotation.z += this._offsetOrientation.roll - roll;
  96. this._offsetOrientation.yaw = yaw;
  97. this._offsetOrientation.pitch = pitch;
  98. this._offsetOrientation.roll = roll;
  99. }
  100. }
  101. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  102. super.attachControl(element, noPreventDefault);
  103. window.addEventListener("deviceorientation", this._deviceOrientationHandler);
  104. }
  105. public detachControl(element: HTMLElement): void {
  106. super.detachControl(element);
  107. window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
  108. }
  109. }
  110. }