Pixel-Composer/scripts/node_sampler/node_sampler.gml

124 lines
3.1 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-08-18 06:16:20 +02:00
newInput(0, nodeValue_Surface("Surface in", self));
2022-01-13 05:24:03 +01:00
2024-08-18 09:13:41 +02:00
newInput(1, nodeValue_Vec2("Position", self, [ 0, 0 ]))
2022-12-27 13:30:02 +01:00
.setUnitRef(function(index) { return getDimension(index); });
2023-01-09 03:14:20 +01:00
2024-08-18 09:13:41 +02:00
newInput(2, nodeValue_Int("Sampling size", self, 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-08-18 06:16:20 +02:00
newInput(3, nodeValue_Bool("Alpha", self, false));
2024-01-10 06:36:33 +01:00
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Color", self, VALUE_TYPE.color, c_white));
2022-12-21 02:30:23 +01:00
attribute_oversample(true);
attributes.oversample = 1;
static getPreviewValues = function() { return getInputData(0); }
2022-01-13 05:24:03 +01:00
2024-08-27 05:30:03 +02:00
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
PROCESSOR_OVERLAY_CHECK
2024-08-08 06:57:51 +02: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-08-27 05:30:03 +02:00
}
2022-01-13 05:24:03 +01:00
2024-08-27 05:30:03 +02:00
static processData = function(_output, _data, _output_index, _array_index = 0) {
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 || py < 0 || px >= ww || py >= hh) {
switch(attributes.oversample) {
case 0 : continue;
case 1 :
px = clamp(px, 0, ww - 1);
py = clamp(py, 0, hh - 1);
break;
case 2 :
px = safe_mod(px, ww - 1);
py = safe_mod(py, hh - 1);
break;
case 3 :
a += 255;
amo++;
continue;
}
}
2023-01-09 03:14:20 +01:00
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++;
}
if(amo > 0) {
r /= amo;
g /= amo;
b /= amo;
a /= amo;
}
2023-01-09 03:14:20 +01:00
2024-08-27 05:30:03 +02:00
return make_color_rgba(r, g, b, _alp? a : 255);
}
2023-01-01 02:06:02 +01:00
2024-08-27 05:30:03 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
2023-01-04 02:30:04 +01:00
var bbox = drawGetBbox(xx, yy, _s);
2024-08-08 06:57:51 +02: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);
2024-08-27 05:30:03 +02:00
}
2022-01-13 05:24:03 +01:00
}