Pixel-Composer/scripts/distribution_function/distribution_function.gml

68 lines
1.7 KiB
Plaintext
Raw Normal View History

2023-02-14 02:48:33 +01:00
function get_point_from_dist(distMap, attempt = 4) {
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;
}
2023-02-21 04:48:50 +01:00
function get_points_from_dist(distMap, amount, seed = 0, attempt = 8) {
2023-02-14 02:48:33 +01:00
if(amount < 1) return [];
if(!is_surface(distMap)) return [];
var surf = surface_create_valid(amount, 1);
surface_set_target(surf);
2023-03-19 09:17:39 +01:00
DRAW_CLEAR
2023-02-14 02:48:33 +01:00
BLEND_OVERRIDE;
shader_set(sh_sample_points);
shader_set_uniform_f(shader_get_uniform(sh_sample_points, "dimension"),
2023-09-08 21:37:36 +02:00
surface_get_width_safe(distMap) / amount, surface_get_height_safe(distMap));
2023-02-21 04:48:50 +01:00
shader_set_uniform_i(shader_get_uniform(sh_sample_points, "attempt"), attempt);
2023-02-14 02:48:33 +01:00
shader_set_uniform_f(shader_get_uniform(sh_sample_points, "seed"), seed);
2023-03-19 09:17:39 +01:00
draw_surface_stretched_safe(distMap, 0, 0, amount, 1);
2023-02-14 02:48:33 +01:00
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);
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
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) / 255;
var _y = color_get_green(cc) / 255;
2023-02-21 04:48:50 +01:00
var _v = color_get_blue(cc) / 255;
pos[i] = [_x, _y, _v];
2023-02-14 02:48:33 +01:00
}
}
buffer_delete(b);
surface_free(surf);
return pos;
}