- [Bake Path] Fix error when setting segment length to higher than 1.

This commit is contained in:
Tanasart 2024-07-16 09:29:46 +07:00
parent ba00cccd40
commit aa93cc5bf2
7 changed files with 465 additions and 351 deletions

View file

@ -1047,6 +1047,7 @@
{"name":"node_scale","order":8,"path":"scripts/node_scale/node_scale.yy",},
{"name":"node_scatter_points","order":6,"path":"scripts/node_scatter_points/node_scatter_points.yy",},
{"name":"node_scatter","order":6,"path":"scripts/node_scatter/node_scatter.yy",},
{"name":"node_segment_filter","order":21,"path":"scripts/node_segment_filter/node_segment_filter.yy",},
{"name":"node_sequence_anim","order":5,"path":"scripts/node_sequence_anim/node_sequence_anim.yy",},
{"name":"node_shadow_cast","order":14,"path":"scripts/node_shadow_cast/node_shadow_cast.yy",},
{"name":"node_shadow","order":13,"path":"scripts/node_shadow/node_shadow.yy",},

View file

@ -1457,6 +1457,7 @@
{"id":{"name":"node_scatter_points","path":"scripts/node_scatter_points/node_scatter_points.yy",},},
{"id":{"name":"node_scatter","path":"scripts/node_scatter/node_scatter.yy",},},
{"id":{"name":"node_sdf","path":"scripts/node_sdf/node_sdf.yy",},},
{"id":{"name":"node_segment_filter","path":"scripts/node_segment_filter/node_segment_filter.yy",},},
{"id":{"name":"node_seperate_shape","path":"scripts/node_seperate_shape/node_seperate_shape.yy",},},
{"id":{"name":"node_sequence_anim","path":"scripts/node_sequence_anim/node_sequence_anim.yy",},},
{"id":{"name":"node_shadow_cast","path":"scripts/node_shadow_cast/node_shadow_cast.yy",},},

View file

@ -135,10 +135,8 @@ function array_overlap(arr0, arr1) {
});
}
function array_empty(arr) {
INLINE
return is_array(arr) && array_length(arr) == 0;
}
function array_empty(arr) { INLINE return is_array(arr) && array_length(arr) == 0; }
function array_invalid(arr) { INLINE return !is_array(arr) || array_length(arr) == 0; }
function array_find(arr, val) {
INLINE

View file

@ -67,9 +67,16 @@ function Node_Line(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) cons
inputs[| 26] = nodeValue("Clamp range", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false );
inputs[| 27] = nodeValue("Data Type", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
.setDisplay(VALUE_DISPLAY.enum_scroll, [ "None", "Path", "Segments" ]);
inputs[| 28] = nodeValue("Segments", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [[]])
.setDisplay(VALUE_DISPLAY.vector)
.setArrayDepth(1);
input_display_list = [
["Output", true], 0, 1,
["Line data", false], 6, 7, 19, 2, 20,
["Line data", false], 27, 6, 7, 28, 19, 2, 20,
["Line settings", false], 17, 3, 11, 12, 8, 25, 9, 26, 13, 14,
["Wiggle", false], 4, 5,
["Render", false], 10, 24, 15, 16,
@ -87,7 +94,7 @@ function Node_Line(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) cons
attribute_surface_depth();
attribute_interpolation();
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) { #region
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
draw_set_color(COLORS._main_accent);
for( var i = 0, n = array_length(lines); i < n; i++ ) {
var points = lines[i];
@ -109,9 +116,9 @@ function Node_Line(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) cons
// draw_circle(x0, y0, 4, false);
}
}
} #endregion
}
static step = function() { #region
static step = function() {
var px = !getInputData(17);
var _tex = inputs[| 18].value_from != noone;
var _flen = getInputData(19);
@ -128,13 +135,19 @@ function Node_Line(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) cons
inputs[| 2].setVisible(!_flen);
inputs[| 20].setVisible( _flen);
} #endregion
static onValueUpdate = function(index = 0) { #region
var _dtype = getInputData(27);
inputs[| 6].setVisible(_dtype == 0);
inputs[| 7].setVisible(_dtype == 1, _dtype == 1);
inputs[| 28].setVisible(_dtype == 2, _dtype == 2);
}
static onValueUpdate = function(index = 0) {
if(index == 11) ds_map_clear(widthMap);
} #endregion
}
static processData = function(_outData, _data, _output_index, _array_index) { #region
static processData = function(_outData, _data, _output_index, _array_index) {
#region data
var _dim = _data[0];
var _bg = _data[1];
@ -142,7 +155,7 @@ function Node_Line(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) cons
var _wid = _data[3];
var _wig = _data[4];
var _sed = _data[5];
var _ang = _data[6] % 360;
var _ang = _data[6];
var _pat = _data[7];
var _ratio = _data[8];
var _shift = _data[9];
@ -156,6 +169,7 @@ function Node_Line(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) cons
var _colP = _data[15];
var _colW = _data[16];
var _1px = _data[17];
var _text = _data[18];
var _fixL = _data[19];
var _segL = _data[20];
@ -168,8 +182,16 @@ function Node_Line(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) cons
var _colb = _data[24];
var _ratInv = _data[25];
var _clamp = _data[26];
var _dtype = _data[27];
var _segs = _data[28];
#endregion
/////// Guard clauses
if(_dtype == 1 && _pat == noone) return _outData;
if(_dtype == 2 && (array_invalid(_segs) || array_invalid(_segs[0]))) return _outData;
if(IS_FIRST_FRAME || inputs[| 11].is_anim)
ds_map_clear(widthMap);
@ -182,16 +204,8 @@ function Node_Line(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) cons
var _rtStr = min(_rangeMin, _rangeMax);
var _rtMax = max(_rangeMin, _rangeMax);
var _use_path = is_struct(_pat);
var _useTex = inputs[| 18].value_from != noone;
if(_useTex) {
_cap = false;
_1px = false;
}
if(_ang < 0) _ang = 360 + _ang;
inputs[| 6].setVisible(!_use_path);
var _useTex = is_surface(_text);
if(_useTex) { _cap = false; _1px = false; }
random_set_seed(_sed);
var _sedIndex = 0;
@ -204,12 +218,56 @@ function Node_Line(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) cons
var _ow, _nw, _oa, _na, _oc, _nc, _owg, _nwg;
var _pathData = [];
if(_use_path) { #region
switch(_dtype) {
case 0 :
_ang = (_ang % 360 + 360) % 360;
var x0, y0, x1, y1;
var _0 = point_rectangle_overlap(_dim[0], _dim[1], (_ang + 180) % 360);
var _1 = point_rectangle_overlap(_dim[0], _dim[1], _ang);
x0 = _0[0]; y0 = _0[1];
x1 = _1[0]; y1 = _1[1];
var _l = point_distance(x0, y0, x1, y1);
var _d = point_direction(x0, y0, x1, y1);
var _od = _d, _nd = _d;
var ww = _rtMax / _seg;
var _total = _rtMax;
var _prog_curr = frac(_shift) - ww;
var _prog = _prog_curr + 1;
var _prog_total = 0;
var points = [];
while(_total > 0) {
if(_prog_curr >= 1) _prog_curr = 0;
else _prog_curr = min(_prog_curr + min(_total, ww), 1);
_prog_total += min(_total, ww);
_nx = x0 + lengthdir_x(_l * _prog_curr, _d);
_ny = y0 + lengthdir_y(_l * _prog_curr, _d);
var wgLen = random1D(_sed + _sedIndex, -_wig, _wig); _sedIndex++;
_nx += lengthdir_x(wgLen, _d + 90);
_ny += lengthdir_y(wgLen, _d + 90);
if(_prog_total > _rtStr) //prevent drawing point before range start.
array_push(points, { x: _nx, y: _ny, prog: _prog_total / _rtMax, progCrop: _prog_curr, weight: 1 });
if(_prog_curr > _prog)
_total -= (_prog_curr - _prog);
_prog = _prog_curr;
_ox = _nx;
_oy = _ny;
}
lines = [ points ];
break;
case 1 :
var lineLen = 1;
if(struct_has(_pat, "getLineCount"))
lineLen = _pat.getLineCount();
if(struct_has(_pat, "getPathData"))
_pathData = _pat.getPathData();
if(struct_has(_pat, "getLineCount")) lineLen = _pat.getLineCount();
if(struct_has(_pat, "getPathData")) _pathData = _pat.getPathData();
lines = array_verify(lines, lineLen);
var _lineAmo = 0;
@ -342,58 +400,55 @@ function Node_Line(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) cons
}
array_resize(lines, _lineAmo);
#endregion
break;
} else { #region
var x0, y0, x1, y1;
var _0 = point_rectangle_overlap(_dim[0], _dim[1], (_ang + 180) % 360);
var _1 = point_rectangle_overlap(_dim[0], _dim[1], _ang);
x0 = _0[0]; y0 = _0[1];
x1 = _1[0]; y1 = _1[1];
case 2 :
if(!is_array(_segs[0][0])) //spreaded single path
_segs = [ _segs ];
lines = array_create(array_length(_segs));
var _l = point_distance(x0, y0, x1, y1);
var _d = point_direction(x0, y0, x1, y1);
var _od = _d, _nd = _d;
for (var i = 0, n = array_length(_segs); i < n; i++) {
var _seg = _segs[i];
if(array_empty(_seg)) continue;
var ww = _rtMax / _seg;
var _total = _rtMax;
var _prog_curr = frac(_shift) - ww;
var _prog = _prog_curr + 1;
var _prog_total = 0;
var points = [];
var _uselen = array_length(_seg[0]) >= 3;
var _lin = array_create(array_length(_seg));
while(_total > 0) {
if(_prog_curr >= 1) _prog_curr = 0;
else _prog_curr = min(_prog_curr + min(_total, ww), 1);
_prog_total += min(_total, ww);
if(_uselen) {
for (var j = 0, m = array_length(_seg); j < m; j++)
_lin[j] = { x: _seg[j][0], y: _seg[j][1], prog: _seg[j][2], progCrop: _seg[j][2], weight: 1 };
_nx = x0 + lengthdir_x(_l * _prog_curr, _d);
_ny = y0 + lengthdir_y(_l * _prog_curr, _d);
} else {
var wgLen = random1D(_sed + _sedIndex, -_wig, _wig); _sedIndex++;
_nx += lengthdir_x(wgLen, _d + 90);
_ny += lengthdir_y(wgLen, _d + 90);
var _l, _len = [ 0 ], _lenTotal = 0;
var ox = _seg[0][0], oy = _seg[0][1], nx, ny;
if(_prog_total > _rtStr) //prevent drawing point before range start.
array_push(points, { x: _nx, y: _ny, prog: _prog_total / _rtMax, progCrop: _prog_curr, weight: 1 });
for (var j = 1, m = array_length(_seg); j < m; j++) {
nx = _seg[j][0];
ny = _seg[j][1];
_l = point_distance(ox, oy, nx, ny);
if(_prog_curr > _prog)
_total -= (_prog_curr - _prog);
_prog = _prog_curr;
_ox = _nx;
_oy = _ny;
_len[j] = _l;
_lenTotal += _l;
ox = nx;
oy = ny;
}
lines = [ points ];
} #endregion
for (var j = 0, m = array_length(_seg); j < m; j++)
_lin[j] = { x: _seg[j][0], y: _seg[j][1], prog: _len[j] / _lenTotal, progCrop: _len[j] / _lenTotal, weight: 1 };
}
#region draw
lines[i] = _lin;
}
break;
}
surface_set_target(_colorPass);
if(_bg) draw_clear_alpha(0, 1);
else DRAW_CLEAR
if(_useTex) { #region
if(_useTex) {
var tex = surface_get_texture(_tex);
shader_set(sh_draw_mapping);
@ -402,7 +457,7 @@ function Node_Line(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) cons
shader_set_2("scale", _texSca);
shader_set_interpolation(_tex);
} #endregion
}
for( var i = 0, n = array_length(lines); i < n; i++ ) {
var points = lines[i];
@ -613,8 +668,7 @@ function Node_Line(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) cons
surface_reset_target();
}
#endregion
return [ _colorPass, _widthPass ];
} #endregion
}
}

View file

@ -1,13 +1,15 @@
function Node_Path_Bake(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Bake Path";
setDimension(96, 48);;
setDimension(96, 48);
inputs[| 0] = nodeValue("Path", self, JUNCTION_CONNECT.input, VALUE_TYPE.pathnode, noone)
.setVisible(true, true);
inputs[| 1] = nodeValue("Segment length", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 1);
outputs[| 0] = nodeValue("Segment", self, JUNCTION_CONNECT.output, VALUE_TYPE.float, [[]])
inputs[| 2] = nodeValue("Spread single path", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true);
outputs[| 0] = nodeValue("Segments", self, JUNCTION_CONNECT.output, VALUE_TYPE.float, [[]])
.setDisplay(VALUE_DISPLAY.vector)
.setArrayDepth(1);
@ -20,7 +22,9 @@ function Node_Path_Bake(_x, _y, _group = noone) : Node(_x, _y, _group) construct
var _segs = outputs[| 0].getValue();
var ox, oy, nx, ny;
if(path_amount == 1) _segs = [ _segs ];
if(array_invalid(_segs) || array_invalid(_segs[0])) return;
if(!is_array(_segs[0][0])) _segs = [ _segs ];
draw_set_color(COLORS._main_icon);
for( var i = 0, n = array_length(_segs); i < n; i++ ) {
@ -41,6 +45,7 @@ function Node_Path_Bake(_x, _y, _group = noone) : Node(_x, _y, _group) construct
static update = function() {
var _path = getInputData(0);
var _dist = getInputData(1);
var _sped = getInputData(2);
if(_path == noone) return;
if(_dist <= 0) return;
@ -54,17 +59,17 @@ function Node_Path_Bake(_x, _y, _group = noone) : Node(_x, _y, _group) construct
for( var i = 0; i < _amo; i++ ) {
var _len = _path.getLength(i);
var _seg = [];
_segs[i] = _seg;
if(_len == 0) continue;
for( var j = 0; j <= _len; j += _dist ) {
_p = _path.getPointDistance(j, i, _p);
_seg[j] = [ _p.x, _p.y ];
array_push(_seg, [ _p.x, _p.y, j / _len ]);
}
}
_segs[i] = _seg;
}
if(_amo == 1) _segs = _segs[0];
if(_sped && _amo == 1) _segs = _segs[0];
outputs[| 0].setValue(_segs);
}

View file

@ -0,0 +1,42 @@
function Node_Segment_Filter(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Segment Filter";
setDimension(96, 48);
inputs[| 0] = nodeValue("Segments", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [[]])
.setDisplay(VALUE_DISPLAY.vector)
.setArrayDepth(1);
inputs[| 1] = nodeValue("Loop", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false);
outputs[| 0] = nodeValue("Segments", self, JUNCTION_CONNECT.output, VALUE_TYPE.float, [[]])
.setVisible(false)
.setArrayDepth(1);
input_display_list = [
["Segments", false], 0,
["Filter", false], 1,
];
path_preview_surface = noone;
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
}
static update = function(frame = CURRENT_FRAME) {
var _segments = getInputData(0);
if(!is_array(_segments) || array_empty(_segments) || !is_array(_segments[0])) return;
if(!is_array(_segments[0][0])) //spreaded single path
_segments = [ _segments ];
}
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
var bbox = drawGetBbox(xx, yy, _s);
}
}

View file

@ -0,0 +1,13 @@
{
"$GMScript":"",
"%Name":"node_segment_filter",
"isCompatibility":false,
"isDnD":false,
"name":"node_segment_filter",
"parent":{
"name":"path",
"path":"folders/nodes/data/value/path.yy",
},
"resourceType":"GMScript",
"resourceVersion":"2.0",
}