Last active
August 12, 2023 04:04
-
-
Save keijiro/3ecda3e33f376e8af345c6fbfb924e00 to your computer and use it in GitHub Desktop.
CFD.hlsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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