cenestral

Noise

Jun 22nd, 2021 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    
  2.     function lerp_inverse(a,b,v){
  3.         return(v-a)/(b-a);
  4.     }
  5.    
  6.    
  7.     function Noise() constructor{
  8.        
  9.         seed_object = 0;
  10.        
  11.         set_seed = function(_seed){
  12.             seed_object = _seed;
  13.             p_table = make_permutation(512);
  14.         }
  15.    
  16.         static make_permutation = function(size){
  17.             var _P = [];   
  18.             for(var i = 0; i < size; i++){array_push(_P,i);}
  19.             seed_object.sarray_shuffle(_P);
  20.             for(var i = 0; i < size; i++){array_push(_P,_P[i])}
  21.             return _P;
  22.         }
  23.            
  24.         static value_noise = function(xx,yy){
  25.             var fx = floor(xx);
  26.             var fy = floor(yy);
  27.             var perm = 512;
  28.        
  29.             var X = fx mod perm;
  30.             var Y = fy mod perm;
  31.        
  32.             if X<0{X = perm+X;}
  33.             if Y<0{Y = perm+Y;}
  34.        
  35.             var xf = (xx-fx);
  36.             var yf = (yy-fy);
  37.        
  38.             var valueTopRight = p_table[p_table[X+1]+Y+1];
  39.             var valueTopLeft  = p_table[p_table[X]+Y+1];
  40.        
  41.             var valueBottomRight = p_table[p_table[X+1]+Y];
  42.             var valueBottomLeft  = p_table[p_table[X]+Y];
  43.        
  44.             var u = xf*xf*(3-2*xf);
  45.        
  46.             return (lerp(lerp(valueTopLeft, valueTopRight,u),lerp(valueBottomLeft, valueBottomRight,u),1-(yf*yf*(3-2*yf))))/perm;
  47.         }
  48.            
  49.         static generate_noise_map = function(width,height,offset_x,offset_y,scale_x,scale_y,octaves,persistance,lacunarity){
  50.             ///@func generate_noise_map(width,height,offset_x,offset_y,scale_x,scale_y,octaves,persistance,lacunarity)
  51.             ///@param  width
  52.             ///@param  height
  53.             ///@param  offset_x
  54.             ///@param  offset_y
  55.             ///@param  scale_x
  56.             ///@param  scale_y
  57.             ///@param  octaves
  58.             ///@param  persistance
  59.             ///@param  lacunarity
  60.            
  61.            
  62.            
  63.            
  64.             var noise_array;
  65.             noise_array[width-1][height-1] = 0;
  66.            
  67.    
  68.             octaves     = max(octaves,1);
  69.             scale_x     = max(scale_x,0.0001); 
  70.             scale_y     = max(scale_y,0.0001); 
  71.             persistance = max(persistance,0.0001);
  72.             lacunarity  = max(lacunarity,0.0001);
  73.    
  74.            
  75.             var noise_max_height = 1;
  76.             var noise_min_height = 0;
  77.            
  78.             for(var yy=0;yy<height;yy++){
  79.                 for(var xx=0;xx<width;xx++){           
  80.                     var amp = 1;
  81.                     var freq = 1;
  82.                     var noise = 0; 
  83.                     for (var o =0;o<octaves;o++){
  84.                         var sample_x = (xx+offset_x)/scale_x*freq;
  85.                         var sample_y = (yy+offset_y)/scale_y*freq;
  86.                         var noise_value = value_noise(sample_x,sample_y);
  87.                         noise+=noise_value*amp
  88.                        
  89.                         amp  *=persistance;
  90.                         freq *=lacunarity;
  91.                     }
  92.                    
  93.                     noise_max_height = max(noise_max_height,noise)
  94.                     noise_min_height = min(noise_min_height,noise)
  95.                    
  96.                     noise_array[xx][yy] = noise;
  97.                 }
  98.             }
  99.    
  100.            
  101.             //normalizing
  102.             for(var yy=0;yy<height;yy++){
  103.                 for(var xx=0;xx<width;xx++){
  104.                     noise_array[xx][yy] = lerp_inverse(noise_min_height,noise_max_height,noise_array[xx][yy])
  105.                 }
  106.             }
  107.            
  108.            
  109.             return noise_array;
  110.            
  111.         }
  112.     }
Add Comment
Please, Sign In to add comment