commit 6ebdbbf1bdfb2d76bccd9ef3339aa687382a440d
Author: Leszek Koltunski <leszek@distorted.org>
Date:   Thu Nov 24 00:58:08 2016 +0000

    DEFORM: add support for Regions

diff --git a/src/main/java/org/distorted/library/DistortedObject.java b/src/main/java/org/distorted/library/DistortedObject.java
index 239fda7..3c04ce7 100644
--- a/src/main/java/org/distorted/library/DistortedObject.java
+++ b/src/main/java/org/distorted/library/DistortedObject.java
@@ -746,6 +746,21 @@ public abstract class DistortedObject
     return mV.add(EffectNames.DISTORT, vector, center, null);
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
+ * a (possibly changing in time) point on the Object.
+ *
+ * @param vector Vector of force that deforms the shape of the whole Object.
+ * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
+ * @param region Region that masks the Effect.
+ * @return       ID of the effect added, or -1 if we failed to add one.
+ */
+  public long deform(Data3D vector, Data3D center, Data4D region)
+    {
+    return mV.add(EffectNames.DEFORM, vector, center, region);
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
diff --git a/src/main/java/org/distorted/library/EffectNames.java b/src/main/java/org/distorted/library/EffectNames.java
index 97f4e19..fbe1b5d 100644
--- a/src/main/java/org/distorted/library/EffectNames.java
+++ b/src/main/java/org/distorted/library/EffectNames.java
@@ -111,7 +111,7 @@ public enum EffectNames
    * <p>
    * Unity: (forceX,forceY) = (0,0)
    */
-  DEFORM           ( EffectTypes.VERTEX  ,   new float[] {0.0f,0.0f}      , 3, false, true ),
+  DEFORM           ( EffectTypes.VERTEX  ,   new float[] {0.0f,0.0f}      , 3, true, true ),
  /**
    * Pull (or push away) all points around a center point to (from) it.
    * <p>
diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index 782dada..14d5449 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -202,7 +202,7 @@ void restrictZ(inout float v)
 //
 // Constants:
 // a) A : valid values: (0,infinity). 'Bendiness' if the surface - the higher A is, the more the surface
-//        bends. A<=0 destroys the system,
+//        bends. A<=0 destroys the system.
 // b) B : valid values: <-1,1>. The amount side edges get 'sucked' inwards when we pull the middle of the
 //        top edge up. B=0 --> not at all, B=1: a looot. B=-0.5: the edges will actually be pushed outwards
 //        quite a bit. One can also set it to <-1 or >1, but it will look a bit ridiculous.
@@ -214,30 +214,30 @@ void restrictZ(inout float v)
 
 void deform(in int effect, inout vec4 v)
   {
-  const vec2 one = vec2(1.0,1.0);
+  const vec2 ONE = vec2(1.0,1.0);
 
   const float A = 0.5;
   const float B = 0.2;
   const float C = 5.0;
 
   vec2 center = vUniforms[effect+1].yz;
-  vec2 force  = vUniforms[effect].xy;
-  vec2 dist   = center-v.xy;
-  vec2 aDist  = abs(dist.xy);
-  vec2 maxdist= u_objD.xy + abs(center);
+  vec2 ps     = center-v.xy;
+  vec2 aPS    = abs(ps);
+  vec2 maxps  = u_objD.xy + abs(center);
+  vec2 force  = vUniforms[effect].xy * degree_region(vUniforms[effect+2],ps);
   vec2 aForce = abs(force);
 
-  vec2 Aw = A*maxdist;
-  vec2 quot = dist / maxdist;
+  vec2 Aw = A*maxps;
+  vec2 quot = ps / maxps;
   quot = quot*quot;                          // ( (x/W)^2 , (y/H)^2 ) where x,y are distances from V to center
 
   float denomV = 1.0 / (aForce.y + Aw.x);
   float denomH = 1.0 / (aForce.x + Aw.y);
 
-  vec2 vertCorr= one - aDist / ( aForce+C*aDist + (one-sign(aForce)) );
+  vec2 vertCorr= ONE - aPS / ( aForce+C*aPS + (ONE-sign(aForce)) );
 
-  float mvXvert = -B * dist.x * aForce.y * (1.0-quot.y) * denomV;    // impact the vertical   component of the force vector has on horizontal movement
-  float mvYhorz = -B * dist.y * aForce.x * (1.0-quot.x) * denomH;    // impact the horizontal component of the force vector has on vertical   movement
+  float mvXvert = -B * ps.x * aForce.y * (1.0-quot.y) * denomV;      // impact the vertical   component of the force vector has on horizontal movement
+  float mvYhorz = -B * ps.y * aForce.x * (1.0-quot.x) * denomH;      // impact the horizontal component of the force vector has on vertical   movement
   float mvYvert =  force.y * (1.0-quot.x*Aw.x*denomV) * vertCorr.y;  // impact the vertical   component of the force vector has on vertical   movement
   float mvXhorz = -force.x * (1.0-quot.y*Aw.y*denomH) * vertCorr.x;  // impact the horizontal component of the force vector has on horizontal movement
 
