Commit f400b4a1 authored by amandaghassaei's avatar amandaghassaei

fixed boundaries

parent c6b4d447
......@@ -17,6 +17,35 @@
}
</script>
<script id="boundaryConditionsShader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_texture;
uniform float u_scale;
uniform vec2 u_textureSize;
uniform vec2 u_obstaclePosition;//scaled to texture size
uniform float u_obstacleRad;
void main() {
vec2 fragCoord = gl_FragCoord.xy;
vec2 dir = fragCoord - vec2(0.5, 0.5) - u_obstaclePosition;
float dist = length(dir);
if (dist < u_obstacleRad-2.0){
gl_FragColor = vec4(0);
return;
}
if (dist < u_obstacleRad){
gl_FragColor = u_scale*texture2D(u_texture, (fragCoord + dir/dist)/u_textureSize);
return;
}
gl_FragColor = texture2D(u_texture, fragCoord/u_textureSize);
}
</script>
<script id="2d-render-shader" type="x-shader/x-fragment">
precision mediump float;
......@@ -28,7 +57,7 @@
vec2 fragCoord = gl_FragCoord.xy;
if (texture2D(u_obstacle, fragCoord/u_textureSize).x == 1.0){
float color = 0.5;
gl_FragColor = vec4(color, color, color, 1);
gl_FragColor = vec4(0.925, 0, 0.55, 1);
return;
}
float mat1 = texture2D(u_material, fragCoord/u_textureSize).x;
......@@ -46,16 +75,26 @@
uniform float u_const;
vec2 repeatBoundary(vec2 coord){
coord -= vec2(0.5, 0.5);
if (coord.x < 0.0) coord.x = u_textureSize.x-1.0;
else if (coord.x >= u_textureSize.x-1.0) coord.x = 0.0;
if (coord.y < 0.0) coord.y = u_textureSize.y-1.0;
else if (coord.y >= u_textureSize.y-1.0) coord.y = 0.0;
coord += vec2(0.5, 0.5);
return coord;
}
void main() {
vec2 fragCoord = gl_FragCoord.xy;
vec2 currentVelocity = texture2D(u_velocity, fragCoord/u_textureSize).xy;
float n = texture2D(u_pressure, (fragCoord+vec2(0.0, 1.0))/u_textureSize).x;
float s = texture2D(u_pressure, (fragCoord+vec2(0.0, -1.0))/u_textureSize).x;
float e = texture2D(u_pressure, (fragCoord+vec2(1.0, 0.0))/u_textureSize).x;
float w = texture2D(u_pressure, (fragCoord+vec2(-1.0, 0.0))/u_textureSize).x;
float n = texture2D(u_pressure, repeatBoundary(fragCoord+vec2(0.0, 1.0))/u_textureSize).x;
float s = texture2D(u_pressure, repeatBoundary(fragCoord+vec2(0.0, -1.0))/u_textureSize).x;
float e = texture2D(u_pressure, repeatBoundary(fragCoord+vec2(1.0, 0.0))/u_textureSize).x;
float w = texture2D(u_pressure, repeatBoundary(fragCoord+vec2(-1.0, 0.0))/u_textureSize).x;
gl_FragColor = vec4(currentVelocity-u_const*vec2(e-w, n-s), 0, 0);
}
......@@ -70,16 +109,29 @@
uniform float u_const;
vec2 repeatBoundary(vec2 coord){
coord -= vec2(0.5, 0.5);
if (coord.x < 0.0) coord.x = u_textureSize.x-1.0;
else if (coord.x >= u_textureSize.x-1.0) coord.x = 0.0;
if (coord.y < 0.0) coord.y = u_textureSize.y-1.0;
else if (coord.y >= u_textureSize.y-1.0) coord.y = 0.0;
coord += vec2(0.5, 0.5);
return coord;
}
void main() {
vec2 fragCoord = gl_FragCoord.xy;
//finite difference formulation of divergence
float n = texture2D(u_velocity, (fragCoord+vec2(0.0, 1.0))/u_textureSize).y;
float s = texture2D(u_velocity, (fragCoord+vec2(0.0, -1.0))/u_textureSize).y;
float e = texture2D(u_velocity, (fragCoord+vec2(1.0, 0.0))/u_textureSize).x;
float w = texture2D(u_velocity, (fragCoord+vec2(-1.0, 0.0))/u_textureSize).x;
//periodic boundary
float n = texture2D(u_velocity, repeatBoundary(fragCoord+vec2(0.0, 1.0))/u_textureSize).y;
float s = texture2D(u_velocity, repeatBoundary(fragCoord+vec2(0.0, -1.0))/u_textureSize).y;
float e = texture2D(u_velocity, repeatBoundary(fragCoord+vec2(1.0, 0.0))/u_textureSize).x;
float w = texture2D(u_velocity, repeatBoundary(fragCoord+vec2(-1.0, 0.0))/u_textureSize).x;
float div = u_const*(e-w + n-s);
gl_FragColor = vec4(div, 0, 0, 0);
......@@ -244,10 +296,12 @@
}
//periodic boundary in y
if (pos.x < 0.0) pos.x += u_textureSize.x-1.0;
if (pos.x >= u_textureSize.x-1.0) pos.x -= u_textureSize.x-1.0;
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;
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);
......
......@@ -74,6 +74,9 @@ function initGL() {
GPU.setUniformForProgram("render", "u_material", 0, "1i");
GPU.setUniformForProgram("render", "u_obstacle", 1, "1i");
GPU.createProgram("boundary", "2d-vertex-shader", "boundaryConditionsShader");
GPU.setUniformForProgram("boundary", "u_texture", 0, "1i");
resetWindow();
render();
......@@ -86,17 +89,19 @@ function render(){
// //advect velocity
GPU.setSize(width, height);
GPU.step("advectVel", ["velocity", "velocity"], "nextVelocity");
GPU.swapTextures("velocity", "nextVelocity");
//diffuse velocity
GPU.setProgram("jacobi");
var alpha = dx*dx/(nu*dt);
GPU.setUniformForProgram("jacobi", "u_alpha", alpha, "1f");
GPU.setUniformForProgram("jacobi", "u_reciprocalBeta", 1/(4+alpha), "1f");
for (var i=0;i<2;i++){
GPU.step("jacobi", ["velocity", "velocity"], "nextVelocity");
GPU.step("jacobi", ["nextVelocity", "nextVelocity"], "velocity");
}
GPU.setProgram("boundary");
GPU.setUniformForProgram("boundary", "u_scale", -1, "1f");
GPU.step("boundary", ["nextVelocity"], "velocity");
// diffuse velocity
// GPU.setProgram("jacobi");
// var alpha = dx*dx/(nu*dt);
// GPU.setUniformForProgram("jacobi", "u_alpha", alpha, "1f");
// GPU.setUniformForProgram("jacobi", "u_reciprocalBeta", 1/(4+alpha), "1f");
// for (var i=0;i<1;i++){
// GPU.step("jacobi", ["velocity", "velocity"], "nextVelocity");
// GPU.step("jacobi", ["nextVelocity", "nextVelocity"], "velocity");
// }
//apply force
GPU.setProgram("force");
......@@ -109,7 +114,9 @@ function render(){
GPU.setUniformForProgram("force", "u_mouseEnable", 0.0, "1f");
}
GPU.step("force", ["velocity"], "nextVelocity");
GPU.swapTextures("velocity", "nextVelocity");
GPU.setProgram("boundary");
GPU.setUniformForProgram("boundary", "u_scale", -1, "1f");
GPU.step("boundary", ["nextVelocity"], "velocity");
// compute pressure
GPU.step("diverge", ["velocity"], "velocityDivergence");//calc velocity divergence
......@@ -120,10 +127,16 @@ function render(){
GPU.step("jacobi", ["velocityDivergence", "pressure"], "nextPressure");//diffuse velocity
GPU.step("jacobi", ["velocityDivergence", "nextPressure"], "pressure");//diffuse velocity
}
GPU.setProgram("boundary");
GPU.setUniformForProgram("boundary", "u_scale", 1, "1f");
GPU.step("boundary", ["pressure"], "nextPressure");
GPU.swapTextures("nextPressure", "pressure");
// subtract pressure gradient
GPU.step("gradientSubtraction", ["velocity", "pressure"], "nextVelocity");
GPU.swapTextures("velocity", "nextVelocity");
GPU.setProgram("boundary");
GPU.setUniformForProgram("boundary", "u_scale", -1, "1f");
GPU.step("boundary", ["nextVelocity"], "velocity");
// move material
GPU.setSize(actualWidth, actualHeight);
......@@ -151,6 +164,8 @@ function resetWindow(){
width = Math.floor(actualWidth/scale);
height = Math.floor(actualHeight/scale);
obstaclePosition = [actualWidth/10, actualHeight/2];
canvas.width = actualWidth;
canvas.height = actualHeight;
canvas.clientWidth = body.clientWidth;
......@@ -175,6 +190,10 @@ function resetWindow(){
GPU.setUniformForProgram("jacobi" ,"u_textureSize", [width, height], "2f");
GPU.setProgram("render");
GPU.setUniformForProgram("render" ,"u_textureSize", [actualWidth, actualHeight], "2f");
GPU.setProgram("boundary");
GPU.setUniformForProgram("boundary" ,"u_textureSize", [width, height], "2f");
GPU.setUniformForProgram("boundary" ,"u_obstaclePosition", [obstaclePosition[0]/scale, obstaclePosition[1]/scale], "2f");
GPU.setUniformForProgram("boundary" ,"u_obstacleRad", obstacleRad/scale, "1f");
var velocity = new Float32Array(width*height*4);
for (var i=0;i<height;i++){
......@@ -195,11 +214,15 @@ function resetWindow(){
GPU.initTextureFromData("nextPressure", width, height, "FLOAT", new Float32Array(width*height*4), true);
GPU.initFrameBufferForTexture("nextPressure", true);
var numCols = Math.floor(actualHeight/10);
if (numCols%2 == 1) numCols--;
var colHeight = actualHeight/numCols;
var material = new Float32Array(actualWidth*actualHeight*4);
for (var i=0;i<actualHeight;i++){
for (var j=0;j<actualWidth;j++){
var index = 4*(i*actualWidth+j);
if (j==0 && (Math.floor(i/10))%2) material[index] = 1.0;
if (j==0 && (Math.floor(i/colHeight))%2) material[index] = 1.0;
}
}
GPU.initTextureFromData("material", actualWidth, actualHeight, "FLOAT", material, true);
......@@ -207,8 +230,6 @@ function resetWindow(){
GPU.initTextureFromData("nextMaterial", actualWidth, actualHeight, "FLOAT", material, true);
GPU.initFrameBufferForTexture("nextMaterial", true);
obstaclePosition = [actualWidth/10, actualHeight/2];
var obstacle = new Float32Array(actualWidth*actualHeight*4);
for (var i=0;i<actualHeight;i++){
for (var j=0;j<actualWidth;j++){
......
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