Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectDistort.java @ 50be8733

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
import org.distorted.library.type.Static4D;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
/**
28
 * Distort the Mesh by applying a 3D vector of force.
29
 */
30
public class VertexEffectDistort extends VertexEffect
31
  {
32
  private Data3D mVector, mCenter;
33
  private Data4D mRegion;
34

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

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

    
129
        "vec3 center = vUniforms[effect+1].yzw;                        \n"
130
      + "vec3 ps = center-v;                                           \n"
131
      + "vec3 force = vUniforms[effect].xyz;                           \n"
132
      + "vec4 region= vUniforms[effect+2];                             \n"
133
      + "float d = degree(region,center,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 - (1.0 - sign(tp)));                   \n"
141

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

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

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

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
/**
163
 * Distort a (possibly changing in time) part of the Mesh by a (possibly changing in time) vector of force.
164
 *
165
 * @param vector vector of force the Center of the Effect is currently being dragged with.
166
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
167
 * @param region Region that masks the Effect.
168
 */
169
  public VertexEffectDistort(Data3D vector, Data3D center, Data4D region)
170
    {
171
    super(EffectName.DISTORT);
172
    mVector = vector;
173
    mCenter = center;
174
    mRegion = (region==null ? new Static4D(0,0,0, Float.MAX_VALUE) : region);
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
/**
179
 * Distort the whole Mesh by a (possibly changing in time) vector of force.
180
 *
181
 * @param vector vector of force the Center of the Effect is currently being dragged with.
182
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
183
 */
184
  public VertexEffectDistort(Data3D vector, Data3D center)
185
    {
186
    super(EffectName.DISTORT);
187
    mVector = vector;
188
    mCenter = center;
189
    mRegion = new Static4D(0,0,0, Float.MAX_VALUE);
190
    }
191
  }
192

    
193

    
(22-22/26)