Pixel-Composer/scripts/node_surface_replace/node_surface_replace.gml

135 lines
4.3 KiB
Text
Raw Normal View History

2023-03-29 15:02:03 +02:00
function Node_Surface_Replace(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
name = "Replace Image";
2024-08-18 06:16:20 +02:00
newInput(0, nodeValue_Surface("Base Image", self));
2023-03-29 15:02:03 +02:00
2024-08-18 09:13:41 +02:00
newInput(1, nodeValue_Surface("Target Image", self))
2023-03-30 07:54:25 +02:00
.setArrayDepth(1);
2023-03-29 15:02:03 +02:00
2024-08-18 09:13:41 +02:00
newInput(2, nodeValue_Surface("Replacement Image", self))
2023-03-30 07:54:25 +02:00
.setArrayDepth(1);
2023-03-29 15:02:03 +02:00
2024-08-18 09:13:41 +02:00
newInput(3, nodeValue_Float("Color Threshold", self, 0.1, "How similiar the color need to be in order to be count as matched." ))
.setDisplay(VALUE_DISPLAY.slider);
2023-03-29 15:02:03 +02:00
2024-08-18 06:16:20 +02:00
newInput(4, nodeValue_Bool("Draw Base Image", self, true ));
2023-03-29 15:02:03 +02:00
2024-08-18 06:16:20 +02:00
newInput(5, nodeValue_Bool("Fast Mode", self, true ));
2023-03-30 07:54:25 +02:00
2024-08-18 09:13:41 +02:00
newInput(6, nodeValue_Float("Pixel Threshold", self, 0.1, "How many pixel need to me matched to replace with replacement image." ))
.setDisplay(VALUE_DISPLAY.slider);
2023-04-11 13:34:01 +02:00
2024-08-18 06:16:20 +02:00
newInput(7, nodeValue_Enum_Scroll("Array mode", self, 0, { data: [ "Match index", "Randomized" ], update_hover: false }));
2023-04-11 13:34:01 +02:00
2024-08-18 09:13:41 +02:00
newInput(8, nodeValue_Float("Seed", self, seed_random(6)))
2024-08-08 06:57:51 +02:00
.setDisplay(VALUE_DISPLAY._default, { side_button : button(function() { randomize(); inputs[8].setValue(seed_random(6)); }).setIcon(THEME.icon_random, 0, COLORS._main_icon) });
2023-04-11 13:34:01 +02:00
2024-08-18 09:13:41 +02:00
newInput(9, nodeValue_Bool("Replace Empty", self, false))
2024-05-24 13:16:10 +02:00
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Surface Out", self, VALUE_TYPE.surface, noone));
2023-03-29 15:02:03 +02:00
input_display_list = [
["Surfaces", true], 0, 1, 2, 7, 8,
["Searching", false], 5, 3, 6,
2024-05-24 13:16:10 +02:00
["Render", false], 4, 9,
2023-03-29 15:02:03 +02:00
];
2024-05-24 13:16:10 +02:00
temp_surface = [ surface_create(1, 1), surface_create(1, 1), surface_create(1, 1) ];
2023-03-29 15:02:03 +02:00
static matchTemplate = function(_index, _surf, _base, _target, _cthr, _pthr, _fst) {
2023-03-30 07:54:25 +02:00
surface_set_shader(_surf, _fst? sh_surface_replace_fast_find : sh_surface_replace_find, false);
2023-09-08 21:37:36 +02:00
shader_set_f("dimension", surface_get_width_safe(_base), surface_get_height_safe(_base));
2023-07-14 20:34:35 +02:00
2023-04-11 13:34:01 +02:00
shader_set_surface("target", _target);
2023-09-08 21:37:36 +02:00
shader_set_f("targetDimension", surface_get_width_safe(_target), surface_get_height_safe(_target));
2023-07-14 20:34:35 +02:00
shader_set_f("colorThreshold", _cthr);
shader_set_f("pixelThreshold", _pthr);
2023-03-30 07:54:25 +02:00
shader_set_f("index", _index);
shader_set_i("mode", getInputData(7));
shader_set_f("seed", getInputData(8));
2023-04-11 13:34:01 +02:00
var dest = getInputData(2);
2023-04-11 13:34:01 +02:00
var size = is_array(dest)? array_length(dest) : 1;
shader_set_f("size", size);
2023-03-30 07:54:25 +02:00
BLEND_ADD
draw_surface_safe(_base);
BLEND_NORMAL
surface_reset_shader();
}
2024-05-24 13:16:10 +02:00
static replaceTemplate = function(_index, _base, _res, _replace, _fst) {
shader_set(_fst? sh_surface_replace_fast_replace : sh_surface_replace_replace);
surface_set_target_ext(0, temp_surface[1]);
surface_set_target_ext(1, temp_surface[2]);
2023-09-08 21:37:36 +02:00
shader_set_f("dimension", surface_get_width_safe(_base), surface_get_height_safe(_base));
2023-03-30 07:54:25 +02:00
shader_set_surface("replace", _replace);
2023-09-08 21:37:36 +02:00
shader_set_f("replace_dim", surface_get_width_safe(_replace), surface_get_height_safe(_replace));
2023-03-30 07:54:25 +02:00
shader_set_surface("findRes", _res);
shader_set_f("index", _index);
draw_surface_safe(_base);
surface_reset_shader();
}
2023-04-11 13:34:01 +02:00
static step = function() {
var _mode = getInputData(7);
2024-08-08 06:57:51 +02:00
inputs[8].setVisible(_mode == 1);
2023-04-11 13:34:01 +02:00
}
2023-08-17 16:56:54 +02:00
static processData = function(_outSurf, _data, _output_index, _array_index) {
2023-03-29 15:02:03 +02:00
var _bas = _data[0];
var _tar = _data[1];
var _rep = _data[2];
2023-03-30 07:54:25 +02:00
var _drw = _data[4];
var _fst = _data[5];
2023-03-29 15:02:03 +02:00
var _cthr = _data[3];
var _pthr = _data[6];
2024-05-24 13:16:10 +02:00
var _oalp = _data[9];
2023-04-11 13:34:01 +02:00
if(!is_array(_tar)) _tar = [ _tar ];
2023-03-30 07:54:25 +02:00
if(!is_array(_rep)) _rep = [ _rep ];
2023-03-29 15:02:03 +02:00
var _sw = surface_get_width_safe(_bas);
var _sh = surface_get_height_safe(_bas);
2024-05-24 13:16:10 +02:00
for (var i = 0, n = array_length(temp_surface); i < n; i++) {
temp_surface[i] = surface_verify(temp_surface[i], _sw, _sh);
surface_clear(temp_surface[i]);
}
var tamo = array_length(_tar);
var ramo = array_length(_rep);
for( var i = 0; i < tamo; i++ ) matchTemplate(i / tamo, temp_surface[0], _bas, _tar[i], _cthr, _pthr, _fst);
// return temp_surface[0];
2023-03-29 15:02:03 +02:00
2024-05-24 13:16:10 +02:00
var amo = max(tamo, ramo);
for( var i = 0; i < amo; i++ ) {
var _ri = i % ramo;
replaceTemplate(_ri / amo, _bas, temp_surface[0], _rep[_ri], _fst);
}
_outSurf = surface_verify(_outSurf, _sw, _sh);
2023-03-30 07:54:25 +02:00
surface_set_target(_outSurf);
2023-12-15 12:56:36 +01:00
DRAW_CLEAR
2024-05-24 13:16:10 +02:00
BLEND_ALPHA
2023-03-30 07:54:25 +02:00
if(_drw) draw_surface_safe(_bas);
2024-05-24 13:16:10 +02:00
if(_oalp) {
BLEND_SUBTRACT
draw_surface_safe(temp_surface[2]);
BLEND_ALPHA
}
draw_surface_safe(temp_surface[1]);
2023-03-30 07:54:25 +02:00
surface_reset_target();
2023-03-30 07:54:25 +02:00
return _outSurf;
2023-03-29 15:02:03 +02:00
}
}