refractionPostProcess.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { __decorate } from "../tslib.es6.js";
  2. import { Texture } from "../Materials/Textures/texture.js";
  3. import { PostProcess } from "./postProcess.js";
  4. import "../Shaders/refraction.fragment.js";
  5. import { RegisterClass } from "../Misc/typeStore.js";
  6. import { serialize } from "../Misc/decorators.js";
  7. import { SerializationHelper } from "../Misc/decorators.serialization.js";
  8. /**
  9. * Post process which applies a refraction texture
  10. * @see https://doc.babylonjs.com/features/featuresDeepDive/postProcesses/usePostProcesses#refraction
  11. */
  12. export class RefractionPostProcess extends PostProcess {
  13. /**
  14. * Gets or sets the refraction texture
  15. * Please note that you are responsible for disposing the texture if you set it manually
  16. */
  17. get refractionTexture() {
  18. return this._refTexture;
  19. }
  20. set refractionTexture(value) {
  21. if (this._refTexture && this._ownRefractionTexture) {
  22. this._refTexture.dispose();
  23. }
  24. this._refTexture = value;
  25. this._ownRefractionTexture = false;
  26. }
  27. /**
  28. * Gets a string identifying the name of the class
  29. * @returns "RefractionPostProcess" string
  30. */
  31. getClassName() {
  32. return "RefractionPostProcess";
  33. }
  34. /**
  35. * Initializes the RefractionPostProcess
  36. * @see https://doc.babylonjs.com/features/featuresDeepDive/postProcesses/usePostProcesses#refraction
  37. * @param name The name of the effect.
  38. * @param refractionTextureUrl Url of the refraction texture to use
  39. * @param color the base color of the refraction (used to taint the rendering)
  40. * @param depth simulated refraction depth
  41. * @param colorLevel the coefficient of the base color (0 to remove base color tainting)
  42. * @param options The required width/height ratio to downsize to before computing the render pass.
  43. * @param camera The camera to apply the render pass to.
  44. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  45. * @param engine The engine which the post process will be applied. (default: current engine)
  46. * @param reusable If the post process can be reused on the same frame. (default: false)
  47. */
  48. constructor(name, refractionTextureUrl, color, depth, colorLevel, options, camera, samplingMode, engine, reusable) {
  49. super(name, "refraction", ["baseColor", "depth", "colorLevel"], ["refractionSampler"], options, camera, samplingMode, engine, reusable);
  50. this._ownRefractionTexture = true;
  51. this.color = color;
  52. this.depth = depth;
  53. this.colorLevel = colorLevel;
  54. this.refractionTextureUrl = refractionTextureUrl;
  55. this.onActivateObservable.add((cam) => {
  56. this._refTexture = this._refTexture || new Texture(refractionTextureUrl, cam.getScene());
  57. });
  58. this.onApplyObservable.add((effect) => {
  59. effect.setColor3("baseColor", this.color);
  60. effect.setFloat("depth", this.depth);
  61. effect.setFloat("colorLevel", this.colorLevel);
  62. effect.setTexture("refractionSampler", this._refTexture);
  63. });
  64. }
  65. // Methods
  66. /**
  67. * Disposes of the post process
  68. * @param camera Camera to dispose post process on
  69. */
  70. dispose(camera) {
  71. if (this._refTexture && this._ownRefractionTexture) {
  72. this._refTexture.dispose();
  73. this._refTexture = null;
  74. }
  75. super.dispose(camera);
  76. }
  77. /**
  78. * @internal
  79. */
  80. static _Parse(parsedPostProcess, targetCamera, scene, rootUrl) {
  81. return SerializationHelper.Parse(() => {
  82. return new RefractionPostProcess(parsedPostProcess.name, parsedPostProcess.refractionTextureUrl, parsedPostProcess.color, parsedPostProcess.depth, parsedPostProcess.colorLevel, parsedPostProcess.options, targetCamera, parsedPostProcess.renderTargetSamplingMode, scene.getEngine(), parsedPostProcess.reusable);
  83. }, parsedPostProcess, scene, rootUrl);
  84. }
  85. }
  86. __decorate([
  87. serialize()
  88. ], RefractionPostProcess.prototype, "color", void 0);
  89. __decorate([
  90. serialize()
  91. ], RefractionPostProcess.prototype, "depth", void 0);
  92. __decorate([
  93. serialize()
  94. ], RefractionPostProcess.prototype, "colorLevel", void 0);
  95. __decorate([
  96. serialize()
  97. ], RefractionPostProcess.prototype, "refractionTextureUrl", void 0);
  98. RegisterClass("BABYLON.RefractionPostProcess", RefractionPostProcess);
  99. //# sourceMappingURL=refractionPostProcess.js.map