Pixel-Composer/scripts/node_sdf/node_sdf.gml

79 lines
2.2 KiB
Plaintext
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_SDF(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2023-02-14 02:48:33 +01:00
name = "SDF";
2024-08-18 06:16:20 +02:00
newInput(0, nodeValue_Surface("Surface in", self));
2023-02-14 02:48:33 +01:00
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Bool("Active", self, true));
2023-02-14 02:48:33 +01:00
active_index = 1;
2024-08-18 06:16:20 +02:00
newInput(2, nodeValue_Enum_Button("Side", self, 2, [ "Inside", "Outside", "Both" ]));
2023-02-14 02:48:33 +01:00
2024-08-18 09:13:41 +02:00
newInput(3, nodeValue_Float("Max distance", self, 1))
.setDisplay(VALUE_DISPLAY.slider, { range: [ 0, 2, 0.01 ] });
2023-02-14 02:48:33 +01:00
newInput(4, nodeValue_Bool("Keep Alpha", self, false));
newInput(5, nodeValue_Bool("Invert", self, false));
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Surface out", self, VALUE_TYPE.surface, noone));
2023-02-14 02:48:33 +01:00
input_display_list = [ 1,
["Surfaces", false], 0,
["SDF", false], 2, 3,
["Render", false], 4, 5,
2023-02-14 02:48:33 +01:00
]
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2023-02-23 07:02:19 +01:00
temp_surface = [ surface_create(1, 1), surface_create(1, 1) ];
2023-02-14 02:48:33 +01:00
static processData = function(_outSurf, _data, _output_index, _array_index) {
2023-02-14 02:48:33 +01:00
var inSurf = _data[0];
var _side = _data[2];
var _dist = _data[3];
var _alph = _data[4];
var _invt = _data[5];
2023-09-08 21:37:36 +02:00
var sw = surface_get_width_safe(inSurf);
var sh = surface_get_height_safe(inSurf);
2023-07-25 20:12:40 +02:00
var _n = max(sw, sh);
2023-03-19 09:17:39 +01:00
var cDep = attrDepth();
2023-02-14 02:48:33 +01:00
2023-07-25 20:12:40 +02:00
temp_surface[0] = surface_verify(temp_surface[0], _n, _n, cDep);
temp_surface[1] = surface_verify(temp_surface[1], _n, _n, cDep);
2023-03-19 09:17:39 +01:00
_outSurf = surface_verify(_outSurf, sw, sh, cDep);
2023-02-14 02:48:33 +01:00
2023-11-15 02:35:30 +01:00
surface_set_shader(temp_surface[0], sh_sdf_tex);
draw_surface_safe(inSurf);
2023-11-15 02:35:30 +01:00
surface_reset_shader();
2023-02-14 02:48:33 +01:00
2023-09-14 16:29:39 +02:00
var _step = ceil(log2(_n));
var stepSize = power(2, _step);
var bg = 0;
2023-02-14 02:48:33 +01:00
2023-09-14 16:29:39 +02:00
repeat(_step) {
2023-02-14 02:48:33 +01:00
stepSize /= 2;
bg = !bg;
2023-11-15 02:35:30 +01:00
surface_set_shader(temp_surface[bg], sh_sdf);
shader_set_f("dimension", _n, _n);
shader_set_f("stepSize", stepSize);
shader_set_i("side", _side);
draw_surface_safe(temp_surface[!bg]);
2023-11-15 02:35:30 +01:00
surface_reset_shader();
2023-02-14 02:48:33 +01:00
}
2023-11-15 02:35:30 +01:00
surface_set_shader(_outSurf, sh_sdf_dist);
shader_set_surface("original", inSurf);
shader_set_i("side", _side);
shader_set_f("max_distance", _dist);
shader_set_i("alpha", _alph);
shader_set_i("invert", _invt);
draw_surface_safe(temp_surface[bg]);
2023-11-15 02:35:30 +01:00
surface_reset_shader();
2023-02-14 02:48:33 +01:00
return _outSurf;
}
2023-02-14 02:48:33 +01:00
}