Skip to content

Instantly share code, notes, and snippets.

@keijiro
Last active August 12, 2023 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keijiro/3ecda3e33f376e8af345c6fbfb924e00 to your computer and use it in GitHub Desktop.
Save keijiro/3ecda3e33f376e8af345c6fbfb924e00 to your computer and use it in GitHub Desktop.
CFD.hlsl
#version 150
// Pseudo fluids, heavily inspired by flockaroo's single-pass CFD
// https://www.shadertoy.com/view/MdKXRy
in VertexData {
vec4 v_position;
vec3 v_normal;
vec2 v_texcoord;
} inData;
out vec4 fragColor;
uniform sampler2D prevFrame;
uniform sampler2D texture1;
uniform int iFrame;
const float tau = 6.28318530717958647;
const int angles = 7;
const float resolution = 640;
const float velocity = 8;
vec2 cos_sin(float x) { return vec2(cos(x), sin(x)); }
vec2 rot90(vec2 v) { return v.yx * vec2(1, -1); }
float outflow(vec2 uv, float l, float phi)
{
float acc = 0;
for (int i = 0; i < angles; i++)
{
vec2 dir = cos_sin(tau / angles * (i + phi));
acc += dot(dir, texture(prevFrame, uv + dir * l).xy - 0.5);
}
return acc / angles;
}
void main(void)
{
const float delta = 1.0 / resolution;
vec2 uv = inData.v_texcoord;
float phi = 0.02 * iFrame;
vec2 acc = vec2(0.0);
float l = delta;
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < angles; j++)
{
vec2 dir = cos_sin(tau / angles * (j + phi));
acc += rot90(dir) * outflow(uv + dir * l, l, phi);
}
l *= 2;
}
fragColor = texture(prevFrame, uv + velocity * acc * delta / angles);
fragColor.xy += clamp(1 - length(uv - 0.5) * 5, 0, 1) * (uv - 0.5) * 0.1;
if ((iFrame & 0x3ff) < 15) fragColor = texture2D(texture1, uv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment