Pixel-Composer/scripts/node_scatter_points/node_scatter_points.gml

135 lines
4 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Scatter_Points(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-11-09 07:59:03 +01:00
name = "Scatter Points";
2023-02-14 02:48:33 +01:00
color = COLORS.node_blend_number;
2024-05-02 11:05:02 +02:00
setDimension(96, 48);
2023-02-14 02:48:33 +01:00
2023-11-24 10:41:53 +01:00
onSurfaceSize = function() { return getInputData(7, DEF_SURF); };
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_Area("Point area", self, DEF_AREA_REF, { onSurfaceSize } ))
.setUnitRef(onSurfaceSize, VALUE_UNIT.reference);
2023-02-14 02:48:33 +01:00
2024-08-18 09:13:41 +02:00
newInput(1, nodeValue_Enum_Button("Point distribution", self, 0, [ "Area", "Border", "Map" ]))
2023-02-14 02:48:33 +01:00
.rejectArray();
2024-08-18 09:13:41 +02:00
newInput(2, nodeValue_Enum_Button("Scatter", self, 1, [ "Uniform", "Random" ]))
2023-02-14 02:48:33 +01:00
.rejectArray();
2024-08-18 09:13:41 +02:00
newInput(3, nodeValue_Int("Point amount", self, 2, "Amount of particle spawn in that frame."))
2023-02-14 02:48:33 +01:00
.rejectArray();
2024-08-18 09:13:41 +02:00
newInput(4, nodeValue_Surface("Distribution map", self))
2023-02-14 02:48:33 +01:00
.rejectArray();
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) })
2023-02-14 02:48:33 +01:00
.rejectArray();
2024-08-18 06:16:20 +02:00
newInput(6, nodeValue_Bool("Fixed position", self, false, "Fix point position, and only select point in the area."));
2023-02-21 04:48:50 +01:00
2024-08-18 06:16:20 +02:00
newInput(7, nodeValue_Vec2("Reference dimension", self, DEF_SURF));
2023-02-21 04:48:50 +01:00
2024-08-18 06:16:20 +02:00
newInput(8, nodeValue_Surface("Reference value", self));
2023-11-09 07:59:03 +01:00
2024-08-18 06:16:20 +02:00
newInput(9, nodeValue_Bool("Output 3D", self, false));
2023-11-09 07:59:03 +01:00
2024-08-18 06:16:20 +02:00
newInput(10, nodeValue_Enum_Button("Normal", self, 0, [ "X", "Y", "Z" ]));
2023-11-09 07:59:03 +01:00
2024-08-18 06:16:20 +02:00
newInput(11, nodeValue_Float("Plane position", self, 0));
2023-11-09 07:59:03 +01:00
2023-02-21 04:48:50 +01:00
input_display_list = [
["Base", false], 5, 6, 7,
["Scatter", false], 0, 1, 4, 2, 3,
2024-01-09 03:39:40 +01:00
["3D", true, 9], 10, 11
2023-02-21 04:48:50 +01:00
];
2023-02-14 02:48:33 +01:00
2024-08-08 06:57:51 +02:00
outputs[0] = nodeValue_Output("Points", self, VALUE_TYPE.float, [ ])
2023-02-14 02:48:33 +01:00
.setDisplay(VALUE_DISPLAY.vector);
2024-08-06 14:04:41 +02:00
static step = function() {
var _dist = getInputData(1);
2023-02-14 02:48:33 +01:00
2024-08-08 06:57:51 +02:00
inputs[2].setVisible(_dist != 2);
inputs[4].setVisible(_dist == 2, _dist == 2);
2024-08-06 14:04:41 +02:00
}
2023-02-14 02:48:33 +01:00
2024-08-06 14:04:41 +02:00
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
2024-08-08 06:57:51 +02:00
inputs[0].drawOverlay(hover, active, _x, _y, _s, _mx, _my, _snx, _sny);
2024-08-06 14:04:41 +02:00
}
2023-02-14 02:48:33 +01:00
2024-08-06 14:04:41 +02:00
static getPreviewValues = function() { return getInputData(8); }
2023-02-21 04:48:50 +01:00
2024-08-06 14:04:41 +02:00
static update = function(frame = CURRENT_FRAME) {
var _area = getInputData(0);
var _dist = getInputData(1);
var _scat = getInputData(2);
var _amo = getInputData(3);
var _distMap = getInputData(4);
var _seed = getInputData(5);
var _fix = getInputData(6);
var _fixRef = getInputData(7);
2023-02-21 04:48:50 +01:00
2023-11-09 07:59:03 +01:00
var _3d = getInputData( 9);
__temp_3dNorm = getInputData(10);
__temp_3dPos = getInputData(11);
2024-08-08 06:57:51 +02:00
inputs[7].setVisible(_fix);
2023-02-21 04:48:50 +01:00
var pos = [];
2023-02-14 02:48:33 +01:00
2024-01-24 06:19:34 +01:00
random_set_seed(_seed);
2023-02-21 04:48:50 +01:00
if(_fix) {
2023-11-09 07:59:03 +01:00
var ref = getInputData(8);
2023-02-21 04:48:50 +01:00
ref = surface_verify(ref, _fixRef[0], _fixRef[1]);
2024-08-08 06:57:51 +02:00
inputs[8].setValue(ref);
2023-02-21 04:48:50 +01:00
}
var aBox = area_get_bbox(_area);
2023-02-14 02:48:33 +01:00
if(_dist != 2) {
2023-02-21 04:48:50 +01:00
pos = [];
for( var i = 0; i < _amo; i++ ) {
if(_fix) {
2024-01-24 06:19:34 +01:00
var p = area_get_random_point([_fixRef[0], _fixRef[1], _fixRef[0], _fixRef[1]], _dist, _scat, i, _amo);
2023-02-21 04:48:50 +01:00
if(point_in_rectangle(p[0], p[1], aBox[0], aBox[1], aBox[2], aBox[3]))
array_push(pos, p);
} else
2024-01-24 06:19:34 +01:00
pos[i] = area_get_random_point(_area, _dist, _scat, i, _amo);
2023-02-21 04:48:50 +01:00
}
2023-02-14 02:48:33 +01:00
} else {
pos = [];
2023-02-28 09:43:01 +01:00
var p = get_points_from_dist(_distMap, _amo, _seed, 8);
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(p); i < n; i++ ) {
2023-02-14 02:48:33 +01:00
if(p[i] == 0) continue;
2023-02-21 04:48:50 +01:00
if(_fix) {
p[i][0] *= _fixRef[0];
p[i][1] *= _fixRef[1];
} else {
p[i][0] = _area[0] + _area[2] * (p[i][0] * 2 - 1);
p[i][1] = _area[1] + _area[3] * (p[i][1] * 2 - 1);
}
2023-02-14 02:48:33 +01:00
array_push(pos, p[i]);
}
}
2023-11-09 07:59:03 +01:00
if(_3d)
pos = array_map(pos, function(value, index) {
var val = value;
switch(__temp_3dNorm) {
case 0 : val = [ __temp_3dPos, value[0], value[1] ]; break;
case 1 : val = [ value[0], __temp_3dPos, value[1] ]; break;
case 2 : val = [ value[0], value[1], __temp_3dPos ]; break;
}
return val;
});
2024-08-08 06:57:51 +02:00
outputs[0].setValue(pos);
2024-08-06 14:04:41 +02:00
}
2023-02-14 02:48:33 +01:00
2024-08-06 14:04:41 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
2023-02-14 02:48:33 +01:00
var bbox = drawGetBbox(xx, yy, _s);
draw_sprite_fit(s_node_scatter_point, 0, bbox.xc, bbox.yc, bbox.w, bbox.h);
2024-08-06 14:04:41 +02:00
}
2023-02-14 02:48:33 +01:00
}