Pixel-Composer/scripts/node_posterize/node_posterize.gml

155 lines
4.6 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Posterize(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2022-01-13 05:24:03 +01:00
name = "Posterize";
2024-08-18 06:16:20 +02:00
newInput(0, nodeValue_Surface("Surface in", self));
2023-01-25 06:49:00 +01:00
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Palette("Palette", self, array_clone(DEF_PALETTE)));
2022-01-13 05:24:03 +01:00
2024-08-18 06:16:20 +02:00
newInput(2, nodeValue_Bool("Use palette", self, true));
2022-01-13 05:24:03 +01:00
2024-08-18 09:13:41 +02:00
newInput(3, nodeValue_Int("Steps", self, 4))
2024-03-24 04:58:08 +01:00
.setDisplay(VALUE_DISPLAY.slider, { range: [2, 16, 0.1] });
2022-01-13 05:24:03 +01:00
2024-08-18 09:13:41 +02:00
newInput(4, nodeValue_Float("Gamma", self, 1))
2024-05-19 07:55:09 +02:00
.setDisplay(VALUE_DISPLAY.slider, { range: [0, 2, 0.01] })
2023-12-23 09:39:55 +01:00
.setMappable(7);
2022-01-13 05:24:03 +01:00
2024-08-18 06:16:20 +02:00
newInput(5, nodeValue_Bool("Active", self, true));
2023-02-14 05:32:32 +01:00
active_index = 5;
2023-08-01 19:21:51 +02:00
2024-08-18 06:16:20 +02:00
newInput(6, nodeValue_Bool("Posterize alpha", self, true));
2023-02-14 05:32:32 +01:00
2023-12-23 09:39:55 +01:00
//////////////////////////////////////////////////////////////////////////////////////////////////
2024-08-18 06:16:20 +02:00
newInput(7, nodeValueMap("Gamma map", self));
2023-12-23 09:39:55 +01:00
//////////////////////////////////////////////////////////////////////////////////////////////////
2024-08-18 06:16:20 +02:00
newInput(8, nodeValue_Enum_Button("Space", self, 0, [ "RGB", "LAB" ]));
2024-05-03 13:40:46 +02:00
2024-01-09 03:39:40 +01:00
input_display_list = [ 5, 0,
2024-05-03 13:40:46 +02:00
["Palette", false, 2], 1, 3, 4, 7, 8,
2024-07-28 13:55:57 +02:00
["Alpha", false], 6,
2022-01-13 05:24:03 +01:00
];
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Surface out", self, VALUE_TYPE.surface, noone));
2022-01-13 05:24:03 +01:00
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2024-05-19 07:55:09 +02:00
temp_surface = [ surface_create(1, 1), surface_create(1, 1), surface_create(1, 1), surface_create(1, 1) ];
2022-01-18 05:31:19 +01:00
static step = function() {
var _use_pal = getInputData(2);
2022-01-13 05:24:03 +01:00
2024-08-08 06:57:51 +02:00
inputs[1].setVisible(_use_pal);
inputs[3].setVisible(!_use_pal);
inputs[4].setVisible(!_use_pal);
inputs[4].mappableStep();
inputs[8].setVisible(_use_pal);
2022-01-13 05:24:03 +01:00
}
2023-08-17 16:56:54 +02:00
static processData = function(_outSurf, _data, _output_index, _array_index) {
2024-05-19 07:55:09 +02:00
var _surf = _data[0];
2024-05-03 13:40:46 +02:00
var _pal = _data[1];
var _use_pal = _data[2];
2023-08-01 19:21:51 +02:00
var _alp = _data[6];
2024-05-03 13:40:46 +02:00
var _spce = _data[8];
2022-01-13 05:24:03 +01:00
2024-05-03 13:40:46 +02:00
if(_use_pal) {
2023-08-01 19:21:51 +02:00
surface_set_shader(_outSurf, sh_posterize_palette);
shader_set_palette(_pal, "palette", "keys");
2023-08-01 19:21:51 +02:00
shader_set_i("alpha", _alp);
2024-05-03 13:40:46 +02:00
shader_set_i("space", _spce);
2023-02-14 05:32:32 +01:00
2024-05-19 07:55:09 +02:00
draw_surface_safe(_surf);
2023-08-01 19:21:51 +02:00
surface_reset_shader();
2022-01-13 05:24:03 +01:00
} else {
2024-05-19 07:55:09 +02:00
#region get range
var _sw = surface_get_width(_surf);
var _sh = surface_get_height(_surf);
var _itr = ceil(logn(4, _sw * _sh / 1024));
var _sww = ceil(_sw / 2);
var _shh = ceil(_sh / 2);
for (var i = 0, n = array_length(temp_surface); i < n; i++)
temp_surface[i] = surface_verify(temp_surface[i], _sw, _sh);
surface_set_shader(temp_surface[0]);
draw_surface_safe(_surf);
2024-05-19 07:55:09 +02:00
surface_reset_shader();
surface_set_shader(temp_surface[1]);
draw_surface_safe(_surf);
2024-05-19 07:55:09 +02:00
surface_reset_shader();
surface_clear(temp_surface[2]);
surface_clear(temp_surface[3]);
var _ind = 1;
repeat(_itr) {
surface_resize(temp_surface[(_ind) * 2 + 0], _sww, _shh);
surface_resize(temp_surface[(_ind) * 2 + 1], _sww, _shh);
shader_set(sh_get_max_downsampled);
surface_set_target_ext(0, temp_surface[(_ind) * 2 + 0]);
surface_set_target_ext(1, temp_surface[(_ind) * 2 + 1]);
shader_set_f("dimension", _sww, _shh);
shader_set_surface("surfaceMax", temp_surface[(!_ind) * 2 + 0]);
shader_set_surface("surfaceMin", temp_surface[(!_ind) * 2 + 1]);
draw_sprite_stretched(s_fx_pixel, 0, 0, 0, _sww, _shh);
surface_reset_target();
shader_reset();
_sww = ceil(_sww / 2);
_shh = ceil(_shh / 2);
_ind = !_ind;
}
var _sMax = temp_surface[(!_ind) * 2 + 0];
var _sMin = temp_surface[(!_ind) * 2 + 1];
var _ssw = surface_get_width(_sMax);
var _ssh = surface_get_height(_sMax);
var _max = [ 0, 0, 0 ];
var _min = [ 1, 1, 1 ];
2024-05-19 07:55:09 +02:00
var _bMax = buffer_from_surface(_sMax, false);
var _bMin = buffer_from_surface(_sMin, false);
buffer_to_start(_bMax);
buffer_to_start(_bMin);
2024-05-19 07:55:09 +02:00
repeat(_ssw * _ssh) {
var _cc = buffer_read(_bMax, buffer_u32);
_max[0] = max(_max[0], _color_get_red(_cc));
_max[1] = max(_max[1], _color_get_green(_cc));
_max[2] = max(_max[2], _color_get_blue(_cc));
var _cc = buffer_read(_bMin, buffer_u32);
_min[0] = min(_min[0], _color_get_red(_cc));
_min[1] = min(_min[1], _color_get_green(_cc));
_min[2] = min(_min[2], _color_get_blue(_cc));
}
2024-05-19 07:55:09 +02:00
buffer_delete(_bMax);
buffer_delete(_bMin);
#endregion
2023-08-01 19:21:51 +02:00
surface_set_shader(_outSurf, sh_posterize);
2024-05-19 07:55:09 +02:00
shader_set_f("cMax", _max);
shader_set_f("cMin", _min);
shader_set_f("colors", _data[3]);
2024-08-08 06:57:51 +02:00
shader_set_f_map("gamma", _data[4], _data[7], inputs[4]);
2023-12-23 09:39:55 +01:00
shader_set_i("alpha", _alp);
2022-01-13 05:24:03 +01:00
2024-05-19 07:55:09 +02:00
draw_surface_safe(_surf);
2023-08-01 19:21:51 +02:00
surface_reset_shader();
2022-01-13 05:24:03 +01:00
}
return _outSurf;
}
}