1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-20 05:21:38 +02:00

refactor: use leap, vector mult

This commit is contained in:
Justin Lin
2022-04-13 20:59:14 +08:00
parent a6cdb1c2c8
commit 7039e9e379
4 changed files with 25 additions and 48 deletions

View File

@@ -1,17 +1,16 @@
use <_pnoise_comm.scad>;
use <../../util/lerp.scad>;
function _pnoise1_grad1(hashvalue, x) = (hashvalue % 2 == 0) ? x : -x;
_signs = [1, -1];
function _pnoise1_grad1(hashvalue, x) = _signs[hashvalue % 2] * x;
function _pnoise1_impl(x, seed) =
let(
xi = floor(x),
xf = x - xi,
u = _pnoise_fade(xf),
a = _pnoise_lookup_pnoise_table(seed + xi),
b = _pnoise_lookup_pnoise_table(seed + xi + 1)
xf = x - xi
)
_pnoise_lerp(
_pnoise1_grad1(a, xf),
_pnoise1_grad1(b, xf - 1),
u
lerp(
_pnoise1_grad1(_pnoise_lookup_pnoise_table(seed + xi), xf),
_pnoise1_grad1(_pnoise_lookup_pnoise_table(seed + xi + 1), xf - 1),
_pnoise_fade(xf)
);

View File

@@ -1,14 +1,8 @@
use <_pnoise_comm.scad>;
use <../../util/lerp.scad>;
function _pnoise2_grad2(hashvalue, x, y) =
let(case = hashvalue % 8)
case == 0 ? y :
case == 1 ? x + y :
case == 2 ? x:
case == 3 ? x - y:
case == 4 ? -y:
case == 5 ? -x - y:
case == 6 ? -x : -x + y;
_signs = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]];
function _pnoise2_grad2(hashvalue, x, y) = _signs[hashvalue % 8] * [x, y];
function _pnoise2(x, y, seed) =
let(
@@ -22,15 +16,15 @@ function _pnoise2(x, y, seed) =
ba = _pnoise_lookup_pnoise_table(_pnoise_lookup_pnoise_table(seed + xi + 1) + yi),
ab = _pnoise_lookup_pnoise_table(_pnoise_lookup_pnoise_table(seed + xi) + yi + 1),
bb = _pnoise_lookup_pnoise_table(_pnoise_lookup_pnoise_table(seed + xi + 1) + yi + 1),
y1 = _pnoise_lerp(
y1 = lerp(
_pnoise2_grad2(aa, xf, yf),
_pnoise2_grad2(ba, xf - 1, yf),
u
),
y2 = _pnoise_lerp(
y2 = lerp(
_pnoise2_grad2(ab, xf, yf - 1),
_pnoise2_grad2(bb, xf - 1, yf - 1),
u
)
)
_pnoise_lerp(y1, y2, v);
lerp(y1, y2, v);

View File

@@ -1,22 +1,8 @@
use <_pnoise_comm.scad>;
use <../../util/lerp.scad>;
function _pnoise3_grad3(hashvalue, x, y, z) =
let(case = hashvalue % 16)
case == 0 ? x + y :
case == 1 ? -x + y :
case == 2 ? x - y:
case == 3 ? -x - y:
case == 4 ? x + z:
case == 5 ? -x + z:
case == 6 ? x - z :
case == 7 ? -x - z :
case == 8 ? y + z :
case == 9 ? -y + z :
case == 10 ? y - z :
case == 11 ? -y - z :
case == 12 ? y + x :
case == 13 ? -y + z :
case == 14 ? y - x : -y - z;
_signs = [[1, 1, 0], [-1, 1, 0], [1, -1, 0], [-1, -1, 0], [1, 0, 1], [-1, 0, 1], [1, 0, -1], [-1, 0, -1], [0, 1, 1], [0, -1, 1], [0, 1, -1], [0, -1, -1], [1, 1, 0], [0, -1, 1], [-1, 1, 0], [0, -1, -1]];
function _pnoise3_grad3(hashvalue, x, y, z) = _signs[hashvalue % 16] * [x, y, z];
function _pnoise3(x, y, z, seed) =
let(
@@ -70,27 +56,27 @@ function _pnoise3(x, y, z, seed) =
_pnoise_lookup_pnoise_table(seed + xi + 1) + yi + 1
) + zi + 1
),
x1 = _pnoise_lerp(
x1 = lerp(
_pnoise3_grad3(aaa, xf, yf, zf),
_pnoise3_grad3(baa, xf - 1, yf, zf),
u
),
x2 = _pnoise_lerp(
x2 = lerp(
_pnoise3_grad3(aba, xf, yf - 1, zf),
_pnoise3_grad3(bba, xf - 1, yf - 1, zf),
u
),
y1 = _pnoise_lerp(x1, x2, v),
x3 = _pnoise_lerp(
y1 = lerp(x1, x2, v),
x3 = lerp(
_pnoise3_grad3(aab, xf, yf, zf - 1),
_pnoise3_grad3(bab, xf - 1, yf, zf - 1),
u
),
x4 = _pnoise_lerp(
x4 = lerp(
_pnoise3_grad3(abb, xf, yf - 1, zf - 1),
_pnoise3_grad3(bbb, xf - 1, yf - 1, zf - 1),
u
),
y2 = _pnoise_lerp(x3, x4, v)
y2 = lerp(x3, x4, v)
)
_pnoise_lerp(y1, y2, w);
lerp(y1, y2, w);

View File

@@ -1,7 +1,5 @@
_pnoise_table = [151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,190,6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,88,237,149,56,87,174,20,125,136,171,168,68,175,74,165,71,134,139,48,27,166,77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,102,143,54,65,25,63,161,1,216,80,73,209,76,132,187,208,89,18,169,200,196,135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,250,124,123,5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,223,183,170,213,119,248,152,2,44,154,163,70,221,153,101,155,167,43,172,9,129,22,39,253,19,98,108,110,79,113,224,232,178,185,112,104,218,246,97,228,251,34,242,193,238,210,144,12,191,179,162,241,81,51,145,235,249,14,239,107,49,192,214,31,181,199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180];
function _pnoise_fade(t) = pow(t, 3) * (t * (t * 6 - 15) + 10);
function _pnoise_lerp(a, b, t) = a + t * (b - a);
function _pnoise_fade(t) = t ^ 3 * (t * (t * 6 - 15) + 10);
function _pnoise_lookup_pnoise_table(i) = _pnoise_table[abs(i % 256)];