From ffbde13d6306283b346463b2002406ac60d7bdb2 Mon Sep 17 00:00:00 2001
From: amandaghassaei <amandaghassaei@gmail.com>
Date: Mon, 3 Apr 2017 22:16:09 -0400
Subject: [PATCH] add material

---
 index.html | 28 ++++++++++++++++++++++++++++
 js/main.js | 38 ++++++++++++++++++++++++++++----------
 2 files changed, 56 insertions(+), 10 deletions(-)

diff --git a/index.html b/index.html
index c3008d9..ab2eb9d 100644
--- a/index.html
+++ b/index.html
@@ -107,6 +107,34 @@
         }
     </script>
 
+    <script id="addMaterialShader" type="x-shader/x-fragment">
+        precision mediump float;
+
+        uniform sampler2D u_material;
+
+        uniform vec2 u_textureSize;
+
+        uniform vec2 u_mouseCoord;
+        uniform float u_mouseLength;
+        uniform float u_mouseEnable;
+
+        uniform float u_reciprocalRadius;
+
+        void main() {
+
+            vec2 fragCoord = gl_FragCoord.xy;
+
+            float currentMaterial = texture2D(u_material, fragCoord/u_textureSize).x;
+
+            if (u_mouseEnable == 1.0){
+                vec2 pxDist = fragCoord - u_mouseCoord;
+                currentMaterial += u_mouseLength*0.1*exp(-(pxDist.x*pxDist.x+pxDist.y*pxDist.y)*u_reciprocalRadius);
+            }
+
+            gl_FragColor = vec4(currentMaterial-0.002, 0, 0, 0);
+        }
+    </script>
+
     <script id="forceShader" type="x-shader/x-fragment">
         precision mediump float;
 
diff --git a/js/main.js b/js/main.js
index 5833aa5..b74b5d7 100755
--- a/js/main.js
+++ b/js/main.js
@@ -64,6 +64,9 @@ function initGL() {
     GPU.setUniformForProgram("force", "u_dt", dt, "1f");
     GPU.setUniformForProgram("force", "u_velocity", 0, "1i");
 
+    GPU.createProgram("addMaterial", "2d-vertex-shader", "addMaterialShader");
+    GPU.setUniformForProgram("force", "u_material", 0, "1i");
+
     GPU.createProgram("jacobi", "2d-vertex-shader", "jacobiShader");
     GPU.setUniformForProgram("jacobi", "u_b", 0, "1i");
     GPU.setUniformForProgram("jacobi", "u_x", 1, "1i");
@@ -142,12 +145,24 @@ function render(){
 
         // move material
         GPU.setSize(actualWidth, actualHeight);
+
+        //add material
+        GPU.setProgram("addMaterial");
+        if (!mouseout && mouseEnable){
+            GPU.setUniformForProgram("addMaterial", "u_mouseEnable", 1.0, "1f");
+            GPU.setUniformForProgram("addMaterial", "u_mouseCoord", mouseCoordinates, "2f");
+            GPU.setUniformForProgram("addMaterial", "u_mouseLength", Math.sqrt(Math.pow(3*(mouseCoordinates[0]-lastMouseCoordinates[0]),2)
+                +Math.pow(3*(mouseCoordinates[1]-lastMouseCoordinates[1]),2)), "1f");
+        } else {
+            GPU.setUniformForProgram("addMaterial", "u_mouseEnable", 0.0, "1f");
+        }
+        GPU.step("addMaterial", ["material"], "nextMaterial");
+
         GPU.setProgram("advect");
         GPU.setUniformForProgram("advect" ,"u_textureSize", [actualWidth, actualHeight], "2f");
         GPU.setUniformForProgram("advect" ,"u_scale", scale, "1f");
-        GPU.step("advect", ["velocity", "material"], "nextMaterial");
-        GPU.step("render", ["nextMaterial"]);
-        GPU.swapTextures("nextMaterial", "material");
+        GPU.step("advect", ["velocity", "nextMaterial"], "material");
+        GPU.step("render", ["material"]);
 
     } else resetWindow();
 
@@ -186,6 +201,9 @@ function resetWindow(){
     GPU.setProgram("force");
     GPU.setUniformForProgram("force", "u_reciprocalRadius", 0.03/scale, "1f");
     GPU.setUniformForProgram("force" ,"u_textureSize", [width, height], "2f");
+    GPU.setProgram("addMaterial");
+    GPU.setUniformForProgram("addMaterial", "u_reciprocalRadius", 0.03, "1f");
+    GPU.setUniformForProgram("addMaterial" ,"u_textureSize", [actualWidth, actualHeight], "2f");
     GPU.setProgram("jacobi");
     GPU.setUniformForProgram("jacobi" ,"u_textureSize", [width, height], "2f");
     GPU.setProgram("render");
@@ -214,13 +232,13 @@ function resetWindow(){
     GPU.initFrameBufferForTexture("nextPressure", true);
 
     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 (((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;
-        }
-    }
+    // for (var i=0;i<actualHeight;i++){
+    //     for (var j=0;j<actualWidth;j++){
+    //         var index = 4*(i*actualWidth+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;
+    //     }
+    // }
     GPU.initTextureFromData("material", actualWidth, actualHeight, "FLOAT", material, true);
     GPU.initFrameBufferForTexture("material", true);
     GPU.initTextureFromData("nextMaterial", actualWidth, actualHeight, "FLOAT", material, true);
-- 
GitLab