Pixel-Composer/scripts/node_shape_polygon/node_shape_polygon.gml

324 lines
9.3 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Shape_Polygon(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2024-06-20 08:55:04 +02:00
name = "Draw Shape Polygon";
2023-02-19 13:49:20 +01:00
shapesArray = [ "Rectangle", "Ellipse", "Star", "Capsule", "Ring", "Arc", "Gear", "Cross" ];
for( var i = 0, n = array_length(shapesArray); i < n; i++ )
shapesArray[i] = new scrollItem(shapesArray[i], s_node_shape_poly_type, i);
2024-08-18 06:16:20 +02:00
newInput(0, nodeValue_Dimension(self));
2023-02-19 13:49:20 +01:00
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Bool("Background", self, false));
2023-02-19 13:49:20 +01:00
2024-08-18 06:16:20 +02:00
newInput(2, nodeValue_Color("Background color", self, c_black));
2023-02-19 13:49:20 +01:00
2024-08-18 06:16:20 +02:00
newInput(3, nodeValue_Color("Shape color", self, c_white));
2023-02-19 13:49:20 +01:00
2024-08-18 06:16:20 +02:00
newInput(4, nodeValue_Enum_Scroll("Shape", self, 0, shapesArray));
2023-02-19 13:49:20 +01:00
2024-08-18 09:13:41 +02:00
newInput(5, nodeValue_Vec2("Position", self, [ 0.5, 0.5 ]))
2023-02-19 13:49:20 +01:00
.setUnitRef(function(index) { return getDimension(index); }, VALUE_UNIT.reference);
2024-08-18 06:16:20 +02:00
newInput(6, nodeValue_Rotation("Rotation", self, 0));
2023-02-19 13:49:20 +01:00
2024-08-18 09:13:41 +02:00
newInput(7, nodeValue_Vec2("Scale", self, [ 0.5, 0.5 ]))
2023-02-19 13:49:20 +01:00
.setUnitRef(function(index) { return getDimension(index); }, VALUE_UNIT.reference);
2024-08-18 09:13:41 +02:00
newInput(8, nodeValue_Int("Sides", self, 16))
2024-03-24 04:58:08 +01:00
.setDisplay(VALUE_DISPLAY.slider, { range: [2, 64, 0.1] });
2023-02-19 13:49:20 +01:00
2024-08-18 09:13:41 +02:00
newInput(9, nodeValue_Float("Inner radius", self, 0.5))
.setDisplay(VALUE_DISPLAY.slider);
2023-02-19 13:49:20 +01:00
2024-08-18 09:13:41 +02:00
newInput(10, nodeValue_Float("Radius", self, 0.5))
.setDisplay(VALUE_DISPLAY.slider);
2023-02-19 13:49:20 +01:00
2024-08-18 09:13:41 +02:00
newInput(11, nodeValue_Int("Teeth", self, 6))
2024-03-24 04:58:08 +01:00
.setDisplay(VALUE_DISPLAY.slider, { range: [3, 16, 0.1] });
2023-02-19 13:49:20 +01:00
2024-08-18 09:13:41 +02:00
newInput(12, nodeValue_Float("Teeth height", self, 0.2))
.setDisplay(VALUE_DISPLAY.slider);
2023-02-19 13:49:20 +01:00
2024-08-18 09:13:41 +02:00
newInput(13, nodeValue_Float("Teeth taper", self, 0))
.setDisplay(VALUE_DISPLAY.slider, { range: [-0.5, 0.5, 0.01] });
2023-02-19 13:49:20 +01:00
2024-08-18 06:16:20 +02:00
newInput(14, nodeValue_Rotation_Range("Angle range", self, [ 0, 360 ]));
2023-02-19 13:49:20 +01:00
2024-08-18 06:16:20 +02:00
newInput(15, nodeValue_Bool("Round cap", self, false));
2023-03-19 09:17:39 +01:00
2024-08-20 10:15:53 +02:00
newInput(16, nodeValue("Mesh", self, CONNECT_TYPE.input, VALUE_TYPE.mesh, noone))
2023-03-19 09:17:39 +01:00
.setVisible(true, true);
2023-02-19 13:49:20 +01:00
newInput(17, nodeValue_Float("Explode", self, 0))
.setDisplay(VALUE_DISPLAY.slider, { range: [-1, 1, 0.01] });
newInput(18, nodeValue_Rotation("Piece Rotation", self, 0));
2024-08-23 06:14:52 +02:00
////////////
newInput(19, nodeValue_Color("Vertex Color 1", self, c_white));
newInput(20, nodeValue_Color("Vertex Color 2", self, c_white));
newInput(21, nodeValue_Color("Vertex Color 3", self, c_white));
////////////
newInput(22, nodeValue_Float("Piece Scale", self, 1));
newInput(23, nodeValue_Palette("Shape Palette", self, [ cola(c_white) ]));
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Surface out", self, VALUE_TYPE.surface, noone));
2023-03-19 09:17:39 +01:00
2024-09-04 03:57:11 +02:00
newOutput(1, nodeValue_Output("Mesh", self, VALUE_TYPE.mesh, noone));
2023-03-19 09:17:39 +01:00
2024-09-04 03:57:11 +02:00
newOutput(2, nodeValue_Output("Path", self, VALUE_TYPE.pathnode, noone));
2023-02-19 13:49:20 +01:00
2023-03-19 09:17:39 +01:00
input_display_list = [ 16,
2023-05-03 21:42:17 +02:00
["Output", false], 0,
2023-02-19 13:49:20 +01:00
["Transform", false], 5, 6, 7,
2024-08-23 06:14:52 +02:00
["Shape", false], 4, 8, 9, 10, 11, 12, 13, 14, 15, 17,
["Piecewise", false], 18, 22,
["Render", true], 3, 23, 19, 20, 21,
2024-01-08 08:10:50 +01:00
["Background", true, 1], 2,
2023-02-19 13:49:20 +01:00
];
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2023-02-19 13:49:20 +01:00
node_draw_transform_init();
2024-03-14 14:35:19 +01:00
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
2024-08-08 06:57:51 +02:00
if(array_length(current_data) != array_length(inputs)) return;
2023-02-19 13:49:20 +01:00
if(process_amount > 1) return;
2023-03-19 09:17:39 +01:00
draw_set_color(c_grey);
mesh.draw(_x, _y, _s);
node_draw_transform_box(active, _x, _y, _s, _mx, _my, _snx, _sny, 5, 6, 7, true);
draw_set_color(COLORS._main_accent);
path.draw(_x, _y, _s);
2023-02-19 13:49:20 +01:00
}
2024-08-23 06:14:52 +02:00
static vertex_apply = function(_p, _pos, _rot, _color = c_white, _alpha = 1) {
2023-03-19 09:17:39 +01:00
var p = point_rotate(_p.x, _p.y, 0, 0, _rot);
_p.x = _pos[0] + p[0];
_p.y = _pos[1] + p[1];
2024-08-23 06:14:52 +02:00
draw_vertex_color(_p.x, _p.y, _color, _alpha);
2023-02-19 13:49:20 +01:00
}
2023-03-19 09:17:39 +01:00
mesh = new Mesh();
path = new PathSegment();
static processData = function(_outData, _data, _output_index, _array_index) {
2023-02-19 13:49:20 +01:00
var _dim = _data[0];
var _bg = _data[1];
var _bgc = _data[2];
var _shc = _data[3];
var _shp = _data[4];
var _pos = _data[5];
var _rot = _data[6];
var _sca = _data[7];
var _side = _data[8];
var _inner = _data[9];
var _rad = _data[10];
var _teeth = _data[11];
var _thHei = _data[12];
var _thTap = _data[13];
var _aRan = _data[14];
var _cap = _data[15];
2023-03-19 09:17:39 +01:00
var _mesh = _data[16];
var _expld = _data[17];
var _prot = _data[18];
2024-08-23 06:14:52 +02:00
var _psca = _data[22];
var _pall = _data[23];
2023-02-19 13:49:20 +01:00
inputs[ 8].setVisible(false);
inputs[ 9].setVisible(false);
inputs[10].setVisible(false);
inputs[11].setVisible(false);
inputs[12].setVisible(false);
inputs[13].setVisible(false);
inputs[14].setVisible(false);
inputs[15].setVisible(false);
inputs[17].setVisible(false);
2023-02-19 13:49:20 +01:00
var _shapeName = array_safe_get_fast(shapesArray, _shp).name;
2023-02-19 13:49:20 +01:00
switch(_shapeName) {
case "Rectangle" : // 0
break;
case "Ellipse" : // 1
inputs[ 8].setVisible(true);
inputs[17].setVisible(true);
break;
case "Star" : // 2
inputs[ 8].setVisible(true);
inputs[ 9].setVisible(true);
break;
case "Capsule" : // 3
inputs[ 8].setVisible(true);
inputs[10].setVisible(true);
break;
case "Ring" : // 4
inputs[ 8].setVisible(true);
inputs[ 9].setVisible(true);
break;
case "Arc" : // 5
inputs[ 8].setVisible(true);
inputs[ 9].setVisible(true);
inputs[14].setVisible(true);
inputs[15].setVisible(true);
break;
case "Gear" : // 6
inputs[ 9].setVisible(true);
inputs[11].setVisible(true);
inputs[12].setVisible(true);
inputs[13].setVisible(true);
break;
case "Cross" : // 7
inputs[ 9].setVisible(true);
break;
}
var _outSurf = surface_verify(_outData[0], _dim[0], _dim[1], attrDepth());
2023-02-19 13:49:20 +01:00
var data = {
side: _side,
inner: _inner,
radius: _rad,
radRan: _aRan,
teeth: _teeth,
teethH: _thHei,
teethT: _thTap,
cap: _cap,
explode: _expld,
2023-02-19 13:49:20 +01:00
};
2024-08-23 06:14:52 +02:00
var tri0 = colorMultiply(_shc, _data[19]);
var tri1 = colorMultiply(_shc, _data[20]);
var tri2 = colorMultiply(_shc, _data[21]);
2023-02-19 13:49:20 +01:00
surface_set_target(_outSurf);
2024-01-08 08:10:50 +01:00
if(_bg) draw_clear(_bgc);
2023-03-19 09:17:39 +01:00
else DRAW_CLEAR
2023-02-19 13:49:20 +01:00
draw_primitive_begin(pr_trianglelist);
2024-08-08 06:57:51 +02:00
outputs[2].setVisible(_mesh == noone);
2023-03-19 09:17:39 +01:00
if(_mesh != noone) {
for( var j = 0; j < array_length(_mesh.triangles); j++ ) {
var tri = _mesh.triangles[j];
2023-02-19 13:49:20 +01:00
var p0 = tri[0];
var p1 = tri[1];
var p2 = tri[2];
2023-03-19 09:17:39 +01:00
draw_vertex(p0.x, p0.y);
draw_vertex(p1.x, p1.y);
draw_vertex(p2.x, p2.y);
}
} else {
var shapeData = [];
switch(_shapeName) {
case "Rectangle" : shapeData = SHAPE_rectangle( _sca ); break;
case "Ellipse" : shapeData = SHAPE_circle( _sca, data); break;
case "Star" : shapeData = SHAPE_star( _sca, data); break;
case "Capsule" : shapeData = SHAPE_capsule( _sca, data); break;
case "Ring" : shapeData = SHAPE_ring( _sca, data); break;
case "Arc" : shapeData = SHAPE_arc( _sca, data); break;
case "Gear" : shapeData = SHAPE_gear( _sca, data); break;
case "Cross" : shapeData = SHAPE_cross( _sca, data); break;
default:
draw_primitive_end();
draw_set_alpha(1);
surface_reset_target();
return [ _outSurf, mesh, path ];
2023-03-19 09:17:39 +01:00
}
var points = shapeData[0];
var segment = shapeData[1];
2024-08-23 06:14:52 +02:00
if(_prot != 0 || _psca != 1)
for( var i = 0, n = array_length(points); i < n; i++ ) {
2024-08-23 06:14:52 +02:00
if(points[i].type == SHAPE_TYPE.points) continue;
var _tri = points[i].triangles;
for( var j = 0; j < array_length(_tri); j++ ) {
var tri = _tri[j];
var cx = (tri[0].x + tri[1].x + tri[2].x) / 3;
var cy = (tri[0].y + tri[1].y + tri[2].y) / 3;
2024-08-23 06:14:52 +02:00
var p = point_rotate(tri[0].x - cx, tri[0].y - cy, 0, 0, _prot);
tri[0].x = cx + _psca * p[0];
tri[0].y = cy + _psca * p[1];
2024-08-23 06:14:52 +02:00
var p = point_rotate(tri[1].x - cx, tri[1].y - cy, 0, 0, _prot);
tri[1].x = cx + _psca * p[0];
tri[1].y = cy + _psca * p[1];
2024-08-23 06:14:52 +02:00
var p = point_rotate(tri[2].x - cx, tri[2].y - cy, 0, 0, _prot);
tri[2].x = cx + _psca * p[0];
tri[2].y = cy + _psca * p[1];
}
}
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(segment); i < n; i++ ) {
2023-03-19 09:17:39 +01:00
var _p = segment[i];
var p = point_rotate(_p.x, _p.y, 0, 0, _rot);
_p.x = _pos[0] + p[0];
_p.y = _pos[1] + p[1];
}
path.setSegment(segment);
var shapes = [];
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(points); i < n; i++ ) {
2023-03-19 09:17:39 +01:00
if(points[i].type == SHAPE_TYPE.points)
shapes[i] = polygon_triangulate(points[i].points)[0];
2024-08-23 06:14:52 +02:00
else
2023-03-19 09:17:39 +01:00
shapes[i] = points[i].triangles;
}
2024-08-23 06:14:52 +02:00
var _plen = array_length(_pall);
2023-03-19 09:17:39 +01:00
mesh.triangles = [];
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(shapes); i < n; i++ ) {
2023-03-19 09:17:39 +01:00
var triangles = shapes[i];
2024-08-23 06:14:52 +02:00
var shapetyp = points[i].type;
2024-01-08 08:10:50 +01:00
2023-03-19 09:17:39 +01:00
for( var j = 0; j < array_length(triangles); j++ ) {
var tri = triangles[j];
2024-08-23 06:14:52 +02:00
var shapeind = shapetyp == SHAPE_TYPE.rectangle? floor(j / 2) : j;
var trc = array_safe_get(_pall, shapeind % _plen, c_white)
vertex_apply(tri[0], _pos, _rot, colorMultiply(trc, tri0));
vertex_apply(tri[1], _pos, _rot, colorMultiply(trc, tri1));
vertex_apply(tri[2], _pos, _rot, colorMultiply(trc, tri2));
2023-03-19 09:17:39 +01:00
array_push(mesh.triangles, tri);
}
2023-02-19 13:49:20 +01:00
}
mesh.calcCoM();
2023-02-19 13:49:20 +01:00
}
draw_primitive_end();
2024-01-08 08:10:50 +01:00
draw_set_alpha(1);
2023-02-19 13:49:20 +01:00
surface_reset_target();
return [ _outSurf, mesh, path ];
2023-02-19 13:49:20 +01:00
}
}