| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | #ifdef __cplusplus |
| 78 | extern "C" { |
| 79 | #endif |
| 80 | extern float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap); |
| 81 | extern float stb_perlin_noise3_seed(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, int seed); |
| 82 | extern float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves); |
| 83 | extern float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves); |
| 84 | extern float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves); |
| 85 | extern float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed); |
| 86 | #ifdef __cplusplus |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | #ifdef STB_PERLIN_IMPLEMENTATION |
| 91 | |
| 92 | #include <math.h> |
| 93 | |
| 94 | |
| 95 | |
| 96 | static unsigned char stb__perlin_randtab[512] = |
| 97 | { |
| 98 | 23, 125, 161, 52, 103, 117, 70, 37, 247, 101, 203, 169, 124, 126, 44, 123, |
| 99 | 152, 238, 145, 45, 171, 114, 253, 10, 192, 136, 4, 157, 249, 30, 35, 72, |
| 100 | 175, 63, 77, 90, 181, 16, 96, 111, 133, 104, 75, 162, 93, 56, 66, 240, |
| 101 | 8, 50, 84, 229, 49, 210, 173, 239, 141, 1, 87, 18, 2, 198, 143, 57, |
| 102 | 225, 160, 58, 217, 168, 206, 245, 204, 199, 6, 73, 60, 20, 230, 211, 233, |
| 103 | 94, 200, 88, 9, 74, 155, 33, 15, 219, 130, 226, 202, 83, 236, 42, 172, |
| 104 | 165, 218, 55, 222, 46, 107, 98, 154, 109, 67, 196, 178, 127, 158, 13, 243, |
| 105 | 65, 79, 166, 248, 25, 224, 115, 80, 68, 51, 184, 128, 232, 208, 151, 122, |
| 106 | 26, 212, 105, 43, 179, 213, 235, 148, 146, 89, 14, 195, 28, 78, 112, 76, |
| 107 | 250, 47, 24, 251, 140, 108, 186, 190, 228, 170, 183, 139, 39, 188, 244, 246, |
| 108 | 132, 48, 119, 144, 180, 138, 134, 193, 82, 182, 120, 121, 86, 220, 209, 3, |
| 109 | 91, 241, 149, 85, 205, 150, 113, 216, 31, 100, 41, 164, 177, 214, 153, 231, |
| 110 | 38, 71, 185, 174, 97, 201, 29, 95, 7, 92, 54, 254, 191, 118, 34, 221, |
| 111 | 131, 11, 163, 99, 234, 81, 227, 147, 156, 176, 17, 142, 69, 12, 110, 62, |
| 112 | 27, 255, 0, 194, 59, 116, 242, 252, 19, 21, 187, 53, 207, 129, 64, 135, |
| 113 | 61, 40, 167, 237, 102, 223, 106, 159, 197, 189, 215, 137, 36, 32, 22, 5, |
| 114 | |
| 115 | |
| 116 | 23, 125, 161, 52, 103, 117, 70, 37, 247, 101, 203, 169, 124, 126, 44, 123, |
| 117 | 152, 238, 145, 45, 171, 114, 253, 10, 192, 136, 4, 157, 249, 30, 35, 72, |
| 118 | 175, 63, 77, 90, 181, 16, 96, 111, 133, 104, 75, 162, 93, 56, 66, 240, |
| 119 | 8, 50, 84, 229, 49, 210, 173, 239, 141, 1, 87, 18, 2, 198, 143, 57, |
| 120 | 225, 160, 58, 217, 168, 206, 245, 204, 199, 6, 73, 60, 20, 230, 211, 233, |
| 121 | 94, 200, 88, 9, 74, 155, 33, 15, 219, 130, 226, 202, 83, 236, 42, 172, |
| 122 | 165, 218, 55, 222, 46, 107, 98, 154, 109, 67, 196, 178, 127, 158, 13, 243, |
| 123 | 65, 79, 166, 248, 25, 224, 115, 80, 68, 51, 184, 128, 232, 208, 151, 122, |
| 124 | 26, 212, 105, 43, 179, 213, 235, 148, 146, 89, 14, 195, 28, 78, 112, 76, |
| 125 | 250, 47, 24, 251, 140, 108, 186, 190, 228, 170, 183, 139, 39, 188, 244, 246, |
| 126 | 132, 48, 119, 144, 180, 138, 134, 193, 82, 182, 120, 121, 86, 220, 209, 3, |
| 127 | 91, 241, 149, 85, 205, 150, 113, 216, 31, 100, 41, 164, 177, 214, 153, 231, |
| 128 | 38, 71, 185, 174, 97, 201, 29, 95, 7, 92, 54, 254, 191, 118, 34, 221, |
| 129 | 131, 11, 163, 99, 234, 81, 227, 147, 156, 176, 17, 142, 69, 12, 110, 62, |
| 130 | 27, 255, 0, 194, 59, 116, 242, 252, 19, 21, 187, 53, 207, 129, 64, 135, |
| 131 | 61, 40, 167, 237, 102, 223, 106, 159, 197, 189, 215, 137, 36, 32, 22, 5, |
| 132 | }; |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | static unsigned char stb__perlin_randtab_grad_idx[512] = |
| 142 | { |
| 143 | 7, 9, 5, 0, 11, 1, 6, 9, 3, 9, 11, 1, 8, 10, 4, 7, |
| 144 | 8, 6, 1, 5, 3, 10, 9, 10, 0, 8, 4, 1, 5, 2, 7, 8, |
| 145 | 7, 11, 9, 10, 1, 0, 4, 7, 5, 0, 11, 6, 1, 4, 2, 8, |
| 146 | 8, 10, 4, 9, 9, 2, 5, 7, 9, 1, 7, 2, 2, 6, 11, 5, |
| 147 | 5, 4, 6, 9, 0, 1, 1, 0, 7, 6, 9, 8, 4, 10, 3, 1, |
| 148 | 2, 8, 8, 9, 10, 11, 5, 11, 11, 2, 6, 10, 3, 4, 2, 4, |
| 149 | 9, 10, 3, 2, 6, 3, 6, 10, 5, 3, 4, 10, 11, 2, 9, 11, |
| 150 | 1, 11, 10, 4, 9, 4, 11, 0, 4, 11, 4, 0, 0, 0, 7, 6, |
| 151 | 10, 4, 1, 3, 11, 5, 3, 4, 2, 9, 1, 3, 0, 1, 8, 0, |
| 152 | 6, 7, 8, 7, 0, 4, 6, 10, 8, 2, 3, 11, 11, 8, 0, 2, |
| 153 | 4, 8, 3, 0, 0, 10, 6, 1, 2, 2, 4, 5, 6, 0, 1, 3, |
| 154 | 11, 9, 5, 5, 9, 6, 9, 8, 3, 8, 1, 8, 9, 6, 9, 11, |
| 155 | 10, 7, 5, 6, 5, 9, 1, 3, 7, 0, 2, 10, 11, 2, 6, 1, |
| 156 | 3, 11, 7, 7, 2, 1, 7, 3, 0, 8, 1, 1, 5, 0, 6, 10, |
| 157 | 11, 11, 0, 2, 7, 0, 10, 8, 3, 5, 7, 1, 11, 1, 0, 7, |
| 158 | 9, 0, 11, 5, 10, 3, 2, 3, 5, 9, 7, 9, 8, 4, 6, 5, |
| 159 | |
| 160 | |
| 161 | 7, 9, 5, 0, 11, 1, 6, 9, 3, 9, 11, 1, 8, 10, 4, 7, |
| 162 | 8, 6, 1, 5, 3, 10, 9, 10, 0, 8, 4, 1, 5, 2, 7, 8, |
| 163 | 7, 11, 9, 10, 1, 0, 4, 7, 5, 0, 11, 6, 1, 4, 2, 8, |
| 164 | 8, 10, 4, 9, 9, 2, 5, 7, 9, 1, 7, 2, 2, 6, 11, 5, |
| 165 | 5, 4, 6, 9, 0, 1, 1, 0, 7, 6, 9, 8, 4, 10, 3, 1, |
| 166 | 2, 8, 8, 9, 10, 11, 5, 11, 11, 2, 6, 10, 3, 4, 2, 4, |
| 167 | 9, 10, 3, 2, 6, 3, 6, 10, 5, 3, 4, 10, 11, 2, 9, 11, |
| 168 | 1, 11, 10, 4, 9, 4, 11, 0, 4, 11, 4, 0, 0, 0, 7, 6, |
| 169 | 10, 4, 1, 3, 11, 5, 3, 4, 2, 9, 1, 3, 0, 1, 8, 0, |
| 170 | 6, 7, 8, 7, 0, 4, 6, 10, 8, 2, 3, 11, 11, 8, 0, 2, |
| 171 | 4, 8, 3, 0, 0, 10, 6, 1, 2, 2, 4, 5, 6, 0, 1, 3, |
| 172 | 11, 9, 5, 5, 9, 6, 9, 8, 3, 8, 1, 8, 9, 6, 9, 11, |
| 173 | 10, 7, 5, 6, 5, 9, 1, 3, 7, 0, 2, 10, 11, 2, 6, 1, |
| 174 | 3, 11, 7, 7, 2, 1, 7, 3, 0, 8, 1, 1, 5, 0, 6, 10, |
| 175 | 11, 11, 0, 2, 7, 0, 10, 8, 3, 5, 7, 1, 11, 1, 0, 7, |
| 176 | 9, 0, 11, 5, 10, 3, 2, 3, 5, 9, 7, 9, 8, 4, 6, 5, |
| 177 | }; |
| 178 | |
| 179 | static float stb__perlin_lerp(float a, float b, float t) |
| 180 | { |
| 181 | return a + (b-a) * t; |
| 182 | } |
| 183 | |
| 184 | static int stb__perlin_fastfloor(float a) |
| 185 | { |
| 186 | int ai = (int) a; |
| 187 | return (a < ai) ? ai-1 : ai; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | static float stb__perlin_grad(int grad_idx, float x, float y, float z) |
| 192 | { |
| 193 | static float basis[12][4] = |
| 194 | { |
| 195 | { 1, 1, 0 }, |
| 196 | { -1, 1, 0 }, |
| 197 | { 1,-1, 0 }, |
| 198 | { -1,-1, 0 }, |
| 199 | { 1, 0, 1 }, |
| 200 | { -1, 0, 1 }, |
| 201 | { 1, 0,-1 }, |
| 202 | { -1, 0,-1 }, |
| 203 | { 0, 1, 1 }, |
| 204 | { 0,-1, 1 }, |
| 205 | { 0, 1,-1 }, |
| 206 | { 0,-1,-1 }, |
| 207 | }; |
| 208 | |
| 209 | float *grad = basis[grad_idx]; |
| 210 | return grad[0]*x + grad[1]*y + grad[2]*z; |
| 211 | } |
| 212 | |
| 213 | float stb_perlin_noise3_internal(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed) |
| 214 | { |
| 215 | float u,v,w; |
| 216 | float n000,n001,n010,n011,n100,n101,n110,n111; |
| 217 | float n00,n01,n10,n11; |
| 218 | float n0,n1; |
| 219 | |
| 220 | unsigned int x_mask = (x_wrap-1) & 255; |
| 221 | unsigned int y_mask = (y_wrap-1) & 255; |
| 222 | unsigned int z_mask = (z_wrap-1) & 255; |
| 223 | int px = stb__perlin_fastfloor(x); |
| 224 | int py = stb__perlin_fastfloor(y); |
| 225 | int pz = stb__perlin_fastfloor(z); |
| 226 | int x0 = px & x_mask, x1 = (px+1) & x_mask; |
| 227 | int y0 = py & y_mask, y1 = (py+1) & y_mask; |
| 228 | int z0 = pz & z_mask, z1 = (pz+1) & z_mask; |
| 229 | int r0,r1, r00,r01,r10,r11; |
| 230 | |
| 231 | #define stb__perlin_ease(a) (((a*6-15)*a + 10) * a * a * a) |
| 232 | |
| 233 | x -= px; u = stb__perlin_ease(x); |
| 234 | y -= py; v = stb__perlin_ease(y); |
| 235 | z -= pz; w = stb__perlin_ease(z); |
| 236 | |
| 237 | r0 = stb__perlin_randtab[x0+seed]; |
| 238 | r1 = stb__perlin_randtab[x1+seed]; |
| 239 | |
| 240 | r00 = stb__perlin_randtab[r0+y0]; |
| 241 | r01 = stb__perlin_randtab[r0+y1]; |
| 242 | r10 = stb__perlin_randtab[r1+y0]; |
| 243 | r11 = stb__perlin_randtab[r1+y1]; |
| 244 | |
| 245 | n000 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z0], x , y , z ); |
| 246 | n001 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z1], x , y , z-1 ); |
| 247 | n010 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z0], x , y-1, z ); |
| 248 | n011 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z1], x , y-1, z-1 ); |
| 249 | n100 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z0], x-1, y , z ); |
| 250 | n101 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z1], x-1, y , z-1 ); |
| 251 | n110 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z0], x-1, y-1, z ); |
| 252 | n111 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z1], x-1, y-1, z-1 ); |
| 253 | |
| 254 | n00 = stb__perlin_lerp(n000,n001,w); |
| 255 | n01 = stb__perlin_lerp(n010,n011,w); |
| 256 | n10 = stb__perlin_lerp(n100,n101,w); |
| 257 | n11 = stb__perlin_lerp(n110,n111,w); |
| 258 | |
| 259 | n0 = stb__perlin_lerp(n00,n01,v); |
| 260 | n1 = stb__perlin_lerp(n10,n11,v); |
| 261 | |
| 262 | return stb__perlin_lerp(n0,n1,u); |
| 263 | } |
| 264 | |
| 265 | float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap) |
| 266 | { |
| 267 | return stb_perlin_noise3_internal(x,y,z,x_wrap,y_wrap,z_wrap,0); |
| 268 | } |
| 269 | |
| 270 | float stb_perlin_noise3_seed(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, int seed) |
| 271 | { |
| 272 | return stb_perlin_noise3_internal(x,y,z,x_wrap,y_wrap,z_wrap, (unsigned char) seed); |
| 273 | } |
| 274 | |
| 275 | float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves) |
| 276 | { |
| 277 | int i; |
| 278 | float frequency = 1.0f; |
| 279 | float prev = 1.0f; |
| 280 | float amplitude = 0.5f; |
| 281 | float sum = 0.0f; |
| 282 | |
| 283 | for (i = 0; i < octaves; i++) { |
| 284 | float r = stb_perlin_noise3_internal(x*frequency,y*frequency,z*frequency,0,0,0,(unsigned char)i); |
| 285 | r = offset - (float) fabs(r); |
| 286 | r = r*r; |
| 287 | sum += r*amplitude*prev; |
| 288 | prev = r; |
| 289 | frequency *= lacunarity; |
| 290 | amplitude *= gain; |
| 291 | } |
| 292 | return sum; |
| 293 | } |
| 294 | |
| 295 | float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves) |
| 296 | { |
| 297 | int i; |
| 298 | float frequency = 1.0f; |
| 299 | float amplitude = 1.0f; |
| 300 | float sum = 0.0f; |
| 301 | |
| 302 | for (i = 0; i < octaves; i++) { |
| 303 | sum += stb_perlin_noise3_internal(x*frequency,y*frequency,z*frequency,0,0,0,(unsigned char)i)*amplitude; |
| 304 | frequency *= lacunarity; |
| 305 | amplitude *= gain; |
| 306 | } |
| 307 | return sum; |
| 308 | } |
| 309 | |
| 310 | float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves) |
| 311 | { |
| 312 | int i; |
| 313 | float frequency = 1.0f; |
| 314 | float amplitude = 1.0f; |
| 315 | float sum = 0.0f; |
| 316 | |
| 317 | for (i = 0; i < octaves; i++) { |
| 318 | float r = stb_perlin_noise3_internal(x*frequency,y*frequency,z*frequency,0,0,0,(unsigned char)i)*amplitude; |
| 319 | sum += (float) fabs(r); |
| 320 | frequency *= lacunarity; |
| 321 | amplitude *= gain; |
| 322 | } |
| 323 | return sum; |
| 324 | } |
| 325 | |
| 326 | float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed) |
| 327 | { |
| 328 | float u,v,w; |
| 329 | float n000,n001,n010,n011,n100,n101,n110,n111; |
| 330 | float n00,n01,n10,n11; |
| 331 | float n0,n1; |
| 332 | |
| 333 | int px = stb__perlin_fastfloor(x); |
| 334 | int py = stb__perlin_fastfloor(y); |
| 335 | int pz = stb__perlin_fastfloor(z); |
| 336 | int x_wrap2 = (x_wrap ? x_wrap : 256); |
| 337 | int y_wrap2 = (y_wrap ? y_wrap : 256); |
| 338 | int z_wrap2 = (z_wrap ? z_wrap : 256); |
| 339 | int x0 = px % x_wrap2, x1; |
| 340 | int y0 = py % y_wrap2, y1; |
| 341 | int z0 = pz % z_wrap2, z1; |
| 342 | int r0,r1, r00,r01,r10,r11; |
| 343 | |
| 344 | if (x0 < 0) x0 += x_wrap2; |
| 345 | if (y0 < 0) y0 += y_wrap2; |
| 346 | if (z0 < 0) z0 += z_wrap2; |
| 347 | x1 = (x0+1) % x_wrap2; |
| 348 | y1 = (y0+1) % y_wrap2; |
| 349 | z1 = (z0+1) % z_wrap2; |
| 350 | |
| 351 | #define stb__perlin_ease(a) (((a*6-15)*a + 10) * a * a * a) |
| 352 | |
| 353 | x -= px; u = stb__perlin_ease(x); |
| 354 | y -= py; v = stb__perlin_ease(y); |
| 355 | z -= pz; w = stb__perlin_ease(z); |
| 356 | |
| 357 | r0 = stb__perlin_randtab[x0]; |
| 358 | r0 = stb__perlin_randtab[r0+seed]; |
| 359 | r1 = stb__perlin_randtab[x1]; |
| 360 | r1 = stb__perlin_randtab[r1+seed]; |
| 361 | |
| 362 | r00 = stb__perlin_randtab[r0+y0]; |
| 363 | r01 = stb__perlin_randtab[r0+y1]; |
| 364 | r10 = stb__perlin_randtab[r1+y0]; |
| 365 | r11 = stb__perlin_randtab[r1+y1]; |
| 366 | |
| 367 | n000 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z0], x , y , z ); |
| 368 | n001 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z1], x , y , z-1 ); |
| 369 | n010 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z0], x , y-1, z ); |
| 370 | n011 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z1], x , y-1, z-1 ); |
| 371 | n100 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z0], x-1, y , z ); |
| 372 | n101 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z1], x-1, y , z-1 ); |
| 373 | n110 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z0], x-1, y-1, z ); |
| 374 | n111 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z1], x-1, y-1, z-1 ); |
| 375 | |
| 376 | n00 = stb__perlin_lerp(n000,n001,w); |
| 377 | n01 = stb__perlin_lerp(n010,n011,w); |
| 378 | n10 = stb__perlin_lerp(n100,n101,w); |
| 379 | n11 = stb__perlin_lerp(n110,n111,w); |
| 380 | |
| 381 | n0 = stb__perlin_lerp(n00,n01,v); |
| 382 | n1 = stb__perlin_lerp(n10,n11,v); |
| 383 | |
| 384 | return stb__perlin_lerp(n0,n1,u); |
| 385 | } |
| 386 | #endif |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |