Pixel-Composer/scripts/perlin_noise/perlin_noise.gml

32 lines
697 B
Text
Raw Normal View History

2023-01-17 08:11:55 +01:00
function random2D (_x, _y, _s) {
2022-01-13 05:24:03 +01:00
random_set_seed((_x + _y) * (_x + _y + 1) / 2 + _y + _s);
return random(1);
}
2023-01-17 08:11:55 +01:00
function noise2D(_x, _y, _seed) {
2022-01-13 05:24:03 +01:00
var fl_x = floor(_x);
var fl_y = floor(_y);
2023-01-17 08:11:55 +01:00
var a = random2D(fl_x + 0, fl_y + 0, _seed);
var b = random2D(fl_x + 1, fl_y + 0, _seed);
var c = random2D(fl_x + 0, fl_y + 1, _seed);
var d = random2D(fl_x + 1, fl_y + 1, _seed);
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
return lerp(lerp(a, b, _x - fl_x),
lerp(c, d, _x - fl_x), _y - fl_y);
2022-01-13 05:24:03 +01:00
}
function perlin_noise(_x, _y, _amp, _seed) {
var res = 0;
2023-01-17 08:11:55 +01:00
var amp = power(2., _amp - 1.) / (power(2., _amp) - 1.);
2022-01-13 05:24:03 +01:00
repeat(_amp) {
2023-01-17 08:11:55 +01:00
res += noise2D(_x, _y, _seed) * amp;
2022-01-13 05:24:03 +01:00
_x /= 2;
_y /= 2;
2023-01-17 08:11:55 +01:00
amp *= 0.5;
2022-01-13 05:24:03 +01:00
}
2023-01-17 08:11:55 +01:00
return res;
2022-01-13 05:24:03 +01:00
}