refractionPostProcess.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { Color3 } from "../Maths/math.color";
  2. import { Camera } from "../Cameras/camera";
  3. import { Effect } from "../Materials/effect";
  4. import { Texture } from "../Materials/Textures/texture";
  5. import { PostProcess, PostProcessOptions } from "./postProcess";
  6. import { Engine } from "../Engines/engine";
  7. import "../Shaders/refraction.fragment";
  8. /**
  9. * Post process which applies a refractin texture
  10. * @see https://doc.babylonjs.com/how_to/how_to_use_postprocesses#refraction
  11. */
  12. export class RefractionPostProcess extends PostProcess {
  13. private _refTexture: Texture;
  14. private _ownRefractionTexture = true;
  15. /**
  16. * Gets or sets the refraction texture
  17. * Please note that you are responsible for disposing the texture if you set it manually
  18. */
  19. public get refractionTexture(): Texture {
  20. return this._refTexture;
  21. }
  22. public set refractionTexture(value: Texture) {
  23. if (this._refTexture && this._ownRefractionTexture) {
  24. this._refTexture.dispose();
  25. }
  26. this._refTexture = value;
  27. this._ownRefractionTexture = false;
  28. }
  29. /**
  30. * Initializes the RefractionPostProcess
  31. * @see https://doc.babylonjs.com/how_to/how_to_use_postprocesses#refraction
  32. * @param name The name of the effect.
  33. * @param refractionTextureUrl Url of the refraction texture to use
  34. * @param color the base color of the refraction (used to taint the rendering)
  35. * @param depth simulated refraction depth
  36. * @param colorLevel the coefficient of the base color (0 to remove base color tainting)
  37. * @param camera The camera to apply the render pass to.
  38. * @param options The required width/height ratio to downsize to before computing the render pass.
  39. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  40. * @param engine The engine which the post process will be applied. (default: current engine)
  41. * @param reusable If the post process can be reused on the same frame. (default: false)
  42. */
  43. constructor(
  44. name: string,
  45. refractionTextureUrl: string,
  46. /** the base color of the refraction (used to taint the rendering) */
  47. public color: Color3,
  48. /** simulated refraction depth */
  49. public depth: number,
  50. /** the coefficient of the base color (0 to remove base color tainting) */
  51. public colorLevel: number,
  52. options: number | PostProcessOptions,
  53. camera: Camera,
  54. samplingMode?: number,
  55. engine?: Engine,
  56. reusable?: boolean
  57. ) {
  58. super(name, "refraction", ["baseColor", "depth", "colorLevel"], ["refractionSampler"], options, camera, samplingMode, engine, reusable);
  59. this.onActivateObservable.add((cam: Camera) => {
  60. this._refTexture = this._refTexture || new Texture(refractionTextureUrl, cam.getScene());
  61. });
  62. this.onApplyObservable.add((effect: Effect) => {
  63. effect.setColor3("baseColor", this.color);
  64. effect.setFloat("depth", this.depth);
  65. effect.setFloat("colorLevel", this.colorLevel);
  66. effect.setTexture("refractionSampler", this._refTexture);
  67. });
  68. }
  69. // Methods
  70. /**
  71. * Disposes of the post process
  72. * @param camera Camera to dispose post process on
  73. */
  74. public dispose(camera: Camera): void {
  75. if (this._refTexture && this._ownRefractionTexture) {
  76. this._refTexture.dispose();
  77. (<any>this._refTexture) = null;
  78. }
  79. super.dispose(camera);
  80. }
  81. }