From ee78b0bd317ef899e06cae1443073e0d29e67f4c Mon Sep 17 00:00:00 2001 From: Tanasart Date: Fri, 22 Nov 2024 10:04:05 +0700 Subject: [PATCH] smoke refactor --- scripts/_smokeSim_domain/_smokeSim_domain.gml | 48 ++++++------------- scripts/node_value/node_value.gml | 2 +- .../sh_fd_advect_material_a_16_glsl.fsh | 8 ++-- .../sh_fd_calculate_pressure_jacobi_glsl.fsh | 10 ++-- .../sh_fd_calculate_pressure_srj_glsl.fsh | 14 +++--- ..._fd_calculate_velocity_divergence_glsl.fsh | 8 ++-- .../sh_fd_subtract_pressure_gradient_glsl.fsh | 10 ++-- 7 files changed, 41 insertions(+), 59 deletions(-) diff --git a/scripts/_smokeSim_domain/_smokeSim_domain.gml b/scripts/_smokeSim_domain/_smokeSim_domain.gml index e35b4a116..d60a9ba58 100644 --- a/scripts/_smokeSim_domain/_smokeSim_domain.gml +++ b/scripts/_smokeSim_domain/_smokeSim_domain.gml @@ -198,41 +198,23 @@ function smokeSim_Domain(_width, _height) constructor { shader_reset(); surface_reset_target(); - if (pressure_type >= 0) { - shader_set(sh_fd_calculate_pressure_jacobi_glsl); - shader_set_f("texel_size", tx_width, tx_height); - shader_set_f("max_force", max_force); - - repeat (pressure_type) { - surface_set_target(sf_pressure_t); - draw_surface_safe(sf_pressure); - surface_reset_target(); - - temporary = sf_pressure; - sf_pressure = sf_pressure_t; - sf_pressure_t = temporary; - } - shader_reset(); + shader_set(sh_fd_calculate_pressure_srj_glsl); + shader_set_f("texel_size", tx_width, tx_height); + shader_set_f("max_force", max_force); - } else { - shader_set(sh_fd_calculate_pressure_srj_glsl); - shader_set_f("texel_size", tx_width, tx_height); - shader_set_f("max_force", max_force); + var length = array_length(pressure_relax); + for (var i = 0; i < length; ++i) { + if (pressure_relax[i] != -1) shader_set_f("precalculated", 1 - pressure_relax[i], 0.25 * pressure_relax[i]); + surface_set_target(sf_pressure_t); + draw_surface_safe(sf_pressure); + surface_reset_target(); - var length = array_length(pressure_relax); - for (var i = 0; i < length; ++i) { - if (pressure_relax[i] != -1) shader_set_f("precalculated", 1 - pressure_relax[i], 0.25 * pressure_relax[i]); - surface_set_target(sf_pressure_t); - draw_surface_safe(sf_pressure); - surface_reset_target(); - - temporary = sf_pressure; - sf_pressure = sf_pressure_t; - sf_pressure_t = temporary; - } - shader_reset(); - } - + temporary = sf_pressure; + sf_pressure = sf_pressure_t; + sf_pressure_t = temporary; + } + shader_reset(); + // Calculates the gradient of pressure and subtracts it from the velocity. surface_set_target(sf_velocity_t); shader_set(sh_fd_subtract_pressure_gradient_glsl); diff --git a/scripts/node_value/node_value.gml b/scripts/node_value/node_value.gml index b6f400635..ccc124c1b 100644 --- a/scripts/node_value/node_value.gml +++ b/scripts/node_value/node_value.gml @@ -1722,7 +1722,7 @@ function NodeValue(_name, _node, _connect, _type, _value, _tooltip = "") constru value_from = _valueFrom; array_push(_valueFrom.value_to, self); - if(_valueFrom.node.inline_context != noone) { + if(!LOADING && !APPENDING && _valueFrom.node.inline_context != noone) { var _inline = _valueFrom.node.inline_context; if(node.manual_ungroupable) _inline.addNode(node); } diff --git a/shaders/sh_fd_advect_material_a_16_glsl/sh_fd_advect_material_a_16_glsl.fsh b/shaders/sh_fd_advect_material_a_16_glsl/sh_fd_advect_material_a_16_glsl.fsh index 1bae18869..91919f124 100644 --- a/shaders/sh_fd_advect_material_a_16_glsl/sh_fd_advect_material_a_16_glsl.fsh +++ b/shaders/sh_fd_advect_material_a_16_glsl/sh_fd_advect_material_a_16_glsl.fsh @@ -34,10 +34,10 @@ void main() { color = phi_hat_next + (texture2D(gm_BaseTexture, v_vTexcoord).w - phi_hat_now) * precalculated_2.z; vec2 coord = floor(from / texel_size + 0.5) * texel_size; - float top_left = clampForce(texture2D(gm_BaseTexture, coord + precalculated_1.zw).w); - float bottom_right = clampForce(texture2D(gm_BaseTexture, coord + precalculated_1.xy).w); - float top_right = clampForce(texture2D(gm_BaseTexture, coord + precalculated_1.xw).w); - float bottom_left = clampForce(texture2D(gm_BaseTexture, coord + precalculated_1.zy).w); + float top_left = clamp(texture2D(gm_BaseTexture, coord + precalculated_1.zw).w, 0., 1.); + float bottom_right = clamp(texture2D(gm_BaseTexture, coord + precalculated_1.xy).w, 0., 1.); + float top_right = clamp(texture2D(gm_BaseTexture, coord + precalculated_1.xw).w, 0., 1.); + float bottom_left = clamp(texture2D(gm_BaseTexture, coord + precalculated_1.zy).w, 0., 1.); color = clamp(color, min(min(min(top_left, top_right), bottom_left), bottom_right), max(max(max(top_left, top_right), bottom_left), bottom_right)); } diff --git a/shaders/sh_fd_calculate_pressure_jacobi_glsl/sh_fd_calculate_pressure_jacobi_glsl.fsh b/shaders/sh_fd_calculate_pressure_jacobi_glsl/sh_fd_calculate_pressure_jacobi_glsl.fsh index c164851b7..8d03b51e5 100644 --- a/shaders/sh_fd_calculate_pressure_jacobi_glsl/sh_fd_calculate_pressure_jacobi_glsl.fsh +++ b/shaders/sh_fd_calculate_pressure_jacobi_glsl/sh_fd_calculate_pressure_jacobi_glsl.fsh @@ -11,10 +11,10 @@ void main() { gl_FragColor.zw = texture2D(gm_BaseTexture, v_vTexcoord).zw; float divergence = gl_FragColor.y; - float right = clampForce(texture2D(gm_BaseTexture, v_vTexcoord + vec2(texel_size.x, 0.0)).x); - float left = clampForce(texture2D(gm_BaseTexture, v_vTexcoord - vec2(texel_size.x, 0.0)).x); - float bottom = clampForce(texture2D(gm_BaseTexture, v_vTexcoord + vec2(0.0, texel_size.y)).x); - float top = clampForce(texture2D(gm_BaseTexture, v_vTexcoord - vec2(0.0, texel_size.y)).x); + float right = texture2D(gm_BaseTexture, v_vTexcoord + vec2(texel_size.x, 0.0)).x; + float left = texture2D(gm_BaseTexture, v_vTexcoord - vec2(texel_size.x, 0.0)).x; + float bottom = texture2D(gm_BaseTexture, v_vTexcoord + vec2(0.0, texel_size.y)).x; + float top = texture2D(gm_BaseTexture, v_vTexcoord - vec2(0.0, texel_size.y)).x; - gl_FragColor.x = (left + right + top + bottom - divergence) * 0.25; + gl_FragColor.x = clamp((left + right + top + bottom - divergence) * 0.25, 0., 1.); } diff --git a/shaders/sh_fd_calculate_pressure_srj_glsl/sh_fd_calculate_pressure_srj_glsl.fsh b/shaders/sh_fd_calculate_pressure_srj_glsl/sh_fd_calculate_pressure_srj_glsl.fsh index 6161195fe..75716a70f 100644 --- a/shaders/sh_fd_calculate_pressure_srj_glsl/sh_fd_calculate_pressure_srj_glsl.fsh +++ b/shaders/sh_fd_calculate_pressure_srj_glsl/sh_fd_calculate_pressure_srj_glsl.fsh @@ -10,15 +10,15 @@ vec2 clampForce(vec2 v) { return vec2(clampForce(v.x), clampForce(v.y)); } void main() { gl_FragColor = texture2D(gm_BaseTexture, v_vTexcoord); - float pressure = clampForce(gl_FragColor.x); - float divergence = clampForce(gl_FragColor.y); + float pressure = gl_FragColor.x; + float divergence = gl_FragColor.y; - float right = clampForce(texture2D(gm_BaseTexture, v_vTexcoord + vec2(texel_size.x, 0.0)).x); - float left = clampForce(texture2D(gm_BaseTexture, v_vTexcoord - vec2(texel_size.x, 0.0)).x); - float bottom = clampForce(texture2D(gm_BaseTexture, v_vTexcoord + vec2(0.0, texel_size.y)).x); - float top = clampForce(texture2D(gm_BaseTexture, v_vTexcoord - vec2(0.0, texel_size.y)).x); + float right = texture2D(gm_BaseTexture, v_vTexcoord + vec2(texel_size.x, 0.0)).x; + float left = texture2D(gm_BaseTexture, v_vTexcoord - vec2(texel_size.x, 0.0)).x; + float bottom = texture2D(gm_BaseTexture, v_vTexcoord + vec2(0.0, texel_size.y)).x; + float top = texture2D(gm_BaseTexture, v_vTexcoord - vec2(0.0, texel_size.y)).x; pressure = precalculated.x * pressure + (left + right + top + bottom - divergence) * precalculated.y; - gl_FragColor.x = clampForce(pressure); + gl_FragColor.x = pressure; } diff --git a/shaders/sh_fd_calculate_velocity_divergence_glsl/sh_fd_calculate_velocity_divergence_glsl.fsh b/shaders/sh_fd_calculate_velocity_divergence_glsl/sh_fd_calculate_velocity_divergence_glsl.fsh index 124d0f031..250aafd67 100644 --- a/shaders/sh_fd_calculate_velocity_divergence_glsl/sh_fd_calculate_velocity_divergence_glsl.fsh +++ b/shaders/sh_fd_calculate_velocity_divergence_glsl/sh_fd_calculate_velocity_divergence_glsl.fsh @@ -9,10 +9,10 @@ float clampForce(float v) { return clamp(v, -max_force, max_force); } vec2 clampForce(vec2 v) { return vec2(clampForce(v.x), clampForce(v.y)); } void main() { - vec2 right = clampForce(texture2D(gm_BaseTexture, v_vTexcoord + vec2(texel_size.x, 0.0)).xy); - vec2 left = clampForce(texture2D(gm_BaseTexture, v_vTexcoord - vec2(texel_size.x, 0.0)).xy); - vec2 bottom = clampForce(texture2D(gm_BaseTexture, v_vTexcoord + vec2(0.0, texel_size.y)).xy); - vec2 top = clampForce(texture2D(gm_BaseTexture, v_vTexcoord - vec2(0.0, texel_size.y)).xy); + vec2 right = clampForce(texture2D(gm_BaseTexture, v_vTexcoord + vec2(texel_size.x, 0.0)).xy) / max_force; + vec2 left = clampForce(texture2D(gm_BaseTexture, v_vTexcoord - vec2(texel_size.x, 0.0)).xy) / max_force; + vec2 bottom = clampForce(texture2D(gm_BaseTexture, v_vTexcoord + vec2(0.0, texel_size.y)).xy) / max_force; + vec2 top = clampForce(texture2D(gm_BaseTexture, v_vTexcoord - vec2(0.0, texel_size.y)).xy) / max_force; gl_FragColor = vec4(initial_value_pressure, (right.x - left.x) + (bottom.y - top.y), 0., 0.); } diff --git a/shaders/sh_fd_subtract_pressure_gradient_glsl/sh_fd_subtract_pressure_gradient_glsl.fsh b/shaders/sh_fd_subtract_pressure_gradient_glsl/sh_fd_subtract_pressure_gradient_glsl.fsh index 17976fa7f..556f3d7e5 100644 --- a/shaders/sh_fd_subtract_pressure_gradient_glsl/sh_fd_subtract_pressure_gradient_glsl.fsh +++ b/shaders/sh_fd_subtract_pressure_gradient_glsl/sh_fd_subtract_pressure_gradient_glsl.fsh @@ -9,15 +9,15 @@ float clampForce(float v) { return clamp(v, -max_force, max_force); } vec2 clampForce(vec2 v) { return vec2(clampForce(v.x), clampForce(v.y)); } void main() { - float right = clampForce(texture2D(texture_pressure, v_vTexcoord + vec2(texel_size.x, 0.0)).x); - float left = clampForce(texture2D(texture_pressure, v_vTexcoord - vec2(texel_size.x, 0.0)).x); - float bottom = clampForce(texture2D(texture_pressure, v_vTexcoord + vec2(0.0, texel_size.y)).x); - float top = clampForce(texture2D(texture_pressure, v_vTexcoord - vec2(0.0, texel_size.y)).x); + float right = texture2D(texture_pressure, v_vTexcoord + vec2(texel_size.x, 0.0)).x; + float left = texture2D(texture_pressure, v_vTexcoord - vec2(texel_size.x, 0.0)).x; + float bottom = texture2D(texture_pressure, v_vTexcoord + vec2(0.0, texel_size.y)).x; + float top = texture2D(texture_pressure, v_vTexcoord - vec2(0.0, texel_size.y)).x; vec2 gradient = 0.5 * vec2(right - left, bottom - top); vec2 velocity = clampForce(texture2D(gm_BaseTexture, v_vTexcoord).xy); velocity -= gradient; - gl_FragColor = vec4(clampForce(velocity), 0.0, 0.0); + gl_FragColor = vec4(clampForce(velocity), 0.0, 1.0); }