vrDistortionCorrectionPostProcess.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Vector2 } from "../Maths/math";
  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. * Initializes the VRDistortionCorrectionPostProcess
  21. * @param name The name of the effect.
  22. * @param camera The camera to apply the render pass to.
  23. * @param isRightEye If this is for the right eye distortion
  24. * @param vrMetrics All the required metrics for the VR camera
  25. */
  26. constructor(name: string, camera: Camera, isRightEye: boolean, vrMetrics: VRCameraMetrics) {
  27. super(name, "vrDistortionCorrection", [
  28. 'LensCenter',
  29. 'Scale',
  30. 'ScaleIn',
  31. 'HmdWarpParam'
  32. ], null, vrMetrics.postProcessScaleFactor, camera, Texture.BILINEAR_SAMPLINGMODE);
  33. this._isRightEye = isRightEye;
  34. this._distortionFactors = vrMetrics.distortionK;
  35. this._postProcessScaleFactor = vrMetrics.postProcessScaleFactor;
  36. this._lensCenterOffset = vrMetrics.lensCenterOffset;
  37. this.adaptScaleToCurrentViewport = true;
  38. this.onSizeChangedObservable.add(() => {
  39. this._scaleIn = new Vector2(2, 2 / this.aspectRatio);
  40. this._scaleFactor = new Vector2(.5 * (1 / this._postProcessScaleFactor), .5 * (1 / this._postProcessScaleFactor) * this.aspectRatio);
  41. this._lensCenter = new Vector2(this._isRightEye ? 0.5 - this._lensCenterOffset * 0.5 : 0.5 + this._lensCenterOffset * 0.5, 0.5);
  42. });
  43. this.onApplyObservable.add((effect: Effect) => {
  44. effect.setFloat2("LensCenter", this._lensCenter.x, this._lensCenter.y);
  45. effect.setFloat2("Scale", this._scaleFactor.x, this._scaleFactor.y);
  46. effect.setFloat2("ScaleIn", this._scaleIn.x, this._scaleIn.y);
  47. effect.setFloat4("HmdWarpParam", this._distortionFactors[0], this._distortionFactors[1], this._distortionFactors[2], this._distortionFactors[3]);
  48. });
  49. }
  50. }
  51. /**
  52. * VRMultiviewToSingleview used to convert multiview texture arrays to standard textures for scenarios such as webVR
  53. * This will not be used for webXR as it supports displaying texture arrays directly
  54. */
  55. export class VRMultiviewToSingleview extends PostProcess {
  56. /**
  57. * Initializes a VRMultiviewToSingleview
  58. * @param name name of the post process
  59. * @param camera camera to be applied to
  60. * @param scaleFactor scaling factor to the size of the output texture
  61. */
  62. constructor(name: string, camera: Camera, scaleFactor: number) {
  63. super(name, "vrMultiviewToSingleview", ["imageIndex"], ["multiviewSampler"], scaleFactor, camera, Texture.BILINEAR_SAMPLINGMODE);
  64. this.onSizeChangedObservable.add(() => {
  65. });
  66. this.onApplyObservable.add((effect: Effect) => {
  67. if (camera._scene.activeCamera && camera._scene.activeCamera.isLeftCamera) {
  68. effect.setInt("imageIndex", 0);
  69. }else {
  70. effect.setInt("imageIndex", 1);
  71. }
  72. effect.setTexture("multiviewSampler", camera._multiviewTexture);
  73. });
  74. }
  75. }