SkPerlinNoiseShader.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright 2013 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkPerlinNoiseShader_DEFINED
  8. #define SkPerlinNoiseShader_DEFINED
  9. #include "include/core/SkShader.h"
  10. /** \class SkPerlinNoiseShader
  11. SkPerlinNoiseShader creates an image using the Perlin turbulence function.
  12. It can produce tileable noise if asked to stitch tiles and provided a tile size.
  13. In order to fill a large area with repeating noise, set the stitchTiles flag to
  14. true, and render exactly a single tile of noise. Without this flag, the result
  15. will contain visible seams between tiles.
  16. The algorithm used is described here :
  17. http://www.w3.org/TR/SVG/filters.html#feTurbulenceElement
  18. */
  19. class SK_API SkPerlinNoiseShader {
  20. public:
  21. /**
  22. * This will construct Perlin noise of the given type (Fractal Noise or Turbulence).
  23. *
  24. * Both base frequencies (X and Y) have a usual range of (0..1) and must be non-negative.
  25. *
  26. * The number of octaves provided should be fairly small, with a limit of 255 enforced.
  27. * Each octave doubles the frequency, so 10 octaves would produce noise from
  28. * baseFrequency * 1, * 2, * 4, ..., * 512, which quickly yields insignificantly small
  29. * periods and resembles regular unstructured noise rather than Perlin noise.
  30. *
  31. * If tileSize isn't NULL or an empty size, the tileSize parameter will be used to modify
  32. * the frequencies so that the noise will be tileable for the given tile size. If tileSize
  33. * is NULL or an empty size, the frequencies will be used as is without modification.
  34. */
  35. static sk_sp<SkShader> MakeFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
  36. int numOctaves, SkScalar seed,
  37. const SkISize* tileSize = nullptr);
  38. static sk_sp<SkShader> MakeTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
  39. int numOctaves, SkScalar seed,
  40. const SkISize* tileSize = nullptr);
  41. /**
  42. * Creates an Improved Perlin Noise shader. The z value is roughly equivalent to the seed of the
  43. * other two types, but minor variations to z will only slightly change the noise.
  44. */
  45. static sk_sp<SkShader> MakeImprovedNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
  46. int numOctaves, SkScalar z);
  47. static void RegisterFlattenables();
  48. private:
  49. SkPerlinNoiseShader() = delete;
  50. };
  51. #endif