Pixel-Composer/scripts/node_FLIP_render/node_FLIP_render.gml

257 lines
8.1 KiB
Plaintext
Raw Normal View History

2023-12-29 14:30:54 +01:00
function Node_FLIP_Render(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Render";
color = COLORS.node_blend_fluid;
icon = THEME.fluid_sim;
manual_ungroupable = false;
inputs[| 0] = nodeValue("Domain", self, JUNCTION_CONNECT.input, VALUE_TYPE.fdomain, noone)
.setVisible(true, true);
2024-01-07 12:18:20 +01:00
inputs[| 1] = nodeValue("Merge threshold", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0.75)
2023-12-30 14:21:56 +01:00
.setDisplay(VALUE_DISPLAY.slider);
2024-03-21 13:57:44 +01:00
inputs[| 2] = nodeValue("Lifespan", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, [ 0, 0 ])
.setDisplay(VALUE_DISPLAY.range, { linked : true });
2023-12-30 14:21:56 +01:00
2024-03-22 09:44:11 +01:00
inputs[| 3] = nodeValue("Particle expansion", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 20);
2023-12-31 14:09:49 +01:00
inputs[| 4] = nodeValue("Draw obstracles", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true);
2024-01-07 12:18:20 +01:00
inputs[| 5] = nodeValue("Fluid particle", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, noone);
inputs[| 6] = nodeValue("Render type", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
2024-01-28 09:53:41 +01:00
.setDisplay(VALUE_DISPLAY.enum_scroll, [ new scrollItem("Particle", s_node_flip_render, 0),
new scrollItem("Line", s_node_flip_render, 1), ] );
2024-01-07 12:18:20 +01:00
inputs[| 7] = nodeValue("Threshold", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true);
inputs[| 8] = nodeValue("Additive", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true);
inputs[| 9] = nodeValue("Alpha", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 1, 1 ])
.setDisplay(VALUE_DISPLAY.slider_range);
2024-01-07 12:18:20 +01:00
2024-03-21 13:57:44 +01:00
inputs[| 10] = nodeValue("Segments", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 1);
2024-03-22 09:44:11 +01:00
inputs[| 11] = nodeValue("Color Over Velocity", self, JUNCTION_CONNECT.input, VALUE_TYPE.gradient, new gradientObject(c_white));
inputs[| 12] = nodeValue("Velocity Map", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 10 ])
.setDisplay(VALUE_DISPLAY.range);
2024-01-07 12:18:20 +01:00
input_display_list = [ 0, 5,
2024-03-21 13:57:44 +01:00
["Rendering", false], 6, 10, 3, 4, 9,
2024-03-22 09:44:11 +01:00
["Effect", false], 11, 12, 2,
2024-01-07 12:18:20 +01:00
["Post Processing", false], 8, 7, 1,
2023-12-30 14:21:56 +01:00
];
2023-12-29 14:30:54 +01:00
outputs[| 0] = nodeValue("Rendered", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
2024-01-07 12:18:20 +01:00
seed = irandom_range(100000, 999999);
temp_surface = [ noone ];
array_push(attributeEditors, "FLIP Solver");
attributes.update = true;
array_push(attributeEditors, ["Update domain", function() { return attributes.update; },
new checkBox(function() {
attributes.update = !attributes.update;
triggerRender();
})]);
2024-03-22 09:44:11 +01:00
attributes.debugDraw = false;
array_push(attributeEditors, ["Draw Fluid Particles", function() { return attributes.debugDraw; },
new checkBox(function() {
attributes.debugDraw = !attributes.debugDraw;
})]);
2024-03-21 13:57:44 +01:00
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) { #region
var domain = getInputData(0);
if(!instance_exists(domain)) return;
if(domain.domain == noone) return;
2024-03-22 09:44:11 +01:00
if(attributes.debugDraw) {
var _m = min(array_length(domain.particlePos) / 2 - 1, domain.numParticles);
2024-03-21 13:57:44 +01:00
2024-03-22 09:44:11 +01:00
draw_set_color(COLORS._main_accent);
2024-03-21 13:57:44 +01:00
2024-03-22 09:44:11 +01:00
for( var i = 0; i < _m; i++ ) {
var _px = domain.particlePos[i * 2 + 0] - 1;
var _py = domain.particlePos[i * 2 + 1] - 1;
draw_circle(_x + _px * _s, _y + _py * _s, 1, false);
}
2024-03-21 13:57:44 +01:00
}
} #endregion
2024-01-07 12:18:20 +01:00
static step = function() {
var _typ = getInputData(6);
var _thr = getInputData(7);
2024-03-21 13:57:44 +01:00
inputs[| 1].setVisible(_typ == 0 && _thr);
inputs[| 3].setVisible(_typ == 0);
inputs[| 5].setVisible(_typ == 0, _typ == 0);
inputs[| 10].setVisible(_typ == 1);
2024-01-07 12:18:20 +01:00
}
2023-12-29 14:30:54 +01:00
static update = function(frame = CURRENT_FRAME) {
var domain = getInputData(0);
if(!instance_exists(domain)) return;
2024-01-07 12:18:20 +01:00
if(domain.domain == noone) return;
2023-12-29 14:30:54 +01:00
2024-01-07 12:18:20 +01:00
if(attributes.update && IS_PLAYING) domain.step();
2023-12-30 14:21:56 +01:00
var _bln = getInputData(1);
var _vap = getInputData(2);
2023-12-31 14:09:49 +01:00
var _exp = getInputData(3);
var _obs = getInputData(4);
2024-01-07 12:18:20 +01:00
var _spr = getInputData(5);
var _typ = getInputData(6);
var _thr = getInputData(7);
var _add = getInputData(8);
var _alp = getInputData(9);
2024-03-21 13:57:44 +01:00
var _seg = getInputData(10);
2024-03-22 09:44:11 +01:00
var _cvl = getInputData(11);
var _vlr = getInputData(12);
2023-12-29 14:30:54 +01:00
var _outSurf = outputs[| 0].getValue();
2024-03-21 13:57:44 +01:00
var _maxpart = domain.maxParticles;
2023-12-29 14:30:54 +01:00
var _padd = domain.particleSize;
var _ww = domain.width - _padd * 2;
var _hh = domain.height - _padd * 2;
_outSurf = surface_verify(_outSurf, _ww, _hh);
temp_surface[0] = surface_verify(temp_surface[0], _ww, _hh);
outputs[| 0].setValue(_outSurf);
2024-03-22 09:44:11 +01:00
var _x, _y, _px, _py, _r, _l, _a, _v, _sx, _sy;
2023-12-31 14:09:49 +01:00
var _rad = domain.particleRadius * _exp;
2023-12-30 14:21:56 +01:00
var _mx = min(array_length(domain.particlePos) / 2 - 1, domain.numParticles);
2023-12-29 14:30:54 +01:00
2024-01-07 12:18:20 +01:00
var _useSpr = is_surface(_spr);
var _sprw, _sprh;
if(_useSpr) {
_sprw = 0.5 * surface_get_width_safe(_spr);
_sprh = 0.5 * surface_get_height_safe(_spr);
} else if(is_array(_spr) && array_length(_spr)) {
_useSpr = is_surface(_spr[0]);
_sprw = 0.5 * surface_get_width_safe(_spr[0]);
_sprh = 0.5 * surface_get_height_safe(_spr[0]);
}
2024-03-22 09:44:11 +01:00
var _useMapRange = array_length(_cvl.keys) > 1;
var _vMapRange = _vlr[1] - _vlr[0];
var _cc = _cvl.keys[0].value;
2024-01-07 12:18:20 +01:00
random_set_seed(seed);
surface_set_shader(temp_surface[0], _useSpr? noone : sh_FLIP_draw_droplet);
if(_add) BLEND_ADD
else BLEND_ALPHA_MULP
2023-12-29 14:30:54 +01:00
2024-01-07 12:18:20 +01:00
if(_typ == 0) {
for( var i = 0; i < _mx; i++ ) {
2024-03-22 09:44:11 +01:00
_x = domain.particlePos[i * 2 + 0];
_y = domain.particlePos[i * 2 + 1];
_sx = domain.particleVel[i * 2 + 0];
_sy = domain.particleVel[i * 2 + 1];
_l = domain.particleLife[i];
2023-12-29 14:30:54 +01:00
2024-01-07 12:18:20 +01:00
if(_x == 0 && _y == 0) continue;
2024-03-22 09:44:11 +01:00
2024-01-07 12:18:20 +01:00
_x -= _padd;
_y -= _padd;
_r = 1;
_a = random_range(_alp[0], _alp[1]);
2024-03-21 13:57:44 +01:00
_v = irandom_range(_vap[0], _vap[1]);
if(_v) {
_r = (_v - _l) / _v;
2024-01-07 12:18:20 +01:00
if(_r * _rad < 0.5) continue;
}
2024-03-22 09:44:11 +01:00
if(_useMapRange) {
var _vel = sqrt(_sx * _sx + _sy * _sy);
var _vmap = (_vel - _vlr[0]) / _vMapRange;
_vmap = power(clamp(_vmap, 0, 1), 5);
_cc = _cvl.eval(_vmap);
}
2024-01-07 12:18:20 +01:00
if(_useSpr) {
2024-03-22 09:44:11 +01:00
if(is_array(_spr)) draw_surface_ext(_spr[i % array_length(_spr)], _x - _sprw * _r, _y - _sprh * _r, _r, _r, 0, _cc, _a * _r);
else draw_surface_ext(_spr, _x - _sprw * _r, _y - _sprh * _r, _r, _r, 0, _cc, _a * _r);
2024-01-07 12:18:20 +01:00
} else {
2024-03-22 09:44:11 +01:00
draw_circle_color_alpha(_x, _y, _rad, _cc, _cc, _a * _r, 0);
2024-01-07 12:18:20 +01:00
}
}
} else if(_typ == 1) {
2024-03-21 13:57:44 +01:00
var _segg = min(_seg, CURRENT_FRAME);
var _ox, _oy, _nx, _ny;
2024-01-07 12:18:20 +01:00
for( var i = 0; i < _mx; i++ ) {
2024-03-21 13:57:44 +01:00
_l = domain.particleLife[i];
_v = irandom_range(_vap[0], _vap[1]);
2024-01-07 12:18:20 +01:00
2024-03-21 13:57:44 +01:00
var fstFr = max(0, CURRENT_FRAME - _segg);
var lstFr = _v? min(CURRENT_FRAME, CURRENT_FRAME - _l + _v) : CURRENT_FRAME;
2024-01-07 12:18:20 +01:00
2024-03-21 13:57:44 +01:00
if(lstFr <= fstFr) continue;
2024-01-07 12:18:20 +01:00
2024-03-21 13:57:44 +01:00
_ox = lstFr == CURRENT_FRAME? domain.particlePos[i * 2 + 0] : domain.particleHist[(lstFr + 1) * _maxpart * 2 + i * 2 + 0];
_oy = lstFr == CURRENT_FRAME? domain.particlePos[i * 2 + 1] : domain.particleHist[(lstFr + 1) * _maxpart * 2 + i * 2 + 1];
2024-01-07 12:18:20 +01:00
2024-03-21 13:57:44 +01:00
if(_ox == 0 && _oy == 0) continue;
2024-01-07 12:18:20 +01:00
2024-03-21 13:57:44 +01:00
_ox -= _padd;
_oy -= _padd;
for( var j = lstFr; j > fstFr; j-- ) {
_nx = domain.particleHist[j * _maxpart * 2 + i * 2 + 0];
_ny = domain.particleHist[j * _maxpart * 2 + i * 2 + 1];
if(_nx == 0 && _ny == 0) continue;
2024-03-22 09:44:11 +01:00
if(_useMapRange) {
var _dx = _ox - _nx;
var _dy = _oy - _ny;
var _vel = sqrt(_dx * _dx + _dy * _dy);
var _vmap = (_vel - _vlr[0]) / _vMapRange;
_vmap = power(clamp(_vmap, 0, 1), 5);
_cc = _cvl.eval(_vmap);
}
2024-03-21 13:57:44 +01:00
_nx -= _padd;
_ny -= _padd;
2024-03-22 09:44:11 +01:00
draw_set_color(_cc);
2024-03-21 13:57:44 +01:00
draw_line(_ox, _oy, _nx, _ny);
_ox = _nx;
_oy = _ny;
}
2023-12-30 14:21:56 +01:00
}
2023-12-29 14:30:54 +01:00
}
BLEND_NORMAL
2023-12-31 14:09:49 +01:00
surface_reset_shader();
2023-12-29 14:30:54 +01:00
2023-12-31 14:09:49 +01:00
surface_set_target(_outSurf);
DRAW_CLEAR
2023-12-30 14:21:56 +01:00
2024-01-07 12:18:20 +01:00
if(_thr) {
shader_set(sh_FLIP_render_threshold);
shader_set_f("threshold", 1 - _bln);
draw_surface(temp_surface[0], 0, 0);
shader_reset();
} else
2023-12-31 14:09:49 +01:00
draw_surface(temp_surface[0], 0, 0);
if(_obs)
for( var i = 0, n = array_length(domain.obstracles); i < n; i++ )
domain.obstracles[i].draw();
surface_reset_target();
2023-12-29 14:30:54 +01:00
}
2023-12-30 14:21:56 +01:00
2023-12-29 14:30:54 +01:00
}