| 1 | // SimplexNoise1234 |
| 2 | // Copyright © 2003-2011, Stefan Gustavson |
| 3 | // |
| 4 | // Contact: stegu@itn.liu.se |
| 5 | // |
| 6 | // This library is public domain software, released by the author |
| 7 | // into the public domain in February 2011. You may do anything |
| 8 | // you like with it. You may even remove all attributions, |
| 9 | // but of course I'd appreciate it if you kept my name somewhere. |
| 10 | // |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | // General Public License for more details. |
| 15 | |
| 16 | // Modified by the LOVE Development Team to remove 3D and 4D implementations due |
| 17 | // to patent issues. |
| 18 | |
| 19 | /** \file |
| 20 | \brief Implements the SimplexNoise1234 class for producing Perlin simplex noise. |
| 21 | \author Stefan Gustavson (stegu@itn.liu.se) |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * This implementation is "Simplex Noise" as presented by |
| 26 | * Ken Perlin at a relatively obscure and not often cited course |
| 27 | * session "Real-Time Shading" at Siggraph 2001 (before real |
| 28 | * time shading actually took on), under the title "hardware noise". |
| 29 | * The 3D function is numerically equivalent to his Java reference |
| 30 | * code available in the PDF course notes, although I re-implemented |
| 31 | * it from scratch to get more readable code. The 1D, 2D and 4D cases |
| 32 | * were implemented from scratch by me from Ken Perlin's text. |
| 33 | * |
| 34 | * This is a highly reusable class. It has no dependencies |
| 35 | * on any other file, apart from its own header file. |
| 36 | */ |
| 37 | |
| 38 | |
| 39 | #include "simplexnoise1234.h" |
| 40 | |
| 41 | #define FASTFLOOR(x) ( ((x)>0) ? ((int)x) : (((int)x)-1) ) |
| 42 | |
| 43 | //--------------------------------------------------------------------- |
| 44 | // Static data |
| 45 | |
| 46 | /* |
| 47 | * Permutation table. This is just a random jumble of all numbers 0-255, |
| 48 | * repeated twice to avoid wrapping the index at 255 for each lookup. |
| 49 | * This needs to be exactly the same for all instances on all platforms, |
| 50 | * so it's easiest to just keep it as static explicit data. |
| 51 | * This also removes the need for any initialisation of this class. |
| 52 | * |
| 53 | * Note that making this an int[] instead of a char[] might make the |
| 54 | * code run faster on platforms with a high penalty for unaligned single |
| 55 | * byte addressing. Intel x86 is generally single-byte-friendly, but |
| 56 | * some other CPUs are faster with 4-aligned reads. |
| 57 | * However, a char[] is smaller, which avoids cache trashing, and that |
| 58 | * is probably the most important aspect on most architectures. |
| 59 | * This array is accessed a *lot* by the noise functions. |
| 60 | * A vector-valued noise over 3D accesses it 96 times, and a |
| 61 | * float-valued 4D noise 64 times. We want this to fit in the cache! |
| 62 | */ |
| 63 | unsigned char SimplexNoise1234::perm[512] = {151,160,137,91,90,15, |
| 64 | 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, |
| 65 | 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, |
| 66 | 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, |
| 67 | 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, |
| 68 | 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, |
| 69 | 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, |
| 70 | 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, |
| 71 | 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, |
| 72 | 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, |
| 73 | 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, |
| 74 | 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, |
| 75 | 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180, |
| 76 | 151,160,137,91,90,15, |
| 77 | 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, |
| 78 | 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, |
| 79 | 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, |
| 80 | 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, |
| 81 | 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, |
| 82 | 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, |
| 83 | 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, |
| 84 | 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, |
| 85 | 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, |
| 86 | 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, |
| 87 | 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, |
| 88 | 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 |
| 89 | }; |
| 90 | |
| 91 | //--------------------------------------------------------------------- |
| 92 | |
| 93 | /* |
| 94 | * Helper functions to compute gradients-dot-residualvectors (1D to 4D) |
| 95 | * Note that these generate gradients of more than unit length. To make |
| 96 | * a close match with the value range of classic Perlin noise, the final |
| 97 | * noise values need to be rescaled to fit nicely within [-1,1]. |
| 98 | * (The simplex noise functions as such also have different scaling.) |
| 99 | * Note also that these noise functions are the most practical and useful |
| 100 | * signed version of Perlin noise. To return values according to the |
| 101 | * RenderMan specification from the SL noise() and pnoise() functions, |
| 102 | * the noise values need to be scaled and offset to [0,1], like this: |
| 103 | * float SLnoise = (SimplexNoise1234::noise(x,y,z) + 1.0) * 0.5; |
| 104 | */ |
| 105 | |
| 106 | float SimplexNoise1234::grad( int hash, float x ) { |
| 107 | int h = hash & 15; |
| 108 | float grad = 1.0f + (h & 7); // Gradient value 1.0, 2.0, ..., 8.0 |
| 109 | if (h&8) grad = -grad; // Set a random sign for the gradient |
| 110 | return ( grad * x ); // Multiply the gradient with the distance |
| 111 | } |
| 112 | |
| 113 | float SimplexNoise1234::grad( int hash, float x, float y ) { |
| 114 | int h = hash & 7; // Convert low 3 bits of hash code |
| 115 | float u = h<4 ? x : y; // into 8 simple gradient directions, |
| 116 | float v = h<4 ? y : x; // and compute the dot product with (x,y). |
| 117 | return ((h&1)? -u : u) + ((h&2)? -2.0f*v : 2.0f*v); |
| 118 | } |
| 119 | |
| 120 | // 1D simplex noise |
| 121 | float SimplexNoise1234::noise(float x) { |
| 122 | |
| 123 | int i0 = FASTFLOOR(x); |
| 124 | int i1 = i0 + 1; |
| 125 | float x0 = x - i0; |
| 126 | float x1 = x0 - 1.0f; |
| 127 | |
| 128 | float n0, n1; |
| 129 | |
| 130 | float t0 = 1.0f - x0*x0; |
| 131 | // if(t0 < 0.0f) t0 = 0.0f; |
| 132 | t0 *= t0; |
| 133 | n0 = t0 * t0 * grad(perm[i0 & 0xff], x0); |
| 134 | |
| 135 | float t1 = 1.0f - x1*x1; |
| 136 | // if(t1 < 0.0f) t1 = 0.0f; |
| 137 | t1 *= t1; |
| 138 | n1 = t1 * t1 * grad(perm[i1 & 0xff], x1); |
| 139 | // The maximum value of this noise is 8*(3/4)^4 = 2.53125 |
| 140 | // A factor of 0.395 will scale to fit exactly within [-1,1] |
| 141 | return 0.395f * (n0 + n1); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | // 2D simplex noise |
| 146 | float SimplexNoise1234::noise(float x, float y) { |
| 147 | |
| 148 | #define F2 0.366025403 // F2 = 0.5*(sqrt(3.0)-1.0) |
| 149 | #define G2 0.211324865 // G2 = (3.0-Math.sqrt(3.0))/6.0 |
| 150 | |
| 151 | float n0, n1, n2; // Noise contributions from the three corners |
| 152 | |
| 153 | // Skew the input space to determine which simplex cell we're in |
| 154 | float s = (x+y)*F2; // Hairy factor for 2D |
| 155 | float xs = x + s; |
| 156 | float ys = y + s; |
| 157 | int i = FASTFLOOR(xs); |
| 158 | int j = FASTFLOOR(ys); |
| 159 | |
| 160 | float t = (float)(i+j)*G2; |
| 161 | float X0 = i-t; // Unskew the cell origin back to (x,y) space |
| 162 | float Y0 = j-t; |
| 163 | float x0 = x-X0; // The x,y distances from the cell origin |
| 164 | float y0 = y-Y0; |
| 165 | |
| 166 | // For the 2D case, the simplex shape is an equilateral triangle. |
| 167 | // Determine which simplex we are in. |
| 168 | int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords |
| 169 | if(x0>y0) {i1=1; j1=0;} // lower triangle, XY order: (0,0)->(1,0)->(1,1) |
| 170 | else {i1=0; j1=1;} // upper triangle, YX order: (0,0)->(0,1)->(1,1) |
| 171 | |
| 172 | // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and |
| 173 | // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where |
| 174 | // c = (3-sqrt(3))/6 |
| 175 | |
| 176 | float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords |
| 177 | float y1 = y0 - j1 + G2; |
| 178 | float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords |
| 179 | float y2 = y0 - 1.0f + 2.0f * G2; |
| 180 | |
| 181 | // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds |
| 182 | int ii = i & 0xff; |
| 183 | int jj = j & 0xff; |
| 184 | |
| 185 | // Calculate the contribution from the three corners |
| 186 | float t0 = 0.5f - x0*x0-y0*y0; |
| 187 | if(t0 < 0.0f) n0 = 0.0f; |
| 188 | else { |
| 189 | t0 *= t0; |
| 190 | n0 = t0 * t0 * grad(perm[ii+perm[jj]], x0, y0); |
| 191 | } |
| 192 | |
| 193 | float t1 = 0.5f - x1*x1-y1*y1; |
| 194 | if(t1 < 0.0f) n1 = 0.0f; |
| 195 | else { |
| 196 | t1 *= t1; |
| 197 | n1 = t1 * t1 * grad(perm[ii+i1+perm[jj+j1]], x1, y1); |
| 198 | } |
| 199 | |
| 200 | float t2 = 0.5f - x2*x2-y2*y2; |
| 201 | if(t2 < 0.0f) n2 = 0.0f; |
| 202 | else { |
| 203 | t2 *= t2; |
| 204 | n2 = t2 * t2 * grad(perm[ii+1+perm[jj+1]], x2, y2); |
| 205 | } |
| 206 | |
| 207 | // Add contributions from each corner to get the final noise value. |
| 208 | // The result is scaled to return values in the interval [-1,1]. |
| 209 | return 45.23f * (n0 + n1 + n2); // TODO: The scale factor is preliminary! |
| 210 | } |
| 211 | |