blob: 1623520cd6d33dc4e470a08718dbf02df3f91a05 [file] [log] [blame]
sugoi@google.come3b4c502013-04-05 13:47:09 +00001/*
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 Malita14d54c22017-05-18 11:52:59 -040025class SK_API SkPerlinNoiseShader {
sugoi@google.come3b4c502013-04-05 13:47:09 +000026public:
sugoi@google.come3b4c502013-04-05 13:47:09 +000027 /**
28 * This will construct Perlin noise of the given type (Fractal Noise or Turbulence).
29 *
Kevin Lubick6ee268d2018-05-15 13:59:54 -040030 * Both base frequencies (X and Y) have a usual range of (0..1) and must be non-negative.
sugoi@google.come3b4c502013-04-05 13:47:09 +000031 *
Kevin Lubick6ee268d2018-05-15 13:59:54 -040032 * The number of octaves provided should be fairly small, with a limit of 255 enforced.
sugoi@google.come3b4c502013-04-05 13:47:09 +000033 * 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 */
reedfe630452016-03-25 09:08:00 -070041 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 Reedf2ae2b22017-05-30 15:22:54 -040047 /**
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);
reedfe630452016-03-25 09:08:00 -070053
Cary Clark4dc5a452018-05-21 11:56:57 -040054 static void InitializeFlattenables();
sugoi@google.come3b4c502013-04-05 13:47:09 +000055
56private:
Florin Malita14d54c22017-05-18 11:52:59 -040057 SkPerlinNoiseShader() = delete;
sugoi@google.come3b4c502013-04-05 13:47:09 +000058};
59
60#endif