Pixel-Composer/scripts/node_FLIP_render/node_FLIP_render.gml
2024-07-27 12:10:34 +07:00

257 lines
No EOL
8.1 KiB
Text

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);
inputs[| 1] = nodeValue("Merge threshold", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 0.75)
.setDisplay(VALUE_DISPLAY.slider);
inputs[| 2] = nodeValue("Lifespan", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, [ 0, 0 ])
.setDisplay(VALUE_DISPLAY.range, { linked : true });
inputs[| 3] = nodeValue("Particle expansion", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, 20);
inputs[| 4] = nodeValue("Draw obstracles", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true);
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)
.setDisplay(VALUE_DISPLAY.enum_scroll, [ new scrollItem("Particle", s_node_flip_render, 0),
new scrollItem("Line", s_node_flip_render, 1), ] );
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);
inputs[| 10] = nodeValue("Segments", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 1);
inputs[| 11] = nodeValue("Color Over Velocity", self, JUNCTION_CONNECT.input, VALUE_TYPE.gradient, new gradientObject(cola(c_white)));
inputs[| 12] = nodeValue("Velocity Map", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 10 ])
.setDisplay(VALUE_DISPLAY.range);
input_display_list = [ 0, 5,
["Rendering", false], 6, 10, 3, 4, 9,
["Effect", false], 11, 12, 2,
["Post Processing", false], 8, 7, 1,
];
outputs[| 0] = nodeValue("Rendered", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
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();
})]);
attributes.debugDraw = false;
array_push(attributeEditors, ["Draw Fluid Particles", function() { return attributes.debugDraw; },
new checkBox(function() {
attributes.debugDraw = !attributes.debugDraw;
})]);
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;
if(attributes.debugDraw) {
var _m = min(array_length(domain.particlePos) / 2 - 1, domain.numParticles);
draw_set_color(COLORS._main_accent);
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);
}
}
} #endregion
static step = function() {
var _typ = getInputData(6);
var _thr = getInputData(7);
inputs[| 1].setVisible(_typ == 0 && _thr);
inputs[| 3].setVisible(_typ == 0);
inputs[| 5].setVisible(_typ == 0, _typ == 0);
inputs[| 10].setVisible(_typ == 1);
}
static update = function(frame = CURRENT_FRAME) {
var domain = getInputData(0);
if(!instance_exists(domain)) return;
if(domain.domain == noone) return;
if(attributes.update && IS_PLAYING) domain.step();
var _bln = getInputData(1);
var _vap = getInputData(2);
var _exp = getInputData(3);
var _obs = getInputData(4);
var _spr = getInputData(5);
var _typ = getInputData(6);
var _thr = getInputData(7);
var _add = getInputData(8);
var _alp = getInputData(9);
var _seg = getInputData(10);
var _cvl = getInputData(11);
var _vlr = getInputData(12);
var _outSurf = outputs[| 0].getValue();
var _maxpart = domain.maxParticles;
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);
var _x, _y, _px, _py, _r, _l, _a, _v, _sx, _sy;
var _rad = domain.particleRadius * _exp;
var _mx = min(array_length(domain.particlePos) / 2 - 1, domain.numParticles);
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]);
}
var _useMapRange = array_length(_cvl.keys) > 1;
var _vMapRange = _vlr[1] - _vlr[0];
var _cc = _cvl.keys[0].value;
random_set_seed(seed);
surface_set_shader(temp_surface[0], _useSpr? noone : sh_FLIP_draw_droplet);
if(_add) BLEND_ADD
else BLEND_ALPHA_MULP
if(_typ == 0) {
for( var i = 0; i < _mx; i++ ) {
_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];
if(_x == 0 && _y == 0) continue;
_x -= _padd;
_y -= _padd;
_r = 1;
_a = random_range(_alp[0], _alp[1]);
_v = irandom_range(_vap[0], _vap[1]);
if(_v) {
_r = (_v - _l) / _v;
if(_r * _rad < 0.5) continue;
}
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);
}
if(_useSpr) {
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);
} else {
draw_circle_color_alpha(_x, _y, _rad, _cc, _cc, _a * _r, 0);
}
}
} else if(_typ == 1) {
var _segg = min(_seg, CURRENT_FRAME);
var _ox, _oy, _nx, _ny;
for( var i = 0; i < _mx; i++ ) {
_l = domain.particleLife[i];
_v = irandom_range(_vap[0], _vap[1]);
var fstFr = max(0, CURRENT_FRAME - _segg);
var lstFr = _v? min(CURRENT_FRAME, CURRENT_FRAME - _l + _v) : CURRENT_FRAME;
if(lstFr <= fstFr) continue;
_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];
if(_ox == 0 && _oy == 0) continue;
_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;
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);
}
_nx -= _padd;
_ny -= _padd;
draw_set_color(_cc);
draw_line(_ox, _oy, _nx, _ny);
_ox = _nx;
_oy = _ny;
}
}
}
BLEND_NORMAL
surface_reset_shader();
surface_set_target(_outSurf);
DRAW_CLEAR
if(_thr) {
shader_set(sh_FLIP_render_threshold);
shader_set_f("threshold", 1 - _bln);
draw_surface_safe(temp_surface[0]);
shader_reset();
} else
draw_surface_safe(temp_surface[0]);
if(_obs)
for( var i = 0, n = array_length(domain.obstracles); i < n; i++ )
domain.obstracles[i].draw();
surface_reset_target();
}
}