Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectDistort.java @ 1e667536

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted 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                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.effect;
21

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

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

    
33
  private Data3D mVector, mCenter;
34
  private Data4D mRegion;
35

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

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

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

    
125
  static String code()
126
    {
127
    return
128

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

    
134
      + "v += d*force;                                                 \n"
135

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

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

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

    
154
      + "  n = normalize(n);                                           \n";
155

    
156
    }
157

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

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

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

    
199

    
(22-22/31)