Pixel-Composer/scripts/node_find_pixel/node_find_pixel.gml

89 lines
2.8 KiB
Plaintext
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Find_Pixel(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2023-02-14 02:48:33 +01:00
name = "Find pixel";
w = 96;
inputs[| 0] = nodeValue("Surface in", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, 0);
inputs[| 1] = nodeValue("Search color", self, JUNCTION_CONNECT.input, VALUE_TYPE.color, c_black);
inputs[| 2] = nodeValue("Tolerance", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0)
.setDisplay(VALUE_DISPLAY.slider);
inputs[| 3] = nodeValue("Find all", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false)
2023-02-14 02:48:33 +01:00
outputs[| 0] = nodeValue("Position", self, JUNCTION_CONNECT.output, VALUE_TYPE.integer, [ 0, 0 ])
.setDisplay(VALUE_DISPLAY.vector);
static getPreviewValues = function() { return getInputData(0); }
2023-02-14 02:48:33 +01:00
2023-02-23 07:02:19 +01:00
temp_surface = [ surface_create(1, 1) ];
2023-02-14 02:48:33 +01:00
static processData = function(_output, _data, _output_index, _array_index = 0) { #region
2023-02-14 02:48:33 +01:00
var _surf = _data[0];
var _col = _data[1];
var _tol = _data[2];
var _all = _data[3];
2023-02-14 02:48:33 +01:00
if(!is_surface(_surf)) return [0, 0];
var _buff = buffer_from_surface(_surf, false);
var _sw = surface_get_width_safe(_surf);
var _sh = surface_get_height_safe(_surf);
buffer_seek(_buff, buffer_seek_start, 0);
var res = [];
var r = color_get_red(_col) / 255;
var g = color_get_green(_col) / 255;
var b = color_get_blue(_col) / 255;
for( var i = 0; i < _sh; i++ )
for( var j = 0; j < _sw; j++ ) {
var _c = buffer_read(_buff, buffer_u32);
var _r = ((_c & 0x000000FF) >> 0) / 255;
var _g = ((_c & 0x0000FF00) >> 8) / 255;
var _b = ((_c & 0x00FF0000) >> 16) / 255;
var _a = ((_c & 0xFF000000) >> 24) / 255;
if(_a == 0) continue;
if((abs(r - _r) + abs(g - _g) + abs(b - _b)) / 3 <= _tol) {
if(_all) array_push(res, [ j, i ]);
else return [ j, i ];
}
}
buffer_delete(_buff);
return _all? res : [ -1, -1 ];
//temp_surface[0] = surface_verify(temp_surface[0], 1, 1);
2023-02-14 02:48:33 +01:00
//surface_set_shader(temp_surface[0], sh_find_pixel);
// shader_set_surface("texture", _surf);
// shader_set_dim("dimension", _surf);
// draw_sprite_ext(s_fx_pixel, 0, 0, 0, 1, 1, 0, _col, 1);
//surface_reset_shader();
2023-02-14 02:48:33 +01:00
//var pos = surface_get_pixel(temp_surface[0], 0, 0);
//var _x = round(color_get_red(pos) / 255 * surface_get_width_safe(_surf));
//var _y = round(color_get_green(pos) / 255 * surface_get_height_safe(_surf));
2023-02-14 02:48:33 +01:00
//return [ _x, _y ];
} #endregion
2023-02-14 02:48:33 +01:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region
2023-02-14 02:48:33 +01:00
var bbox = drawGetBbox(xx, yy, _s);
if(bbox.h <= 0) return;
var col = getInputData(1);
2023-02-14 02:48:33 +01:00
if(is_array(col)) {
drawPalette(col, bbox.x0, bbox.y0, bbox.w, bbox.h);
return;
}
draw_set_color(col);
draw_rectangle(bbox.x0, bbox.y0, bbox.x1, bbox.y1, 0);
} #endregion
2023-02-14 02:48:33 +01:00
}