noisy spikes

This commit is contained in:
Laila van Reenen 2024-12-21 19:42:50 +01:00
parent b4e3e88873
commit 1a1187392e
2 changed files with 157 additions and 32 deletions

102
perlin_noise.scad Normal file
View File

@ -0,0 +1,102 @@
$fn=3;
// randomize = true;
// setseed = 123456;
// seed = randomize ? floor(rands(0, 1000000, 1)[0]) : setseed;
// echo(Seed = seed);
{
// Functions
{
// Main noise generator
//
// b = base random array, don't set this
// a = recursion array, don't set this!
// no = number of octaves, you can set this, number of elements is no^2
// o = current octave iteration, don't set this
// seed = rands seed, you can set this
//
// Returns: Perlin noise array, length(no ^ 2)
function perlin_noise(b=[], a=[], no=4, o=0, seed=0) =
(o > no)
? scalevec(2, a) // Done, return the array
: (len(b) == 0)
? perlin_noise(rands(0, 1, no ^ 2, seed), a=[], no=no, o=o, seed=seed)
: (len(a) == 0)
? perlin_noise(b=b, a=octave(o=0, b=b), no=no, o=o + 1, seed=seed)
: perlin_noise(b=b, a=addvec(a, octave(o=o, b=b)), no=no, o=o + 1, seed=seed);
}
// ^ Main noise generator
{
// Octave generator
//
// o = octave
// b = base random array
//
// Returns: array of values from a single noise octave
function octave(o=0, b=[]) =
expand(c=len(b), a=[
for(i=[0 : len(b) / (2 ^ o) : len(b) - 1]) b[i]
]);
}
// ^ Octave generator
{
// Expand octave
//
// c = base array length
// a = octave array
//
// Returns: array of length=length(base) with interpolated octave values
function expand(c=0, a=[]) = [
for(i=[0 : c - 1])
(len(a) == 1)
? a[0]
: (len(a) == c)
? a[i] * (c / len(a) / c)
: (interp(v=[
a[floor(i / (c / len(a)))],
(floor(i / (c / len(a))) + 1 == len(a))
? a[0]
: a[floor(i / (c / len(a))) + 1]],
s=c / len(a))
* (i % (c / len(a)))
+ a[floor(i / (c / len(a)))])
* ((c / len(a)) / c)
];
}
// ^ Expand octave
{
// Helper functions
function interp(v=[], s=0) = (v[1] - v[0]) / s;
function addvec(a=[], b=[]) = [
for(i=[0 : len(a) - 1])
a[i] + b[i]
];
function scalevec(d=2, v=[]) = [
for(i=[0 : len(v) - 1])
v[i] / d
];
}
// ^ Helper functions
}
// ^ Functions
// // Example:
// //
// // no = 8 means we will get a noise array with 8 ^ 2 = 64 values (range 0-1)
// // seed = seed, this is set up with the variables above, you could set it manually. Defaults to 0 if not set
// a = noise(no=8, seed=seed);
// // This displays the values.
// repeat = 1;
// for(i=[0 : len(a) - 1], x=[0 : repeat]) {
// translate([i + len(a) * x, 0, 0]) cylinder(a[i] * 100, d=1);
// }

View File

@ -11,6 +11,8 @@ SKIRT_FLAP_SPIKE_LENGTH_LOWER = 37;
SKIRT_FLAP_SPIKE_SEPERATION = SKIRT_FLAP_LENTH/(SKIRT_FLAP_SPIKE_NUM+1);
include <perlin_noise.scad>
module skirt_flap_part(endpart) {
difference() {
union() {
@ -32,7 +34,7 @@ module skirt_flap_part(endpart) {
}
}
// rotate([0, 0, 90]) {
// rotate([180, 0, 90]) {
// skirt_flap_part(endpart = false);
// translate([SKIRT_FLAP_SPIKE_SEPERATION, 0, SKIRT_FLAP_THIKNES+0.1])
// skirt_flap_part(endpart = false);
@ -43,46 +45,67 @@ module skirt_flap_part(endpart) {
// }
STEP = 0.1;
STEP = 0.01;
function point_r_fn(x) = 1-(0.8*x+0.2)^3;
function point_sheer_x_fn(x) = 0.3-(1.6*x)^3;
function point_sheer_y_fn(x) = max(0, x-0.3);
function addl(list, c = 0) = c < len(list) - 1 ? list[c] + addl(list, c + 1) : list[c];
function add(list, c = 0) = c < len(list) - 1 ? list[c] + addl(list, c + 1) : list[c];
function addl(list, l, c = 0) = c < l ? list[c] + addl(list, l, c + 1) : list[c];
point_sheer_move = 0;
$fn = 10;
$fn = 30;
multmatrix(m = [
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 2.5, 0],
[0, 0, 0, 1],
])
union() {
for (i = [0:STEP:1]) {
move_x = (i == 0) ? 0 : (i == STEP) ? (point_sheer_x_fn(0)*STEP) : addl([ for (ii = [0:STEP:i-STEP]) point_sheer_x_fn(ii) ]) * STEP;
move_y = (i == 0) ? 0 : (i == STEP) ? (point_sheer_y_fn(0)*STEP) : addl([ for (ii = [0:STEP:i-STEP]) point_sheer_y_fn(ii) ]) * STEP;
translate([move_x, move_y, i])
module spike(size, f_d, f_sx, f_sy, step)
{
multmatrix(m = [
[1, 0, point_sheer_x_fn(i), 0],
[0, 1, point_sheer_y_fn(i), 0],
[size[0], 0, 0, 0],
[ 0, size[1], 0, 0],
[ 0, 0, size[2], 0],
[ 0, 0, 0, 1]
])
rotate([0, 0, -90])
union() {
for (pos = [0:step:1-step]) {
i = round(pos/step);
move_x = (i == 0) ? 0 : addl(f_sx, i-1) * step;
move_y = (i == 0) ? 0 : addl(f_sy, i-1) * step;
translate([move_x, move_y, pos])
multmatrix(m = [
[1, 0, f_sx[i], 0],
[0, 1, f_sy[i], 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
])
cylinder(h = STEP, r1 = point_r_fn(i), r2 = point_r_fn(i+STEP));
cylinder(h = step, r1 = f_d[i], r2 = f_d[i+1]);
}
}
}
sheer = 0.8;
noise = [
perlin_noise(no=4, seed=rands(0, 100000, 1)[0]),
perlin_noise(no=4, seed=rands(0, 100000, 1)[0]),
perlin_noise(no=4, seed=rands(0, 100000, 1)[0])
];
// multmatrix(m = [
// [1, 0, point_sheer_fn(0), 0],
// [0, 1, 0, 0],
// [0, 0, 1, 0],
// [0, 0, 0, 1],
// ])
// cylinder(r = 1, h = STEP);
// translate([STEP*point_sheer_fn(0), 0, STEP])
// cylinder(r = 1, h = 0.3);
echo(noise);
spike(
[20, 20, 50],
// [1, 1, 1],
[ for (i = [0:STEP:1]) point_r_fn(i) + noise[0][round(i*STEP)]*0.2 ],
[ for (i = [0:STEP:1]) point_sheer_x_fn(i) + noise[1][round(i*STEP)]*0.2 ],
[ for (i = [0:STEP:1]) point_sheer_y_fn(i) + noise[2][round(i*STEP)]*0.2 ],
STEP
);
// test_list = [0.3, 0.295904, 0.267232, 0.189408, 0.037856, -0.212, -0.584736, -1.10493, -1.79715, -2.68598, -3.796];
// for (i = [0:1:6])
// {
// echo(i);
// number = (i == 0) ? 0 : addl(test_list, i);
// translate([number, 0, 0])
// cube([1, 1, 1]);
// echo(number);
// }