Pixel-Composer/scripts/node_sdf/node_sdf.gml

73 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";
uniform_sdf_dim = shader_get_uniform(sh_sdf, "dimension");
uniform_sdf_stp = shader_get_uniform(sh_sdf, "stepSize");
uniform_sdf_sid = shader_get_uniform(sh_sdf, "side");
uniform_dst_sid = shader_get_uniform(sh_sdf_dist, "side");
uniform_dst_dst = shader_get_uniform(sh_sdf_dist, "max_distance");
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
2024-08-08 06:57:51 +02:00
outputs[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,
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
2023-09-14 16:29:39 +02:00
static processData = function(_outSurf, _data, _output_index, _array_index) { #region
2023-02-14 02:48:33 +01:00
var inSurf = _data[0];
var _side = _data[2];
var _dist = _data[3];
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);
2023-07-25 20:12:40 +02:00
shader_set_uniform_f(uniform_sdf_dim, _n, _n );
2023-02-14 02:48:33 +01:00
shader_set_uniform_f(uniform_sdf_stp, stepSize);
shader_set_uniform_i(uniform_sdf_sid, _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);
2023-02-14 02:48:33 +01:00
shader_set_uniform_i(uniform_dst_sid, _side);
shader_set_uniform_f(uniform_dst_dst, _dist);
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-09-14 16:29:39 +02:00
} #endregion
2023-02-14 02:48:33 +01:00
}