From 8990719a0f60264761b1c037f0803f009e3d782b Mon Sep 17 00:00:00 2001 From: amandaghassaei <amandaghassaei@gmail.com> Date: Mon, 3 Apr 2017 01:23:06 -0400 Subject: [PATCH] scale --- GPUMath.js | 4 ++-- index.html | 6 +++--- main.js | 29 ++++++++++++++++------------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/GPUMath.js b/GPUMath.js index 0ecc5c0..0d045a0 100644 --- a/GPUMath.js +++ b/GPUMath.js @@ -91,8 +91,8 @@ function initGPUMath(){ GPUMath.prototype.setSize = function(width, height){ gl.viewport(0, 0, width, height); - canvas.clientWidth = width; - canvas.clientHeight = height; + // canvas.clientWidth = width; + // canvas.clientHeight = height; }; GPUMath.prototype.setProgram = function(programName){ diff --git a/index.html b/index.html index bf4df93..0877de2 100644 --- a/index.html +++ b/index.html @@ -90,9 +90,9 @@ uniform vec2 u_mouseDir; uniform float u_mouseEnable; - uniform float u_dt; + uniform float u_reciprocalRadius; - const float reciprocalRadius = 0.01; + uniform float u_dt; void main() { @@ -102,7 +102,7 @@ if (u_mouseEnable == 1.0){ vec2 pxDist = fragCoord - u_mouseCoord; - currentVelocity += u_mouseDir*u_dt*exp(-(pxDist.x*pxDist.x+pxDist.y*pxDist.y)*reciprocalRadius); + currentVelocity += u_mouseDir*u_dt*exp(-(pxDist.x*pxDist.x+pxDist.y*pxDist.y)*u_reciprocalRadius); } gl_FragColor = vec4(currentVelocity, 0, 0); diff --git a/main.js b/main.js index 24f3bd3..7aca938 100755 --- a/main.js +++ b/main.js @@ -1,6 +1,8 @@ //used a lot of ideas from https://bl.ocks.org/robinhouston/ed597847175cf692ecce to clean this code up var width, height; +var body; +var scale = 4; var lastMouseCoordinates = [0,0]; var mouseCoordinates = [0,0]; @@ -25,6 +27,7 @@ function initGL() { }); canvas = document.getElementById("glcanvas"); + body = document.getElementsByTagName("body")[0]; canvas.onmousemove = onMouseMove; canvas.onmousedown = onMouseDown; @@ -35,11 +38,6 @@ function initGL() { GPU = initGPUMath(); - canvas.width = canvas.clientWidth; - canvas.height = canvas.clientHeight; - width = canvas.clientWidth; - height = canvas.clientHeight; - // setup a GLSL programs GPU.createProgram("advect", "2d-vertex-shader", "advectShader"); GPU.setUniformForProgram("advect", "u_dt", dt, "1f"); @@ -57,6 +55,7 @@ function initGL() { GPU.createProgram("force", "2d-vertex-shader", "forceShader"); GPU.setUniformForProgram("force", "u_dt", dt, "1f"); + GPU.setUniformForProgram("force", "u_reciprocalRadius", 0.1*scale, "1f"); GPU.setUniformForProgram("force", "u_velocity", 0, "1i"); GPU.createProgram("jacobi", "2d-vertex-shader", "jacobiShader"); @@ -84,7 +83,7 @@ function render(){ 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<3;i++){ + for (var i=0;i<2;i++){ GPU.step("jacobi", ["velocity", "velocity"], "nextVelocity"); GPU.step("jacobi", ["nextVelocity", "nextVelocity"], "velocity"); } @@ -131,10 +130,14 @@ function onResize(){ } function resetWindow(){ - canvas.width = canvas.clientWidth; - canvas.height = canvas.clientHeight; - width = canvas.clientWidth; - height = canvas.clientHeight; + + width = Math.floor(body.clientWidth/scale); + height = Math.floor(body.clientHeight/scale); + + canvas.width = width; + canvas.height = height; + canvas.clientWidth = body.clientWidth; + canvas.clientHeight = body.clientHeight; GPU.setSize(width, height); @@ -175,8 +178,8 @@ function resetWindow(){ for (var i=0;i<height;i++){ for (var j=0;j<width;j++){ var index = 4*(i*width+j); - if (((Math.floor(i/50))%2 && (Math.floor(j/50))%2) - || ((Math.floor(i/50))%2 == 0 && (Math.floor(j/50))%2 == 0)) material[index] = 1.0; + if (((Math.floor(i/10))%2 && (Math.floor(j/10))%2) + || ((Math.floor(i/10))%2 == 0 && (Math.floor(j/10))%2 == 0)) material[index] = 1.0; } } GPU.initTextureFromData("material", width, height, "FLOAT", material, true); @@ -189,7 +192,7 @@ function resetWindow(){ function onMouseMove(e){ lastMouseCoordinates = mouseCoordinates; - mouseCoordinates = [e.clientX, height-e.clientY]; + mouseCoordinates = [e.clientX/scale, (body.clientHeight-e.clientY)/scale]; } function onMouseDown(){ -- GitLab