Pixel-Composer/scripts/node_scatter/node_scatter.gml

156 lines
5.3 KiB
Text
Raw Normal View History

2022-12-22 03:09:55 +01:00
function Node_Scatter(_x, _y, _group = -1) : Node_Processor(_x, _y, _group) constructor {
2022-01-13 05:24:03 +01:00
name = "Scatter";
2022-12-21 02:30:23 +01:00
shader = sh_blend_normal_dim;
uniform_dim = shader_get_uniform(shader, "dimension");
uniform_pos = shader_get_uniform(shader, "position");
uniform_sca = shader_get_uniform(shader, "scale");
uniform_rot = shader_get_uniform(shader, "rotation");
uniform_for = shader_get_sampler_index(shader, "fore");
2022-01-13 05:24:03 +01:00
inputs[| 0] = nodeValue(0, "Surface in", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, 0);
2022-11-21 06:38:44 +01:00
inputs[| 1] = nodeValue(1, "Dimension", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, def_surf_size2 )
2022-01-13 05:24:03 +01:00
.setDisplay(VALUE_DISPLAY.vector);
inputs[| 2] = nodeValue(2, "Amount", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 8);
inputs[| 3] = nodeValue(3, "Scale", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 1, 1, 1, 1 ] )
.setDisplay(VALUE_DISPLAY.vector_range);
inputs[| 4] = nodeValue(4, "Angle", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, [0, 0])
.setDisplay(VALUE_DISPLAY.rotation_range);
inputs[| 5] = nodeValue(5, "Area", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, [ def_surf_size / 2, def_surf_size / 2, def_surf_size / 2, def_surf_size / 2, AREA_SHAPE.rectangle ])
2022-01-19 03:05:13 +01:00
.setDisplay(VALUE_DISPLAY.area, function() { return inputs[| 1].getValue(); });
2022-01-13 05:24:03 +01:00
inputs[| 6] = nodeValue(6, "Distribution", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
2022-01-19 03:05:13 +01:00
.setDisplay(VALUE_DISPLAY.enum_button, [ "Area", "Border" ]);
2022-01-13 05:24:03 +01:00
2023-01-25 06:49:00 +01:00
inputs[| 7] = nodeValue(7, "Point at center", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false, "Rotate each copy to face the spawn center.");
2022-01-13 05:24:03 +01:00
2022-01-19 03:05:13 +01:00
inputs[| 8] = nodeValue(8, "Uniform scaling", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true);
2022-01-13 05:24:03 +01:00
inputs[| 9] = nodeValue(9, "Scatter", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 1)
2022-01-19 03:05:13 +01:00
.setDisplay(VALUE_DISPLAY.enum_button, [ "Uniform", "Random" ]);
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
inputs[| 10] = nodeValue(10, "Seed", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, irandom(9999999));
inputs[| 11] = nodeValue(11, "Random blend", self, JUNCTION_CONNECT.input, VALUE_TYPE.color, c_white)
.setDisplay(VALUE_DISPLAY.gradient);
inputs[| 12] = nodeValue(12, "Alpha", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 1, 1 ])
.setDisplay(VALUE_DISPLAY.slider_range, [0, 1, 0.01]);
2022-01-13 05:24:03 +01:00
2022-09-21 06:09:40 +02:00
outputs[| 0] = nodeValue(0, "Surface out", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, PIXEL_SURFACE);
2022-01-13 05:24:03 +01:00
input_display_list = [ 0, 1, 10,
2023-01-17 08:11:55 +01:00
["Scatter", false], 5, 6, 9, 2,
["Transform", false], 3, 8, 7, 4,
["Render", false], 11, 12
2022-01-13 05:24:03 +01:00
];
2022-09-21 06:09:40 +02:00
temp_surf = [ PIXEL_SURFACE, PIXEL_SURFACE ];
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) {
inputs[| 5].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-12-22 03:09:55 +01:00
var _inSurf = _data[0], surf;
2022-01-13 05:24:03 +01:00
if(_inSurf == 0)
return;
2022-12-22 03:09:55 +01:00
var _dim = _data[1];
var _amount = _data[2];
var _scale = _data[3];
var _rota = _data[4];
2022-01-13 05:24:03 +01:00
2022-12-22 03:09:55 +01:00
var _area = _data[5];
2022-01-13 05:24:03 +01:00
2022-12-22 03:09:55 +01:00
var _dist = _data[6];
var _scat = _data[9];
2022-01-13 05:24:03 +01:00
2022-12-22 03:09:55 +01:00
var _pint = _data[7];
var _unis = _data[8];
2022-01-13 05:24:03 +01:00
2022-12-22 03:09:55 +01:00
var seed = _data[10];
2023-01-17 08:11:55 +01:00
var color = inputs[| 11].getValue();
var _bldTyp = inputs[| 11].getExtraData();
var alpha = _data[12];
2022-01-13 05:24:03 +01:00
var _in_w, _in_h;
2022-12-27 04:00:50 +01:00
_outSurf = surface_verify(_outSurf, _dim[0], _dim[1]);
2022-01-13 05:24:03 +01:00
var ww = surface_get_width(_outSurf);
var hh = surface_get_height(_outSurf);
for(var i = 0; i < 2; i++) {
2022-12-27 04:00:50 +01:00
temp_surf[i] = surface_verify(temp_surf[i], ww, hh);
2022-01-13 05:24:03 +01:00
surface_set_target(temp_surf[i]);
draw_clear_alpha(0, 0);
surface_reset_target();
}
BLEND_OVERRIDE
2023-01-17 08:11:55 +01:00
var _sed = seed;
2022-01-13 05:24:03 +01:00
var res_index = 0, bg = 0;
for(var i = 0; i < _amount; i++) {
2023-01-17 08:11:55 +01:00
var sp = area_get_random_point(_area, _dist, _scat, i, _amount, _sed); _sed += 20;
2022-01-13 05:24:03 +01:00
var _x = sp[0];
var _y = sp[1];
2023-01-17 08:11:55 +01:00
var _scx = random_range_seed(_scale[0], _scale[1], _sed); _sed++;
var _scy = random_range_seed(_scale[2], _scale[3], _sed); _sed++;
2022-01-13 05:24:03 +01:00
if(_unis) _scy = _scx;
2023-01-17 08:11:55 +01:00
var _r = (_pint? point_direction(_area[0], _area[1], _x, _y) : 0) + random_range_seed(_rota[0], _rota[1], _sed); _sed++;
2022-01-13 05:24:03 +01:00
surf = _inSurf;
if(is_array(_inSurf))
2023-01-17 08:11:55 +01:00
surf = _inSurf[irandom_seed(array_length(_inSurf) - 1, _sed)]; _sed++;
2022-01-13 05:24:03 +01:00
var sw = surface_get_width(surf);
var sh = surface_get_height(surf);
2022-09-21 06:09:40 +02:00
if(_dist != AREA_DISTRIBUTION.area || _scat != AREA_SCATTER.uniform) {
2022-01-13 05:24:03 +01:00
_x -= sw / 2;
_y -= sh / 2;
}
2023-01-17 08:11:55 +01:00
var clr = gradient_eval(color, random_seed(1, _sed), _bldTyp[| 0]); _sed++;
var alp = random_range_seed(alpha[0], alpha[1], _sed); _sed++;
2022-01-13 05:24:03 +01:00
surface_set_target(temp_surf[bg]);
2022-12-21 02:30:23 +01:00
shader_set(shader);
2022-01-13 05:24:03 +01:00
shader_set_uniform_f_array(uniform_dim, [ sw / ww, sh / hh ]);
shader_set_uniform_f_array(uniform_pos, [ _x / ww, _y / hh]);
shader_set_uniform_f_array(uniform_sca, [ _scx, _scy ])
shader_set_uniform_f(uniform_rot, degtorad(_r));
texture_set_stage(uniform_for, surface_get_texture(surf));
2023-01-17 08:11:55 +01:00
draw_surface_ext_safe(temp_surf[!bg], 0, 0, 1, 1, 0, clr, alp);
2022-01-13 05:24:03 +01:00
shader_reset();
surface_reset_target();
res_index = bg;
bg = !bg;
}
BLEND_NORMAL
2022-01-13 05:24:03 +01:00
surface_set_target(_outSurf);
draw_clear_alpha(0, 0);
2023-01-25 06:49:00 +01:00
BLEND_OVERRIDE
2022-01-13 05:24:03 +01:00
draw_surface_safe(temp_surf[res_index], 0, 0);
BLEND_NORMAL
2022-12-27 04:00:50 +01:00
surface_reset_target();
2022-12-22 03:09:55 +01:00
return _outSurf;
2022-01-13 05:24:03 +01:00
}
}