sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 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 | |
| 8 | #ifndef SkPerlinNoiseShader_DEFINED |
| 9 | #define SkPerlinNoiseShader_DEFINED |
| 10 | |
| 11 | #include "SkShader.h" |
| 12 | |
| 13 | /** \class SkPerlinNoiseShader |
| 14 | |
| 15 | SkPerlinNoiseShader creates an image using the Perlin turbulence function. |
| 16 | |
| 17 | It can produce tileable noise if asked to stitch tiles and provided a tile size. |
| 18 | In order to fill a large area with repeating noise, set the stitchTiles flag to |
| 19 | true, and render exactly a single tile of noise. Without this flag, the result |
| 20 | will contain visible seams between tiles. |
| 21 | |
| 22 | The algorithm used is described here : |
| 23 | http://www.w3.org/TR/SVG/filters.html#feTurbulenceElement |
| 24 | */ |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 25 | class SK_API SkPerlinNoiseShader { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 26 | public: |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 27 | /** |
| 28 | * This will construct Perlin noise of the given type (Fractal Noise or Turbulence). |
| 29 | * |
Kevin Lubick | 6ee268d | 2018-05-15 13:59:54 -0400 | [diff] [blame] | 30 | * Both base frequencies (X and Y) have a usual range of (0..1) and must be non-negative. |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 31 | * |
Kevin Lubick | 6ee268d | 2018-05-15 13:59:54 -0400 | [diff] [blame] | 32 | * The number of octaves provided should be fairly small, with a limit of 255 enforced. |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 33 | * Each octave doubles the frequency, so 10 octaves would produce noise from |
| 34 | * baseFrequency * 1, * 2, * 4, ..., * 512, which quickly yields insignificantly small |
| 35 | * periods and resembles regular unstructured noise rather than Perlin noise. |
| 36 | * |
| 37 | * If tileSize isn't NULL or an empty size, the tileSize parameter will be used to modify |
| 38 | * the frequencies so that the noise will be tileable for the given tile size. If tileSize |
| 39 | * is NULL or an empty size, the frequencies will be used as is without modification. |
| 40 | */ |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 41 | static sk_sp<SkShader> MakeFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
| 42 | int numOctaves, SkScalar seed, |
| 43 | const SkISize* tileSize = nullptr); |
| 44 | static sk_sp<SkShader> MakeTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
| 45 | int numOctaves, SkScalar seed, |
| 46 | const SkISize* tileSize = nullptr); |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 47 | /** |
| 48 | * Creates an Improved Perlin Noise shader. The z value is roughly equivalent to the seed of the |
| 49 | * other two types, but minor variations to z will only slightly change the noise. |
| 50 | */ |
| 51 | static sk_sp<SkShader> MakeImprovedNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
| 52 | int numOctaves, SkScalar z); |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 53 | |
Mike Klein | fa5f6ce | 2018-10-20 08:21:31 -0400 | [diff] [blame] | 54 | static void RegisterFlattenables(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 55 | |
| 56 | private: |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 57 | SkPerlinNoiseShader() = delete; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | #endif |