Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectDistort.java @ 7bebb196

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
// Point (Px,Py) gets moved by vector (Wx,Wy,Wz) where Wx/Wy = Vx/Vy i.e. Wx=aVx and Wy=aVy where
52
// 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)
53
// 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) ]
54
// Computations above are valid for screen (0,0)x(w,h) but here we have (-w/2,-h/2)x(w/2,h/2)
55
//
56
// the vertical part
57
// 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.
58
// H(v.x,v.y) = |PS|>|SX| ? 0 : f(|PX|)
59
// 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|)
60
// ( i.e. normalize( dx, dy, -|PS|/f'(|PX|))
61
//
62
// 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.
63
// Solution:
64
// 1. Decompose the V into two subcomponents, one parallel to SX and another perpendicular.
65
// 2. Convince yourself (draw!) that the perpendicular component has no effect on normals.
66
// 3. The parallel component changes the length of |SX| by the factor of a=(|SX|-|Vpar|)/|SX| (where the length
67
//    can be negative depending on the direction)
68
// 4. that in turn leaves the x and y parts of the normal unchanged and multiplies the z component by a!
69
//
70
// |Vpar| = (u_dVx[i]*dx - u_dVy[i]*dy) / sqrt(ps_sq) = (Vx*dx-Vy*dy)/ sqrt(ps_sq)  (-Vy because y is inverted)
71
// 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
72
//
73
// Side of the bubble
74
//
75
// choose from one of the three bubble shapes: the cone, the thin bubble and the thick bubble
76
// Case 1:
77
// f(t) = t, i.e. f(x) = uz * x/|SX|   (a cone)
78
// -|PS|/f'(|PX|) = -|PS|*|SX|/uz but since ps_sq=|PS|^2 and d=|PX|/|SX| then |PS|*|SX| = ps_sq/(1-d)
79
// so finally -|PS|/f'(|PX|) = -ps_sq/(uz*(1-d))
80
//
81
// Case 2:
82
// f(t) = 3t^2 - 2t^3 --> f(0)=0, f'(0)=0, f'(1)=0, f(1)=1 (the bell curve)
83
// here we have t = x/|SX| which makes f'(|PX|) = 6*uz*|PS|*|PX|/|SX|^3.
84
// so -|PS|/f'(|PX|) = (-|SX|^3)/(6uz|PX|) =  (-|SX|^2) / (6*uz*d) but
85
// d = |PX|/|SX| and ps_sq = |PS|^2 so |SX|^2 = ps_sq/(1-d)^2
86
// so finally -|PS|/f'(|PX|) = -ps_sq/ (6uz*d*(1-d)^2)
87
//
88
// Case 3:
89
// 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,
90
// f(0.5)=0.7 and f'(t)= t(t-1)^2 >=0 for t>=0 so this produces a fuller, thicker bubble!
91
// then -|PS|/f'(|PX|) = (-|PS|*|SX)) / (12uz*d*(d-1)^2) but |PS|*|SX| = ps_sq/(1-d) (see above!)
92
// so finally -|PS|/f'(|PX|) = -ps_sq/ (12uz*d*(1-d)^3)
93
//
94
// Now, new requirement: we have to be able to add up normal vectors, i.e. distort already distorted surfaces.
95
// 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 ).
96
// 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)
97
// 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
98
// can simply add up the first and second components.
99
//
100
// Thus we actually want to compute N(v.x,v.y) = a*(-(dx/|PS|)*f'(|PX|), -(dy/|PS|)*f'(|PX|), 1) and keep adding
101
// the first two components. (a is the horizontal part)
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
/**
104
 * Have to call this before the shaders get compiled (i.e before Distorted.onCreate()) for the Effect to work.
105
 */
106
  public static void enable()
107
    {
108
    addEffect(EffectName.DISTORT,
109

    
110
        "vec3 center = vUniforms[effect+1].yzw; \n"
111
      + "vec3 ps = center-v; \n"
112
      + "vec3 force = vUniforms[effect].xyz; \n"
113
      + "float d = degree(vUniforms[effect+2],center,ps); \n"
114
      + "float denom = dot(ps+(1.0-d)*force,ps); \n"
115
      + "float one_over_denom = 1.0/(denom-0.001*(sign(denom)-1.0)); \n"          // = denom==0 ? 1000:1/denom;
116

    
117
       //v.z += force.z*d;                                                        // cone
118
       //b = -(force.z*(1.0-d))*one_over_denom;                                   //
119

    
120
       //v.z += force.z*d*d*(3.0-2.0*d);                                          // thin bubble
121
       //b = -(6.0*force.z*d*(1.0-d)*(1.0-d))*one_over_denom;                     //
122

    
123
      + "v.z += force.z*d*d*(3.0*d*d -8.0*d +6.0); \n"                            // thick bubble
124
      + "float b = -(12.0*force.z*d*(1.0-d)*(1.0-d)*(1.0-d))*one_over_denom; \n"  //
125

    
126
      + "v.xy += d*force.xy; \n"
127
      + "n.xy += n.z*b*ps.xy;"
128
      );
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
/**
133
 * Distort a (possibly changing in time) part of the Mesh by a (possibly changing in time) vector of force.
134
 *
135
 * @param vector vector of force the Center of the Effect is currently being dragged with.
136
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
137
 * @param region Region that masks the Effect.
138
 */
139
  public VertexEffectDistort(Data3D vector, Data3D center, Data4D region)
140
    {
141
    super(EffectName.DISTORT);
142
    mVector = vector;
143
    mCenter = center;
144
    mRegion = (region==null ? new Static4D(0,0,0, Float.MAX_VALUE) : region);
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
/**
149
 * Distort the whole Mesh by a (possibly changing in time) vector of force.
150
 *
151
 * @param vector vector of force the Center of the Effect is currently being dragged with.
152
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
153
 */
154
  public VertexEffectDistort(Data3D vector, Data3D center)
155
    {
156
    super(EffectName.DISTORT);
157
    mVector = vector;
158
    mCenter = center;
159
    mRegion = new Static4D(0,0,0, Float.MAX_VALUE);
160
    }
161
  }
162

    
163

    
(22-22/26)