Project

General

Profile

Download (10.9 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / effect / VertexEffectDistort.java @ c0255951

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.effect;
22

    
23
import org.distorted.library.type.Data3D;
24
import org.distorted.library.type.Data4D;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
/**
28
 * Distort the Mesh by applying a 3D vector of force.
29
 */
30
public class VertexEffectDistort extends VertexEffect
31
  {
32
  private static final EffectName NAME = EffectName.DISTORT;
33

    
34
  private final Data3D mVector, mCenter;
35
  private final Data4D mRegion;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
/**
39
 * Only for use by the library itself.
40
 *
41
 * @y.exclude
42
 */
43
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
44
    {
45
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
46
    mRegion.get(uniforms,index+REGION_OFFSET,currentDuration,step);
47

    
48
    return mVector.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
49
    }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
// PUBLIC API
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
// Def: P-current vertex being distorted, S - center of the effect, X- point where half-line SP meets
55
// the region circle (if P is inside the circle); (Vx,Vy,Vz) - force vector.
56
//
57
// horizontal part
58
// Point (Px,Py) gets moved by vector (Wx,Wy) where Wx/Wy = Vx/Vy i.e. Wx=aVx and Wy=aVy where
59
// a=Py/Sy (N --> when (Px,Py) is above (Sx,Sy)) or a=Px/Sx (W) or a=(w-Px)/(w-Sx) (E) or a=(h-Py)/(h-Sy) (S)
60
// It remains to be computed which of the N,W,E or S case we have: answer: a = min[ Px/Sx , Py/Sy , (w-Px)/(w-Sx) , (h-Py)/(h-Sy) ]
61
// Computations above are valid for screen (0,0)x(w,h) but here we have (-w/2,-h/2)x(w/2,h/2)
62
//
63
// vertical part
64
// Let vector PS = (dx,dy), f(x) (0<=x<=|SX|) be the shape of the side of the bubble.
65
// H(Px,Py) = |PS|>|SX| ? 0 : f(|PX|)
66
// N(Px,Py) = |PS|>|SX| ? (0,0,1) : ( -(dx/|PS|)sin(beta), -(dy/|PS|)sin(beta), cos(beta) ) where tan(beta) is f'(|PX|)
67
// ( i.e. normalize( dx, dy, -|PS|/f'(|PX|) )
68
//
69
// Now we also have to take into account the effect horizontal move by V=(Vx,Vy) will have on the normal vector.
70
// Solution:
71
// 1. Decompose the V into two subcomponents, one parallel to SX and another perpendicular.
72
// 2. Convince yourself (draw!) that the perpendicular component has no effect on normals.
73
// 3. The parallel component changes the length of |SX| by the factor of a=(|SX|-|Vpar|)/|SX| (where the length
74
//    can be negative depending on the direction)
75
// 4. that in turn leaves the x and y parts of the normal unchanged and multiplies the z component by a!
76
//
77
// |Vpar| = (Vx*dx-Vy*dy) / sqrt(ps_sq)  (-Vy because y is inverted)
78
// a =  (|SX| - |Vpar|)/|SX| = 1 - |Vpar|/((sqrt(ps_sq)/(1-d)) = 1 - (1-d)*|Vpar|/sqrt(ps_sq) = 1-(1-d)*(Vx*dx-Vy*dy)/ps_sq
79
//
80
// Side of the bubble
81
//
82
// choose from one of the three bubble shapes: the cone, the thin bubble and the thick bubble
83
// Case 1:
84
// f(t) = t, i.e. f(x) = Vz * x/|SX|   (a cone)
85
// -|PS|/f'(|PX|) = -|PS|*|SX|/Vz but since ps_sq=|PS|^2 and d=|PX|/|SX| then |PS|*|SX| = ps_sq/(1-d)
86
// so finally -|PS|/f'(|PX|) = -ps_sq/(Vz*(1-d))
87
//
88
// Case 2:
89
// f(t) = 3t^2 - 2t^3 --> f(0)=0, f'(0)=0, f'(1)=0, f(1)=1 (the bell curve)
90
// here we have t = x/|SX| which makes f'(|PX|) = 6*Vz*|PS|*|PX|/|SX|^3.
91
// so -|PS|/f'(|PX|) = (-|SX|^3)/(6Vz|PX|) =  (-|SX|^2) / (6*Vz*d) but
92
// d = |PX|/|SX| and ps_sq = |PS|^2 so |SX|^2 = ps_sq/(1-d)^2
93
// so finally -|PS|/f'(|PX|) = -ps_sq/ (6Vz*d*(1-d)^2)
94
//
95
// Case 3:
96
// f(t) = 3t^4-8t^3+6t^2 would be better as this satisfies f(0)=0, f'(0)=0, f'(1)=0, f(1)=1,
97
// f(0.5)=0.7 and f'(t)= t(t-1)^2 >=0 for t>=0 so this produces a fuller, thicker bubble!
98
// then -|PS|/f'(|PX|) = (-|PS|*|SX)) / (12Vz*d*(d-1)^2) but |PS|*|SX| = ps_sq/(1-d) (see above!)
99
// so finally -|PS|/f'(|PX|) = -ps_sq/ (12Vz*d*(1-d)^3)
100
//
101
// Now, new requirement: we have to be able to add up normal vectors, i.e. distort already distorted surfaces.
102
// If a surface is given by z = f(x,y), then the normal vector at (x0,y0) is given by (-df/dx (x0,y0), -df/dy (x0,y0), 1 ).
103
// so if we have two surfaces defined by f1(x,y) and f2(x,y) with their normals expressed as (f1x,f1y,1) and (f2x,f2y,1)
104
// then the normal to g = f1+f2 is simply given by (f1x+f2x,f1y+f2y,1), i.e. if the third components are equal, then we
105
// can simply add up the first and second components.
106
//
107
// Thus we actually want to compute N(Px,Py) = a*(-(dx/|PS|)*f'(|PX|), -(dy/|PS|)*f'(|PX|), 1) and keep adding
108
// the first two components. (a is the horizontal part)
109
//
110
// 2019-01-09 fixes for stuff discovered with the Earth testing app
111
// Now, the above stuff works only if the normal vector is close to (0,0,1). To make it work for any situation,
112
// rotate the normal, force and ps vectors along the axis (n.y,-n.x,0) and angle A between the normal and (0,0,1)
113
// then modify the rotated normal (so (0,0,1)) and rotate the modified normal back.
114
//
115
// if we have a vector (vx,vy,vz) that w want to rotate with a rotation that turns the normal into (0,0,1), then
116
// a) axis of rotation = (n.x,n.y,n.z) x (0,0,1) = (n.y,-n.x,0)
117
// b) angle of rotation A: cosA = (n.x,n.y,n.z)*(0,0,1) = n.z     (and sinA = + sqrt(1-n.z^2))
118
//
119
// so normalized axisNor = (n.x/ sinA, -n.y/sinA, 0) and now from Rodrigues rotation formula
120
// Vrot = v*cosA + (axisNor x v)*sinA + axisNor*(axis*v)*(1-cosA)
121
//
122
// which makes Vrot = (a+n.y*c , b-n.y*c , v*n) where
123
// a = vx*nz-vz*nx , b = vy*nz-vz*ny , c = (vx*ny-vy*nx)/(1+nz)    (unless n=(0,0,-1))
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  static String code()
127
    {
128
    return
129

    
130
        "vec3 ps = vUniforms[effect+1].yzw - v;                        \n"
131
      + "vec3 force = vUniforms[effect].xyz;                           \n"
132
      + "vec4 region= vUniforms[effect+2];                             \n"
133
      + "float d = degree(region,ps);                                  \n"
134

    
135
      + "if( d>0.0 )                                                   \n"
136
      + "  {                                                           \n"
137
      + "  v += d*force;                                               \n"
138

    
139
      + "  float tp = 1.0+n.z;                                         \n"
140
      + "  float tr = 1.0 / (tp - (sign(tp)-1.0));                     \n"   // proper way to compute 1/x is
141
                                                                             // 1/(x-(sign(x)-1)) and NOT 1/(x+1-sign(x))
142

    
143
      + "  float ap = ps.x*n.z - ps.z*n.x;                             \n"   // rotate the ps vector
144
      + "  float bp = ps.y*n.z - ps.z*n.y;                             \n"   //
145
      + "  float cp =(ps.x*n.y - ps.y*n.x)*tr;                         \n"   //
146
      + "  vec3 psRot = vec3( ap+n.y*cp , bp-n.x*cp , dot(ps,n) );     \n"   //
147

    
148
      + "  float len = length(psRot);                                  \n"
149
      + "  float corr= sign(len)-1.0;                                  \n"   // make N (0,0,1) if ps=(0,0,0)
150
      + "  vec3 N = vec3( -dot(force,n)*psRot.xy, region.w*len-corr ); \n"   // modified rotated normal
151
                                                                             // dot(force,n) is rotated force.z
152
      + "  float an = N.x*n.z + N.z*n.x;                               \n"   // now create the normal vector
153
      + "  float bn = N.y*n.z + N.z*n.y;                               \n"   // back from our modified normal
154
      + "  float cn =(N.x*n.y - N.y*n.x)*tr;                           \n"   // rotated back
155
      + "  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!
156

    
157
      + "  n = normalize(n);                                           \n"
158
      + "  }";
159

    
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
/**
164
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
165
 */
166
  public static void enable()
167
    {
168
    addEffect( NAME, code() );
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
/**
173
 * Distort a (possibly changing in time) part of the Mesh by a (possibly changing in time) vector of force.
174
 *
175
 * @param vector vector of force the Center of the Effect is currently being dragged with.
176
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
177
 * @param region Region that masks the Effect.
178
 */
179
  public VertexEffectDistort(Data3D vector, Data3D center, Data4D region)
180
    {
181
    super(NAME);
182
    mVector = vector;
183
    mCenter = center;
184
    mRegion = (region==null ? MAX_REGION : region);
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
/**
189
 * Distort the whole Mesh by a (possibly changing in time) vector of force.
190
 *
191
 * @param vector vector of force the Center of the Effect is currently being dragged with.
192
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
193
 */
194
  public VertexEffectDistort(Data3D vector, Data3D center)
195
    {
196
    super(NAME);
197
    mVector = vector;
198
    mCenter = center;
199
    mRegion = MAX_REGION;
200
    }
201
  }
202

    
203

    
(25-25/34)