Project

General

Profile

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

library / src / main / res / raw / main_vertex_shader.glsl @ 94f6d472

1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 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
precision lowp float;
21

    
22
#if __VERSION__ != 100
23
in vec3 a_Position;                  // Per-vertex position.
24
in vec3 a_Normal;                    // Per-vertex normal vector.
25
in vec2 a_TexCoordinate;             // Per-vertex texture coordinate.
26
out vec3 v_Position;                 //
27
out vec3 v_Normal;                   //
28
out vec2 v_TexCoordinate;            //
29
#else
30
attribute vec3 a_Position;           // Per-vertex position.
31
attribute vec3 a_Normal;             // Per-vertex normal vector.
32
attribute vec2 a_TexCoordinate;      // Per-vertex texture coordinate.
33
varying vec3 v_Position;             //
34
varying vec3 v_Normal;               //
35
varying vec2 v_TexCoordinate;        //
36
#endif
37

    
38
uniform vec3 u_objD;                 // half of object width x half of object height X half the depth;
39
                                     // point (0,0,0) is the center of the object
40

    
41
uniform float u_Depth;               // max absolute value of v.z ; beyond that the vertex would be culled by the near or far planes.
42
                                     // I read OpenGL ES has a built-in uniform variable gl_DepthRange.near = n,
43
                                     // .far = f, .diff = f-n so maybe u_Depth is redundant
44
                                     // Update: this struct is only available in fragment shaders
45

    
46
uniform mat4 u_MVPMatrix;            // the combined model/view/projection matrix.
47
uniform mat4 u_MVMatrix;             // the combined model/view matrix.
48

    
49
#if NUM_VERTEX>0
50
uniform int vNumEffects;             // total number of vertex effects
51
uniform int vType[NUM_VERTEX];       // their types.
52
uniform vec4 vUniforms[3*NUM_VERTEX];// i-th effect is 3 consecutive vec4's: [3*i], [3*i+1], [3*i+2].
53
                                     // The first vec4 is the Interpolated values,
54
                                     // next is half cache half Center, the third -  the Region.
55

    
56
//////////////////////////////////////////////////////////////////////////////////////////////
57
// HELPER FUNCTIONS
58
//////////////////////////////////////////////////////////////////////////////////////////////
59
// The trick below is the if-less version of the
60
//
61
// t = dx<0.0 ? (u_objD.x-v.x) / (u_objD.x-ux) : (u_objD.x+v.x) / (u_objD.x+ux);
62
// h = dy<0.0 ? (u_objD.y-v.y) / (u_objD.y-uy) : (u_objD.y+v.y) / (u_objD.y+uy);
63
// d = min(t,h);
64
//
65
// float d = min(-ps.x/(sign(ps.x)*u_objD.x+p.x),-ps.y/(sign(ps.y)*u_objD.y+p.y))+1.0;
66
//
67
// We still have to avoid division by 0 when p.x = +- u_objD.x or p.y = +- u_objD.y (i.e on the edge of the Object)
68
// We do that by first multiplying the above 'float d' with sign(denominator1*denominator2)^2.
69
//
70
//////////////////////////////////////////////////////////////////////////////////////////////
71
// return degree of the point as defined by the bitmap rectangle
72

    
73
float degree_bitmap(in vec2 S, in vec2 PS)
74
  {
75
  vec2 A = sign(PS)*u_objD.xy + S;
76

    
77
  vec2 signA = sign(A);                           //
78
  vec2 signA_SQ = signA*signA;                    // div = PS/A if A!=0, 0 otherwise.
79
  vec2 div = signA_SQ*PS/(A-(vec2(1,1)-signA_SQ));//
80

    
81
  return 1.0-max(div.x,div.y);
82
  }
83

    
84
//////////////////////////////////////////////////////////////////////////////////////////////
85
// Return degree of the point as defined by the Region. Currently only supports circular regions.
86
//
87
// Let us first introduce some notation.
88
// Let 'PS' be the vector from point P (the current vertex) to point S (the center of the effect).
89
// Let region.xy be the vector from point S to point O (the center point of the region circle)
90
// Let region.z be the radius of the region circle.
91
// (This all should work regardless if S is inside or outside of the circle).
92
//
93
// Then, the degree of a point with respect to a given (circular!) Region is defined by:
94
//
95
// If P is outside the circle, return 0.
96
// Otherwise, let X be the point where the halfline SP meets the region circle - then return |PX|/||SX|,
97
// aka the 'degree' of point P.
98
//
99
// We solve the triangle OPX.
100
// We know the lengths |PO|, |OX| and the angle OPX, because cos(OPX) = cos(180-OPS) = -cos(OPS) = -PS*PO/(|PS|*|PO|)
101
// then from the law of cosines PX^2 + PO^2 - 2*PX*PO*cos(OPX) = OX^2 so PX = -a + sqrt(a^2 + OX^2 - PO^2)
102
// where a = PS*PO/|PS| but we are really looking for d = |PX|/(|PX|+|PS|) = 1/(1+ (|PS|/|PX|) ) and
103
// |PX|/|PS| = -b + sqrt(b^2 + (OX^2-PO^2)/PS^2) where b=PS*PO/|PS|^2 which can be computed with only one sqrt.
104

    
105
float degree_region(in vec4 region, in vec2 PS)
106
  {
107
  vec2 PO  = PS + region.xy;
108
  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
109

    
110
  if( D<=0.0 ) return 0.0;
111

    
112
  float ps_sq = dot(PS,PS);
113
  float one_over_ps_sq = 1.0/(ps_sq-(sign(ps_sq)-1.0));  // return 1.0 if ps_sq = 0.0
114
                                                         // Important: if we want to write
115
                                                         // b = 1/a if a!=0, b=1 otherwise
116
                                                         // we need to write that as
117
                                                         // b = 1 / ( a-(sign(a)-1) )
118
                                                         // [ and NOT 1 / ( a + 1 - sign(a) ) ]
119
                                                         // because the latter, if 0<a<2^-24,
120
                                                         // will suffer from round-off error and in this case
121
                                                         // a + 1.0 = 1.0 !! so 1 / (a+1-sign(a)) = 1/0 !
122
  float DOT  = dot(PS,PO)*one_over_ps_sq;
123

    
124
  return 1.0 / (1.0 + 1.0/(sqrt(DOT*DOT+D*one_over_ps_sq)-DOT));
125
  }
126

    
127
//////////////////////////////////////////////////////////////////////////////////////////////
128
// return min(degree_bitmap,degree_region). Just like degree_region, currently only supports circles.
129

    
130
float degree(in vec4 region, in vec2 S, in vec2 PS)
131
  {
132
  vec2 PO  = PS + region.xy;
133
  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
134

    
135
  if( D<=0.0 ) return 0.0;
136

    
137
  vec2 A = sign(PS)*u_objD.xy + S;
138
  vec2 signA = sign(A);
139
  vec2 signA_SQ = signA*signA;
140
  vec2 div = signA_SQ*PS/(A-(vec2(1,1)-signA_SQ));
141
  float E = 1.0-max(div.x,div.y);
142

    
143
  float ps_sq = dot(PS,PS);
144
  float one_over_ps_sq = 1.0/(ps_sq-(sign(ps_sq)-1.0));  // return 1.0 if ps_sq = 0.0
145
  float DOT  = dot(PS,PO)*one_over_ps_sq;
146

    
147
  return min(1.0/(1.0 + 1.0/(sqrt(DOT*DOT+D*one_over_ps_sq)-DOT)),E);
148
  }
149

    
150
//////////////////////////////////////////////////////////////////////////////////////////////
151
// Clamp v.z to (-u_Depth,u_Depth) with the following function:
152
// define h to be, say, 0.7; let H=u_Depth
153
//      if v.z < -hH then v.z = (-(1-h)^2 * H^2)/(v.z+(2h-1)H) -H   (function satisfying f(-hH)=-hH, f'(-hH)=1, lim f(x) = -H)
154
// else if v.z >  hH then v.z = (-(1-h)^2 * H^2)/(v.z-(2h-1)H) +H   (function satisfying f(+hH)=+hH, f'(+hH)=1, lim f(x) = +H)
155
// else v.z = v.z
156

    
157
void restrictZ(inout float v)
158
  {
159
  const float h = 0.7;
160
  float signV = 2.0*max(0.0,sign(v))-1.0;
161
  float c = ((1.0-h)*(h-1.0)*u_Depth*u_Depth)/(v-signV*(2.0*h-1.0)*u_Depth) +signV*u_Depth;
162
  float b = max(0.0,sign(abs(v)-h*u_Depth));
163

    
164
  v = b*c+(1.0-b)*v; // Avoid branching: if abs(v)>h*u_Depth, then v=c; otherwise v=v.
165
  }
166

    
167
//////////////////////////////////////////////////////////////////////////////////////////////
168
// DEFORM EFFECT
169
//
170
// Deform the whole shape of the Object by force V. Algorithm is as follows:
171
//
172
// Suppose we apply force (Vx,Vy) at point (Cx,Cy) (i.e. the center of the effect). Then, first of all,
173
// divide the rectangle into 4 smaller rectangles along the 1 horizontal + 1 vertical lines that pass
174
// through (Cx,Cy). Now suppose we have already understood the following case:
175
//
176
// A vertical (0,Vy) force applied to a rectangle (WxH) in size, at center which is the top-left corner
177
// of the rectangle.  (*)
178
//
179
// If we understand (*), then we understand everything, because in order to compute the movement of the
180
// whole rectangle we can apply (*) 8 times: for each one of the 4 sub-rectangles, apply (*) twice,
181
// once for the vertical component of the force vector, the second time for the horizontal one.
182
//
183
// Let's then compute (*):
184
// 1) the top-left point will move by exactly (0,Vy)
185
// 2) we arbitrarily decide that the top-right point will move by (|Vy|/(|Vy|+A*W))*Vy, where A is some
186
//    arbitrary constant (const float A below). The F(V,W) = (|Vy|/(|Vy|+A*W)) comes from the following:
187
//    a) we want F(V,0) = 1
188
//    b) we want lim V->inf (F) = 1
189
//    c) we actually want F() to only depend on W/V, which we have here.
190
// 3) then the top edge of the rectangle will move along the line Vy*G(x), where G(x) = (1 - (A*W/(|Vy|+A*W))*(x/W)^2)
191
// 4) Now we decide that the left edge of the rectangle will move along Vy*H(y), where H(y) = (1 - |y|/(|Vy|+C*|y|))
192
//    where C is again an arbitrary constant. Again, H(y) comes from the requirement that no matter how
193
//    strong we push the left edge of the rectangle up or down, it can never 'go over itself', but its
194
//    length will approach 0 if squeezed very hard.
195
// 5) The last point we need to compute is the left-right motion of the top-right corner (i.e. if we push
196
//    the top-left corner up very hard, we want to have the top-right corner not only move up, but also to
197
//    the left at least a little bit).
198
//    We arbitrarily decide that, in addition to moving up-down by Vy*F(V,W), the corner will also move
199
//    left-right by I(V,W) = B*W*F(V,W), where B is again an arbitrary constant.
200
// 6) combining 3), 4) and 5) together, we arrive at a movement of an arbitrary point (x,y) away from the
201
//    top-left corner:
202
//    X(x,y) = -B*x * (|Vy|/(|Vy|+A*W)) * (1-(y/H)^2)                               (**)
203
//    Y(x,y) = Vy * (1 - |y|/(|Vy|+C*|y|)) * (1 - (A*W/(|Vy|+A*W))*(x/W)^2)         (**)
204
//
205
// We notice that formulas (**) have been construed so that it is possible to continously mirror them
206
// left-right and up-down (i.e. apply not only to the 'bottom-right' rectangle of the 4 subrectangles
207
// but to all 4 of them!).
208
//
209
// Constants:
210
// a) A : valid values: (0,infinity). 'Bendiness' if the surface - the higher A is, the more the surface
211
//        bends. A<=0 destroys the system.
212
// b) B : valid values: <-1,1>. The amount side edges get 'sucked' inwards when we pull the middle of the
213
//        top edge up. B=0 --> not at all, B=1: a looot. B=-0.5: the edges will actually be pushed outwards
214
//        quite a bit. One can also set it to <-1 or >1, but it will look a bit ridiculous.
215
// c) C : valid values: <1,infinity). The derivative of the H(y) function at 0, i.e. the rate of 'squeeze'
216
//        surface gets along the force line. C=1: our point gets pulled very closely to points above it
217
//        even when we apply only small vertical force to it. The higher C is, the more 'uniform' movement
218
//        along the force line is.
219
//        0<=C<1 looks completely ridiculous and C<0 destroys the system.
220

    
221
#ifdef DEFORM
222
void deform(in int effect, inout vec3 v, inout vec3 n)
223
  {
224
  const vec2 ONE = vec2(1.0,1.0);
225

    
226
  const float A = 0.5;
227
  const float B = 0.2;
228
  const float C = 5.0;
229

    
230
  vec2 center = vUniforms[effect+1].yz;
231
  vec2 ps     = center-v.xy;
232
  vec2 aPS    = abs(ps);
233
  vec2 maxps  = u_objD.xy + abs(center);
234
  float d     = degree_region(vUniforms[effect+2],ps);
235
  vec3 force  = vUniforms[effect].xyz * d;
236
  vec2 aForce = abs(force.xy);
237
  float denom = dot(ps+(1.0-d)*force.xy,ps);
238
  float one_over_denom = 1.0/(denom-0.001*(sign(denom)-1.0));
239
  vec2 Aw = A*maxps;
240
  vec2 quot = ps / maxps;
241
  quot = quot*quot;                          // ( (x/W)^2 , (y/H)^2 ) where x,y are distances from V to center
242

    
243
  float denomV = 1.0 / (aForce.y + Aw.x);
244
  float denomH = 1.0 / (aForce.x + Aw.y);
245

    
246
  vec2 vertCorr= ONE - aPS / ( aForce+C*aPS + (ONE-sign(aForce)) );  // avoid division by 0 when force and PS both are 0
247

    
248
  float mvXvert = -B * ps.x * aForce.y * (1.0-quot.y) * denomV;      // impact the vertical   component of the force vector has on horizontal movement
249
  float mvYhorz = -B * ps.y * aForce.x * (1.0-quot.x) * denomH;      // impact the horizontal component of the force vector has on vertical   movement
250
  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
251
  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
252

    
253
  v.x -= (mvXvert+mvXhorz);
254
  v.y -= (mvYvert+mvYhorz);
255

    
256
  v.z += force.z*d*d*(3.0*d*d -8.0*d +6.0);                          // thick bubble
257
  float b = -(12.0*force.z*d*(1.0-d)*(1.0-d)*(1.0-d))*one_over_denom;//
258

    
259
  n.xy += n.z*b*ps;
260
  }
261
#endif
262

    
263
//////////////////////////////////////////////////////////////////////////////////////////////
264
// DISTORT EFFECT
265
//
266
// Point (Px,Py) gets moved by vector (Wx,Wy,Wz) where Wx/Wy = Vx/Vy i.e. Wx=aVx and Wy=aVy where
267
// 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)
268
// 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) ]
269
// Computations above are valid for screen (0,0)x(w,h) but here we have (-w/2,-h/2)x(w/2,h/2)
270
//
271
// the vertical part
272
// Let |(v.x,v.y),(ux,uy)| = |PS|, ux-v.x=dx,uy-v.y=dy, f(x) (0<=x<=|SX|) be the shape of the side of the bubble.
273
// H(v.x,v.y) = |PS|>|SX| ? 0 : f(|PX|)
274
// N(v.x,v.y) = |PS|>|SX| ? (0,0,1) : ( -(dx/|PS|)sin(beta), -(dy/|PS|)sin(beta), cos(beta) ) where tan(beta) is f'(|PX|)
275
// ( i.e. normalize( dx, dy, -|PS|/f'(|PX|))
276
//
277
// Now we also have to take into account the effect horizontal move by V=(u_dVx[i],u_dVy[i]) will have on the normal vector.
278
// Solution:
279
// 1. Decompose the V into two subcomponents, one parallel to SX and another perpendicular.
280
// 2. Convince yourself (draw!) that the perpendicular component has no effect on normals.
281
// 3. The parallel component changes the length of |SX| by the factor of a=(|SX|-|Vpar|)/|SX| (where the length
282
//    can be negative depending on the direction)
283
// 4. that in turn leaves the x and y parts of the normal unchanged and multiplies the z component by a!
284
//
285
// |Vpar| = (u_dVx[i]*dx - u_dVy[i]*dy) / sqrt(ps_sq) = (Vx*dx-Vy*dy)/ sqrt(ps_sq)  (-Vy because y is inverted)
286
// 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
287
//
288
// Side of the bubble
289
//
290
// choose from one of the three bubble shapes: the cone, the thin bubble and the thick bubble
291
// Case 1:
292
// f(t) = t, i.e. f(x) = uz * x/|SX|   (a cone)
293
// -|PS|/f'(|PX|) = -|PS|*|SX|/uz but since ps_sq=|PS|^2 and d=|PX|/|SX| then |PS|*|SX| = ps_sq/(1-d)
294
// so finally -|PS|/f'(|PX|) = -ps_sq/(uz*(1-d))
295
//
296
// Case 2:
297
// f(t) = 3t^2 - 2t^3 --> f(0)=0, f'(0)=0, f'(1)=0, f(1)=1 (the bell curve)
298
// here we have t = x/|SX| which makes f'(|PX|) = 6*uz*|PS|*|PX|/|SX|^3.
299
// so -|PS|/f'(|PX|) = (-|SX|^3)/(6uz|PX|) =  (-|SX|^2) / (6*uz*d) but
300
// d = |PX|/|SX| and ps_sq = |PS|^2 so |SX|^2 = ps_sq/(1-d)^2
301
// so finally -|PS|/f'(|PX|) = -ps_sq/ (6uz*d*(1-d)^2)
302
//
303
// Case 3:
304
// 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,
305
// f(0.5)=0.7 and f'(t)= t(t-1)^2 >=0 for t>=0 so this produces a fuller, thicker bubble!
306
// then -|PS|/f'(|PX|) = (-|PS|*|SX)) / (12uz*d*(d-1)^2) but |PS|*|SX| = ps_sq/(1-d) (see above!)
307
// so finally -|PS|/f'(|PX|) = -ps_sq/ (12uz*d*(1-d)^3)
308
//
309
// Now, new requirement: we have to be able to add up normal vectors, i.e. distort already distorted surfaces.
310
// 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 ).
311
// 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)
312
// 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
313
// can simply add up the first and second components.
314
//
315
// Thus we actually want to compute N(v.x,v.y) = a*(-(dx/|PS|)*f'(|PX|), -(dy/|PS|)*f'(|PX|), 1) and keep adding
316
// the first two components. (a is the horizontal part)
317

    
318
#ifdef DISTORT
319
void distort(in int effect, inout vec3 v, inout vec3 n)
320
  {
321
  vec2 center = vUniforms[effect+1].yz;
322
  vec2 ps = center-v.xy;
323
  vec3 force = vUniforms[effect].xyz;
324
  float d = degree(vUniforms[effect+2],center,ps);
325
  float denom = dot(ps+(1.0-d)*force.xy,ps);
326
  float one_over_denom = 1.0/(denom-0.001*(sign(denom)-1.0));          // = denom==0 ? 1000:1/denom;
327

    
328
  //v.z += force.z*d;                                                  // cone
329
  //b = -(force.z*(1.0-d))*one_over_denom;                             //
330

    
331
  //v.z += force.z*d*d*(3.0-2.0*d);                                    // thin bubble
332
  //b = -(6.0*force.z*d*(1.0-d)*(1.0-d))*one_over_denom;               //
333

    
334
  v.z += force.z*d*d*(3.0*d*d -8.0*d +6.0);                            // thick bubble
335
  float b = -(12.0*force.z*d*(1.0-d)*(1.0-d)*(1.0-d))*one_over_denom;  //
336

    
337
  v.xy += d*force.xy;
338
  n.xy += n.z*b*ps;
339
  }
340
#endif
341

    
342
//////////////////////////////////////////////////////////////////////////////////////////////
343
// SINK EFFECT
344
//
345
// Pull P=(v.x,v.y) towards center of the effect with P' = P + (1-h)*dist(S-P)
346
// when h>1 we are pushing points away from S: P' = P + (1/h-1)*dist(S-P)
347

    
348
#ifdef SINK
349
void sink(in int effect,inout vec3 v)
350
  {
351
  vec2 center = vUniforms[effect+1].yz;
352
  vec2 ps = center-v.xy;
353
  float h = vUniforms[effect].x;
354
  float t = degree(vUniforms[effect+2],center,ps) * (1.0-h)/max(1.0,h);
355

    
356
  v.xy += t*ps;
357
  }
358
#endif
359

    
360
//////////////////////////////////////////////////////////////////////////////////////////////
361
// PINCH EFFECT
362
//
363
// Pull P=(v.x,v.y) towards the line that
364
// a) passes through the center of the effect
365
// b) forms angle defined in the 2nd interpolated value with the X-axis
366
// with P' = P + (1-h)*dist(line to P)
367
// when h>1 we are pushing points away from S: P' = P + (1/h-1)*dist(line to P)
368

    
369
#ifdef PINCH
370
void pinch(in int effect,inout vec3 v)
371
  {
372
  vec2 center = vUniforms[effect+1].yz;
373
  vec2 ps = center-v.xy;
374
  float h = vUniforms[effect].x;
375
  float t = degree(vUniforms[effect+2],center,ps) * (1.0-h)/max(1.0,h);
376
  float angle = vUniforms[effect].y;
377
  vec2 dir = vec2(sin(angle),-cos(angle));
378

    
379
  v.xy += t*dot(ps,dir)*dir;
380
  }
381
#endif
382

    
383
//////////////////////////////////////////////////////////////////////////////////////////////
384
// SWIRL EFFECT
385
//
386
// Let d be the degree of the current vertex V with respect to center of the effect S and Region vRegion.
387
// This effect rotates the current vertex V by vInterpolated.x radians clockwise around the circle dilated
388
// by (1-d) around the center of the effect S.
389

    
390
#ifdef SWIRL
391
void swirl(in int effect, inout vec3 v)
392
  {
393
  vec2 center  = vUniforms[effect+1].yz;
394
  vec2 PS = center-v.xy;
395
  vec4 SO = vUniforms[effect+2];
396
  float d1_circle = degree_region(SO,PS);
397
  float d1_bitmap = degree_bitmap(center,PS);
398

    
399
  float alpha = vUniforms[effect].x;
400
  float sinA = sin(alpha);
401
  float cosA = cos(alpha);
402

    
403
  vec2 PS2 = vec2( PS.x*cosA+PS.y*sinA,-PS.x*sinA+PS.y*cosA ); // vector PS rotated by A radians clockwise around center.
404
  vec4 SG = (1.0-d1_circle)*SO;                                // coordinates of the dilated circle P is going to get rotated around
405
  float d2 = max(0.0,degree(SG,center,PS2));                   // make it a max(0,deg) because otherwise when center=left edge of the
406
                                                               // bitmap some points end up with d2<0 and they disappear off view.
407
  v.xy += min(d1_circle,d1_bitmap)*(PS - PS2/(1.0-d2));        // if d2=1 (i.e P=center) we should have P unchanged. How to do it?
408
  }
409
#endif
410

    
411
//////////////////////////////////////////////////////////////////////////////////////////////
412
// WAVE EFFECT
413
//
414
// Directional sinusoidal wave effect.
415
//
416
// This is an effect from a (hopefully!) generic family of effects of the form (vec3 V: |V|=1 , f(x,y) )  (*)
417
// i.e. effects defined by a unit vector and an arbitrary function. Those effects are defined to move each
418
// point (x,y,0) of the XY plane to the point (x,y,0) + V*f(x,y).
419
//
420
// In this case V is defined by angles A and B (sines and cosines of which are precomputed in
421
// EffectQueueVertex and passed in the uniforms).
422
// Let's move V to start at the origin O, let point C be the endpoint of V, and let C' be C's projection
423
// to the XY plane. Then A is defined to be the angle C0C' and angle B is the angle C'O(axisY).
424
//
425
// Also, in this case f(x,y) = amplitude*sin(x/length), with those 2 parameters passed in uniforms.
426
//
427
//////////////////////////////////////////////////////////////////////////////////////////////
428
// How to compute any generic effect of type (*)
429
//////////////////////////////////////////////////////////////////////////////////////////////
430
//
431
// By definition, the vertices move by f(x,y)*V.
432
//
433
// Normals are much more complicated.
434
// Let angle X be the angle (0,Vy,Vz)(0,Vy,0)(Vx,Vy,Vz).
435
// Let angle Y be the angle (Vx,0,Vz)(Vx,0,0)(Vx,Vy,Vz).
436
//
437
// Then it can be shown that the resulting surface, at point to which point (x0,y0,0) got moved to,
438
// has 2 tangent vectors given by
439
//
440
// SX = (1.0+cosX*fx , cosY*sinX*fx , |sinY|*sinX*fx);  (**)
441
// SY = (cosX*sinY*fy , 1.0+cosY*fy , |sinX|*sinY*fy);  (***)
442
//
443
// and then obviously the normal N is given by N= SX x SY .
444
//
445
// We still need to remember the note from the distort function about adding up normals:
446
// we first need to 'normalize' the normals to make their third components equal, and then we
447
// simply add up the first and the second component while leaving the third unchanged.
448
//
449
// How to see facts (**) and (***) ? Briefly:
450
// a) compute the 2D analogon and conclude that in this case the tangent SX is given by
451
//    SX = ( cosA*f'(x) +1, sinA*f'(x) )    (where A is the angle vector V makes with X axis )
452
// b) cut the resulting surface with plane P which
453
//    - includes vector V
454
//    - crosses plane XY along line parallel to X axis
455
// c) apply the 2D analogon and notice that the tangent vector to the curve that is the common part of P
456
//    and our surface (I am talking about the tangent vector which belongs to P) is given by
457
//    (1+cosX*fx,0,sinX*fx) rotated by angle (90-|Y|) (where angles X,Y are defined above) along vector (1,0,0).
458
//
459
//    Matrix of rotation:
460
//
461
//    |sinY|  cosY
462
//    -cosY  |sinY|
463
//
464
// d) compute the above and see that this is equal precisely to SX from (**).
465
// e) repeat points b,c,d in direction Y and come up with (***).
466
//
467
//////////////////////////////////////////////////////////////////////////////////////////////
468
// Note: we should avoid passing certain combinations of parameters to this function. One such known
469
// combination is ( A: small but positive, B: any, amplitude >= length ).
470
// In this case, certain 'unlucky' points have their normals almost horizontal (they got moved by (almost!)
471
// amplitude, and other point length (i.e. <=amplitude) away got moved by 0, so the slope in this point is
472
// very steep). Visual effect is: vast majority of surface pretty much unchanged, but random 'unlucky'
473
// points very dark)
474
//
475
// Generally speaking I'd keep to amplitude < length, as the opposite case has some other problems as well.
476

    
477
#ifdef WAVE
478
void wave(in int effect, inout vec3 v, inout vec3 n)
479
  {
480
  vec2 center     = vUniforms[effect+1].yz;
481
  float amplitude = vUniforms[effect  ].x;
482
  float length    = vUniforms[effect  ].y;
483

    
484
  vec2 ps = center - v.xy;
485
  float deg = amplitude*degree_region(vUniforms[effect+2],ps);
486

    
487
  if( deg != 0.0 && length != 0.0 )
488
    {
489
    float phase = vUniforms[effect  ].z;
490
    float alpha = vUniforms[effect  ].w;
491
    float beta  = vUniforms[effect+1].x;
492

    
493
    float sinA = sin(alpha);
494
    float cosA = cos(alpha);
495
    float sinB = sin(beta);
496
    float cosB = cos(beta);
497

    
498
    float angle= 1.578*(ps.x*cosB-ps.y*sinB) / length + phase;
499

    
500
    vec3 dir= vec3(sinB*cosA,cosB*cosA,sinA);
501

    
502
    v += sin(angle)*deg*dir;
503

    
504
    if( n.z != 0.0 )
505
      {
506
      float sqrtX = sqrt(dir.y*dir.y + dir.z*dir.z);
507
      float sqrtY = sqrt(dir.x*dir.x + dir.z*dir.z);
508

    
509
      float sinX = ( sqrtY==0.0 ? 0.0 : dir.z / sqrtY);
510
      float cosX = ( sqrtY==0.0 ? 1.0 : dir.x / sqrtY);
511
      float sinY = ( sqrtX==0.0 ? 0.0 : dir.z / sqrtX);
512
      float cosY = ( sqrtX==0.0 ? 1.0 : dir.y / sqrtX);
513

    
514
      float abs_z = dir.z <0.0 ? -(sinX*sinY) : (sinX*sinY);
515

    
516
      float tmp = 1.578*cos(angle)*deg/length;
517

    
518
      float fx =-cosB*tmp;
519
      float fy = sinB*tmp;
520

    
521
      vec3 sx = vec3 (1.0+cosX*fx,cosY*sinX*fx,abs_z*fx);
522
      vec3 sy = vec3 (cosX*sinY*fy,1.0+cosY*fy,abs_z*fy);
523

    
524
      vec3 normal = cross(sx,sy);
525

    
526
      if( normal.z<=0.0 )                   // Why this bizarre shit rather than the straightforward
527
        {                                   //
528
        normal.x= 0.0;                      // if( normal.z>0.0 )
529
        normal.y= 0.0;                      //   {
530
        normal.z= 1.0;                      //   n.x = (n.x*normal.z + n.z*normal.x);
531
        }                                   //   n.y = (n.y*normal.z + n.z*normal.y);
532
                                            //   n.z = (n.z*normal.z);
533
                                            //   }
534
      n.x = (n.x*normal.z + n.z*normal.x);  //
535
      n.y = (n.y*normal.z + n.z*normal.y);  // ? Because if we do the above, my shitty Nexus4 crashes
536
      n.z = (n.z*normal.z);                 // during shader compilation!
537
      }
538
    }
539
  }
540
#endif
541

    
542
#endif  // NUM_VERTEX>0
543

    
544
//////////////////////////////////////////////////////////////////////////////////////////////
545

    
546
void main()
547
  {
548
  vec3 v = 2.0*u_objD*a_Position;
549
  vec3 n = a_Normal;
550

    
551
#if NUM_VERTEX>0
552
  int j=0;
553

    
554
  for(int i=0; i<vNumEffects; i++)
555
    {
556
#ifdef DISTORT
557
    if( vType[i]==DISTORT) distort(j,v,n); else
558
#endif
559
#ifdef DEFORM
560
    if( vType[i]==DEFORM ) deform (j,v,n); else
561
#endif
562
#ifdef SINK
563
    if( vType[i]==SINK   ) sink   (j,v);   else
564
#endif
565
#ifdef PINCH
566
    if( vType[i]==PINCH  ) pinch  (j,v);   else
567
#endif
568
#ifdef SWIRL
569
    if( vType[i]==SWIRL  ) swirl  (j,v);   else
570
#endif
571
#ifdef WAVE
572
    if( vType[i]==WAVE   ) wave   (j,v,n); else
573
#endif
574
    {}
575

    
576
    j+=3;
577
    }
578

    
579
  restrictZ(v.z);
580
#endif
581
   
582
  v_Position      = v;
583
  v_TexCoordinate = a_TexCoordinate;
584
  v_Normal        = normalize(vec3(u_MVMatrix*vec4(n,0.0)));
585
  gl_Position     = u_MVPMatrix*vec4(v,1.0);
586
  }                               
(7-7/9)