Pixel-Composer/scripts/node_sampler/node_sampler.gml

103 lines
3.0 KiB
Plaintext
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Sampler(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2022-01-13 05:24:03 +01:00
name = "Sampler";
2024-05-02 11:05:02 +02:00
setDimension(96, 48);
2023-01-01 02:06:02 +01:00
2024-03-14 14:35:19 +01:00
inputs[| 0] = nodeValue("Surface in", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, noone);
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 1] = nodeValue("Position", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 0 ])
2022-12-27 04:00:50 +01:00
.setDisplay(VALUE_DISPLAY.vector)
2022-12-27 13:30:02 +01:00
.setUnitRef(function(index) { return getDimension(index); });
2023-01-09 03:14:20 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 2] = nodeValue("Sampling size", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 1, "Size of square around the position to sample and average pixel color.")
2024-03-24 04:58:08 +01:00
.setDisplay(VALUE_DISPLAY.slider, { range: [1, 3, 0.1] });
2022-01-13 05:24:03 +01:00
2024-01-10 06:36:33 +01:00
inputs[| 3] = nodeValue("Alpha", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false);
2023-02-14 05:32:32 +01:00
outputs[| 0] = nodeValue("Color", self, JUNCTION_CONNECT.output, VALUE_TYPE.color, c_white);
2022-12-21 02:30:23 +01:00
static getPreviewValues = function() { return getInputData(0); }
2022-01-13 05:24:03 +01:00
2024-03-14 14:35:19 +01:00
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) { #region
PROCESSOR_OVERLAY_CHECK
2024-03-14 14:35:19 +01:00
inputs[| 1].drawOverlay(hover, active, _x, _y, _s, _mx, _my, _snx, _sny);
2023-01-09 03:14:20 +01:00
var _suf = current_data[0];
if(!is_surface(_suf)) return;
2023-09-08 21:37:36 +02:00
var ww = surface_get_width_safe(_suf);
var hh = surface_get_height_safe(_suf);
2023-01-09 03:14:20 +01:00
var _pos = current_data[1];
var _sam = 1 + (current_data[2] - 1) * 2;
var x0 = _pos[0] + 0.5 - _sam / 2;
var x1 = _pos[0] + 0.5 + _sam / 2;
var y0 = _pos[1] + 0.5 - _sam / 2;
var y1 = _pos[1] + 0.5 + _sam / 2;
x0 = clamp(x0, 0, ww);
x1 = clamp(x1, 0, ww);
y0 = clamp(y0, 0, hh);
y1 = clamp(y1, 0, hh);
x0 = _x + x0 * _s;
x1 = _x + x1 * _s;
y0 = _y + y0 * _s;
y1 = _y + y1 * _s;
draw_set_color(COLORS._main_accent);
draw_rectangle(x0, y0, x1, y1, true);
2024-01-10 06:36:33 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2024-01-10 06:36:33 +01:00
static processData = function(_output, _data, _output_index, _array_index = 0) { #region
2022-12-21 02:30:23 +01:00
var _surf = _data[0];
2024-01-10 06:36:33 +01:00
var _pos = _data[1];
var _sam = _data[2];
var _alp = _data[3];
2022-12-21 02:30:23 +01:00
if(!is_surface(_surf)) return c_black;
2022-01-13 05:24:03 +01:00
2023-09-08 21:37:36 +02:00
var ww = surface_get_width_safe(_surf);
var hh = surface_get_height_safe(_surf);
2023-01-09 03:14:20 +01:00
2024-01-10 06:36:33 +01:00
var r = 0, g = 0, b = 0, a = 0, amo = 0;
2023-01-09 03:14:20 +01:00
_sam -= 1;
for( var i = -_sam; i <= _sam; i++ )
for( var j = -_sam; j <= _sam; j++ ) {
var px = _pos[0] + i;
var py = _pos[1] + j;
if(px < 0) continue;
if(py < 0) continue;
if(px >= ww) continue;
if(py >= hh) continue;
2024-01-10 06:36:33 +01:00
var cc = int64(surface_get_pixel_ext(_surf, px, py));
2023-01-09 03:14:20 +01:00
r += color_get_red(cc);
g += color_get_green(cc);
b += color_get_blue(cc);
2024-01-10 06:36:33 +01:00
a += color_get_alpha(cc);
2023-01-09 03:14:20 +01:00
amo++;
}
r /= amo;
g /= amo;
b /= amo;
2024-01-10 06:36:33 +01:00
a /= amo;
2023-01-09 03:14:20 +01:00
2024-01-10 06:36:33 +01:00
return _alp? make_color_rgba(r, g, b, a) : make_color_rgb(r, g, b);
} #endregion
2023-01-01 02:06:02 +01:00
2024-01-10 06:36:33 +01:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region
2023-01-04 02:30:04 +01:00
var bbox = drawGetBbox(xx, yy, _s);
2024-01-10 06:36:33 +01:00
var col = outputs[| 0].getValue();
2023-01-09 03:14:20 +01:00
if(bbox.h <= 0) return;
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
if(is_array(col)) {
drawPalette(col, bbox.x0, bbox.y0, bbox.w, bbox.h);
return;
}
2024-01-10 06:36:33 +01:00
drawColor(col, bbox.x0, bbox.y0, bbox.w, bbox.h);
} #endregion
2022-01-13 05:24:03 +01:00
}