Pixel-Composer/scripts/distribution_function/distribution_function.gml

69 lines
1.6 KiB
Text
Raw Normal View History

function get_point_from_dist(distMap, attempt = 4) { #region
2023-02-14 02:48:33 +01:00
if(!is_surface(distMap)) return noone;
2023-09-08 21:37:36 +02:00
var w = surface_get_width_safe(distMap);
var h = surface_get_height_safe(distMap);
2023-02-14 02:48:33 +01:00
var v = 0;
var res = noone;
repeat(attempt) {
var _x = irandom(w - 1);
var _y = irandom(h - 1);
2023-03-31 06:59:08 +02:00
var p = surface_get_pixel(distMap, _x, _y);
2023-02-14 02:48:33 +01:00
var r = color_get_red(p);
if(r > v) {
v = r;
res = [_x / w, _y / h];
}
}
return res;
} #endregion
2023-02-14 02:48:33 +01:00
function get_points_from_dist(distMap, amount, seed = 0, attempt = 8) { #region
2023-02-14 02:48:33 +01:00
if(amount < 1) return [];
if(!is_surface(distMap)) return [];
//print($"===== Get points from dist {amount} =====");
2023-02-14 02:48:33 +01:00
if(!struct_has(self, "__dist_surf"))
__dist_surf = surface_create_valid(amount, 1);
else
__dist_surf = surface_verify(__dist_surf, amount, 1);
var _sw = surface_get_width_safe(distMap);
var _sh = surface_get_height_safe(distMap);
surface_set_shader(__dist_surf, sh_sample_points);
shader_set_f("dimension", _sw / amount, _sh);
shader_set_i("attempt", attempt);
shader_set_f("seed", seed);
2023-02-14 02:48:33 +01:00
draw_surface_stretched_safe(distMap, 0, 0, amount, 1);
surface_reset_shader();
2023-02-14 02:48:33 +01:00
var b = buffer_create(amount * 4, buffer_fixed, 4);
buffer_get_surface(b, __dist_surf, 0);
2023-02-14 02:48:33 +01:00
buffer_seek(b, buffer_seek_start, 0);
var pos = array_create(amount);
for( var i = 0; i < amount; i++ ) {
//print($" Reading buffer {i}");
2023-02-14 02:48:33 +01:00
var cc = buffer_read(b, buffer_u32);
2023-02-14 02:48:33 +01:00
if(cc == 0) pos[i] = 0;
else {
2024-01-09 03:39:40 +01:00
var _x = _color_get_red(cc);
var _y = _color_get_green(cc);
var _v = _color_get_blue(cc);
2023-02-21 04:48:50 +01:00
pos[i] = [_x, _y, _v];
2023-02-14 02:48:33 +01:00
}
}
buffer_delete(b);
return pos;
} #endregion