Pixel-Composer/scripts/node_dither/node_dither.gml

146 lines
5.1 KiB
Plaintext
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Dither(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2022-01-13 05:24:03 +01:00
static dither2 = [ 0, 2,
3, 1 ];
static dither4 = [ 0, 8, 2, 10,
12, 4, 14, 6,
3, 11, 1, 9,
15, 7, 13, 5];
static dither8 = [ 0, 32, 8, 40, 2, 34, 10, 42,
48, 16, 56, 24, 50, 18, 58, 26,
12, 44, 4, 36, 14, 46, 6, 38,
60, 28, 52, 20, 62, 30, 54, 22,
3, 35, 11, 43, 1, 33, 9, 41,
51, 19, 59, 27, 49, 17, 57, 25,
15, 47, 7, 39, 13, 45, 5, 37,
63, 31, 55, 23, 61, 29, 53, 21];
name = "Dither";
2023-02-14 05:32:32 +01:00
inputs[| 0] = nodeValue("Surface in", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, 0);
2023-07-21 12:40:20 +02:00
inputs[| 1] = nodeValue("Palette", self, JUNCTION_CONNECT.input, VALUE_TYPE.color, DEF_PALETTE )
2022-01-13 05:24:03 +01:00
.setDisplay(VALUE_DISPLAY.palette);
2023-02-14 05:32:32 +01:00
inputs[| 2] = nodeValue("Pattern", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
2022-01-13 05:24:03 +01:00
.setDisplay(VALUE_DISPLAY.enum_scroll, [ "2 x 2 Bayer", "4 x 4 Bayer", "8 x 8 Bayer", "Custom" ]);
2023-02-14 05:32:32 +01:00
inputs[| 3] = nodeValue("Dither map", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, 0)
2023-01-01 02:06:02 +01:00
.setVisible(false);
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 4] = nodeValue("Contrast", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 1)
2022-01-13 05:24:03 +01:00
.setDisplay(VALUE_DISPLAY.slider, [1, 5, 0.1]);
2023-02-14 05:32:32 +01:00
inputs[| 5] = nodeValue("Contrast map", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, 0);
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 6] = nodeValue("Mode", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
2023-01-01 02:06:02 +01:00
.setDisplay(VALUE_DISPLAY.enum_button, [ "Color", "Alpha" ]);
2023-02-14 05:32:32 +01:00
inputs[| 7] = nodeValue("Mask", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, 0);
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 8] = nodeValue("Mix", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 1)
.setDisplay(VALUE_DISPLAY.slider, [0, 1, 0.01]);
inputs[| 9] = nodeValue("Active", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true);
active_index = 9;
outputs[| 0] = nodeValue("Surface out", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
input_display_list = [ 9,
2023-05-03 21:42:17 +02:00
["Output", true], 0, 7, 8,
2023-01-01 02:06:02 +01:00
["Pattern", false], 2, 3,
["Dither", false], 6, 1, 4, 5
]
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2023-08-17 16:56:54 +02:00
static processData = function(_outSurf, _data, _output_index, _array_index) {
2022-01-13 05:24:03 +01:00
var _pal = _data[1];
var _typ = _data[2];
var _map = _data[3];
var _con = _data[4];
var _conMap = _data[5];
2023-01-01 02:06:02 +01:00
var _mode = _data[6];
2022-01-13 05:24:03 +01:00
var _colors = array_create(array_length(_pal) * 4);
for(var i = 0; i < array_length(_pal); i++) {
_colors[i * 4 + 0] = color_get_red(_pal[i]) / 255;
_colors[i * 4 + 1] = color_get_green(_pal[i]) / 255;
_colors[i * 4 + 2] = color_get_blue(_pal[i]) / 255;
_colors[i * 4 + 3] = 1;
}
2023-01-01 02:06:02 +01:00
shader = _mode? sh_alpha_hash : sh_dither;
uniform_dither_size = shader_get_uniform(shader, "ditherSize");
uniform_dither = shader_get_uniform(shader, "dither");
uniform_dim = shader_get_uniform(shader, "dimension");
uniform_color = shader_get_uniform(shader, "palette");
uniform_key = shader_get_uniform(shader, "keys");
uniform_constrast = shader_get_uniform(shader, "contrast");
uniform_con_map_use = shader_get_uniform(shader, "useConMap");
uniform_con_map = shader_get_sampler_index(shader, "conMap");
uniform_map_use = shader_get_uniform(shader, "useMap");
uniform_map = shader_get_sampler_index(shader, "map");
uniform_map_dim = shader_get_uniform(shader, "mapDimension");
inputs[| 3].setVisible(_typ == 3);
inputs[| 1].setVisible(_mode == 0);
inputs[| 4].setVisible(_mode == 0);
inputs[| 5].setVisible(_mode == 0);
2022-01-13 05:24:03 +01:00
surface_set_target(_outSurf);
2023-03-19 09:17:39 +01:00
DRAW_CLEAR
2023-02-14 05:32:32 +01:00
BLEND_OVERRIDE;
2022-01-13 05:24:03 +01:00
2022-12-21 02:30:23 +01:00
shader_set(shader);
2023-01-01 02:06:02 +01:00
2023-02-14 05:32:32 +01:00
shader_set_uniform_f_array_safe(uniform_dim, [ surface_get_width(_data[0]), surface_get_height(_data[0]) ] );
2023-01-01 02:06:02 +01:00
2022-01-13 05:24:03 +01:00
switch(_typ) {
case 0 :
shader_set_uniform_i(uniform_map_use, 0);
shader_set_uniform_f(uniform_dither_size, 2);
2023-02-14 05:32:32 +01:00
shader_set_uniform_f_array_safe(uniform_dither, dither2);
2022-01-13 05:24:03 +01:00
break;
case 1 :
shader_set_uniform_i(uniform_map_use, 0);
shader_set_uniform_f(uniform_dither_size, 4);
2023-02-14 05:32:32 +01:00
shader_set_uniform_f_array_safe(uniform_dither, dither4);
2022-01-13 05:24:03 +01:00
break;
case 2 :
shader_set_uniform_i(uniform_map_use, 0);
shader_set_uniform_f(uniform_dither_size, 8);
2023-02-14 05:32:32 +01:00
shader_set_uniform_f_array_safe(uniform_dither, dither8);
2022-01-13 05:24:03 +01:00
break;
case 3 :
if(is_surface(_map)) {
shader_set_uniform_i(uniform_map_use, 1);
2023-02-14 05:32:32 +01:00
shader_set_uniform_f_array_safe(uniform_map_dim, [ surface_get_width(_map), surface_get_height(_map) ]);
2022-01-13 05:24:03 +01:00
texture_set_stage(uniform_map, surface_get_texture(_map));
}
break;
}
2023-01-01 02:06:02 +01:00
if(_mode == 0) {
shader_set_uniform_i(uniform_con_map_use, _conMap == DEF_SURFACE? 0 : 1);
texture_set_stage(uniform_con_map, surface_get_texture(_conMap));
shader_set_uniform_f(uniform_constrast, _con);
2023-02-14 05:32:32 +01:00
shader_set_uniform_f_array_safe(uniform_color, _colors);
2023-01-01 02:06:02 +01:00
shader_set_uniform_i(uniform_key, array_length(_pal));
}
2022-01-13 05:24:03 +01:00
draw_surface_safe(_data[0], 0, 0);
shader_reset();
2023-02-14 05:32:32 +01:00
BLEND_NORMAL;
2022-01-13 05:24:03 +01:00
surface_reset_target();
2023-02-14 05:32:32 +01:00
_outSurf = mask_apply(_data[0], _outSurf, _data[7], _data[8]);
2022-12-27 04:00:50 +01:00
return _outSurf;
2022-01-13 05:24:03 +01:00
}
}