babylon.vrDistortionCorrectionPostProcess.ts 2.0 KB

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