commit 1e667536a38d77ccc13bf95e394ed7d04c469720
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Tue May 26 13:53:47 2020 +0100

    1) Cube: convert it to the latest library. Main difference: objects are rendered better, individual cubits have rounded corners.
    2) Examples: some adjustments to MeshJoin & Predeform
    3) Library: fix a bug in main_vertex_shader's 'degree' function, which didn't work proprely in case of a vertex which was exactly at the center (i.e. vector PS was zero)

diff --git a/src/main/java/org/distorted/library/effect/VertexEffectDistort.java b/src/main/java/org/distorted/library/effect/VertexEffectDistort.java
index d37fb69..8e8805a 100644
--- a/src/main/java/org/distorted/library/effect/VertexEffectDistort.java
+++ b/src/main/java/org/distorted/library/effect/VertexEffectDistort.java
@@ -131,29 +131,28 @@ public class VertexEffectDistort extends VertexEffect
       + "vec4 region= vUniforms[effect+2];                             \n"
       + "float d = degree(region,ps);                                  \n"
 
-      + "if( d>0.0 )                                                   \n"
-      + "  {                                                           \n"
-      + "  v += d*force;                                               \n"
+      + "v += d*force;                                                 \n"
 
-      + "  float tp = 1.0+n.z;                                         \n"
-      + "  float tr = 1.0 / (tp - (1.0 - sign(tp)));                   \n"
+      + "float tp = 1.0+n.z;                                           \n"
+      + "float tr = 1.0 / (tp - (sign(tp)-1.0));                       \n"   // proper way to compute 1/x is
+                                                                             // 1/(x-(sign(x)-1)) and NOT 1/(x+1-sign(x))
 
-      + "  float ap = ps.x*n.z - ps.z*n.x;                             \n"   // likewise rotate the ps vector
-      + "  float bp = ps.y*n.z - ps.z*n.y;                             \n"   //
-      + "  float cp =(ps.x*n.y - ps.y*n.x)*tr;                         \n"   //
-      + "  vec3 psRot = vec3( ap+n.y*cp , bp-n.x*cp , dot(ps,n) );     \n"   //
+      + "float ap = ps.x*n.z - ps.z*n.x;                               \n"   // rotate the ps vector
+      + "float bp = ps.y*n.z - ps.z*n.y;                               \n"   //
+      + "float cp =(ps.x*n.y - ps.y*n.x)*tr;                           \n"   //
+      + "vec3 psRot = vec3( ap+n.y*cp , bp-n.x*cp , dot(ps,n) );       \n"   //
 
-      + "  float len = length(psRot);                                  \n"
-      + "  float corr= sign(len)-1.0;                                  \n"   // make N (0,0,1) if ps=(0,0,0)
-      + "  vec3 N = vec3( -dot(force,n)*psRot.xy, region.w*len-corr ); \n"   // modified rotated normal
+      + "float len = length(psRot);                                    \n"
+      + "float corr= sign(len)-1.0;                                    \n"   // make N (0,0,1) if ps=(0,0,0)
+      + "vec3 N = vec3( -dot(force,n)*psRot.xy, region.w*len-corr );   \n"   // modified rotated normal
                                                                              // dot(force,n) is rotated force.z
-      + "  float an = N.x*n.z + N.z*n.x;                               \n"   // now create the normal vector
-      + "  float bn = N.y*n.z + N.z*n.y;                               \n"   // back from our modified normal
-      + "  float cn =(N.x*n.y - N.y*n.x)*tr;                           \n"   // rotated back
-      + "  n = vec3( an+n.y*cn , bn-n.x*cn , -N.x*n.x-N.y*n.y+N.z*n.z);\n"   // notice 4 signs change!
+      + "float an = N.x*n.z + N.z*n.x;                                 \n"   // now create the normal vector
+      + "float bn = N.y*n.z + N.z*n.y;                                 \n"   // back from our modified normal
+      + "float cn =(N.x*n.y - N.y*n.x)*tr;                             \n"   // rotated back
+      + "n = vec3( an+n.y*cn , bn-n.x*cn , -N.x*n.x-N.y*n.y+N.z*n.z);  \n"   // notice 4 signs change!
+
+      + "  n = normalize(n);                                           \n";
 
-      + "  n = normalize(n);                                           \n"
-      + "  }                                                           \n";
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/res/raw/main_vertex_shader.glsl b/src/main/res/raw/main_vertex_shader.glsl
index 82b8cb8..6c72b4b 100644
--- a/src/main/res/raw/main_vertex_shader.glsl
+++ b/src/main/res/raw/main_vertex_shader.glsl
@@ -75,24 +75,18 @@ uniform int vAssociation[NUM_VERTEX];// Associations of the vertex effects. Thos
 
 float degree(in vec4 region, in vec3 PS)
   {
-  vec3 PO  = PS + region.xyz;
-  float D = region.w*region.w-dot(PO,PO);      // D = |OX|^2 - |PO|^2
+  float ps_sq = dot(PS,PS);
 
-  if( D<=0.0 ) return 0.0;
+  if( ps_sq==0.0 ) return 1.0;
 
-  float ps_sq = dot(PS,PS);
-  float one_over_ps_sq = 1.0/(ps_sq-(sign(ps_sq)-1.0));  // return 1.0 if ps_sq = 0.0
-                                                         // Important: if we want to write
-                                                         // b = 1/a if a!=0, b=1 otherwise
-                                                         // we need to write that as
-                                                         // b = 1 / ( a-(sign(a)-1) )
-                                                         // [ and NOT 1 / ( a + 1 - sign(a) ) ]
-                                                         // because the latter, if 0<a<2^-24,
-                                                         // will suffer from round-off error and in this case
-                                                         // a + 1.0 = 1.0 !! so 1 / (a+1-sign(a)) = 1/0 !
-  float DOT  = dot(PS,PO)*one_over_ps_sq;
-
-  return 1.0 / (1.0 + 1.0/(sqrt(DOT*DOT+D*one_over_ps_sq)-DOT));
+  vec3 PO = PS + region.xyz;
+  float d = region.w*region.w-dot(PO,PO);
+
+  if( d<=0.0 ) return 0.0;
+
+  float b = dot(PS,PO)/ps_sq;
+
+  return 1.0 / (1.0 + 1.0/(sqrt(b*b + d/ps_sq)-b));
   }
 
 #endif  // NUM_VERTEX>0
