Pixel-Composer/scripts/node_wrap/node_wrap.gml

254 lines
8.4 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Warp(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2022-01-13 05:24:03 +01:00
name = "Warp";
2023-02-14 05:32:32 +01:00
inputs[| 0] = nodeValue("Surface in", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, 0);
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 1] = nodeValue("Top left", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 0 ] )
2022-12-27 04:00:50 +01:00
.setDisplay(VALUE_DISPLAY.vector)
2022-12-27 13:30:02 +01:00
.setUnitRef(function(index) { return getDimension(index); });
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 2] = nodeValue("Top right", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ def_surf_size, 0 ] )
2022-12-27 04:00:50 +01:00
.setDisplay(VALUE_DISPLAY.vector)
2022-12-27 13:30:02 +01:00
.setUnitRef(function(index) { return getDimension(index); });
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 3] = nodeValue("Bottom left", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, def_surf_size ] )
2022-12-27 04:00:50 +01:00
.setDisplay(VALUE_DISPLAY.vector)
2022-12-27 13:30:02 +01:00
.setUnitRef(function(index) { return getDimension(index); });
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 4] = nodeValue("Bottom right", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ def_surf_size, def_surf_size ] )
2022-12-27 04:00:50 +01:00
.setDisplay(VALUE_DISPLAY.vector)
2022-12-27 13:30:02 +01:00
.setUnitRef(function(index) { return getDimension(index); });
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 5] = nodeValue("Active", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true);
active_index = 5;
outputs[| 0] = nodeValue("Surface out", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
input_display_list = [ 5,
["Surface", false], 0,
["Wrap", false], 1, 2, 3, 4
]
2022-01-13 05:24:03 +01:00
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2023-03-21 03:01:53 +01:00
attribute_interpolation();
2023-03-19 09:17:39 +01:00
2022-01-13 05:24:03 +01:00
drag_side = -1;
2022-12-19 13:35:30 +01:00
drag_mx = 0;
drag_my = 0;
drag_s = [[0, 0], [0, 0]];
2022-01-13 05:24:03 +01:00
2022-12-19 13:35:30 +01:00
static drawOverlay = function(active, _x, _y, _s, _mx, _my, _snx, _sny) {
2022-01-13 05:24:03 +01:00
if(array_length(current_data) < ds_list_size(inputs)) return;
var _surf = outputs[| 0].getValue();
if(is_array(_surf)) {
if(array_length(_surf) == 0) return;
_surf = _surf[preview_index];
2022-01-13 05:24:03 +01:00
}
var tl = current_data[1];
var tr = current_data[2];
var bl = current_data[3];
var br = current_data[4];
2022-12-19 13:35:30 +01:00
tl[0] = _x + tl[0] * _s;
tr[0] = _x + tr[0] * _s;
bl[0] = _x + bl[0] * _s;
br[0] = _x + br[0] * _s;
2022-01-13 05:24:03 +01:00
2022-12-19 13:35:30 +01:00
tl[1] = _y + tl[1] * _s;
tr[1] = _y + tr[1] * _s;
bl[1] = _y + bl[1] * _s;
br[1] = _y + br[1] * _s;
2022-01-13 05:24:03 +01:00
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS._main_accent);
2022-01-13 05:24:03 +01:00
draw_line(tl[0], tl[1], tr[0], tr[1]);
draw_line(tl[0], tl[1], bl[0], bl[1]);
draw_line(br[0], br[1], tr[0], tr[1]);
draw_line(br[0], br[1], bl[0], bl[1]);
2022-12-19 13:35:30 +01:00
if(inputs[| 1].drawOverlay(active, _x, _y, _s, _mx, _my, _snx, _sny)) active = false;
if(inputs[| 2].drawOverlay(active, _x, _y, _s, _mx, _my, _snx, _sny)) active = false;
if(inputs[| 3].drawOverlay(active, _x, _y, _s, _mx, _my, _snx, _sny)) active = false;
if(inputs[| 4].drawOverlay(active, _x, _y, _s, _mx, _my, _snx, _sny)) active = false;
2022-01-13 05:24:03 +01:00
2022-09-21 06:09:40 +02:00
var dx = 0;
var dy = 0;
2022-01-13 05:24:03 +01:00
if(drag_side > -1) {
2022-12-19 13:35:30 +01:00
dx = (_mx - drag_mx) / _s;
dy = (_my - drag_my) / _s;
2022-01-13 05:24:03 +01:00
2022-12-10 05:06:01 +01:00
if(mouse_release(mb_left)) {
2022-01-13 05:24:03 +01:00
drag_side = -1;
UNDO_HOLDING = false;
}
}
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS.node_overlay_gizmo_inactive);
2022-01-13 05:24:03 +01:00
if(drag_side == 0) {
draw_line_width(tl[0], tl[1], tr[0], tr[1], 3);
2022-12-19 13:35:30 +01:00
var _tlx = value_snap(drag_s[0][0] + dx, _snx);
var _tly = value_snap(drag_s[0][1] + dy, _sny);
2022-01-13 05:24:03 +01:00
2022-12-19 13:35:30 +01:00
var _trx = value_snap(drag_s[1][0] + dx, _snx);
var _try = value_snap(drag_s[1][1] + dy, _sny);
2022-01-13 05:24:03 +01:00
inputs[| 1].setValue([ _tlx, _tly ])
if(inputs[| 2].setValue([ _trx, _try ])) UNDO_HOLDING = true;
} else if(drag_side == 1) {
draw_line_width(tl[0], tl[1], bl[0], bl[1], 3);
2022-12-19 13:35:30 +01:00
var _tlx = value_snap(drag_s[0][0] + dx, _snx);
var _tly = value_snap(drag_s[0][1] + dy, _sny);
var _blx = value_snap(drag_s[1][0] + dx, _snx);
var _bly = value_snap(drag_s[1][1] + dy, _sny);
2022-01-13 05:24:03 +01:00
inputs[| 1].setValue([ _tlx, _tly ]);
if(inputs[| 3].setValue([ _blx, _bly ])) UNDO_HOLDING = true;
} else if(drag_side == 2) {
draw_line_width(br[0], br[1], tr[0], tr[1], 3);
2022-12-19 13:35:30 +01:00
var _brx = value_snap(drag_s[0][0] + dx, _snx);
var _bry = value_snap(drag_s[0][1] + dy, _sny);
var _trx = value_snap(drag_s[1][0] + dx, _snx);
var _try = value_snap(drag_s[1][1] + dy, _sny);
2022-01-13 05:24:03 +01:00
inputs[| 4].setValue([ _brx, _bry ]);
if(inputs[| 2].setValue([ _trx, _try ])) UNDO_HOLDING = true;
} else if(drag_side == 3) {
draw_line_width(br[0], br[1], bl[0], bl[1], 3);
2022-12-19 13:35:30 +01:00
var _brx = value_snap(drag_s[0][0] + dx, _snx);
var _bry = value_snap(drag_s[0][1] + dy, _sny);
var _blx = value_snap(drag_s[1][0] + dx, _snx);
var _bly = value_snap(drag_s[1][1] + dy, _sny);
2022-01-13 05:24:03 +01:00
inputs[| 4].setValue([ _brx, _bry ]);
if(inputs[| 3].setValue([ _blx, _bly ])) UNDO_HOLDING = true;
2022-12-19 13:35:30 +01:00
} else if(active) {
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS._main_accent);
2022-01-13 05:24:03 +01:00
if(distance_to_line_infinite(_mx, _my, tl[0], tl[1], tr[0], tr[1]) < 12) {
draw_line_width(tl[0], tl[1], tr[0], tr[1], 3);
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, active)) {
2022-01-13 05:24:03 +01:00
drag_side = 0;
2022-12-19 13:35:30 +01:00
drag_mx = _mx;
drag_my = _my;
drag_s = [ current_data[1], current_data[2] ];
2022-01-13 05:24:03 +01:00
}
} else if(distance_to_line_infinite(_mx, _my, tl[0], tl[1], bl[0], bl[1]) < 12) {
draw_line_width(tl[0], tl[1], bl[0], bl[1], 3);
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, active)) {
2022-01-13 05:24:03 +01:00
drag_side = 1;
2022-12-19 13:35:30 +01:00
drag_mx = _mx;
drag_my = _my;
drag_s = [ current_data[1], current_data[3] ];
2022-01-13 05:24:03 +01:00
}
} else if(distance_to_line_infinite(_mx, _my, br[0], br[1], tr[0], tr[1]) < 12) {
draw_line_width(br[0], br[1], tr[0], tr[1], 3);
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, active)) {
2022-01-13 05:24:03 +01:00
drag_side = 2;
2022-12-19 13:35:30 +01:00
drag_mx = _mx;
drag_my = _my;
drag_s = [ current_data[4], current_data[2] ];
2022-01-13 05:24:03 +01:00
}
} else if(distance_to_line_infinite(_mx, _my, br[0], br[1], bl[0], bl[1]) < 12) {
draw_line_width(br[0], br[1], bl[0], bl[1], 3);
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, active)) {
2022-01-13 05:24:03 +01:00
drag_side = 3;
2022-12-19 13:35:30 +01:00
drag_mx = _mx;
drag_my = _my;
drag_s = [ current_data[4], current_data[3] ];
2022-01-13 05:24:03 +01:00
}
}
}
2022-12-19 13:35:30 +01:00
inputs[| 1].drawOverlay(active, _x, _y, _s, _mx, _my, _snx, _sny);
inputs[| 2].drawOverlay(active, _x, _y, _s, _mx, _my, _snx, _sny);
inputs[| 3].drawOverlay(active, _x, _y, _s, _mx, _my, _snx, _sny);
inputs[| 4].drawOverlay(active, _x, _y, _s, _mx, _my, _snx, _sny);
2022-01-13 05:24:03 +01:00
}
2023-01-01 02:06:02 +01:00
static process_data = function(_outSurf, _data, _output_index, _array_index) {
2022-01-13 05:24:03 +01:00
var tl = _data[1];
var tr = _data[2];
var bl = _data[3];
var br = _data[4];
2023-03-29 15:02:03 +02:00
var sw = surface_get_width(_data[0]);
var sh = surface_get_height(_data[0]);
var teq = round(tl[1]) == round(tr[1]);
var beq = round(bl[1]) == round(br[1]);
var leq = round(tl[0]) == round(bl[0]);
var req = round(tr[0]) == round(br[0]);
if(teq && beq && leq && req) {
surface_set_shader(_outSurf)
shader_set_interpolation(_data[0]);
draw_surface_stretched_safe(_data[0], tl[0], tl[1], tr[0] - tl[0], bl[1] - tl[1]);
surface_reset_shader();
} else {
surface_set_shader(_outSurf, sh_warp_4points);
shader_set_interpolation(_data[0]);
shader_set_f("p0", br[0] / sw, br[1] / sh);
shader_set_f("p1", tr[0] / sw, tr[1] / sh);
shader_set_f("p2", tl[0] / sw, tl[1] / sh);
shader_set_f("p3", bl[0] / sw, bl[1] / sh);
2022-01-13 05:24:03 +01:00
2023-03-29 15:02:03 +02:00
draw_surface(_data[0], 0, 0);
surface_reset_shader();
}
//surface_set_shader(_outSurf);
//shader_set_interpolation(_data[0]);
// draw_set_color(c_white);
2022-01-13 05:24:03 +01:00
2023-03-29 15:02:03 +02:00
// var tex = surface_get_texture(_data[0]);
// draw_primitive_begin_texture(pr_trianglestrip, tex);
2022-01-13 05:24:03 +01:00
2023-03-29 15:02:03 +02:00
// var res = 4;
// var _i0, _i1, _j0, _j1;
// var tl_x = tl[0];
// var tl_y = tl[1];
// var tr_x = tr[0];
// var tr_y = tr[1];
// var bl_x = bl[0];
// var bl_y = bl[1];
// var br_x = br[0];
// var br_y = br[1];
// for( var i = 0; i < res; i++ ) {
// for( var j = 0; j < res; j++ ) {
// _i0 = i / res;
// _i1 = (i + 1) / res;
// _j0 = j / res;
// _j1 = (j + 1) / res;
2022-01-13 05:24:03 +01:00
2023-03-29 15:02:03 +02:00
// var _tlx = lerp(lerp(tl_x, tr_x, _i0), lerp(bl_x, br_x, _i0), _j0);
// var _tly = lerp(lerp(tl_y, tr_y, _i0), lerp(bl_y, br_y, _i0), _j0);
// var _trx = lerp(lerp(tl_x, tr_x, _i1), lerp(bl_x, br_x, _i1), _j0);
// var _try = lerp(lerp(tl_y, tr_y, _i1), lerp(bl_y, br_y, _i1), _j0);
2022-01-13 05:24:03 +01:00
2023-03-29 15:02:03 +02:00
// var _blx = lerp(lerp(tl_x, tr_x, _i0), lerp(bl_x, br_x, _i0), _j1);
// var _bly = lerp(lerp(tl_y, tr_y, _i0), lerp(bl_y, br_y, _i0), _j1);
// var _brx = lerp(lerp(tl_x, tr_x, _i1), lerp(bl_x, br_x, _i1), _j1);
// var _bry = lerp(lerp(tl_y, tr_y, _i1), lerp(bl_y, br_y, _i1), _j1);
2022-01-13 05:24:03 +01:00
2023-03-29 15:02:03 +02:00
// draw_vertex_texture(_tlx, _tly, _i0, _j0);
// draw_vertex_texture(_trx, _try, _i1, _j0);
2022-01-13 05:24:03 +01:00
2023-03-29 15:02:03 +02:00
// draw_vertex_texture(_blx, _bly, _i0, _j1);
// draw_vertex_texture(_brx, _bry, _i1, _j1);
// }
// }
// draw_primitive_end();
//surface_reset_shader();
2022-01-13 05:24:03 +01:00
return _outSurf;
}
}