Pixel-Composer/scripts/node_average/node_average.gml

77 lines
2.1 KiB
Plaintext
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Average(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2023-01-17 08:11:55 +01:00
name = "Average";
2024-08-18 06:16:20 +02:00
newInput(0, nodeValue_Surface("Surface in", self));
2023-01-17 08:11:55 +01:00
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Surface("Mask", self));
2023-02-14 05:32:32 +01:00
2024-08-18 09:13:41 +02:00
newInput(2, nodeValue_Float("Mix", self, 1))
.setDisplay(VALUE_DISPLAY.slider);
2023-02-14 05:32:32 +01:00
2024-08-18 06:16:20 +02:00
newInput(3, nodeValue_Bool("Active", self, true));
2023-02-14 05:32:32 +01:00
active_index = 3;
2024-08-18 06:16:20 +02:00
newInput(4, nodeValue_Toggle("Channel", self, 0b1111, { data: array_create(4, THEME.inspector_channel) }));
2023-11-24 10:41:53 +01:00
__init_mask_modifier(1); // inputs 5, 6,
input_display_list = [ 3, 4,
2023-11-24 10:41:53 +01:00
["Surfaces", false], 0, 1, 2, 5, 6,
2023-02-14 05:32:32 +01:00
]
2024-08-08 06:57:51 +02:00
outputs[0] = nodeValue_Output("Surface out", self, VALUE_TYPE.surface, noone);
2023-01-17 08:11:55 +01:00
2024-08-08 06:57:51 +02:00
outputs[1] = nodeValue_Output("Color", self, VALUE_TYPE.color, c_black);
2023-02-28 09:43:01 +01:00
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2023-02-28 09:43:01 +01:00
static step = function() {
2023-11-24 10:41:53 +01:00
__step_mask_modifier();
}
2023-11-24 10:41:53 +01:00
static processData = function(_outData, _data, _output_index, _array_index) {
var inSurf = _data[0];
var _outSurf = _outData[0];
2023-01-17 08:11:55 +01:00
if(!is_surface(inSurf)) return [ _outSurf, c_black ];
var lop = ceil(log2(max(surface_get_width_safe(inSurf), surface_get_height_safe(inSurf))));
var side = power(2, lop);
2023-01-17 08:11:55 +01:00
var cc;
if(side / 2 >= 1) {
2023-02-28 09:43:01 +01:00
var _Surf = [ surface_create_valid(side, side), surface_create_valid(side, side) ];
2023-01-17 08:11:55 +01:00
var _ind = 1;
2023-01-25 06:49:00 +01:00
surface_set_shader(_Surf[0], noone);
draw_surface_stretched_safe(inSurf, 0, 0, side, side);
surface_reset_shader();
2023-01-17 08:11:55 +01:00
2023-02-28 09:43:01 +01:00
for( var i = 0; i <= lop; i++ ) {
surface_set_shader(_Surf[_ind], sh_average);
shader_set_f("dimension", side);
draw_surface_safe(_Surf[!_ind]);
surface_reset_shader();
2023-02-28 09:43:01 +01:00
2023-01-17 08:11:55 +01:00
_ind = !_ind;
side /= 2;
}
2023-02-28 09:43:01 +01:00
2023-03-31 06:59:08 +02:00
cc = surface_get_pixel(_Surf[!_ind], 0, 0);
2023-02-14 05:32:32 +01:00
surface_free(_Surf[0]);
surface_free(_Surf[1]);
2023-01-17 08:11:55 +01:00
} else
2023-03-31 06:59:08 +02:00
cc = surface_get_pixel(inSurf, 0, 0);
2023-01-17 08:11:55 +01:00
surface_set_target(_outSurf);
draw_clear(cc);
2023-01-17 08:11:55 +01:00
surface_reset_target();
2023-11-24 10:41:53 +01:00
__process_mask_modifier(_data);
2023-02-14 05:32:32 +01:00
_outSurf = mask_apply(_data[0], _outSurf, _data[1], _data[2]);
_outSurf = channel_apply(_data[0], _outSurf, _data[4]);
2023-02-14 05:32:32 +01:00
return [ _outSurf, cc ];
}
2023-01-17 08:11:55 +01:00
}