Showing posts with label perlin. Show all posts
Showing posts with label perlin. Show all posts

Thursday, June 1, 2017

1 dimensional noise

Singel dimension noise layered in three octaves.
Return value is 0-1.

Usage:



seed=rnd(12);
for (a=[-15:0.1:15])  translate([a,0,0])  cube([0.1,0.1, noise_1d(a+seed)]);

function  noise_1d(i,
low_range=1/4,mid_range=1,hi_range=8,
low_magnitude=1/2,mid_magnitude=0.25,hi_magnitude=0.5)=
 (  
single_noise_1d(i/low_range)*low_magnitude+
single_noise_1d(i/mid_range)*mid_magnitude+
single_noise_1d(i/hi_range)*hi_magnitude

)/(low_magnitude+mid_magnitude+hi_magnitude);


function single_noise_1d(i)=
let(
a=floor(i),
b=ceil(i),
c=smooth_curve(i-a),
arnd=rnd(1,0,a),
brnd=rnd(1,0,b),
out=lerp(arnd,brnd,c)
)
out
;

function smooth_curve(a) =let (b = clamp(a))(b * b * (3 - 2 * b));
function clamp(a, b = 0, c = 1) = min(max(a, b), c);
function lerp(start, end, bias) = (end * bias + start * (1 - bias));
function rnd(a = 1, b = 0, s = []) = 
  s == [] ? 
   (rands(min(a, b), max(   a, b), 1)[0]) 
  : 
   (rands(min(a, b), max(a, b), 1, s)[0])
  ; 


Friday, May 12, 2017

Trilinear interpolation

Interpolate a value between eight values, typically corners of a cube. Lerp
Usage:

my_value=trilinear_lerp( 9,12,3,34, 25,16,7,18, 0.1,0.6,0.5 );

function trilinear_lerp(
V000,V100,V010,V001,
V101,V011,V110,V111,
bias_x,bias_y,bias_z)=
lerp(
    lerp(
        lerp(V000,V001,bias_z),
        lerp(V010,V011,bias_z),
    bias_y
    ),
    lerp(
        lerp(V100,V101,bias_z),
        lerp(V110,V111,bias_z),
    bias_y
    )
    ,bias_x
);
function lerp(start, end, bias) = (end * bias + start * (1 - bias));

d

Simple 3d Noise

Pseudo random number seeded by a point, 0-1.

Usage:


my_noise=  Coldnoise([x,y,z]);


function  Coldnoise(v) =
 let (
   xseed = round(rnd(1e8, -1e8, round(v.x * 1e6))),
   yseed = round(rnd(1e8, -1e8, xseed + round(v.y * 1e6))),
   zseed = round(rnd(1e8, -1e8, yseed + round(v.z * 1e6))),
   noise  =  (rnd(0, 1e8, zseed))%1)
 noise;

 function rnd(a = 1, b = 0, s = []) = 
  s == [] ? 
   (rands(min(a, b), max(   a, b), 1)[0]) 
  : 
   (rands(min(a, b), max(a, b), 1, s)[0])
  ;