refractionTexture.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { Plane } from "../../Maths/math.plane.js";
  2. import { RenderTargetTexture } from "../../Materials/Textures/renderTargetTexture.js";
  3. /**
  4. * Creates a refraction texture used by refraction channel of the standard material.
  5. * It is like a mirror but to see through a material.
  6. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/reflectionTexture#refractiontexture
  7. */
  8. export class RefractionTexture extends RenderTargetTexture {
  9. /**
  10. * Creates a refraction texture used by refraction channel of the standard material.
  11. * It is like a mirror but to see through a material.
  12. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/reflectionTexture#refraction
  13. * @param name Define the texture name
  14. * @param size Define the size of the underlying texture
  15. * @param scene Define the scene the refraction belongs to
  16. * @param generateMipMaps Define if we need to generate mips level for the refraction
  17. */
  18. constructor(name, size, scene, generateMipMaps) {
  19. super(name, size, scene, generateMipMaps, true);
  20. /**
  21. * Define the reflection plane we want to use. The refractionPlane is usually set to the constructed refractor.
  22. * It is possible to directly set the refractionPlane by directly using a Plane(a, b, c, d) where a, b and c give the plane normal vector (a, b, c) and d is a scalar displacement from the refractionPlane to the origin. However in all but the very simplest of situations it is more straight forward to set it to the refractor as stated in the doc.
  23. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/reflectionTexture#refraction
  24. */
  25. this.refractionPlane = new Plane(0, 1, 0, 1);
  26. /**
  27. * Define how deep under the surface we should see.
  28. */
  29. this.depth = 2.0;
  30. this.onBeforeRenderObservable.add(() => {
  31. this.getScene().clipPlane = this.refractionPlane;
  32. });
  33. this.onAfterRenderObservable.add(() => {
  34. this.getScene().clipPlane = null;
  35. });
  36. }
  37. /**
  38. * Clone the refraction texture.
  39. * @returns the cloned texture
  40. */
  41. clone() {
  42. const scene = this.getScene();
  43. if (!scene) {
  44. return this;
  45. }
  46. const textureSize = this.getSize();
  47. const newTexture = new RefractionTexture(this.name, textureSize.width, scene, this._generateMipMaps);
  48. // Base texture
  49. newTexture.hasAlpha = this.hasAlpha;
  50. newTexture.level = this.level;
  51. // Refraction Texture
  52. newTexture.refractionPlane = this.refractionPlane.clone();
  53. if (this.renderList) {
  54. newTexture.renderList = this.renderList.slice(0);
  55. }
  56. newTexture.depth = this.depth;
  57. return newTexture;
  58. }
  59. /**
  60. * Serialize the texture to a JSON representation you could use in Parse later on
  61. * @returns the serialized JSON representation
  62. */
  63. serialize() {
  64. if (!this.name) {
  65. return null;
  66. }
  67. const serializationObject = super.serialize();
  68. serializationObject.mirrorPlane = this.refractionPlane.asArray();
  69. serializationObject.depth = this.depth;
  70. return serializationObject;
  71. }
  72. }
  73. //# sourceMappingURL=refractionTexture.js.map