Pixel-Composer/scripts/node_mk_rain/node_mk_rain.gml

217 lines
7 KiB
Text
Raw Normal View History

2023-11-30 13:07:08 +01:00
function Node_MK_Rain(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
name = "MK Rain";
update_on_frame = true;
2024-03-14 14:35:19 +01:00
inputs[| 0] = nodeValue("Surface in", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, noone);
2023-11-30 13:07:08 +01:00
inputs[| 1] = nodeValue("Direction", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 45)
.setDisplay(VALUE_DISPLAY.rotation);
inputs[| 2] = nodeValue("Density", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 5);
inputs[| 3] = nodeValue("Raindrop width", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 1, 1 ])
.setDisplay(VALUE_DISPLAY.range);
2023-11-30 13:07:08 +01:00
inputs[| 4] = nodeValue("Raindrop length", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 5, 10 ])
.setDisplay(VALUE_DISPLAY.range);
2023-11-30 13:07:08 +01:00
inputs[| 5] = nodeValue("Color", self, JUNCTION_CONNECT.input, VALUE_TYPE.gradient, new gradientObject(c_white));
inputs[| 6] = nodeValue("Alpha", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0.5, 1 ])
.setDisplay(VALUE_DISPLAY.slider_range);
inputs[| 7] = nodeValue("Velocity", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 1, 2 ])
.setDisplay(VALUE_DISPLAY.range);
2023-11-30 13:07:08 +01:00
inputs[| 8] = nodeValue("Seed", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, irandom_range(100_000, 999_999));
inputs[| 9] = nodeValue("Shape", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
2024-01-28 09:53:41 +01:00
.setDisplay(VALUE_DISPLAY.enum_scroll, [ new scrollItem("Rain", s_node_mk_rain_type, 0),
new scrollItem("Snow", s_node_mk_rain_type, 1),
new scrollItem("Texture", s_node_mk_rain_type, 2) ]);
2023-11-30 13:07:08 +01:00
inputs[| 10] = nodeValue("Snow size", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 3, 4 ])
.setDisplay(VALUE_DISPLAY.range);
2023-12-01 05:49:44 +01:00
inputs[| 11] = nodeValue("Texture", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, noone);
2023-12-15 12:56:36 +01:00
inputs[| 12] = nodeValue("Track extension", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 0 ])
.setDisplay(VALUE_DISPLAY.slider_range, { range: [ 0, 10, 0.01 ] });
inputs[| 13] = nodeValue("Size over lifetime", self, JUNCTION_CONNECT.input, VALUE_TYPE.curve, CURVE_DEF_11);
inputs[| 14] = nodeValue("Limited lifespan", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false);
inputs[| 15] = nodeValue("Lifespan", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 1 ], "Lifespan of a droplet as a ratio of the entire animation.")
.setDisplay(VALUE_DISPLAY.slider_range);
inputs[| 16] = nodeValue("Alpha over lifetime", self, JUNCTION_CONNECT.input, VALUE_TYPE.curve, CURVE_DEF_11);
2023-11-30 13:07:08 +01:00
inputs[| 17] = nodeValue("Fade alpha", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false);
2023-12-11 09:59:58 +01:00
input_display_list = [ new Inspector_Sprite(s_MKFX), 0, 8,
["Shape", false], 9, 3, 4, 10, 11,
["Lifespan", false, 14], 15, 13, 16,
["Effect", false], 2, 1, 7,
["Render", false], 5, 6, 17,
2023-11-30 13:07:08 +01:00
];
outputs[| 0] = nodeValue("Surface out", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
2023-12-01 05:49:44 +01:00
static step = function() { #region
2023-11-30 13:07:08 +01:00
var _shap = getSingleValue(9);
inputs[| 3].setVisible(_shap == 0);
inputs[| 4].setVisible(_shap == 0);
inputs[| 10].setVisible(_shap == 1);
2023-12-01 05:49:44 +01:00
inputs[| 11].setVisible(_shap == 2);
inputs[| 17].setVisible(_shap == 0);
2023-12-01 05:49:44 +01:00
} #endregion
2023-11-30 13:07:08 +01:00
2023-12-01 05:49:44 +01:00
static processData = function(_outSurf, _data, _output_index, _array_index) { #region
2023-11-30 13:07:08 +01:00
var _surf = _data[0];
var _dirr = _data[1];
var _dens = _data[2];
var _rwid = _data[3];
var _rhei = _data[4];
var _colr = _data[5];
var _alph = _data[6];
var _velo = _data[7];
var _seed = _data[8];
var _shap = _data[9];
var _snws = _data[10];
2023-12-01 05:49:44 +01:00
var _text = _data[11];
2023-12-15 12:56:36 +01:00
var _trex = _data[12];
2023-11-30 13:07:08 +01:00
var _llif = _data[13];
var _liml = _data[14];
var _life = _data[15];
var _alif = _data[16];
var _afad = _data[17];
2023-11-30 13:07:08 +01:00
if(!is_surface(_surf)) return _outSurf;
2023-12-01 05:49:44 +01:00
if(_shap == 2 && !is_surface(_text)) return _outSurf;
2023-11-30 13:07:08 +01:00
var _sw = surface_get_width_safe(_surf);
var _sh = surface_get_height_safe(_surf);
2023-12-01 05:49:44 +01:00
var _tw = surface_get_width_safe(_text);
var _th = surface_get_height_safe(_text);
2023-11-30 13:07:08 +01:00
var _rad = sqrt(_sw * _sw + _sh * _sh) / 2;
var _rx = _sw / 2;
var _ry = _sh / 2;
var _tr_span_x = lengthdir_x(1, _dirr);
var _tr_span_y = lengthdir_y(1, _dirr);
2023-12-01 05:49:44 +01:00
var _in_span_x = lengthdir_x(1, _dirr + 90);
var _in_span_y = lengthdir_y(1, _dirr + 90);
var prg = CURRENT_FRAME / TOTAL_FRAMES;
var _1c = array_length(_colr.keys) == 1;
var _cc = _1c? _colr.eval(0) : _colr;
draw_set_circle_precision(32);
2023-11-30 13:07:08 +01:00
surface_set_target(_outSurf);
DRAW_CLEAR
draw_surface(_surf, 0, 0);
var _lcc = _cc;
if(_afad) BLEND_ADD
else BLEND_ALPHA_MULP
2023-11-30 13:07:08 +01:00
2023-12-01 05:49:44 +01:00
repeat(_dens) {
2023-12-15 12:56:36 +01:00
random_set_seed(_seed); _seed += 100;
var _velRaw = random_range(_velo[0], _velo[1]);
_velRaw = max(1, _velRaw);
var _vel = _velRaw < 1? _velRaw : floor(_velRaw);
var _vex = _velRaw < 1? 0 : frac(_velRaw);
var _rrad = _rad * (1 + _vex);
var _r_shf = random_range( -_rad, _rad);
2023-12-15 12:56:36 +01:00
var _y_shf = random(1);
2023-11-30 13:07:08 +01:00
2023-12-01 05:49:44 +01:00
var _drpW, _drpH, _drpS;
2023-11-30 13:07:08 +01:00
switch(_shap) {
case 0 :
_drpW = irandom_range(_rwid[0], _rwid[1]);
_drpH = irandom_range(_rhei[0], _rhei[1]);
break;
case 1 :
_drpW = random_range(_snws[0], _snws[1]);
_drpH = _drpW;
break;
2023-12-01 05:49:44 +01:00
case 2 :
_drpW = _tw;
_drpH = _th;
break;
2023-11-30 13:07:08 +01:00
}
2023-12-01 05:49:44 +01:00
var _rmx = _rx + _in_span_x * _r_shf;
var _rmy = _ry + _in_span_y * _r_shf;
2023-12-15 12:56:36 +01:00
var _radH = _rrad + _drpH;
2023-12-01 05:49:44 +01:00
var _radHx = _radH * _tr_span_x;
var _radHy = _radH * _tr_span_y;
2023-11-30 13:07:08 +01:00
2023-12-01 05:49:44 +01:00
var _prg = _y_shf + _vel * prg;
_prg = frac(_prg) - 0.5; // -0.5 - 0.5
2023-12-01 05:49:44 +01:00
if(!_1c) _lcc = _colr.eval(random(1));
var _aa = random_range(_alph[0], _alph[1]);
2023-11-30 13:07:08 +01:00
var _clife = clamp((_prg + 0.5) / random_range(_life[0], _life[1]), 0, 1);
var _scaL = 1;
var _aaL = 1;
if(_liml) {
_scaL = eval_curve_x(_llif, _clife);
_aaL = eval_curve_x(_alif, _clife);
}
draw_set_color(_lcc);
draw_set_alpha(_aa * _aaL);
var _drpX = _rmx - _prg * _radHx * 2;
var _drpY = _rmy - _prg * _radHy * 2;
switch(_shap) {
case 0 :
var _tr_span_w = _tr_span_x * _drpH; // rain drop x span
var _tr_span_h = _tr_span_y * _drpH; // rain drop y span
var _x0 = _drpX - _tr_span_w;
var _x1 = _x0 + _tr_span_w * 2 * _scaL;
var _y0 = _drpY - _tr_span_h;
var _y1 = _y0 + _tr_span_h * 2 * _scaL;
2023-12-01 05:49:44 +01:00
if(_afad) {
if(_drpW == 1) draw_line_color( _x0, _y0, _x1, _y1, _lcc, c_black );
else draw_line_width_color( _x0, _y0, _x1, _y1, _drpW, _lcc, c_black );
} else {
if(_drpW == 1) draw_line( _x0, _y0, _x1, _y1 );
else draw_line_width( _x0, _y0, _x1, _y1, _drpW );
}
break;
case 1 :
draw_circle(_drpX, _drpY, _drpW * _scaL, false);
break;
case 2 :
draw_surface_ext(_text, _drpX, _drpY, _scaL, _scaL, 0, draw_get_color(), draw_get_alpha());
break;
2023-11-30 13:07:08 +01:00
}
}
draw_set_alpha(1);
BLEND_NORMAL
surface_reset_target();
return _outSurf;
2023-12-01 05:49:44 +01:00
} #endregion
2023-11-30 13:07:08 +01:00
}