babylon.noiseProceduralTexture.ts 3.6 KB

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