Pixel-Composer/scripts/distribution_function/distribution_function.gml
2024-01-09 09:39:40 +07:00

68 lines
No EOL
1.7 KiB
Text

function get_point_from_dist(distMap, attempt = 4) {
if(!is_surface(distMap)) return noone;
var w = surface_get_width_safe(distMap);
var h = surface_get_height_safe(distMap);
var v = 0;
var res = noone;
repeat(attempt) {
var _x = irandom(w - 1);
var _y = irandom(h - 1);
var p = surface_get_pixel(distMap, _x, _y);
var r = color_get_red(p);
if(r > v) {
v = r;
res = [_x / w, _y / h];
}
}
return res;
}
function get_points_from_dist(distMap, amount, seed = 0, attempt = 8) {
if(amount < 1) return [];
if(!is_surface(distMap)) return [];
var surf = surface_create_valid(amount, 1);
surface_set_target(surf);
DRAW_CLEAR
BLEND_OVERRIDE;
shader_set(sh_sample_points);
shader_set_uniform_f(shader_get_uniform(sh_sample_points, "dimension"),
surface_get_width_safe(distMap) / amount, surface_get_height_safe(distMap));
shader_set_uniform_i(shader_get_uniform(sh_sample_points, "attempt"), attempt);
shader_set_uniform_f(shader_get_uniform(sh_sample_points, "seed"), seed);
draw_surface_stretched_safe(distMap, 0, 0, amount, 1);
shader_reset();
BLEND_NORMAL;
surface_reset_target();
var b = buffer_create(amount * 4, buffer_fixed, 4);
buffer_get_surface(b, surf, 0);
buffer_seek(b, buffer_seek_start, 0);
var pos = array_create(amount);
var w = surface_get_width_safe(distMap);
var h = surface_get_height_safe(distMap);
for( var i = 0; i < amount; i++ ) {
var cc = buffer_read(b, buffer_u32);
if(cc == 0) pos[i] = 0;
else {
var _x = _color_get_red(cc);
var _y = _color_get_green(cc);
var _v = _color_get_blue(cc);
pos[i] = [_x, _y, _v];
}
}
buffer_delete(b);
surface_free(surf);
return pos;
}