Pixel-Composer/scripts/node_random_shape/node_random_shape.gml

163 lines
4.5 KiB
Text
Raw Normal View History

2023-07-17 19:58:33 +02:00
function Node_Random_Shape(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2024-06-20 08:55:04 +02:00
name = "Draw Random Shape";
2023-07-17 19:58:33 +02:00
2024-08-18 06:16:20 +02:00
newInput(0, nodeValue_Dimension(self));
2023-07-17 19:58:33 +02:00
2024-11-07 07:59:04 +01:00
newInput(1, nodeValueSeed(self));
2023-07-17 19:58:33 +02:00
newInput(2, nodeValue_Enum_Scroll("SSAA", self, 0, [ "None", "2x", "4x", "8x" ]));
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Surface out", self, VALUE_TYPE.surface, noone));
2023-07-17 19:58:33 +02:00
input_display_list = [
["Output", false], 0,
["Shape", false], 1,
["Render", false], 2,
2023-07-17 19:58:33 +02:00
]
temp_surface = [ noone ];
2023-07-17 19:58:33 +02:00
function surfaceContentRatio(_surf) {
var s = 0;
2023-09-08 21:37:36 +02:00
var _sw = surface_get_width_safe(_surf);
var _sh = surface_get_height_safe(_surf);
2023-07-17 19:58:33 +02:00
var total = _sw * _sh;
var _buff = buffer_create(_sw * _sh * 4, buffer_fixed, 4);
buffer_get_surface(_buff, _surf, 0);
buffer_seek(_buff, buffer_seek_start, 0);
repeat(total) {
var b = buffer_read(_buff, buffer_u32);
if(b) s++;
}
buffer_delete(_buff);
return s / total;
}
function generateShape(_dim, _aa = 1) {
var _sw = _dim[0] * _aa;
var _sh = _dim[1] * _aa;
var _shap = surface_create(_sw, _sh);
2023-07-17 19:58:33 +02:00
surface_set_target(_shap);
DRAW_CLEAR
draw_set_color(c_white);
var _amou = choose(1, 1, 2, 2, 3, 3, 3);
repeat(_amou) {
var _side = min(_dim[0], _dim[1]);
var _size = irandom_range(_side * 0.25, _side * 0.75);
var _shape = surface_create(_size * _aa, _size * _aa);
2023-07-17 19:58:33 +02:00
surface_set_target(_shape);
DRAW_CLEAR
draw_set_color(c_white);
var _x1 = _size * _aa;
var _y1 = _size * _aa;
var _r = irandom(4) * 2 * _aa;
2023-07-17 19:58:33 +02:00
switch(irandom(2)) {
case 0 : draw_roundrect_ext(0, 0, _x1, _y1, _r, _r, false); break;
case 1 : draw_ellipse(0, 0, _x1, _y1, false); break;
case 2 : draw_triangle(_x1 / 2, 0, 0, _y1, _x1, _y1, false); break;
2023-07-17 19:58:33 +02:00
}
surface_reset_target();
var _sx = irandom_range(_dim[0] / 2 - _size, _dim[0] / 2) * _aa;
var _sy = irandom_range(_dim[1] / 2 - _size, _dim[1] / 2) * _aa;
draw_surface_safe(_shape, _sx, _sy);
2023-07-17 19:58:33 +02:00
surface_free(_shape);
}
surface_reset_target();
var _surf = surface_create(_sw, _sh);
2023-07-17 19:58:33 +02:00
surface_set_target(_surf);
DRAW_CLEAR
draw_surface_ext_safe(_shap, 0, 0, 1, 1, 0, c_white, 1);
draw_surface_ext_safe(_shap, _sw, 0, -1, 1, 0, c_white, 1);
draw_surface_ext_safe(_shap, 0, _sh, 1, -1, 0, c_white, 1);
draw_surface_ext_safe(_shap, _sw, _sh, -1, -1, 0, c_white, 1);
2023-07-17 19:58:33 +02:00
surface_reset_target();
surface_free(_shap);
return _surf;
}
2023-08-17 16:56:54 +02:00
static processData = function(_outSurf, _data, _output_index, _array_index) {
2023-07-17 19:58:33 +02:00
var _dim = _data[0];
var _seed = _data[1];
var _aa = power(2, _data[2]);
2023-07-17 19:58:33 +02:00
random_set_seed(_seed);
var _adim = [ _dim[0] * _aa, _dim[1] * _aa ];
var _surf = generateShape(_dim, _aa);
2023-07-17 19:58:33 +02:00
var _side = irandom(2);
var _prog = _surf;
2023-07-17 19:58:33 +02:00
if(random(1) < 0.5) {
_prog = surface_create(_adim[0], _adim[1]);
2023-07-17 19:58:33 +02:00
var _size = [ _dim[0] * .75, _dim[1] * 0.75 ];
var _subs = generateShape(_size, _aa);
var _sx = _adim[0] / 2;
var _sy = _adim[1] / 2;
2023-07-17 19:58:33 +02:00
switch(_side) {
case 0 : _sx = irandom_range(_dim[0] / 2 - _size[0], _dim[0] / 2) * _aa; break;
case 1 : _sy = irandom_range(_dim[1] / 2 - _size[1], _dim[1] / 2) * _aa; break;
2023-07-17 19:58:33 +02:00
}
surface_set_target(_prog);
DRAW_CLEAR
if(random(1) < 0.5) {
shader_set(sh_rsh_rotate);
shader_set_f("dimension", _adim[0], _adim[1]);
draw_surface_safe(_surf);
2023-07-17 19:58:33 +02:00
shader_reset();
} else
draw_surface_safe(_surf);
2023-07-17 19:58:33 +02:00
BLEND_SUBTRACT
draw_surface_safe(_subs, _sx, _sy);
2023-07-17 19:58:33 +02:00
BLEND_NORMAL
surface_reset_target();
2023-07-17 19:58:33 +02:00
surface_free(_subs);
surface_free(_surf);
}
2023-07-17 19:58:33 +02:00
// if(surfaceContentRatio(_prog) < 0.2) {
// surface_free(_prog);
// _prog = generateShape(_adim);
// }
2023-07-17 19:58:33 +02:00
var _corn = surface_create(_dim[0], _dim[1]);
temp_surface[0] = surface_verify(temp_surface[0], _adim[0], _adim[1]);
var _cPassAA = temp_surface[0];
surface_set_shader(_cPassAA, sh_rsh_corner, true, BLEND.add);
shader_set_f("dimension", _adim[0], _adim[1]);
2023-07-17 19:58:33 +02:00
shader_set_i("type", choose(0, 0, 1, 1, 1));
draw_surface_safe(_prog);
if(_side == 1) draw_surface_ext_safe(_prog, 0, _adim[1], 1, -1, 0, c_white, 1);
if(_side == 2) draw_surface_ext_safe(_prog, _adim[0], 0, -1, 1, 0, c_white, 1);
2023-07-17 19:58:33 +02:00
surface_reset_shader();
surface_free(_prog);
surface_set_shader(_corn, sh_downsample, true, BLEND.over);
shader_set_dim("dimension", _cPassAA);
shader_set_f("down", _aa);
draw_surface(_cPassAA, 0, 0);
surface_reset_shader();
2023-07-17 19:58:33 +02:00
return _corn;
}
}