babylon.refractionPostProcess.ts 3.5 KB

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