Commit 04306fe7 authored by amandaghassaei's avatar amandaghassaei

bilin interp

parent 30915d2f
......@@ -209,11 +209,38 @@
uniform float u_dt;
vec2 bilinearInterp(vec2 pos, sampler2D texture, vec2 size){
//bilinear interp between nearest cells
vec2 pxCenter = vec2(0.5, 0.5);
vec2 ceiled = ceil(pos);
vec2 floored = floor(pos);
vec2 n = texture2D(texture, (ceiled+pxCenter)/size).xy;//actually ne
vec2 s = texture2D(texture, (floored+pxCenter)/size).xy;//actually sw
if (ceiled.x != floored.x){
vec2 se = texture2D(texture, (vec2(ceiled.x, floored.y)+pxCenter)/size).xy;
vec2 nw = texture2D(texture, (vec2(floored.x, ceiled.y)+pxCenter)/size).xy;
n = n*(pos.x-floored.x) + nw*(ceiled.x-pos.x);
s = se*(pos.x-floored.x) + s*(ceiled.x-pos.x);
}
vec2 materialVal = n;
if (ceiled.y != floored.y){
materialVal = n*(pos.y-floored.y) + s*(ceiled.y-pos.y);
}
return materialVal;
}
void main() {
vec2 fragCoord = gl_FragCoord.xy;
vec2 currentVelocity = u_scale*texture2D(u_velocity, fragCoord/u_textureSize).xy;
vec2 pxCenter = vec2(0.5, 0.5);
//bilinear interp
//vec2 currentVelocity = 1.0/u_scale*texture2D(u_velocity, fragCoord/u_textureSize).xy;
vec2 currentVelocity = 1.0/u_scale*bilinearInterp((fragCoord-pxCenter)*u_scale + pxCenter, u_velocity, u_textureSize*u_scale);
//implicitly solve advection
......@@ -222,7 +249,6 @@
return;
}
vec2 pxCenter = vec2(0.5, 0.5);
vec2 pos = fragCoord - pxCenter - u_dt*currentVelocity;
if (pos.x < 0.0) {
......@@ -238,24 +264,7 @@
if (pos.y < 0.0) pos.y += u_textureSize.y-1.0;
if (pos.y >= u_textureSize.y-1.0) pos.y -= u_textureSize.y-1.0;
//bilinear interp between nearest cells
vec2 ceiled = ceil(pos);
vec2 floored = floor(pos);
vec2 n = texture2D(u_material, (ceiled+pxCenter)/u_textureSize).xy;//actually ne
vec2 s = texture2D(u_material, (floored+pxCenter)/u_textureSize).xy;//actually sw
if (ceiled.x != floored.x){
vec2 se = texture2D(u_material, (vec2(ceiled.x, floored.y)+pxCenter)/u_textureSize).xy;
vec2 nw = texture2D(u_material, (vec2(floored.x, ceiled.y)+pxCenter)/u_textureSize).xy;
n = n*(pos.x-floored.x) + nw*(ceiled.x-pos.x);
s = se*(pos.x-floored.x) + s*(ceiled.x-pos.x);
}
vec2 materialVal = n;
if (ceiled.y != floored.y){
materialVal = n*(pos.y-floored.y) + s*(ceiled.y-pos.y);
}
gl_FragColor = vec4(materialVal, 0, 0);
gl_FragColor = vec4(bilinearInterp(pos, u_material, u_textureSize), 0, 0);
}
</script>
......@@ -347,7 +356,7 @@
It exhibits a phenomenon called <a href="https://en.wikipedia.org/wiki/Vortex_shedding" target="_blank">vortex shedding</a>,
where vortices of alternating spin spontaneously emerge behind the obstacle.
To increase performance, I solved for the velocity vector field of the fluid at a lower resolution than I used to compute the distribution of material moving through the fluid (shown in black and white).
I used bilinear interpolation to smooth out any resulting artifacts from this speedup.
I used bilinear interpolation to smooth out artifacts caused by this speedup.
I ignored the viscous diffusion term from the Navier-Stokes formula to encourage better vortex formation (the implicit advection solving I'm using creates enough diffusion on its own for this system).
<br/><br/>
Click and drag to apply a force to the fluid.
......
......@@ -178,7 +178,7 @@ function resetWindow(){
GPU.setUniformForProgram("advectVel" ,"u_scale", 1, "1f");
GPU.setProgram("advectMat");
GPU.setUniformForProgram("advectMat" ,"u_textureSize", [actualWidth, actualHeight], "2f");
GPU.setUniformForProgram("advectMat" ,"u_scale", scale, "1f");
GPU.setUniformForProgram("advectMat" ,"u_scale", width/actualWidth, "1f");
GPU.setProgram("gradientSubtraction");
GPU.setUniformForProgram("gradientSubtraction" ,"u_textureSize", [width, height], "2f");
GPU.setProgram("diverge");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment