perlin noise function pull request by Jack Mott but deleted before I could pull it
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent 03611a3 · Browse files · 1 file·+106 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+106 added−12 removed
- other:+106 added−12 removed
Provenance
- Commit
a2bc1ea- Tree
3a3ac3a- Author
Sean Barrett <sean@nothings.org>- Committer
Sean Barrett <sean@nothings.org>- Parent
03611a3
1 file changed+106 added−12 removed
modified:stb_perlin.h+106 added-12 removed
1-// stb_perlin.h - v0.2 - perlin noise1+// stb_perlin.h - v0.3 - perlin noise2 // public domain single-file C implementation by Sean Barrett3 //4 // LICENSE5 //2 unmodified lines8 //9 // to create the implementation,10 // #define STB_PERLIN_IMPLEMENTATION11 // in *one* C/CPP file that includes this file.12-13-12+//13+//14 // Documentation:15 //16 // float stb_perlin_noise3( float x,17 // float y,12 unmodified lines30 // wraps at powers of two. The numbers MUST be powers of two. Specify31 // 0 to mean "don't care". (The noise always wraps every 256 due32 // details of the implementation, even if you ask for larger or no33 // wrapping.)34+//35+// Fractal Noise:36+//37+// Three common fractal noise functions are included, which produce 38+// a wide variety of nice effects depending on the parameters 39+// provided. Note that each function will call stb_perlin_noise3 40+// 'octaves' times, so this parameter will affect runtime.41+//42+// float stb_perlin_ridge_noise3(float x, float y, float z,43+// float lacunarity, float gain, float offset, int octaves,44+// int x_wrap, int y_wrap, int z_wrap);45+//46+// float stb_perlin_fbm_noise3(float x, float y, float z,47+// float lacunarity, float gain, int octaves,48+// int x_wrap, int y_wrap, int z_wrap);49+//50+// float stb_perlin_turbulence_noise3(float x, float y, float z,51+// float lacunarity, float gain,int octaves,52+// int x_wrap, int y_wrap, int z_wrap);53+//54+// Typical values to start playing with:55+// octaves = 6 -- number of "octaves" of noise3() to sum56+// lacunarity = ~ 2.0 -- spacing between successive octaves (use exactly 2.0 for wrapping output)57+// gain = 0.5 -- relative weighting applied to each successive octave58+// offset = 1.0? -- used to invert the ridges, may need to be larger, not sure59+// 60+//61+// Contributors:62+// Jack Mott - additional noise functions63+//64 65 66 #ifdef __cplusplus37-extern "C" float stb_perlin_noise3(float x, float y, float z, int x_wrap=0, int y_wrap=0, int z_wrap=0);38-#else67+extern "C" {68+#endif69 extern float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap);70+extern float stb_perlin_ridge_noise3(float x, float y, float z,float lacunarity, float gain, float offset, int octaves,int x_wrap, int y_wrap, int z_wrap);71+extern float stb_perlin_fbm_noise3(float x, float y, float z,float lacunarity, float gain, int octaves,int x_wrap, int y_wrap, int z_wrap);72+extern float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves,int x_wrap, int y_wrap, int z_wrap);73+#ifdef __cplusplus74+}75 #endif76 77 #ifdef STB_PERLIN_IMPLEMENTATION78 44-#include <math.h> // floor()45-79 // not same permutation table as Perlin's reference to avoid copyright issues;80 // Perlin's table can be found at http://mrl.nyu.edu/~perlin/noise/81 // @OPTIMIZE: should this be unsigned char instead of int for cache?49-static int stb__perlin_randtab[512] =82+static unsigned char stb__perlin_randtab[512] =83 {84 23, 125, 161, 52, 103, 117, 70, 37, 247, 101, 203, 169, 124, 126, 44, 123, 85 152, 238, 145, 45, 171, 114, 253, 10, 192, 136, 4, 157, 249, 30, 35, 72, 86 175, 63, 77, 90, 181, 16, 96, 111, 133, 104, 75, 162, 93, 56, 66, 240, 34 unmodified lines121 {122 return a + (b-a) * t;123 }124 125+static int stb__perlin_fastfloor(float a)126+{127+ int ai = (int) a;128+ return (a < ai) ? ai-1 : ai;129+}130+131 // different grad function from Perlin's, but easy to modify to match reference132 static float stb__perlin_grad(int hash, float x, float y, float z)133 {134 static float basis[12][4] =13 unmodified lines148 };149 150 // perlin's gradient has 12 cases so some get used 1/16th of the time151 // and some 2/16ths. We reduce bias by changing those fractions113- // to 5/16ths and 6/16ths, and the same 4 cases get the extra weight.152+ // to 5/64ths and 6/64ths, and the same 4 cases get the extra weight.153 static unsigned char indices[64] =154 {155 0,1,2,3,4,5,6,7,8,9,10,11,156 0,9,1,11,3 unmodified lines160 0,1,2,3,4,5,6,7,8,9,10,11,161 };162 163 // if you use reference permutation table, change 63 below to 15 to match reference164+ // (this is why the ordering of the table above is funky)165 float *grad = basis[indices[hash & 63]];166 return grad[0]*x + grad[1]*y + grad[2]*z;167 }168 6 unmodified lines175 176 unsigned int x_mask = (x_wrap-1) & 255;177 unsigned int y_mask = (y_wrap-1) & 255;178 unsigned int z_mask = (z_wrap-1) & 255;139- int px = (int) floor(x);140- int py = (int) floor(y);141- int pz = (int) floor(z);179+ int px = stb__perlin_fastfloor(x);180+ int py = stb__perlin_fastfloor(y);181+ int pz = stb__perlin_fastfloor(z);182 int x0 = px & x_mask, x1 = (px+1) & x_mask;183 int y0 = py & y_mask, y1 = (py+1) & y_mask;184 int z0 = pz & z_mask, z1 = (pz+1) & z_mask;185 int r0,r1, r00,r01,r10,r11;30 unmodified lines216 n1 = stb__perlin_lerp(n10,n11,v);217 218 return stb__perlin_lerp(n0,n1,u);219 }220+221+float stb_perlin_ridge_noise3(float x, float y, float z,float lacunarity, float gain, float offset, int octaves,int x_wrap, int y_wrap, int z_wrap)222+{223+ int i;224+ float frequency = 1.0f;225+ float prev = 1.0f;226+ float amplitude = 0.5f;227+ float sum = 0.0f;228+229+ for (i = 0; i < octaves; i++) {230+ float r = (float)(stb_perlin_noise3(x*frequency,y*frequency,z*frequency,x_wrap,y_wrap,z_wrap));231+ r = r<0 ? -r : r; // fabs()232+ r = offset - r;233+ r = r*r;234+ sum += r*amplitude*prev;235+ prev = r;236+ frequency *= lacunarity;237+ amplitude *= gain;238+ }239+ return sum;240+}241+242+float stb_perlin_fbm_noise3(float x, float y, float z,float lacunarity, float gain, int octaves,int x_wrap, int y_wrap, int z_wrap)243+{244+ int i;245+ float frequency = 1.0f;246+ float amplitude = 1.0f;247+ float sum = 0.0f;248+ 249+ for (i = 0; i < octaves; i++) {250+ sum += stb_perlin_noise3(x*frequency,y*frequency,z*frequency,x_wrap,y_wrap,z_wrap)*amplitude;251+ frequency *= lacunarity;252+ amplitude *= gain;253+ }254+ return sum;255+}256+257+float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves,int x_wrap, int y_wrap, int z_wrap)258+{259+ int i;260+ float frequency = 1.0f;261+ float amplitude = 1.0f;262+ float sum = 0.0f;263+ 264+ for (i = 0; i < octaves; i++) {265+ float r = stb_perlin_noise3(x*frequency,y*frequency,z*frequency,x_wrap,y_wrap,z_wrap)*amplitude;266+ r = r<0 ? -r : r; // fabs()267+ sum += r;268+ frequency *= lacunarity;269+ amplitude *= gain;270+ }271+ return sum;272+}273+274 #endif // STB_PERLIN_IMPLEMENTATION275 276 /*277 ------------------------------------------------------------------------------39 unmodified lines