24#ifdef __SANITIZE_ADDRESS__
26# if defined(__GNUC__) || defined(__clang__)
27# define ATTR_NO_SIGNED_INT_OVERFLOW [[gnu::no_sanitize("signed-integer-overflow")]]
30# define ATTR_NO_SIGNED_INT_OVERFLOW
33# define ATTR_NO_SIGNED_INT_OVERFLOW
46 return (
x << k) | (
x >> (32 - k));
92 a =
b = c = 0xdeadbeef + (1 << 2) + 13;
103 a =
b = c = 0xdeadbeef + (2 << 2) + 13;
115 a =
b = c = 0xdeadbeef + (3 << 2) + 13;
128 a =
b = c = 0xdeadbeef + (4 << 2) + 13;
162 v.x +=
v.y * 1664525;
163 v.y +=
v.x * 1664525;
165 v.x +=
v.y * 1664525;
166 v.y +=
v.x * 1664525;
331 return float(k & 0x7fffffff) * (1.0f /
float(0x7fffffff));
376template<
typename T>
T static mix(
T v0,
T v1,
float x)
378 return (1 -
x) * v0 +
x * v1;
394 return (1.0 -
y) * (v0 * x1 + v1 *
x) +
y * (
v2 * x1 + v3 *
x);
430 return z1 * (y1 * (v0 * x1 + v1 *
x) +
y * (
v2 * x1 + v3 *
x)) +
431 z * (y1 * (v4 * x1 + v5 *
x) +
y * (v6 * x1 + v7 *
x));
456 return mix(
mix(v0, v1,
v2, v3, v4, v5, v6, v7,
x,
y,
z),
457 mix(v8, v9, v10, v11, v12, v13, v14, v15,
x,
y,
z),
463 return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
468 return (
condition != 0u) ? -value : value;
474 float g = 1u + (h & 7u);
481 float u = h < 4u ?
x :
y;
482 float v = 2.0 * (h < 4u ?
y :
x);
489 float u = h < 8u ?
x :
y;
490 float vt =
ELEM(h, 12u, 14u) ?
x :
z;
491 float v = h < 4u ?
y : vt;
498 float u = h < 24u ?
x :
y;
499 float v = h < 16u ?
y :
z;
500 float s = h < 8u ?
z :
w;
688#if defined(_MSC_VER) && _MSC_VER >= 1930
696 const float roughness,
697 const float lacunarity,
705 for (
int i = 0; i <= int(
detail); i++) {
710 fscale *= lacunarity;
715 float sum2 = sum + t * amp;
716 return normalize ?
mix(0.5f * sum / maxamp + 0.5f, 0.5f * sum2 / (maxamp + amp) + 0.5f, rmd) :
719 return normalize ? 0.5f * sum / maxamp + 0.5f : sum;
725 const float roughness,
726 const float lacunarity,
731 const float roughness,
732 const float lacunarity,
741 for (
int i = 0; i <= int(
detail); i++) {
757 T p,
const float detail,
const float roughness,
const float lacunarity,
const float offset)
759 float pwr = roughness;
765 for (
int i = 1; i <= int(
detail); i++) {
775 value += rmd * increment;
784 const float roughness,
785 const float lacunarity,
793 for (
int i = 0; (weight > 0.001f) && (i <= int(
detail)); i++) {
794 weight = std::min(weight, 1.0f);
798 value += weight * signal;
799 weight *= gain * signal;
804 if ((rmd != 0.0f) && (weight > 0.001f)) {
805 weight = std::min(weight, 1.0f);
807 value += rmd * weight * signal;
816 const float roughness,
817 const float lacunarity,
821 float pwr = roughness;
825 float value = signal;
828 for (
int i = 1; i <= int(
detail); i++) {
830 weight = std::clamp(signal * gain, 0.0f, 1.0f);
834 value += signal * pwr;
1166 return std::abs(
b - a);
1175 return std::abs(a.x -
b.x) + std::abs(a.y -
b.y);
1177 return std::max(std::abs(a.x -
b.x), std::abs(a.y -
b.y));
1179 return std::pow(std::pow(std::abs(a.x -
b.x), params.
exponent) +
1180 std::pow(std::abs(a.y -
b.y), params.
exponent),
1195 return std::abs(a.x -
b.x) + std::abs(a.y -
b.y) + std::abs(a.z -
b.z);
1197 return std::max({std::abs(a.x -
b.x), std::abs(a.y -
b.y), std::abs(a.z -
b.z)});
1199 return std::pow(std::pow(std::abs(a.x -
b.x), params.
exponent) +
1200 std::pow(std::abs(a.y -
b.y), params.
exponent) +
1201 std::pow(std::abs(a.z -
b.z), params.
exponent),
1216 return std::abs(a.x -
b.x) + std::abs(a.y -
b.y) + std::abs(a.z -
b.z) + std::abs(a.w -
b.w);
1219 {std::abs(a.x -
b.x), std::abs(a.y -
b.y), std::abs(a.z -
b.z), std::abs(a.w -
b.w)});
1221 return std::pow(std::pow(std::abs(a.x -
b.x), params.
exponent) +
1222 std::pow(std::abs(a.y -
b.y), params.
exponent) +
1223 std::pow(std::abs(a.z -
b.z), params.
exponent) +
1224 std::pow(std::abs(a.w -
b.w), params.
exponent),
1248 return {0.0f, 0.0f, 0.0f, coord};
1253 float cellPosition =
floorf(coord);
1254 float localPosition = coord - cellPosition;
1257 float targetOffset = 0.0f;
1258 float targetPosition = 0.0f;
1259 for (
int i = -1; i <= 1; i++) {
1260 float cellOffset = i;
1261 float pointPosition = cellOffset +
1264 if (distanceToPoint < minDistance) {
1265 targetOffset = cellOffset;
1266 minDistance = distanceToPoint;
1267 targetPosition = pointPosition;
1280 const bool calc_color)
1282 float cellPosition =
floorf(coord);
1283 float localPosition = coord - cellPosition;
1285 float smoothDistance = 0.0f;
1286 float smoothPosition = 0.0f;
1287 float3 smoothColor = {0.0f, 0.0f, 0.0f};
1289 for (
int i = -2; i <= 2; i++) {
1290 float cellOffset = i;
1291 float pointPosition = cellOffset +
1297 0.0f, 1.0f, 0.5f + 0.5f * (smoothDistance - distanceToPoint) / params.
smoothness);
1298 float correctionFactor = params.
smoothness * h * (1.0f - h);
1299 smoothDistance =
mix(smoothDistance, distanceToPoint, h) - correctionFactor;
1300 correctionFactor /= 1.0f + 3.0f * params.
smoothness;
1304 smoothColor =
mix(smoothColor, cellColor, h) - correctionFactor;
1306 smoothPosition =
mix(smoothPosition, pointPosition, h) - correctionFactor;
1311 octave.
color = smoothColor;
1318 float cellPosition =
floorf(coord);
1319 float localPosition = coord - cellPosition;
1323 float offsetF1 = 0.0f;
1324 float positionF1 = 0.0f;
1325 float offsetF2 = 0.0f;
1326 float positionF2 = 0.0f;
1327 for (
int i = -1; i <= 1; i++) {
1328 float cellOffset = i;
1329 float pointPosition = cellOffset +
1332 if (distanceToPoint < distanceF1) {
1333 distanceF2 = distanceF1;
1334 distanceF1 = distanceToPoint;
1335 offsetF2 = offsetF1;
1336 offsetF1 = cellOffset;
1337 positionF2 = positionF1;
1338 positionF1 = pointPosition;
1340 else if (distanceToPoint < distanceF2) {
1341 distanceF2 = distanceToPoint;
1342 offsetF2 = cellOffset;
1343 positionF2 = pointPosition;
1356 float cellPosition =
floorf(coord);
1357 float localPosition = coord - cellPosition;
1362 float distanceToMidLeft =
fabsf((midPointPosition + leftPointPosition) / 2.0f - localPosition);
1363 float distanceToMidRight =
fabsf((midPointPosition + rightPointPosition) / 2.0f - localPosition);
1365 return math::min(distanceToMidLeft, distanceToMidRight);
1370 float cellPosition =
floorf(coord);
1371 float localPosition = coord - cellPosition;
1373 float closestPoint = 0.0f;
1374 float closestPointOffset = 0.0f;
1376 for (
int i = -1; i <= 1; i++) {
1377 float cellOffset = i;
1378 float pointPosition = cellOffset +
1380 float distanceToPoint =
fabsf(pointPosition - localPosition);
1381 if (distanceToPoint < minDistance) {
1382 minDistance = distanceToPoint;
1383 closestPoint = pointPosition;
1384 closestPointOffset = cellOffset;
1389 float closestPointToClosestPoint = 0.0f;
1390 for (
int i = -1; i <= 1; i++) {
1394 float cellOffset = i + closestPointOffset;
1395 float pointPosition = cellOffset +
1397 float distanceToPoint =
fabsf(closestPoint - pointPosition);
1398 if (distanceToPoint < minDistance) {
1399 minDistance = distanceToPoint;
1400 closestPointToClosestPoint = pointPosition;
1404 return fabsf(closestPointToClosestPoint - closestPoint) / 2.0f;
1411 return {coord.x, coord.y, 0.0f, 0.0f};
1417 float2 localPosition = coord - cellPosition_f;
1418 int2 cellPosition =
int2(cellPosition_f);
1421 int2 targetOffset = {0, 0};
1422 float2 targetPosition = {0.0f, 0.0f};
1423 for (
int j = -1; j <= 1; j++) {
1424 for (
int i = -1; i <= 1; i++) {
1425 int2 cellOffset(i, j);
1429 if (distanceToPoint < minDistance) {
1430 targetOffset = cellOffset;
1431 minDistance = distanceToPoint;
1432 targetPosition = pointPosition;
1446 const bool calc_color)
1449 float2 localPosition = coord - cellPosition_f;
1450 int2 cellPosition =
int2(cellPosition_f);
1452 float smoothDistance = 0.0f;
1453 float3 smoothColor = {0.0f, 0.0f, 0.0f};
1454 float2 smoothPosition = {0.0f, 0.0f};
1456 for (
int j = -2; j <= 2; j++) {
1457 for (
int i = -2; i <= 2; i++) {
1458 int2 cellOffset(i, j);
1461 float distanceToPoint =
voronoi_distance(pointPosition, localPosition, params);
1466 0.5f + 0.5f * (smoothDistance - distanceToPoint) / params.
smoothness);
1467 float correctionFactor = params.
smoothness * h * (1.0f - h);
1468 smoothDistance =
mix(smoothDistance, distanceToPoint, h) - correctionFactor;
1469 correctionFactor /= 1.0f + 3.0f * params.
smoothness;
1473 smoothColor =
mix(smoothColor, cellColor, h) - correctionFactor;
1475 smoothPosition =
mix(smoothPosition, pointPosition, h) - correctionFactor;
1481 octave.
color = smoothColor;
1489 float2 localPosition = coord - cellPosition_f;
1490 int2 cellPosition =
int2(cellPosition_f);
1494 int2 offsetF1 = {0, 0};
1495 float2 positionF1 = {0.0f, 0.0f};
1496 int2 offsetF2 = {0, 0};
1497 float2 positionF2 = {0.0f, 0.0f};
1498 for (
int j = -1; j <= 1; j++) {
1499 for (
int i = -1; i <= 1; i++) {
1500 int2 cellOffset(i, j);
1503 float distanceToPoint =
voronoi_distance(pointPosition, localPosition, params);
1504 if (distanceToPoint < distanceF1) {
1505 distanceF2 = distanceF1;
1506 distanceF1 = distanceToPoint;
1507 offsetF2 = offsetF1;
1508 offsetF1 = cellOffset;
1509 positionF2 = positionF1;
1510 positionF1 = pointPosition;
1512 else if (distanceToPoint < distanceF2) {
1513 distanceF2 = distanceToPoint;
1514 offsetF2 = cellOffset;
1515 positionF2 = pointPosition;
1530 float2 localPosition = coord - cellPosition_f;
1531 int2 cellPosition =
int2(cellPosition_f);
1533 float2 vectorToClosest = {0.0f, 0.0f};
1535 for (
int j = -1; j <= 1; j++) {
1536 for (
int i = -1; i <= 1; i++) {
1537 int2 cellOffset(i, j);
1541 float distanceToPoint =
math::dot(vectorToPoint, vectorToPoint);
1542 if (distanceToPoint < minDistance) {
1543 minDistance = distanceToPoint;
1544 vectorToClosest = vectorToPoint;
1550 for (
int j = -1; j <= 1; j++) {
1551 for (
int i = -1; i <= 1; i++) {
1552 int2 cellOffset(i, j);
1556 float2 perpendicularToEdge = vectorToPoint - vectorToClosest;
1557 if (
math::dot(perpendicularToEdge, perpendicularToEdge) > 0.0001f) {
1558 float distanceToEdge =
math::dot((vectorToClosest + vectorToPoint) / 2.0f,
1560 minDistance =
math::min(minDistance, distanceToEdge);
1571 float2 localPosition = coord - cellPosition_f;
1572 int2 cellPosition =
int2(cellPosition_f);
1574 float2 closestPoint = {0.0f, 0.0f};
1575 int2 closestPointOffset = {0, 0};
1576 float minDistanceSq =
FLT_MAX;
1577 for (
int j = -1; j <= 1; j++) {
1578 for (
int i = -1; i <= 1; i++) {
1579 int2 cellOffset(i, j);
1583 if (distanceToPointSq < minDistanceSq) {
1584 minDistanceSq = distanceToPointSq;
1585 closestPoint = pointPosition;
1586 closestPointOffset = cellOffset;
1592 float2 closestPointToClosestPoint = {0.0f, 0.0f};
1593 for (
int j = -1; j <= 1; j++) {
1594 for (
int i = -1; i <= 1; i++) {
1595 if (i == 0 && j == 0) {
1598 int2 cellOffset =
int2(i, j) + closestPointOffset;
1602 if (distanceToPointSq < minDistanceSq) {
1603 minDistanceSq = distanceToPointSq;
1604 closestPointToClosestPoint = pointPosition;
1609 return math::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
1616 return {coord.x, coord.y, coord.z, 0.0f};
1622 float3 localPosition = coord - cellPosition_f;
1623 int3 cellPosition =
int3(cellPosition_f);
1626 int3 targetOffset = {0, 0, 0};
1627 float3 targetPosition = {0.0f, 0.0f, 0.0f};
1628 for (
int k = -1; k <= 1; k++) {
1629 for (
int j = -1; j <= 1; j++) {
1630 for (
int i = -1; i <= 1; i++) {
1631 int3 cellOffset(i, j, k);
1635 if (distanceToPoint < minDistance) {
1636 targetOffset = cellOffset;
1637 minDistance = distanceToPoint;
1638 targetPosition = pointPosition;
1653 const bool calc_color)
1656 float3 localPosition = coord - cellPosition_f;
1657 int3 cellPosition =
int3(cellPosition_f);
1659 float smoothDistance = 0.0f;
1660 float3 smoothColor = {0.0f, 0.0f, 0.0f};
1661 float3 smoothPosition = {0.0f, 0.0f, 0.0f};
1663 for (
int k = -2; k <= 2; k++) {
1664 for (
int j = -2; j <= 2; j++) {
1665 for (
int i = -2; i <= 2; i++) {
1666 int3 cellOffset(i, j, k);
1669 float distanceToPoint =
voronoi_distance(pointPosition, localPosition, params);
1674 0.5f + 0.5f * (smoothDistance - distanceToPoint) / params.
smoothness);
1675 float correctionFactor = params.
smoothness * h * (1.0f - h);
1676 smoothDistance =
mix(smoothDistance, distanceToPoint, h) - correctionFactor;
1677 correctionFactor /= 1.0f + 3.0f * params.
smoothness;
1681 smoothColor =
mix(smoothColor, cellColor, h) - correctionFactor;
1683 smoothPosition =
mix(smoothPosition, pointPosition, h) - correctionFactor;
1690 octave.
color = smoothColor;
1698 float3 localPosition = coord - cellPosition_f;
1699 int3 cellPosition =
int3(cellPosition_f);
1703 int3 offsetF1 = {0, 0, 0};
1704 float3 positionF1 = {0.0f, 0.0f, 0.0f};
1705 int3 offsetF2 = {0, 0, 0};
1706 float3 positionF2 = {0.0f, 0.0f, 0.0f};
1707 for (
int k = -1; k <= 1; k++) {
1708 for (
int j = -1; j <= 1; j++) {
1709 for (
int i = -1; i <= 1; i++) {
1710 int3 cellOffset(i, j, k);
1713 float distanceToPoint =
voronoi_distance(pointPosition, localPosition, params);
1714 if (distanceToPoint < distanceF1) {
1715 distanceF2 = distanceF1;
1716 distanceF1 = distanceToPoint;
1717 offsetF2 = offsetF1;
1718 offsetF1 = cellOffset;
1719 positionF2 = positionF1;
1720 positionF1 = pointPosition;
1722 else if (distanceToPoint < distanceF2) {
1723 distanceF2 = distanceToPoint;
1724 offsetF2 = cellOffset;
1725 positionF2 = pointPosition;
1741 float3 localPosition = coord - cellPosition_f;
1742 int3 cellPosition =
int3(cellPosition_f);
1744 float3 vectorToClosest = {0.0f, 0.0f, 0.0f};
1746 for (
int k = -1; k <= 1; k++) {
1747 for (
int j = -1; j <= 1; j++) {
1748 for (
int i = -1; i <= 1; i++) {
1749 int3 cellOffset(i, j, k);
1753 float distanceToPoint =
math::dot(vectorToPoint, vectorToPoint);
1754 if (distanceToPoint < minDistance) {
1755 minDistance = distanceToPoint;
1756 vectorToClosest = vectorToPoint;
1763 for (
int k = -1; k <= 1; k++) {
1764 for (
int j = -1; j <= 1; j++) {
1765 for (
int i = -1; i <= 1; i++) {
1766 int3 cellOffset(i, j, k);
1770 float3 perpendicularToEdge = vectorToPoint - vectorToClosest;
1771 if (
math::dot(perpendicularToEdge, perpendicularToEdge) > 0.0001f) {
1772 float distanceToEdge =
math::dot((vectorToClosest + vectorToPoint) / 2.0f,
1774 minDistance =
math::min(minDistance, distanceToEdge);
1786 float3 localPosition = coord - cellPosition_f;
1787 int3 cellPosition =
int3(cellPosition_f);
1789 float3 closestPoint = {0.0f, 0.0f, 0.0f};
1790 int3 closestPointOffset = {0, 0, 0};
1791 float minDistanceSq =
FLT_MAX;
1792 for (
int k = -1; k <= 1; k++) {
1793 for (
int j = -1; j <= 1; j++) {
1794 for (
int i = -1; i <= 1; i++) {
1795 int3 cellOffset(i, j, k);
1799 if (distanceToPointSq < minDistanceSq) {
1800 minDistanceSq = distanceToPointSq;
1801 closestPoint = pointPosition;
1802 closestPointOffset = cellOffset;
1809 float3 closestPointToClosestPoint = {0.0f, 0.0f, 0.0f};
1810 for (
int k = -1; k <= 1; k++) {
1811 for (
int j = -1; j <= 1; j++) {
1812 for (
int i = -1; i <= 1; i++) {
1813 if (i == 0 && j == 0 && k == 0) {
1816 int3 cellOffset =
int3(i, j, k) + closestPointOffset;
1820 if (distanceToPointSq < minDistanceSq) {
1821 minDistanceSq = distanceToPointSq;
1822 closestPointToClosestPoint = pointPosition;
1828 return math::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
1841 float4 localPosition = coord - cellPosition_f;
1842 int4 cellPosition =
int4(cellPosition_f);
1845 int4 targetOffset = {0, 0, 0, 0};
1846 float4 targetPosition = {0.0f, 0.0f, 0.0f, 0.0f};
1847 for (
int u = -1; u <= 1; u++) {
1848 for (
int k = -1; k <= 1; k++) {
1849 for (
int j = -1; j <= 1; j++) {
1850 for (
int i = -1; i <= 1; i++) {
1851 int4 cellOffset(i, j, k, u);
1856 if (distanceToPoint < minDistance) {
1857 targetOffset = cellOffset;
1858 minDistance = distanceToPoint;
1859 targetPosition = pointPosition;
1875 const bool calc_color)
1878 float4 localPosition = coord - cellPosition_f;
1879 int4 cellPosition =
int4(cellPosition_f);
1881 float smoothDistance = 0.0f;
1882 float3 smoothColor = {0.0f, 0.0f, 0.0f};
1883 float4 smoothPosition = {0.0f, 0.0f, 0.0f, 0.0f};
1885 for (
int u = -2; u <= 2; u++) {
1886 for (
int k = -2; k <= 2; k++) {
1887 for (
int j = -2; j <= 2; j++) {
1888 for (
int i = -2; i <= 2; i++) {
1889 int4 cellOffset(i, j, k, u);
1893 float distanceToPoint =
voronoi_distance(pointPosition, localPosition, params);
1898 0.5f + 0.5f * (smoothDistance - distanceToPoint) / params.
smoothness);
1899 float correctionFactor = params.
smoothness * h * (1.0f - h);
1900 smoothDistance =
mix(smoothDistance, distanceToPoint, h) - correctionFactor;
1901 correctionFactor /= 1.0f + 3.0f * params.
smoothness;
1905 smoothColor =
mix(smoothColor, cellColor, h) - correctionFactor;
1907 smoothPosition =
mix(smoothPosition, pointPosition, h) - correctionFactor;
1915 octave.
color = smoothColor;
1923 float4 localPosition = coord - cellPosition_f;
1924 int4 cellPosition =
int4(cellPosition_f);
1928 int4 offsetF1 = {0, 0, 0, 0};
1929 float4 positionF1 = {0.0f, 0.0f, 0.0f, 0.0f};
1930 int4 offsetF2 = {0, 0, 0, 0};
1931 float4 positionF2 = {0.0f, 0.0f, 0.0f, 0.0f};
1932 for (
int u = -1; u <= 1; u++) {
1933 for (
int k = -1; k <= 1; k++) {
1934 for (
int j = -1; j <= 1; j++) {
1935 for (
int i = -1; i <= 1; i++) {
1936 int4 cellOffset(i, j, k, u);
1940 float distanceToPoint =
voronoi_distance(pointPosition, localPosition, params);
1941 if (distanceToPoint < distanceF1) {
1942 distanceF2 = distanceF1;
1943 distanceF1 = distanceToPoint;
1944 offsetF2 = offsetF1;
1945 offsetF1 = cellOffset;
1946 positionF2 = positionF1;
1947 positionF1 = pointPosition;
1949 else if (distanceToPoint < distanceF2) {
1950 distanceF2 = distanceToPoint;
1951 offsetF2 = cellOffset;
1952 positionF2 = pointPosition;
1969 float4 localPosition = coord - cellPosition_f;
1970 int4 cellPosition =
int4(cellPosition_f);
1972 float4 vectorToClosest = {0.0f, 0.0f, 0.0f, 0.0f};
1974 for (
int u = -1; u <= 1; u++) {
1975 for (
int k = -1; k <= 1; k++) {
1976 for (
int j = -1; j <= 1; j++) {
1977 for (
int i = -1; i <= 1; i++) {
1978 int4 cellOffset(i, j, k, u);
1983 float distanceToPoint =
math::dot(vectorToPoint, vectorToPoint);
1984 if (distanceToPoint < minDistance) {
1985 minDistance = distanceToPoint;
1986 vectorToClosest = vectorToPoint;
1994 for (
int u = -1; u <= 1; u++) {
1995 for (
int k = -1; k <= 1; k++) {
1996 for (
int j = -1; j <= 1; j++) {
1997 for (
int i = -1; i <= 1; i++) {
1998 int4 cellOffset(i, j, k, u);
2003 float4 perpendicularToEdge = vectorToPoint - vectorToClosest;
2004 if (
math::dot(perpendicularToEdge, perpendicularToEdge) > 0.0001f) {
2005 float distanceToEdge =
math::dot((vectorToClosest + vectorToPoint) / 2.0f,
2007 minDistance =
math::min(minDistance, distanceToEdge);
2020 float4 localPosition = coord - cellPosition_f;
2021 int4 cellPosition =
int4(cellPosition_f);
2023 float4 closestPoint = {0.0f, 0.0f, 0.0f, 0.0f};
2024 int4 closestPointOffset = {0, 0, 0, 0};
2025 float minDistanceSq =
FLT_MAX;
2026 for (
int u = -1; u <= 1; u++) {
2027 for (
int k = -1; k <= 1; k++) {
2028 for (
int j = -1; j <= 1; j++) {
2029 for (
int i = -1; i <= 1; i++) {
2030 int4 cellOffset(i, j, k, u);
2035 if (distanceToPointSq < minDistanceSq) {
2036 minDistanceSq = distanceToPointSq;
2037 closestPoint = pointPosition;
2038 closestPointOffset = cellOffset;
2046 float4 closestPointToClosestPoint = {0.0f, 0.0f, 0.0f, 0.0f};
2047 for (
int u = -1; u <= 1; u++) {
2048 for (
int k = -1; k <= 1; k++) {
2049 for (
int j = -1; j <= 1; j++) {
2050 for (
int i = -1; i <= 1; i++) {
2051 if (i == 0 && j == 0 && k == 0 && u == 0) {
2054 int4 cellOffset =
int4(i, j, k, u) + closestPointOffset;
2059 if (distanceToPointSq < minDistanceSq) {
2060 minDistanceSq = distanceToPointSq;
2061 closestPointToClosestPoint = pointPosition;
2068 return math::distance(closestPointToClosestPoint, closestPoint) / 2.0f;
2078 const bool calc_color )
2080 float amplitude = 1.0f;
2081 float max_amplitude = 0.0f;
2085 const bool zero_input = params.
detail == 0.0f || params.
roughness == 0.0f;
2096 max_amplitude = 1.0f;
2100 if (i <= params.
detail) {
2101 max_amplitude += amplitude;
2110 if (remainder != 0.0f) {
2111 max_amplitude =
mix(max_amplitude, max_amplitude + amplitude, remainder);
2123 output.color /= max_amplitude;
2127 float4{0.0f, 0.0f, 0.0f, 0.0f};
2137 float amplitude = 1.0f;
2142 const bool zero_input = params.
detail == 0.0f || params.
roughness == 0.0f;
2151 if (i <= params.
detail) {
2159 if (remainder != 0.0f) {
2161 max_amplitude =
mix(max_amplitude, lerp_amplitude, remainder);
2162 float lerp_distance =
mix(
2180 const bool calc_color);
2183 const bool calc_color);
2186 const bool calc_color);
2189 const bool calc_color);
2256 const float orientation)
2261 const float windowed_gaussian_envelope = gaussian_envelop * hann_window;
2267 return windowed_gaussian_envelope * phasor;
2308 const float integral_of_gabor_squared = 0.25f;
2309 const float second_moment = 0.5f;
2322 const float isotropy,
2323 const float base_orientation)
2329 const float3 seed_for_orientation(cell.x, cell.y, i * 3);
2330 const float3 seed_for_kernel_center(cell.x, cell.y, i * 3 + 1);
2331 const float3 seed_for_weight(cell.x, cell.y, i * 3 + 2);
2339 const float orientation = base_orientation + random_orientation * isotropy;
2342 const float2 position_in_kernel_space =
position - kernel_center;
2363 const float isotropy,
2364 const float base_orientation)
2367 const float2 local_position = coordinates - cell_position;
2370 for (
int j = -1; j <= 1; j++) {
2371 for (
int i = -1; i <= 1; i++) {
2373 const float2 current_cell_position = cell_position + cell_offset;
2374 const float2 position_in_cell_space = local_position - cell_offset;
2376 current_cell_position, position_in_cell_space,
frequency, isotropy, base_orientation);
2389 const float3 orientation)
2394 const float windowed_gaussian_envelope = gaussian_envelop * hann_window;
2400 return windowed_gaussian_envelope * phasor;
2408 const float integral_of_gabor_squared = 1.0f / (4.0f * std::numbers::sqrt2);
2409 const float second_moment = 0.5f;
2417 const float isotropy,
2421 if (isotropy == 0.0) {
2426 float inclination =
math::acos(orientation.z);
2435 inclination += random_angles.x * isotropy;
2436 azimuth += random_angles.y * isotropy;
2447 const float isotropy,
2448 const float3 base_orientation)
2454 const float4 seed_for_orientation(cell.x, cell.y, cell.z, i * 3);
2455 const float4 seed_for_kernel_center(cell.x, cell.y, cell.z, i * 3 + 1);
2456 const float4 seed_for_weight(cell.x, cell.y, cell.z, i * 3 + 2);
2459 base_orientation, isotropy, seed_for_orientation);
2462 const float3 position_in_kernel_space =
position - kernel_center;
2482 const float isotropy,
2483 const float3 base_orientation)
2486 const float3 local_position = coordinates - cell_position;
2489 for (
int k = -1; k <= 1; k++) {
2490 for (
int j = -1; j <= 1; j++) {
2491 for (
int i = -1; i <= 1; i++) {
2493 const float3 current_cell_position = cell_position + cell_offset;
2494 const float3 position_in_cell_space = local_position - cell_offset;
2496 current_cell_position, position_in_cell_space,
frequency, isotropy, base_orientation);
2507 const float anisotropy,
2508 const float orientation,
2513 const float2 scaled_coordinates = coordinates *
scale;
2514 const float isotropy = 1.0f -
math::clamp(anisotropy, 0.0f, 1.0f);
2518 scaled_coordinates, sanitized_frequency, isotropy, orientation);
2523 const float normalization_factor = 6.0f * standard_deviation;
2528 *r_value = (phasor.y / normalization_factor) * 0.5f + 0.5f;
2534 *r_phase = (
math::atan2(phasor.y, phasor.x) + std::numbers::pi) / (2.0f * std::numbers::pi);
2539 *r_intensity =
math::length(phasor) / normalization_factor;
2546 const float anisotropy,
2547 const float3 orientation,
2552 const float3 scaled_coordinates = coordinates *
scale;
2553 const float isotropy = 1.0f -
math::clamp(anisotropy, 0.0f, 1.0f);
2558 scaled_coordinates, sanitized_frequency, isotropy, normalized_orientation);
2563 const float normalization_factor = 6.0f * standard_deviation;
2568 *r_value = (phasor.y / normalization_factor) * 0.5f + 0.5f;
2574 *r_phase = (
math::atan2(phasor.y, phasor.x) + std::numbers::pi) / (2.0f * std::numbers::pi);
2579 *r_intensity =
math::length(phasor) / normalization_factor;
#define BLI_assert_unreachable()
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
constexpr IndexRange()=default
VecBase< float, D > normalize(VecOp< float, D >) RET
float distance(float, float) RET
float3x3 scale(float3x3 mat, float2 scale)
float distance_squared(VecT a, VecT b)
T length_squared(const VecBase< T, Size > &a)
T cos(const AngleRadianBase< T > &a)
T clamp(const T &a, const T &min, const T &max)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T min(const T &a, const T &b)
T atan2(const T &y, const T &x)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T sin(const AngleRadianBase< T > &a)
T max(const T &a, const T &b)
T mod(const T &a, const T &b)
static ATTR_NO_SIGNED_INT_OVERFLOW int4 hash_pcg4d_i(int4 v)
static float int_to_float_01(int32_t k)
@ NOISE_SHD_VORONOI_MANHATTAN
@ NOISE_SHD_VORONOI_MINKOWSKI
@ NOISE_SHD_VORONOI_EUCLIDEAN
@ NOISE_SHD_VORONOI_CHEBYCHEV
float3 perlin_float3_fractal_distorted(float position, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize)
BLI_INLINE float negate_if(float value, uint32_t condition)
static float3 hash_int3_to_float3(int3 k)
BLI_INLINE float uint_to_float_01(uint32_t k)
@ NOISE_SHD_PERLIN_MULTIFRACTAL
@ NOISE_SHD_PERLIN_RIDGED_MULTIFRACTAL
@ NOISE_SHD_PERLIN_HYBRID_MULTIFRACTAL
@ NOISE_SHD_PERLIN_HETERO_TERRAIN
float perlin_ridged_multi_fractal(T p, const float detail, const float roughness, const float lacunarity, const float offset, const float gain)
static float2 compute_3d_gabor_noise(const float3 coordinates, const float frequency, const float isotropy, const float3 base_orientation)
uint32_t hash_float(float kx)
static float compute_3d_gabor_standard_deviation()
static float2 compute_2d_gabor_noise_cell(const float2 cell, const float2 position, const float frequency, const float isotropy, const float base_orientation)
BLI_INLINE float perlin_distortion(float position, float strength)
float hash_float_to_float(float k)
float perlin_signed(float position)
void gabor(const float2 coordinates, const float scale, const float frequency, const float anisotropy, const float orientation, float *r_value, float *r_phase, float *r_intensity)
BLI_INLINE void hash_bit_mix(uint32_t &a, uint32_t &b, uint32_t &c)
float4 voronoi_position(const float coord)
BLI_INLINE float fade(float t)
VoronoiOutput voronoi_smooth_f1(const VoronoiParams ¶ms, const float coord, const bool calc_color)
float voronoi_n_sphere_radius(const VoronoiParams ¶ms, const float coord)
static float2 compute_2d_gabor_noise(const float2 coordinates, const float frequency, const float isotropy, const float base_orientation)
float perlin(float position)
BLI_INLINE float random_float_offset(float seed)
static float voronoi_distance_bound(const T a, const T b, const VoronoiParams ¶ms)
BLI_INLINE float floor_fraction(float x, int &i)
float voronoi_distance_to_edge(const VoronoiParams ¶ms, const float coord)
BLI_INLINE uint32_t float_as_uint(float f)
static float4 hash_int4_to_float4(int4 k)
static float2 hash_int2_to_float2(int2 k)
BLI_INLINE float3 random_float3_offset(float seed)
static float3 hash_int2_to_float3(int2 k)
static float2 compute_3d_gabor_noise_cell(const float3 cell, const float3 position, const float frequency, const float isotropy, const float3 base_orientation)
static float3 hash_int4_to_float3(int4 k)
static float2 compute_2d_gabor_kernel(const float2 position, const float frequency, const float orientation)
VoronoiOutput fractal_voronoi_x_fx(const VoronoiParams ¶ms, const T coord, const bool calc_color)
float voronoi_distance(const float a, const float b)
static float2 compute_3d_gabor_kernel(const float3 position, const float frequency, const float3 orientation)
BLI_INLINE float2 random_float2_offset(float seed)
float perlin_fbm(T p, float detail, float roughness, float lacunarity, bool normalize)
VoronoiOutput voronoi_f2(const VoronoiParams ¶ms, const float coord)
float3 hash_float_to_float3(float k)
uint32_t hash(uint32_t kx)
BLI_INLINE float4 random_float4_offset(float seed)
BLI_INLINE float perlin_noise(float position)
static ATTR_NO_SIGNED_INT_OVERFLOW int3 hash_pcg3d_i(int3 v)
float fractal_voronoi_distance_to_edge(const VoronoiParams ¶ms, const T coord)
BLI_INLINE void hash_bit_final(uint32_t &a, uint32_t &b, uint32_t &c)
float4 hash_float_to_float4(float4 k)
float perlin_hybrid_multi_fractal(T p, const float detail, const float roughness, const float lacunarity, const float offset, const float gain)
static float compute_2d_gabor_standard_deviation()
float perlin_select(T p, float detail, float roughness, float lacunarity, float offset, float gain, int type, bool normalize)
BLI_INLINE float noise_grad(uint32_t hash, float x)
VoronoiOutput voronoi_f1(const VoronoiParams ¶ms, const float coord)
float perlin_multi_fractal(T p, const float detail, const float roughness, const float lacunarity)
static constexpr int gabor_impulses_count
@ NOISE_SHD_VORONOI_N_SPHERE_RADIUS
@ NOISE_SHD_VORONOI_SMOOTH_F1
@ NOISE_SHD_VORONOI_DISTANCE_TO_EDGE
static T mix(T v0, T v1, float x)
float perlin_fractal_distorted(T position, float detail, float roughness, float lacunarity, float offset, float gain, float distortion, int type, bool normalize)
static ATTR_NO_SIGNED_INT_OVERFLOW int2 hash_pcg2d_i(int2 v)
BLI_INLINE uint32_t hash_bit_rotate(uint32_t x, uint32_t k)
float perlin_hetero_terrain(T p, const float detail, const float roughness, const float lacunarity, const float offset)
float2 hash_float_to_float2(float2 k)
static float3 compute_3d_orientation(const float3 orientation, const float isotropy, const float4 seed)
float hash_to_float(uint32_t kx)
VecBase< int32_t, 4 > int4
ATTR_WARN_UNUSED_RESULT const BMVert * v
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
MINLINE float smoothstep(float edge0, float edge1, float x)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
VecBase< float, 3 > float3
#define ATTR_NO_SIGNED_INT_OVERFLOW
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float