vrDistortionCorrectionPostProcess.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { Vector2 } from "Math";
  2. import { Camera, VRCameraMetrics } from "Cameras";
  3. import { Effect, Texture } from "Materials";
  4. import { PostProcess } from "PostProcess";
  5. /**
  6. * VRDistortionCorrectionPostProcess used for mobile VR
  7. */
  8. export class VRDistortionCorrectionPostProcess extends PostProcess {
  9. private _isRightEye: boolean;
  10. private _distortionFactors: number[];
  11. private _postProcessScaleFactor: number;
  12. private _lensCenterOffset: number;
  13. private _scaleIn: Vector2;
  14. private _scaleFactor: Vector2;
  15. private _lensCenter: Vector2;
  16. /**
  17. * Initializes the VRDistortionCorrectionPostProcess
  18. * @param name The name of the effect.
  19. * @param camera The camera to apply the render pass to.
  20. * @param isRightEye If this is for the right eye distortion
  21. * @param vrMetrics All the required metrics for the VR camera
  22. */
  23. constructor(name: string, camera: Camera, isRightEye: boolean, vrMetrics: VRCameraMetrics) {
  24. super(name, "vrDistortionCorrection", [
  25. 'LensCenter',
  26. 'Scale',
  27. 'ScaleIn',
  28. 'HmdWarpParam'
  29. ], null, vrMetrics.postProcessScaleFactor, camera, Texture.BILINEAR_SAMPLINGMODE);
  30. this._isRightEye = isRightEye;
  31. this._distortionFactors = vrMetrics.distortionK;
  32. this._postProcessScaleFactor = vrMetrics.postProcessScaleFactor;
  33. this._lensCenterOffset = vrMetrics.lensCenterOffset;
  34. this.adaptScaleToCurrentViewport = true;
  35. this.onSizeChangedObservable.add(() => {
  36. this._scaleIn = new Vector2(2, 2 / this.aspectRatio);
  37. this._scaleFactor = new Vector2(.5 * (1 / this._postProcessScaleFactor), .5 * (1 / this._postProcessScaleFactor) * this.aspectRatio);
  38. this._lensCenter = new Vector2(this._isRightEye ? 0.5 - this._lensCenterOffset * 0.5 : 0.5 + this._lensCenterOffset * 0.5, 0.5);
  39. });
  40. this.onApplyObservable.add((effect: Effect) => {
  41. effect.setFloat2("LensCenter", this._lensCenter.x, this._lensCenter.y);
  42. effect.setFloat2("Scale", this._scaleFactor.x, this._scaleFactor.y);
  43. effect.setFloat2("ScaleIn", this._scaleIn.x, this._scaleIn.y);
  44. effect.setFloat4("HmdWarpParam", this._distortionFactors[0], this._distortionFactors[1], this._distortionFactors[2], this._distortionFactors[3]);
  45. });
  46. }
  47. }