Pixel-Composer/scripts/random_function/random_function.gml

152 lines
3.9 KiB
Text
Raw Normal View History

function UUID_generate(length = 32) { #region
randomize();
static str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static month = "JFRAMJYASOND"
var _id = "";
_id += string_char_at(str, current_year % string_length(str) + 1); //1
_id += string_char_at(month, current_month); //1
_id += string_char_at(str, current_day); //1
_id += string_char_at(str, current_hour); //1
_id += string_char_at(str, current_minute); //1
_id += string_char_at(str, current_second); //1
_id += string_lead_zero(current_time, 6); //6
repeat(length - string_length(_id)) _id += string_char_at(str, irandom_range(1, string_length(str)));
return _id;
} #endregion
function irandom_seed(val, seed) { #region
2023-01-17 08:11:55 +01:00
random_set_seed(floor(seed));
return irandom(val);
} #endregion
2023-01-17 08:11:55 +01:00
function irandom_range_seed(from, to, seed) { #region
2023-01-17 08:11:55 +01:00
random_set_seed(floor(seed));
return irandom_range(from, to);
} #endregion
2023-01-17 08:11:55 +01:00
2024-01-08 08:10:50 +01:00
function seed_random(digits = 6) { #region
return irandom_range(power(10, digits - 1), power(10, digits) - 1);
} #endregion
function random_seed(val, seed) { #region
2023-01-17 08:11:55 +01:00
random_set_seed(floor(seed));
var _s0 = random(val);
random_set_seed(floor(seed) + 1);
var _s1 = random(val);
return lerp(_s0, _s1, frac(seed));
} #endregion
2023-01-17 08:11:55 +01:00
function random_range_seed(from, to, seed) { #region
2023-01-17 08:11:55 +01:00
random_set_seed(floor(seed));
var _s0 = random_range(from, to);
random_set_seed(floor(seed) + 1);
var _s1 = random_range(from, to);
return lerp(_s0, _s1, frac(seed));
} #endregion
2023-01-17 08:11:55 +01:00
function random1D(seed, startRange = 0, endRange = 1) { #region
2023-03-19 09:17:39 +01:00
if(startRange == endRange) return startRange;
2023-01-09 03:14:20 +01:00
var _f = frac(seed);
if(_f == 0) {
2023-07-06 19:49:16 +02:00
random_set_seed(PROJECT.seed + seed);
2023-01-09 03:14:20 +01:00
return random_range(startRange, endRange);
}
2023-07-06 19:49:16 +02:00
random_set_seed(PROJECT.seed + floor(seed));
2023-01-09 03:14:20 +01:00
var f1 = random_range(startRange, endRange);
2023-07-06 19:49:16 +02:00
random_set_seed(PROJECT.seed + floor(seed) + 1);
2023-01-09 03:14:20 +01:00
var f2 = random_range(startRange, endRange);
return lerp(f1, f2, _f);
} #endregion
2022-12-12 09:08:03 +01:00
2023-12-18 04:40:21 +01:00
function __noise(_x) { #region
var i = floor(_x);
var f = frac(_x);
var a = random1D(i);
var b = random1D(i + 1);
var u = f * f * (3.0 - 2.0 * f);
return lerp(a, b, u);
} #endregion
function perlin1D(pos, seed, scale = 1, octave = 1, startRange = 0, endRange = 1) { #region
2023-03-19 09:17:39 +01:00
var amp = power(2., octave - 1.) / (power(2., octave) - 1.);
2023-12-18 04:40:21 +01:00
var n = 0.;
2023-03-19 09:17:39 +01:00
repeat(octave) {
2023-12-18 04:40:21 +01:00
n += __noise(seed + pos * scale) * amp;
amp *= .5;
pos *= 2.;
2023-03-19 09:17:39 +01:00
}
2023-12-18 04:40:21 +01:00
return lerp(startRange, endRange, n);
} #endregion
2023-03-19 09:17:39 +01:00
function wiggle(_min = 0, _max = 1, _freq = 1, _time = 0, _seed = 0, _octave = 1) { #region
2023-12-18 04:40:21 +01:00
return perlin1D(_time, _seed, _freq, _octave, _min, _max);
} #endregion
2023-07-11 20:36:44 +02:00
function getWiggle(_min = 0, _max = 1, _freq = 1, _time = 0, _seed = 0, startTime = noone, endTime = noone) { #region
2022-12-12 09:08:03 +01:00
_freq = max(1, _freq);
2023-01-09 03:14:20 +01:00
2022-12-12 09:08:03 +01:00
var sdMin = floor(_time / _freq) * _freq;
var sdMax = sdMin + _freq;
2023-01-09 03:14:20 +01:00
if(endTime) //Clip at ending
2022-12-12 09:08:03 +01:00
sdMax = min(endTime, sdMax);
2022-01-13 05:24:03 +01:00
2023-07-11 20:36:44 +02:00
var _x0 = (startTime != noone && sdMin <= startTime)? 0.5 : random1D(PROJECT.seed + _seed + sdMin);
var _x1 = (endTime != noone && sdMax >= endTime)? 0.5 : random1D(PROJECT.seed + _seed + sdMax);
2022-01-13 05:24:03 +01:00
2022-12-12 09:08:03 +01:00
var t = (_time - sdMin) / (sdMax - sdMin);
2022-09-21 06:09:40 +02:00
t = -(cos(pi * t) - 1) / 2;
2023-01-09 03:14:20 +01:00
var _lrp = lerp(_x0, _x1, t);
return lerp(_min, _max, _lrp);
} #endregion
2022-01-19 03:05:13 +01:00
function wiggleMap(_seed, _freq, _length) constructor { #region
seed = _seed;
freq = _freq;
len = _length;
amp = 1;
map = array_create(_length);
2024-01-22 14:23:35 +01:00
shf = 0;
static generate = function() {
2023-11-08 08:38:04 +01:00
INLINE
2024-01-22 14:23:35 +01:00
for(var i = 0; i < len; i++) map[i] = wiggle(-1, 1, freq, i + shf, seed);
}
2023-06-21 20:36:53 +02:00
2024-01-22 14:23:35 +01:00
static check = function(_amp, _freq, _seed, _shf = 0) {
2023-11-08 08:38:04 +01:00
INLINE
amp = _amp;
2024-01-22 14:23:35 +01:00
if(seed == _seed && freq == _freq && shf == _shf) return;
seed = _seed;
freq = _freq;
2024-01-22 14:23:35 +01:00
shf = _shf;
generate();
2024-01-22 14:23:35 +01:00
return self;
}
2023-07-25 20:12:40 +02:00
static get = function(i) {
2023-11-08 08:38:04 +01:00
INLINE
if(amp == 0) return 0;
return map[abs(i) % len] * amp;
}
} #endregion