refractionPostProcess.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. import { _TypeStore } from '../Misc/typeStore';
  9. import { SerializationHelper, serialize } from '../Misc/decorators';
  10. declare type Scene = import("../scene").Scene;
  11. /**
  12. * Post process which applies a refraction texture
  13. * @see https://doc.babylonjs.com/how_to/how_to_use_postprocesses#refraction
  14. */
  15. export class RefractionPostProcess extends PostProcess {
  16. private _refTexture: Texture;
  17. private _ownRefractionTexture = true;
  18. /** the base color of the refraction (used to taint the rendering) */
  19. @serialize()
  20. public color: Color3;
  21. /** simulated refraction depth */
  22. @serialize()
  23. public depth: number;
  24. /** the coefficient of the base color (0 to remove base color tainting) */
  25. @serialize()
  26. public colorLevel: number;
  27. /** Gets the url used to load the refraction texture */
  28. @serialize()
  29. public refractionTextureUrl: string;
  30. /**
  31. * Gets or sets the refraction texture
  32. * Please note that you are responsible for disposing the texture if you set it manually
  33. */
  34. public get refractionTexture(): Texture {
  35. return this._refTexture;
  36. }
  37. public set refractionTexture(value: Texture) {
  38. if (this._refTexture && this._ownRefractionTexture) {
  39. this._refTexture.dispose();
  40. }
  41. this._refTexture = value;
  42. this._ownRefractionTexture = false;
  43. }
  44. /**
  45. * Gets a string identifying the name of the class
  46. * @returns "RefractionPostProcess" string
  47. */
  48. public getClassName(): string {
  49. return "RefractionPostProcess";
  50. }
  51. /**
  52. * Initializes the RefractionPostProcess
  53. * @see https://doc.babylonjs.com/how_to/how_to_use_postprocesses#refraction
  54. * @param name The name of the effect.
  55. * @param refractionTextureUrl Url of the refraction texture to use
  56. * @param color the base color of the refraction (used to taint the rendering)
  57. * @param depth simulated refraction depth
  58. * @param colorLevel the coefficient of the base color (0 to remove base color tainting)
  59. * @param camera The camera to apply the render pass to.
  60. * @param options The required width/height ratio to downsize to before computing the render pass.
  61. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  62. * @param engine The engine which the post process will be applied. (default: current engine)
  63. * @param reusable If the post process can be reused on the same frame. (default: false)
  64. */
  65. constructor(
  66. name: string,
  67. refractionTextureUrl: string,
  68. color: Color3,
  69. depth: number,
  70. colorLevel: number,
  71. options: number | PostProcessOptions,
  72. camera: Camera,
  73. samplingMode?: number,
  74. engine?: Engine,
  75. reusable?: boolean
  76. ) {
  77. super(name, "refraction", ["baseColor", "depth", "colorLevel"], ["refractionSampler"], options, camera, samplingMode, engine, reusable);
  78. this.color = color;
  79. this.depth = depth;
  80. this.colorLevel = colorLevel;
  81. this.refractionTextureUrl = refractionTextureUrl;
  82. this.onActivateObservable.add((cam: Camera) => {
  83. this._refTexture = this._refTexture || new Texture(refractionTextureUrl, cam.getScene());
  84. });
  85. this.onApplyObservable.add((effect: Effect) => {
  86. effect.setColor3("baseColor", this.color);
  87. effect.setFloat("depth", this.depth);
  88. effect.setFloat("colorLevel", this.colorLevel);
  89. effect.setTexture("refractionSampler", this._refTexture);
  90. });
  91. }
  92. // Methods
  93. /**
  94. * Disposes of the post process
  95. * @param camera Camera to dispose post process on
  96. */
  97. public dispose(camera: Camera): void {
  98. if (this._refTexture && this._ownRefractionTexture) {
  99. this._refTexture.dispose();
  100. (<any>this._refTexture) = null;
  101. }
  102. super.dispose(camera);
  103. }
  104. /** @hidden */
  105. public static _Parse(parsedPostProcess: any, targetCamera: Camera, scene: Scene, rootUrl: string) {
  106. return SerializationHelper.Parse(() => {
  107. return new RefractionPostProcess(
  108. parsedPostProcess.name, parsedPostProcess.refractionTextureUrl,
  109. parsedPostProcess.color, parsedPostProcess.depth,
  110. parsedPostProcess.colorLevel,
  111. parsedPostProcess.options, targetCamera,
  112. parsedPostProcess.renderTargetSamplingMode,
  113. scene.getEngine(), parsedPostProcess.reusable);
  114. }, parsedPostProcess, scene, rootUrl);
  115. }
  116. }
  117. _TypeStore.RegisteredTypes["BABYLON.RefractionPostProcess"] = RefractionPostProcess;