79extern float stb_perlin_noise3(
float x,
float y,
float z,
int x_wrap,
int y_wrap,
int z_wrap);
108#ifdef STB_PERLIN_IMPLEMENTATION
114static unsigned char stb__perlin_randtab[512] = {
637static unsigned char stb__perlin_randtab_grad_idx[512] = {
1154static float stb__perlin_lerp(
float a,
float b,
float t) {
return a + (b - a) * t; }
1156static int stb__perlin_fastfloor(
float a) {
1158 return (a < ai) ? ai - 1 : ai;
1162static float stb__perlin_grad(
int grad_idx,
float x,
float y,
float z) {
1163 static float basis[12][4] = {
1164 { 1, 1, 0 }, { -1, 1, 0 }, { 1, -1, 0 }, { -1, -1, 0 }, { 1, 0, 1 }, { -1, 0, 1 },
1165 { 1, 0, -1 }, { -1, 0, -1 }, { 0, 1, 1 }, { 0, -1, 1 }, { 0, 1, -1 }, { 0, -1, -1 },
1168 float* grad = basis[grad_idx];
1169 return grad[0] * x + grad[1] * y + grad[2] * z;
1172float stb_perlin_noise3_internal(
1182 float n000, n001, n010, n011, n100, n101, n110, n111;
1183 float n00, n01, n10, n11;
1186 unsigned int x_mask = (x_wrap - 1) & 255;
1187 unsigned int y_mask = (y_wrap - 1) & 255;
1188 unsigned int z_mask = (z_wrap - 1) & 255;
1189 int px = stb__perlin_fastfloor(x);
1190 int py = stb__perlin_fastfloor(y);
1191 int pz = stb__perlin_fastfloor(z);
1192 int x0 = px & x_mask, x1 = (px + 1) & x_mask;
1193 int y0 = py & y_mask, y1 = (py + 1) & y_mask;
1194 int z0 = pz & z_mask, z1 = (pz + 1) & z_mask;
1195 int r0, r1, r00, r01, r10, r11;
1197#define stb__perlin_ease(a) (((a * 6 - 15) * a + 10) * a * a * a)
1200 u = stb__perlin_ease(x);
1202 v = stb__perlin_ease(y);
1204 w = stb__perlin_ease(z);
1206 r0 = stb__perlin_randtab[x0 + seed];
1207 r1 = stb__perlin_randtab[x1 + seed];
1209 r00 = stb__perlin_randtab[r0 + y0];
1210 r01 = stb__perlin_randtab[r0 + y1];
1211 r10 = stb__perlin_randtab[r1 + y0];
1212 r11 = stb__perlin_randtab[r1 + y1];
1214 n000 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00 + z0], x, y, z);
1215 n001 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00 + z1], x, y, z - 1);
1216 n010 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01 + z0], x, y - 1, z);
1217 n011 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01 + z1], x, y - 1, z - 1);
1218 n100 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10 + z0], x - 1, y, z);
1219 n101 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10 + z1], x - 1, y, z - 1);
1220 n110 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11 + z0], x - 1, y - 1, z);
1221 n111 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11 + z1], x - 1, y - 1, z - 1);
1223 n00 = stb__perlin_lerp(n000, n001, w);
1224 n01 = stb__perlin_lerp(n010, n011, w);
1225 n10 = stb__perlin_lerp(n100, n101, w);
1226 n11 = stb__perlin_lerp(n110, n111, w);
1228 n0 = stb__perlin_lerp(n00, n01, v);
1229 n1 = stb__perlin_lerp(n10, n11, v);
1231 return stb__perlin_lerp(n0, n1, u);
1234float stb_perlin_noise3(
float x,
float y,
float z,
int x_wrap,
int y_wrap,
int z_wrap) {
1235 return stb_perlin_noise3_internal(x, y, z, x_wrap, y_wrap, z_wrap, 0);
1247 return stb_perlin_noise3_internal(x, y, z, x_wrap, y_wrap, z_wrap, (
unsigned char)seed);
1260 float frequency = 1.0f;
1262 float amplitude = 0.5f;
1265 for (i = 0; i < octaves; i++) {
1266 float r = stb_perlin_noise3_internal(
1275 r = offset - (float)fabs(r);
1277 sum += r * amplitude * prev;
1279 frequency *= lacunarity;
1287 float frequency = 1.0f;
1288 float amplitude = 1.0f;
1291 for (i = 0; i < octaves; i++) {
1292 sum += stb_perlin_noise3_internal(
1302 frequency *= lacunarity;
1317 float frequency = 1.0f;
1318 float amplitude = 1.0f;
1321 for (i = 0; i < octaves; i++) {
1322 float r = stb_perlin_noise3_internal(
1332 sum += (float)fabs(r);
1333 frequency *= lacunarity;
1349 float n000, n001, n010, n011, n100, n101, n110, n111;
1350 float n00, n01, n10, n11;
1353 int px = stb__perlin_fastfloor(x);
1354 int py = stb__perlin_fastfloor(y);
1355 int pz = stb__perlin_fastfloor(z);
1356 int x_wrap2 = (x_wrap ? x_wrap : 256);
1357 int y_wrap2 = (y_wrap ? y_wrap : 256);
1358 int z_wrap2 = (z_wrap ? z_wrap : 256);
1359 int x0 = px % x_wrap2, x1;
1360 int y0 = py % y_wrap2, y1;
1361 int z0 = pz % z_wrap2, z1;
1362 int r0, r1, r00, r01, r10, r11;
1370 x1 = (x0 + 1) % x_wrap2;
1371 y1 = (y0 + 1) % y_wrap2;
1372 z1 = (z0 + 1) % z_wrap2;
1374#define stb__perlin_ease(a) (((a * 6 - 15) * a + 10) * a * a * a)
1377 u = stb__perlin_ease(x);
1379 v = stb__perlin_ease(y);
1381 w = stb__perlin_ease(z);
1383 r0 = stb__perlin_randtab[x0];
1384 r0 = stb__perlin_randtab[r0 + seed];
1385 r1 = stb__perlin_randtab[x1];
1386 r1 = stb__perlin_randtab[r1 + seed];
1388 r00 = stb__perlin_randtab[r0 + y0];
1389 r01 = stb__perlin_randtab[r0 + y1];
1390 r10 = stb__perlin_randtab[r1 + y0];
1391 r11 = stb__perlin_randtab[r1 + y1];
1393 n000 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00 + z0], x, y, z);
1394 n001 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00 + z1], x, y, z - 1);
1395 n010 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01 + z0], x, y - 1, z);
1396 n011 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01 + z1], x, y - 1, z - 1);
1397 n100 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10 + z0], x - 1, y, z);
1398 n101 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10 + z1], x - 1, y, z - 1);
1399 n110 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11 + z0], x - 1, y - 1, z);
1400 n111 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11 + z1], x - 1, y - 1, z - 1);
1402 n00 = stb__perlin_lerp(n000, n001, w);
1403 n01 = stb__perlin_lerp(n010, n011, w);
1404 n10 = stb__perlin_lerp(n100, n101, w);
1405 n11 = stb__perlin_lerp(n110, n111, w);
1407 n0 = stb__perlin_lerp(n00, n01, v);
1408 n1 = stb__perlin_lerp(n10, n11, v);
1410 return stb__perlin_lerp(n0, n1, u);
float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed)
float stb_perlin_noise3_seed(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, int seed)
float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap)
float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves)
float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves)
float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves)