babylon.noiseProceduralTexture.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. module BABYLON {
  2. /**
  3. * Class used to generate noise procedural textures
  4. */
  5. export class NoiseProceduralTexture extends ProceduralTexture {
  6. private _time = 0;
  7. /** Gets or sets a value between 0 and 1 indicating the overall brightness of the texture (default is 0.2) */
  8. public brightness = 0.2;
  9. /** Defines the number of octaves to process */
  10. public octaves = 3;
  11. /** Defines the level of persistence (0.8 by default) */
  12. public persistence = 0.8;
  13. /** Gets or sets animation speed factor (default is 1) */
  14. public animationSpeedFactor = 1;
  15. /**
  16. * Creates a new NoiseProceduralTexture
  17. * @param name defines the name fo the texture
  18. * @param size defines the size of the texture (default is 256)
  19. * @param scene defines the hosting scene
  20. * @param fallbackTexture defines the texture to use if the NoiseProceduralTexture can't be created
  21. * @param generateMipMaps defines if mipmaps must be generated (true by default)
  22. */
  23. constructor(name: string, size: number = 256, scene: Nullable<Scene> = Engine.LastCreatedScene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  24. super(name, size, "noise", scene, fallbackTexture, generateMipMaps);
  25. this._updateShaderUniforms();
  26. }
  27. private _updateShaderUniforms() {
  28. let scene = this.getScene();
  29. if (!scene) {
  30. return;
  31. }
  32. this._time += scene.getAnimationRatio() * this.animationSpeedFactor * 0.01;
  33. this.setFloat("brightness", this.brightness);
  34. this.setFloat("persistence", this.persistence);
  35. this.setFloat("timeScale", this._time);
  36. }
  37. protected _getDefines(): string {
  38. return "#define OCTAVES " + (this.octaves | 0);
  39. }
  40. /** Generate the current state of the procedural texture */
  41. public render(useCameraPostProcess?: boolean) {
  42. this._updateShaderUniforms();
  43. super.render(useCameraPostProcess);
  44. }
  45. /**
  46. * Serializes this noise procedural texture
  47. * @returns a serialized noise procedural texture object
  48. */
  49. public serialize(): any {
  50. var serializationObject: any = {};
  51. serializationObject.customType = "BABYLON.NoiseProceduralTexture";
  52. serializationObject.brightness = this.brightness;
  53. serializationObject.octaves = this.octaves;
  54. serializationObject.persistence = this.persistence;
  55. serializationObject.animationSpeedFactor = this.animationSpeedFactor;
  56. serializationObject.size = this.getSize().width;
  57. serializationObject.generateMipMaps = this._generateMipMaps;
  58. return serializationObject;
  59. }
  60. /**
  61. * Creates a NoiseProceduralTexture from parsed noise procedural texture data
  62. * @param parsedTexture defines parsed texture data
  63. * @param scene defines the current scene
  64. * @param rootUrl defines the root URL containing noise procedural texture information
  65. * @returns a parsed NoiseProceduralTexture
  66. */
  67. public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): NoiseProceduralTexture {
  68. var texture = new NoiseProceduralTexture(parsedTexture.name, parsedTexture.size, scene, undefined, parsedTexture.generateMipMaps);
  69. texture.brightness = parsedTexture.brightness;
  70. texture.octaves = parsedTexture.octaves;
  71. texture.persistence = parsedTexture.persistence;
  72. texture.animationSpeedFactor = parsedTexture.animationSpeedFactor;
  73. return texture;
  74. }
  75. }
  76. }