vrDistortionCorrectionPostProcess.ts 2.8 KB

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