Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function lerp_inverse(a,b,v){
- return(v-a)/(b-a);
- }
- function Noise() constructor{
- seed_object = 0;
- set_seed = function(_seed){
- seed_object = _seed;
- p_table = make_permutation(512);
- }
- static make_permutation = function(size){
- var _P = [];
- for(var i = 0; i < size; i++){array_push(_P,i);}
- seed_object.sarray_shuffle(_P);
- for(var i = 0; i < size; i++){array_push(_P,_P[i])}
- return _P;
- }
- static value_noise = function(xx,yy){
- var fx = floor(xx);
- var fy = floor(yy);
- var perm = 512;
- var X = fx mod perm;
- var Y = fy mod perm;
- if X<0{X = perm+X;}
- if Y<0{Y = perm+Y;}
- var xf = (xx-fx);
- var yf = (yy-fy);
- var valueTopRight = p_table[p_table[X+1]+Y+1];
- var valueTopLeft = p_table[p_table[X]+Y+1];
- var valueBottomRight = p_table[p_table[X+1]+Y];
- var valueBottomLeft = p_table[p_table[X]+Y];
- var u = xf*xf*(3-2*xf);
- return (lerp(lerp(valueTopLeft, valueTopRight,u),lerp(valueBottomLeft, valueBottomRight,u),1-(yf*yf*(3-2*yf))))/perm;
- }
- static generate_noise_map = function(width,height,offset_x,offset_y,scale_x,scale_y,octaves,persistance,lacunarity){
- ///@func generate_noise_map(width,height,offset_x,offset_y,scale_x,scale_y,octaves,persistance,lacunarity)
- ///@param width
- ///@param height
- ///@param offset_x
- ///@param offset_y
- ///@param scale_x
- ///@param scale_y
- ///@param octaves
- ///@param persistance
- ///@param lacunarity
- var noise_array;
- noise_array[width-1][height-1] = 0;
- octaves = max(octaves,1);
- scale_x = max(scale_x,0.0001);
- scale_y = max(scale_y,0.0001);
- persistance = max(persistance,0.0001);
- lacunarity = max(lacunarity,0.0001);
- var noise_max_height = 1;
- var noise_min_height = 0;
- for(var yy=0;yy<height;yy++){
- for(var xx=0;xx<width;xx++){
- var amp = 1;
- var freq = 1;
- var noise = 0;
- for (var o =0;o<octaves;o++){
- var sample_x = (xx+offset_x)/scale_x*freq;
- var sample_y = (yy+offset_y)/scale_y*freq;
- var noise_value = value_noise(sample_x,sample_y);
- noise+=noise_value*amp
- amp *=persistance;
- freq *=lacunarity;
- }
- noise_max_height = max(noise_max_height,noise)
- noise_min_height = min(noise_min_height,noise)
- noise_array[xx][yy] = noise;
- }
- }
- //normalizing
- for(var yy=0;yy<height;yy++){
- for(var xx=0;xx<width;xx++){
- noise_array[xx][yy] = lerp_inverse(noise_min_height,noise_max_height,noise_array[xx][yy])
- }
- }
- return noise_array;
- }
- }
Add Comment
Please, Sign In to add comment