babylon.noiseProceduralTexture.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.autoClear = false;
  26. this._updateShaderUniforms();
  27. }
  28. private _updateShaderUniforms() {
  29. let scene = this.getScene();
  30. if (!scene) {
  31. return;
  32. }
  33. this._time += scene.getAnimationRatio() * this.animationSpeedFactor * 0.01;
  34. this.setFloat("brightness", this.brightness);
  35. this.setFloat("persistence", this.persistence);
  36. this.setFloat("timeScale", this._time);
  37. }
  38. protected _getDefines(): string {
  39. return "#define OCTAVES " + (this.octaves | 0);
  40. }
  41. /** Generate the current state of the procedural texture */
  42. public render(useCameraPostProcess?: boolean) {
  43. this._updateShaderUniforms();
  44. super.render(useCameraPostProcess);
  45. }
  46. /**
  47. * Serializes this noise procedural texture
  48. * @returns a serialized noise procedural texture object
  49. */
  50. public serialize(): any {
  51. var serializationObject: any = {};
  52. serializationObject.customType = "BABYLON.NoiseProceduralTexture";
  53. serializationObject.brightness = this.brightness;
  54. serializationObject.octaves = this.octaves;
  55. serializationObject.persistence = this.persistence;
  56. serializationObject.animationSpeedFactor = this.animationSpeedFactor;
  57. serializationObject.size = this.getSize().width;
  58. serializationObject.generateMipMaps = this._generateMipMaps;
  59. return serializationObject;
  60. }
  61. /**
  62. * Creates a NoiseProceduralTexture from parsed noise procedural texture data
  63. * @param parsedTexture defines parsed texture data
  64. * @param scene defines the current scene
  65. * @param rootUrl defines the root URL containing noise procedural texture information
  66. * @returns a parsed NoiseProceduralTexture
  67. */
  68. public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): NoiseProceduralTexture {
  69. var texture = new NoiseProceduralTexture(parsedTexture.name, parsedTexture.size, scene, undefined, parsedTexture.generateMipMaps);
  70. texture.brightness = parsedTexture.brightness;
  71. texture.octaves = parsedTexture.octaves;
  72. texture.persistence = parsedTexture.persistence;
  73. texture.animationSpeedFactor = parsedTexture.animationSpeedFactor;
  74. return texture;
  75. }
  76. }
  77. }