Pixel-Composer/scripts/node_trail/node_trail.gml

144 lines
4.8 KiB
Plaintext
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Trail(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2022-01-13 05:24:03 +01:00
name = "Trail";
2023-10-02 14:41:44 +02:00
use_cache = CACHE_USE.manual;
2023-08-05 14:00:33 +02:00
clearCacheOnChange = false;
2023-02-20 10:16:31 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 0] = nodeValue("Surface in", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, 0);
2023-02-20 10:16:31 +01:00
2023-02-23 07:02:19 +01:00
inputs[| 1] = nodeValue("Max life", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 5);
2022-01-13 05:24:03 +01:00
2023-02-23 07:02:19 +01:00
inputs[| 2] = nodeValue("Loop", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false);
2022-01-13 05:24:03 +01:00
2023-02-23 07:02:19 +01:00
inputs[| 3] = nodeValue("Max distance", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, -1, "Maximum distance to search for movement, set to -1 to search the entire image.");
inputs[| 4] = nodeValue("Match color", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true, "Make trail track pixels of the same color, instead of the closet pixels.");
inputs[| 5] = nodeValue("Blend color", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true, "Blend color between two pixel smoothly.");
inputs[| 6] = nodeValue("Alpha over life", self, JUNCTION_CONNECT.input, VALUE_TYPE.curve, CURVE_DEF_11);
2023-02-21 04:48:50 +01:00
2023-02-14 05:32:32 +01:00
outputs[| 0] = nodeValue("Surface out", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
2022-01-13 05:24:03 +01:00
2023-02-21 04:48:50 +01:00
outputs[| 1] = nodeValue("Trail UV", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
2022-01-13 05:24:03 +01:00
input_display_list = [
["Surfaces", true], 0,
2023-02-23 07:02:19 +01:00
["Trail settings", false], 1, 2,
["Tracking", false], 3, 4, 5,
["Modification", false], 6,
2022-01-13 05:24:03 +01:00
];
2023-02-23 07:02:19 +01:00
temp_surface = [ surface_create(1, 1), surface_create(1, 1), surface_create(1, 1) ];
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2023-02-23 07:02:19 +01:00
static step = function() {
var _colr = getInputData(4);
2023-02-23 07:02:19 +01:00
inputs[| 5].setVisible(!_colr);
}
2022-01-13 05:24:03 +01:00
2023-02-19 13:49:20 +01:00
static update = function() {
if(!inputs[| 0].value_from) return;
var _surf = getInputData(0);
var _life = getInputData(1);
var _loop = getInputData(2);
2023-06-17 18:59:20 +02:00
if(!is_real(_loop)) _loop = false;
var _rang = getInputData(3);
var _colr = getInputData(4);
var _blend = getInputData(5);
var _alpha = getInputData(6);
2023-03-19 09:17:39 +01:00
var cDep = attrDepth();
2023-02-19 13:49:20 +01:00
if(!is_surface(_surf)) return;
cacheCurrentFrame(_surf);
2022-01-13 05:24:03 +01:00
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(temp_surface); i < n; i++ ) {
2023-09-08 21:37:36 +02:00
temp_surface[i] = surface_verify(temp_surface[i], surface_get_width_safe(_surf), surface_get_height_safe(_surf), cDep);
2023-02-23 07:02:19 +01:00
surface_set_target(temp_surface[i]);
2023-03-19 09:17:39 +01:00
DRAW_CLEAR
2023-02-21 04:48:50 +01:00
surface_reset_target();
}
2022-01-13 05:24:03 +01:00
2023-02-19 13:49:20 +01:00
var _outSurf = outputs[| 0].getValue();
2023-09-08 21:37:36 +02:00
_outSurf = surface_verify(_outSurf, surface_get_width_safe(_surf), surface_get_height_safe(_surf), cDep);
2023-02-19 13:49:20 +01:00
outputs[| 0].setValue(_outSurf);
2023-02-21 04:48:50 +01:00
var _outUV = outputs[| 1].getValue();
2023-09-08 21:37:36 +02:00
_outUV = surface_verify(_outUV, surface_get_width_safe(_surf), surface_get_height_safe(_surf), cDep);
2023-02-21 04:48:50 +01:00
outputs[| 1].setValue(_outUV);
2022-01-13 05:24:03 +01:00
2023-10-09 16:07:33 +02:00
var curf = CURRENT_FRAME;
2023-02-20 10:16:31 +01:00
var frame_amo = _loop? _life : min(_life, curf);
2023-02-19 13:49:20 +01:00
var st_frame = curf - frame_amo;
2022-01-13 05:24:03 +01:00
2023-08-05 14:00:33 +02:00
BLEND_NORMAL
2023-02-19 13:49:20 +01:00
for(var i = 0; i <= frame_amo; i++) {
var frame_idx = st_frame + i;
2023-08-05 14:00:33 +02:00
var prog = (frame_idx - (curf - _life)) / _life;
var a0 = eval_curve_x(_alpha, 1 - i / (frame_amo + 1));
var a1 = eval_curve_x(_alpha, 1 - (i + 1) / (frame_amo + 1));
2023-02-19 13:49:20 +01:00
2023-10-09 16:07:33 +02:00
if(_loop && frame_idx < 0) frame_idx = TOTAL_FRAMES + frame_idx;
2023-02-20 10:16:31 +01:00
2023-10-09 16:07:33 +02:00
var prev = _loop? safe_mod(frame_idx - 1 + TOTAL_FRAMES, TOTAL_FRAMES) : frame_idx - 1;
2023-02-23 07:02:19 +01:00
var _prevFrame = getCacheFrame(prev);
var _currFrame = getCacheFrame(frame_idx);
2023-02-20 10:16:31 +01:00
2023-02-23 07:02:19 +01:00
if(!is_surface(_currFrame)) continue;
if(!is_surface(_prevFrame)) {
surface_set_target(temp_surface[0]);
2023-08-05 14:00:33 +02:00
draw_surface_ext_safe(_currFrame, 0, 0, 1, 1, 0, c_white, a0);
2023-02-21 04:48:50 +01:00
surface_reset_target();
2023-02-23 07:02:19 +01:00
surface_set_target(temp_surface[2]);
2023-08-05 14:00:33 +02:00
draw_surface_ext_safe(_currFrame, 0, 0, 1, 1, 0, c_white, a1);
2023-02-20 10:16:31 +01:00
surface_reset_target();
continue;
}
2023-08-05 14:00:33 +02:00
shader_set(sh_trail_filler_pass1);
shader_set_dim("dimension", _surf);
2023-09-08 21:37:36 +02:00
shader_set_f("range", _rang? _rang : surface_get_width_safe(_surf) / 2);
2023-08-05 14:00:33 +02:00
shader_set_i("matchColor", _colr);
shader_set_i("blendColor", _blend);
shader_set_f("segmentStart", (frame_amo - i) / frame_amo);
shader_set_f("segmentSize", 1 / frame_amo);
shader_set_surface("prevFrame", _prevFrame);
shader_set_f("alphaPrev", a0);
shader_set_f("alphaCurr", a1);
2022-01-13 05:24:03 +01:00
2023-08-05 14:00:33 +02:00
shader_set_i("mode", 1);
2023-02-23 07:02:19 +01:00
surface_set_target(temp_surface[0]);
2023-08-05 14:00:33 +02:00
draw_surface_safe(_currFrame);
2023-02-21 04:48:50 +01:00
surface_reset_target();
2023-08-05 14:00:33 +02:00
shader_set_i("mode", 0);
2023-02-23 07:02:19 +01:00
surface_set_target(temp_surface[2]);
2023-08-05 14:00:33 +02:00
draw_surface_safe(_currFrame);
2023-02-21 04:48:50 +01:00
surface_reset_target();
2023-02-20 10:16:31 +01:00
shader_reset();
2022-01-13 05:24:03 +01:00
}
2023-08-05 14:00:33 +02:00
BLEND_NORMAL
2022-01-13 05:24:03 +01:00
2023-02-23 07:02:19 +01:00
surface_set_target(temp_surface[1]);
2023-08-05 14:00:33 +02:00
shader_set(sh_trail_filler_pass2);
shader_set_dim("dimension", _surf);
draw_surface_safe(temp_surface[0], 0, 0);
shader_reset();
2023-02-21 04:48:50 +01:00
surface_reset_target();
2023-08-05 14:00:33 +02:00
surface_set_shader(_outUV);
2023-02-23 07:02:19 +01:00
draw_surface_safe(temp_surface[1], 0, 0);
2023-08-05 14:00:33 +02:00
surface_reset_shader();
2023-02-21 04:48:50 +01:00
2023-08-05 14:00:33 +02:00
surface_set_shader(_outSurf);
2023-02-23 07:02:19 +01:00
draw_surface_safe(temp_surface[2], 0, 0);
2023-08-05 14:00:33 +02:00
surface_reset_shader();
2022-01-13 05:24:03 +01:00
}
2023-02-23 07:02:19 +01:00
2022-01-13 05:24:03 +01:00
}