Pixel-Composer/scripts/node_path_wave/node_path_wave.gml

206 lines
5.6 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Path_Wave(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2024-01-22 14:23:35 +01:00
name = "Wave Path";
2024-05-02 12:13:35 +02:00
setDimension(96, 48);;
2023-02-14 02:48:33 +01:00
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_PathNode("Path", self, noone))
2023-02-14 02:48:33 +01:00
.setVisible(true, true);
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Range("Frequency", self, [ 4, 4 ], { linked : true }));
2023-02-14 02:48:33 +01:00
2024-08-18 06:16:20 +02:00
newInput(2, nodeValue_Range("Amplitude", self, [ 4, 4 ], { linked : true }));
2023-02-14 02:48:33 +01:00
2024-08-18 06:16:20 +02:00
newInput(3, nodeValue_Range("Shift", self, [ 0, 0 ], { linked : true }));
2023-02-14 02:48:33 +01:00
2024-08-18 06:16:20 +02:00
newInput(4, nodeValue_Bool("Smooth", self, false));
2023-02-14 02:48:33 +01:00
2024-08-18 09:13:41 +02:00
newInput(5, nodeValue_Float("Seed", self, seed_random(6)))
2024-08-08 06:57:51 +02:00
.setDisplay(VALUE_DISPLAY._default, { side_button : button(function() { randomize(); inputs[5].setValue(seed_random(6)); }).setIcon(THEME.icon_random, 0, COLORS._main_icon) });
2024-01-22 14:23:35 +01:00
2024-08-18 06:16:20 +02:00
newInput(6, nodeValue_Bool("Wiggle", self, false));
2024-01-22 14:23:35 +01:00
2024-08-18 06:16:20 +02:00
newInput(7, nodeValue_Range("Wiggle Amplitude", self, [ -2, 2 ]));
2024-01-22 14:23:35 +01:00
2024-08-18 06:16:20 +02:00
newInput(8, nodeValue_Float("Wiggle Frequency", self, 8));
2024-01-22 14:23:35 +01:00
newInput(9, nodeValue_Curve("Amplitude over length", self, CURVE_DEF_11));
2024-01-22 14:23:35 +01:00
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Path", self, VALUE_TYPE.pathnode, self));
2023-02-14 02:48:33 +01:00
2024-01-22 14:23:35 +01:00
input_display_list = [ 5,
2023-02-14 02:48:33 +01:00
["Path", true], 0,
2024-01-22 14:23:35 +01:00
["Wave", false], 1, 2, 9, 3, 4,
["Wiggle", true, 6], 7, 8,
];
path = 0;
fre = 0;
amp = 0;
shf = 0;
smt = 0;
seed = 0;
wig = 0
wigs = 0
wigf = 0
wig_map = noone;
amp_curve = noone;
p0 = new __vec2();
p = new __vec2();
p1 = new __vec2();
2023-02-14 02:48:33 +01:00
cached_pos = ds_map_create();
2024-03-14 14:35:19 +01:00
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) { #region
var _path = getInputData(0);
2024-03-26 11:48:41 +01:00
if(_path && struct_has(_path, "drawOverlay")) _path.drawOverlay(hover, active, _x, _y, _s, _mx, _my, _snx, _sny);
2024-01-22 14:23:35 +01:00
draw_set_color(COLORS._main_icon);
var _amo = getLineCount();
for( var i = 0; i < _amo; i++ ) {
var _len = getLength(i);
var _stp = 1 / clamp(_len * _s, 1, 64);
var ox, oy, nx, ny;
var _p = new __vec2();
for( var j = 0; j < 1; j += _stp ) {
_p = getPointRatio(j, i, _p);
nx = _x + _p.x * _s;
ny = _y + _p.y * _s;
if(j > 0) draw_line_width(ox, oy, nx, ny, 3);
ox = nx;
oy = ny;
}
}
} #endregion
2023-03-07 14:29:47 +01:00
2024-01-22 14:23:35 +01:00
static getLineCount = function() { #region
return struct_has(path, "getLineCount")? path.getLineCount() : 1;
} #endregion
2023-02-28 09:43:01 +01:00
2024-01-22 14:23:35 +01:00
static getSegmentCount = function(ind = 0) { #region
return struct_has(path, "getSegmentCount")? path.getSegmentCount(ind) : 0;
} #endregion
static getLength = function(ind = 0) { #region
var _fre = fre ; _fre = max(_fre[0], _fre[1]);
var _amo = amp ; _amo = max(_amo[0], _amo[1]);
2023-08-02 19:11:57 +02:00
2024-01-22 14:23:35 +01:00
_fre = max(1, abs(_fre));
var _len = struct_has(path, "getLength")? path.getLength(ind) : 0;
_len *= _fre * sqrt(abs(_amo) + 1 / _fre);
2023-08-02 19:11:57 +02:00
return _len;
2024-01-22 14:23:35 +01:00
} #endregion
2023-03-19 09:17:39 +01:00
2024-01-22 14:23:35 +01:00
static getAccuLength = function(ind = 0) { #region
var _fre = fre ; _fre = max(_fre[0], _fre[1]);
var _amo = amp ; _amo = max(_amo[0], _amo[1]);
2023-08-02 19:11:57 +02:00
2024-01-22 14:23:35 +01:00
_fre = max(1, abs(_fre));
var _len = struct_has(path, "getAccuLength")? path.getAccuLength(ind) : [];
var _mul = _fre * sqrt(abs(_amo) + 1 / _fre);
2023-08-02 19:11:57 +02:00
for( var i = 0, n = array_length(_len); i < n; i++ )
_len[i] *= _mul;
2023-03-19 09:17:39 +01:00
2023-08-02 19:11:57 +02:00
return _len;
2024-01-22 14:23:35 +01:00
} #endregion
2023-08-02 19:11:57 +02:00
2024-01-22 14:23:35 +01:00
static getPointRatio = function(_rat, ind = 0, out = undefined) { #region
2023-10-29 06:29:10 +01:00
if(out == undefined) out = new __vec2(); else { out.x = 0; out.y = 0; }
var _cKey = $"{_rat},{ind}";
if(ds_map_exists(cached_pos, _cKey)) {
var _p = cached_pos[? _cKey];
out.x = _p.x;
out.y = _p.y;
return out;
}
2024-01-22 14:23:35 +01:00
var _path = path;
var _fre = fre ;
var _amp = amp ;
var _shf = shf ;
var _smt = smt ;
var _seed = seed + ind;
var _wig = wig ;
var _wigs = wigs;
var _wigf = wigf;
_amp = random_range_seed(_amp[0], _amp[1], _seed + ind);
_shf = random_range_seed(_shf[0], _shf[1], _seed + 1 + ind);
_fre = random_range_seed(_fre[0], _fre[1], _seed + 2 + ind);
_fre = max(0.01, abs(_fre));
var _t = _shf + _rat * _fre;
if(_wig) {
var _w = wiggle(_wigs[0], _wigs[1], _wigf, _t, _seed);
_amp += _w;
}
2023-02-14 02:48:33 +01:00
2023-03-07 14:29:47 +01:00
if(is_array(_path)) {
2024-03-31 05:36:11 +02:00
_path = array_safe_get_fast(_path, ind);
2023-03-07 14:29:47 +01:00
ind = 0;
}
2023-02-14 02:48:33 +01:00
if(!is_struct(_path) || !struct_has(_path, "getPointRatio"))
2023-10-29 06:29:10 +01:00
return out;
2023-02-14 02:48:33 +01:00
2024-01-22 14:23:35 +01:00
p0 = _path.getPointRatio(clamp(_rat - 0.001, 0, 0.999999), ind, p0);
p = _path.getPointRatio(_rat, ind, p);
p1 = _path.getPointRatio(clamp(_rat + 0.001, 0, 0.999999), ind, p1);
2023-02-14 02:48:33 +01:00
2024-01-22 14:23:35 +01:00
var dir = point_direction(p0.x, p0.y, p1.x, p1.y) + 90;
2023-02-14 02:48:33 +01:00
var prg;
2024-01-22 14:23:35 +01:00
if(_smt) prg = cos(_t * pi * 2);
else prg = (abs(frac(_t) * 2 - 1) - 0.5) * 2;
2023-02-14 02:48:33 +01:00
2024-01-22 14:23:35 +01:00
if(amp_curve) prg *= amp_curve.get(_rat);
out.x = p.x + lengthdir_x(_amp * prg, dir);
out.y = p.y + lengthdir_y(_amp * prg, dir);
2023-02-14 02:48:33 +01:00
cached_pos[? _cKey] = out.clone();
2023-10-29 06:29:10 +01:00
return out;
2024-01-22 14:23:35 +01:00
} #endregion
2023-02-14 02:48:33 +01:00
2023-10-29 06:29:10 +01:00
static getPointDistance = function(_dist, ind = 0, out = undefined) { return getPointRatio(_dist / getLength(), ind, out); }
2023-03-19 09:17:39 +01:00
2024-01-22 14:23:35 +01:00
static getBoundary = function(ind = 0) { #region
return struct_has(path, "getBoundary")? path.getBoundary(ind) : new BoundingBox( 0, 0, 1, 1 );
} #endregion
static update = function() { #region
ds_map_clear(cached_pos);
2024-01-22 14:23:35 +01:00
path = getInputData(0);
fre = getInputData(1);
amp = getInputData(2);
shf = getInputData(3);
smt = getInputData(4);
seed = getInputData(5);
wig = getInputData(6);
wigs = getInputData(7);
wigf = getInputData(8);
var _ampc = getInputData(9);
amp_curve = new curveMap(_ampc, 128);
2024-08-08 06:57:51 +02:00
outputs[0].setValue(self);
2024-01-22 14:23:35 +01:00
} #endregion
2023-02-14 02:48:33 +01:00
2024-01-22 14:23:35 +01:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region
2023-02-14 02:48:33 +01:00
var bbox = drawGetBbox(xx, yy, _s);
2023-02-14 11:40:24 +01:00
draw_sprite_fit(s_node_path_wave, 0, bbox.xc, bbox.yc, bbox.w, bbox.h);
2024-01-22 14:23:35 +01:00
} #endregion
2023-02-14 02:48:33 +01:00
}