| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkPerlinNoiseShader_DEFINED | 8 #ifndef SkPerlinNoiseShader2_DEFINED |
| 9 #define SkPerlinNoiseShader_DEFINED | 9 #define SkPerlinNoiseShader2_DEFINED |
| 10 | 10 |
| 11 #include "SkShader.h" | 11 #include "SkShader.h" |
| 12 | 12 |
| 13 /** \class SkPerlinNoiseShader | 13 /** \class SkPerlinNoiseShader2 |
| 14 | 14 |
| 15 SkPerlinNoiseShader creates an image using the Perlin turbulence function. | 15 SkPerlinNoiseShader2 creates an image using the Perlin turbulence function. |
| 16 | 16 |
| 17 It can produce tileable noise if asked to stitch tiles and provided a tile s
ize. | 17 It can produce tileable noise if asked to stitch tiles and provided a tile s
ize. |
| 18 In order to fill a large area with repeating noise, set the stitchTiles flag
to | 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 resu
lt | 19 true, and render exactly a single tile of noise. Without this flag, the resu
lt |
| 20 will contain visible seams between tiles. | 20 will contain visible seams between tiles. |
| 21 | 21 |
| 22 The algorithm used is described here : | 22 The algorithm used is described here : |
| 23 http://www.w3.org/TR/SVG/filters.html#feTurbulenceElement | 23 http://www.w3.org/TR/SVG/filters.html#feTurbulenceElement |
| 24 */ | 24 */ |
| 25 class SK_API SkPerlinNoiseShader : public SkShader { | 25 class SK_API SkPerlinNoiseShader2 : public SkShader { |
| 26 public: | 26 public: |
| 27 struct StitchData; | 27 struct StitchData; |
| 28 struct PaintingData; | 28 struct PaintingData; |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * About the noise types : the difference between the 2 is just minor tweak
s to the algorithm, | 31 * About the noise types : the difference between the first 2 is just minor
tweaks to the |
| 32 * they're not 2 entirely different noises. The output looks different, but
once the noise is | 32 * algorithm, they're not 2 entirely different noises. The output looks dif
ferent, but once the |
| 33 * generated in the [1, -1] range, the output is brought back in the [0, 1]
range by doing : | 33 * noise is generated in the [1, -1] range, the output is brought back in t
he [0, 1] range by |
| 34 * doing : |
| 34 * kFractalNoise_Type : noise * 0.5 + 0.5 | 35 * kFractalNoise_Type : noise * 0.5 + 0.5 |
| 35 * kTurbulence_Type : abs(noise) | 36 * kTurbulence_Type : abs(noise) |
| 36 * Very little differences between the 2 types, although you can tell the d
ifference visually. | 37 * Very little differences between the 2 types, although you can tell the d
ifference visually. |
| 38 * "Improved" is based on the Improved Perlin Noise algorithm described at |
| 39 * http://mrl.nyu.edu/~perlin/noise/. It is quite distinct from the other t
wo, and the noise is |
| 40 * a 2D slice of a 3D noise texture. Minor changes to the Z coordinate will
result in minor |
| 41 * changes to the noise, making it suitable for animated noise. |
| 37 */ | 42 */ |
| 38 enum Type { | 43 enum Type { |
| 39 kFractalNoise_Type, | 44 kFractalNoise_Type, |
| 40 kTurbulence_Type, | 45 kTurbulence_Type, |
| 46 kImprovedNoise_Type, |
| 41 kFirstType = kFractalNoise_Type, | 47 kFirstType = kFractalNoise_Type, |
| 42 kLastType = kTurbulence_Type | 48 kLastType = kImprovedNoise_Type |
| 43 }; | 49 }; |
| 44 /** | 50 /** |
| 45 * This will construct Perlin noise of the given type (Fractal Noise or Tur
bulence). | 51 * This will construct Perlin noise of the given type (Fractal Noise or Tur
bulence). |
| 46 * | 52 * |
| 47 * Both base frequencies (X and Y) have a usual range of (0..1). | 53 * Both base frequencies (X and Y) have a usual range of (0..1). |
| 48 * | 54 * |
| 49 * The number of octaves provided should be fairly small, although no limit
is enforced. | 55 * The number of octaves provided should be fairly small, although no limit
is enforced. |
| 50 * Each octave doubles the frequency, so 10 octaves would produce noise fro
m | 56 * Each octave doubles the frequency, so 10 octaves would produce noise fro
m |
| 51 * baseFrequency * 1, * 2, * 4, ..., * 512, which quickly yields insignific
antly small | 57 * baseFrequency * 1, * 2, * 4, ..., * 512, which quickly yields insignific
antly small |
| 52 * periods and resembles regular unstructured noise rather than Perlin nois
e. | 58 * periods and resembles regular unstructured noise rather than Perlin nois
e. |
| 53 * | 59 * |
| 54 * If tileSize isn't NULL or an empty size, the tileSize parameter will be
used to modify | 60 * If tileSize isn't NULL or an empty size, the tileSize parameter will be
used to modify |
| 55 * the frequencies so that the noise will be tileable for the given tile si
ze. If tileSize | 61 * the frequencies so that the noise will be tileable for the given tile si
ze. If tileSize |
| 56 * is NULL or an empty size, the frequencies will be used as is without mod
ification. | 62 * is NULL or an empty size, the frequencies will be used as is without mod
ification. |
| 57 */ | 63 */ |
| 58 static SkShader* CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFr
equencyY, | 64 static SkShader* CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFr
equencyY, |
| 59 int numOctaves, SkScalar seed, | 65 int numOctaves, SkScalar seed, |
| 60 const SkISize* tileSize = NULL); | 66 const SkISize* tileSize = NULL); |
| 61 static SkShader* CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFreq
uencyY, | 67 static SkShader* CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFreq
uencyY, |
| 62 int numOctaves, SkScalar seed, | 68 int numOctaves, SkScalar seed, |
| 63 const SkISize* tileSize = NULL); | 69 const SkISize* tileSize = NULL); |
| 64 /** | 70 /** |
| 71 * Creates an Improved Perlin Noise shader. The z value is roughly equivalen
t to the seed of the |
| 72 * other two types, but minor variations to z will only slightly change the
noise. |
| 73 */ |
| 74 static SkShader* CreateImprovedNoise(SkScalar baseFrequencyX, SkScalar baseF
requencyY, |
| 75 int numOctaves, SkScalar z); |
| 76 /** |
| 65 * Create alias for CreateTurbulunce until all Skia users changed | 77 * Create alias for CreateTurbulunce until all Skia users changed |
| 66 * its code to use the new naming | 78 * its code to use the new naming |
| 67 */ | 79 */ |
| 68 static SkShader* CreateTubulence(SkScalar baseFrequencyX, SkScalar baseFrequ
encyY, | 80 static SkShader* CreateTubulence(SkScalar baseFrequencyX, SkScalar baseFrequ
encyY, |
| 69 int numOctaves, SkScalar seed, | 81 int numOctaves, SkScalar seed, |
| 70 const SkISize* tileSize = NULL) { | 82 const SkISize* tileSize = NULL) { |
| 71 return CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, ti
leSize); | 83 return CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, ti
leSize); |
| 72 } | 84 } |
| 73 | 85 |
| 74 | 86 |
| 75 size_t contextSize() const override; | 87 size_t contextSize() const override; |
| 76 | 88 |
| 77 class PerlinNoiseShaderContext : public SkShader::Context { | 89 class PerlinNoiseShaderContext : public SkShader::Context { |
| 78 public: | 90 public: |
| 79 PerlinNoiseShaderContext(const SkPerlinNoiseShader& shader, const Contex
tRec&); | 91 PerlinNoiseShaderContext(const SkPerlinNoiseShader2& shader, const Conte
xtRec&); |
| 80 virtual ~PerlinNoiseShaderContext(); | 92 virtual ~PerlinNoiseShaderContext(); |
| 81 | 93 |
| 82 void shadeSpan(int x, int y, SkPMColor[], int count) override; | 94 void shadeSpan(int x, int y, SkPMColor[], int count) override; |
| 83 void shadeSpan16(int x, int y, uint16_t[], int count) override; | 95 void shadeSpan16(int x, int y, uint16_t[], int count) override; |
| 84 | 96 |
| 85 private: | 97 private: |
| 86 SkPMColor shade(const SkPoint& point, StitchData& stitchData) const; | 98 SkPMColor shade(const SkPoint& point, StitchData& stitchData) const; |
| 87 SkScalar calculateTurbulenceValueForPoint( | 99 SkScalar calculateTurbulenceValueForPoint( |
| 88 int channel, | 100 int channel, |
| 89 StitchData& stitchData, const SkPoint& point) const; | 101 StitchData& stitchData, const SkPoint& point) const; |
| 102 SkScalar calculateImprovedNoiseValueForPoint(int channel, const SkPoint&
point) const; |
| 90 SkScalar noise2D(int channel, | 103 SkScalar noise2D(int channel, |
| 91 const StitchData& stitchData, const SkPoint& noiseVecto
r) const; | 104 const StitchData& stitchData, const SkPoint& noiseVecto
r) const; |
| 92 | 105 |
| 93 SkMatrix fMatrix; | 106 SkMatrix fMatrix; |
| 94 PaintingData* fPaintingData; | 107 PaintingData* fPaintingData; |
| 95 | 108 |
| 96 typedef SkShader::Context INHERITED; | 109 typedef SkShader::Context INHERITED; |
| 97 }; | 110 }; |
| 98 | 111 |
| 99 #if SK_SUPPORT_GPU | 112 #if SK_SUPPORT_GPU |
| 100 const GrFragmentProcessor* asFragmentProcessor(GrContext* context, const SkM
atrix& viewM, | 113 const GrFragmentProcessor* asFragmentProcessor(GrContext* context, const SkM
atrix& viewM, |
| 101 const SkMatrix*, SkFilterQual
ity) const override; | 114 const SkMatrix*, SkFilterQual
ity) const override; |
| 102 #endif | 115 #endif |
| 103 | 116 |
| 104 SK_TO_STRING_OVERRIDE() | 117 SK_TO_STRING_OVERRIDE() |
| 105 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader) | 118 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader2) |
| 106 | 119 |
| 107 protected: | 120 protected: |
| 108 void flatten(SkWriteBuffer&) const override; | 121 void flatten(SkWriteBuffer&) const override; |
| 109 Context* onCreateContext(const ContextRec&, void* storage) const override; | 122 Context* onCreateContext(const ContextRec&, void* storage) const override; |
| 110 | 123 |
| 111 private: | 124 private: |
| 112 SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, SkScalar baseFrequencyX, | 125 SkPerlinNoiseShader2(SkPerlinNoiseShader2::Type type, SkScalar baseFrequency
X, |
| 113 SkScalar baseFrequencyY, int numOctaves, SkScalar seed, | 126 SkScalar baseFrequencyY, int numOctaves, SkScalar seed, |
| 114 const SkISize* tileSize); | 127 const SkISize* tileSize); |
| 115 virtual ~SkPerlinNoiseShader(); | 128 virtual ~SkPerlinNoiseShader2(); |
| 116 | 129 |
| 117 const SkPerlinNoiseShader::Type fType; | 130 const SkPerlinNoiseShader2::Type fType; |
| 118 const SkScalar fBaseFrequencyX; | 131 const SkScalar fBaseFrequencyX; |
| 119 const SkScalar fBaseFrequencyY; | 132 const SkScalar fBaseFrequencyY; |
| 120 const int fNumOctaves; | 133 const int fNumOctaves; |
| 121 const SkScalar fSeed; | 134 const SkScalar fSeed; |
| 122 const SkISize fTileSize; | 135 const SkISize fTileSize; |
| 123 const bool fStitchTiles; | 136 const bool fStitchTiles; |
| 124 | 137 |
| 125 typedef SkShader INHERITED; | 138 typedef SkShader INHERITED; |
| 126 }; | 139 }; |
| 127 | 140 |
| 128 #endif | 141 #endif |
| OLD | NEW |