Pixel-Composer/scripts/node_FLIP_render/node_FLIP_render.gml

253 lines
7.5 KiB
Text
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;
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_Fdomain("Domain", self, noone))
2023-12-29 14:30:54 +01:00
.setVisible(true, true);
2024-08-18 09:13:41 +02:00
newInput(1, nodeValue_Float("Merge threshold", self, 0.75))
2023-12-30 14:21:56 +01:00
.setDisplay(VALUE_DISPLAY.slider);
2024-08-18 06:16:20 +02:00
newInput(2, nodeValue_Range("Lifespan", self, [ 0, 0 ], { linked : true }));
2023-12-30 14:21:56 +01:00
2024-08-18 06:16:20 +02:00
newInput(3, nodeValue_Float("Particle expansion", self, 20));
2023-12-31 14:09:49 +01:00
2024-08-18 06:16:20 +02:00
newInput(4, nodeValue_Bool("Draw obstracles", self, true));
2023-12-31 14:09:49 +01:00
2024-08-18 06:16:20 +02:00
newInput(5, nodeValue_Surface("Fluid particle", self));
2024-01-07 12:18:20 +01:00
2024-08-20 10:15:53 +02:00
newInput(6, nodeValue_Enum_Scroll("Render type", self, 0, [ new scrollItem("Particle", s_node_flip_render, 0),
new scrollItem("Line", s_node_flip_render, 1), ] ));
2024-01-07 12:18:20 +01:00
2024-08-18 06:16:20 +02:00
newInput(7, nodeValue_Bool("Threshold", self, true));
2024-01-07 12:18:20 +01:00
2024-08-18 06:16:20 +02:00
newInput(8, nodeValue_Bool("Additive", self, true));
2024-01-07 12:18:20 +01:00
2024-08-18 06:16:20 +02:00
newInput(9, nodeValue_Slider_Range("Alpha", self, [ 1, 1 ]));
2024-01-07 12:18:20 +01:00
2024-08-18 06:16:20 +02:00
newInput(10, nodeValue_Int("Segments", self, 1));
2024-03-21 13:57:44 +01:00
2024-08-18 06:16:20 +02:00
newInput(11, nodeValue_Gradient("Color Over Velocity", self, new gradientObject(cola(c_white))));
2024-03-22 09:44:11 +01:00
2024-08-18 06:16:20 +02:00
newInput(12, nodeValue_Range("Velocity Map", self, [ 0, 10 ]));
2024-03-22 09:44:11 +01:00
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
2024-08-08 06:57:51 +02:00
outputs[0] = nodeValue_Output("Rendered", self, VALUE_TYPE.surface, noone);
2023-12-29 14:30:54 +01:00
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-08-08 06:57:51 +02: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
2024-08-08 06:57:51 +02: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);
2024-08-08 06:57:51 +02:00
outputs[0].setValue(_outSurf);
2023-12-29 14:30:54 +01:00
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_safe(temp_surface[0]);
2024-01-07 12:18:20 +01:00
shader_reset();
} else
draw_surface_safe(temp_surface[0]);
2023-12-31 14:09:49 +01:00
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
}