babylon.vrDistortionCorrectionPostProcess.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. module BABYLON {
  2. export class VRDistortionCorrectionPostProcess extends PostProcess {
  3. public aspectRatio: number;
  4. private _isRightEye: boolean;
  5. private _distortionFactors: number[];
  6. private _postProcessScaleFactor: number;
  7. private _lensCenterOffset: number;
  8. private _scaleIn: Vector2;
  9. private _scaleFactor: Vector2;
  10. private _lensCenter: Vector2;
  11. //ANY
  12. constructor(name: string, camera: Camera, isRightEye: boolean, vrMetrics: VRCameraMetrics) {
  13. super(name, "vrDistortionCorrection", [
  14. 'LensCenter',
  15. 'Scale',
  16. 'ScaleIn',
  17. 'HmdWarpParam'
  18. ], null, vrMetrics.postProcessScaleFactor, camera, BABYLON.Texture.BILINEAR_SAMPLINGMODE, null, null);
  19. this._isRightEye = isRightEye;
  20. this._distortionFactors = vrMetrics.distortionK;
  21. this._postProcessScaleFactor = vrMetrics.postProcessScaleFactor;
  22. this._lensCenterOffset = vrMetrics.lensCenterOffset;
  23. this.onSizeChanged = () => {
  24. this.aspectRatio = this.width * .5 / this.height;
  25. this._scaleIn = new BABYLON.Vector2(2, 2 / this.aspectRatio);
  26. this._scaleFactor = new BABYLON.Vector2(.5 * (1 / this._postProcessScaleFactor), .5 * (1 / this._postProcessScaleFactor) * this.aspectRatio);
  27. this._lensCenter = new BABYLON.Vector2(this._isRightEye ? 0.5 - this._lensCenterOffset * 0.5 : 0.5 + this._lensCenterOffset * 0.5, 0.5);
  28. };
  29. this.onApply = (effect: Effect) => {
  30. effect.setFloat2("LensCenter", this._lensCenter.x, this._lensCenter.y);
  31. effect.setFloat2("Scale", this._scaleFactor.x, this._scaleFactor.y);
  32. effect.setFloat2("ScaleIn", this._scaleIn.x, this._scaleIn.y);
  33. effect.setFloat4("HmdWarpParam", this._distortionFactors[0], this._distortionFactors[1], this._distortionFactors[2], this._distortionFactors[3]);
  34. };
  35. }
  36. }
  37. }