Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectDistort.java @ 3521c6fe

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
public class VertexEffectDistort extends VertexEffect
29
  {
30
  private Data3D mVector, mCenter;
31
  private Data4D mRegion;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
/**
35
 * Distort a (possibly changing in time) part of the Object by a (possibly changing in time) vector of force.
36
 *
37
 * @param vector vector of force the Center of the Effect is currently being dragged with.
38
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
39
 * @param region Region that masks the Effect.
40
 */
41
  public VertexEffectDistort(Data3D vector, Data3D center, Data4D region)
42
    {
43
    super(EffectName.DISTORT);
44
    mVector = vector;
45
    mCenter = center;
46
    mRegion = (region==null ? new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE) : region);
47
    }
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
/**
51
 * Distort the whole Object by a (possibly changing in time) vector of force.
52
 *
53
 * @param vector vector of force the Center of the Effect is currently being dragged with.
54
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
55
 */
56
  public VertexEffectDistort(Data3D vector, Data3D center)
57
    {
58
    super(EffectName.DISTORT);
59
    mVector = vector;
60
    mCenter = center;
61
    mRegion = new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE);
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
67
    {
68
    mCenter.get(uniforms,index+5,currentDuration,step);
69
    mRegion.get(uniforms,index+8,currentDuration,step);
70
    boolean ret = mVector.get(uniforms,index,currentDuration,step);
71

    
72
    uniforms[index+1] =-uniforms[index+1];
73
    uniforms[index+9] =-uniforms[index+9];
74

    
75
    return ret;
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
// Point (Px,Py) gets moved by vector (Wx,Wy,Wz) where Wx/Wy = Vx/Vy i.e. Wx=aVx and Wy=aVy where
80
// 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)
81
// 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) ]
82
// Computations above are valid for screen (0,0)x(w,h) but here we have (-w/2,-h/2)x(w/2,h/2)
83
//
84
// the vertical part
85
// 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.
86
// H(v.x,v.y) = |PS|>|SX| ? 0 : f(|PX|)
87
// 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|)
88
// ( i.e. normalize( dx, dy, -|PS|/f'(|PX|))
89
//
90
// 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.
91
// Solution:
92
// 1. Decompose the V into two subcomponents, one parallel to SX and another perpendicular.
93
// 2. Convince yourself (draw!) that the perpendicular component has no effect on normals.
94
// 3. The parallel component changes the length of |SX| by the factor of a=(|SX|-|Vpar|)/|SX| (where the length
95
//    can be negative depending on the direction)
96
// 4. that in turn leaves the x and y parts of the normal unchanged and multiplies the z component by a!
97
//
98
// |Vpar| = (u_dVx[i]*dx - u_dVy[i]*dy) / sqrt(ps_sq) = (Vx*dx-Vy*dy)/ sqrt(ps_sq)  (-Vy because y is inverted)
99
// 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
100
//
101
// Side of the bubble
102
//
103
// choose from one of the three bubble shapes: the cone, the thin bubble and the thick bubble
104
// Case 1:
105
// f(t) = t, i.e. f(x) = uz * x/|SX|   (a cone)
106
// -|PS|/f'(|PX|) = -|PS|*|SX|/uz but since ps_sq=|PS|^2 and d=|PX|/|SX| then |PS|*|SX| = ps_sq/(1-d)
107
// so finally -|PS|/f'(|PX|) = -ps_sq/(uz*(1-d))
108
//
109
// Case 2:
110
// f(t) = 3t^2 - 2t^3 --> f(0)=0, f'(0)=0, f'(1)=0, f(1)=1 (the bell curve)
111
// here we have t = x/|SX| which makes f'(|PX|) = 6*uz*|PS|*|PX|/|SX|^3.
112
// so -|PS|/f'(|PX|) = (-|SX|^3)/(6uz|PX|) =  (-|SX|^2) / (6*uz*d) but
113
// d = |PX|/|SX| and ps_sq = |PS|^2 so |SX|^2 = ps_sq/(1-d)^2
114
// so finally -|PS|/f'(|PX|) = -ps_sq/ (6uz*d*(1-d)^2)
115
//
116
// Case 3:
117
// 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,
118
// f(0.5)=0.7 and f'(t)= t(t-1)^2 >=0 for t>=0 so this produces a fuller, thicker bubble!
119
// then -|PS|/f'(|PX|) = (-|PS|*|SX)) / (12uz*d*(d-1)^2) but |PS|*|SX| = ps_sq/(1-d) (see above!)
120
// so finally -|PS|/f'(|PX|) = -ps_sq/ (12uz*d*(1-d)^3)
121
//
122
// Now, new requirement: we have to be able to add up normal vectors, i.e. distort already distorted surfaces.
123
// 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 ).
124
// 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)
125
// 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
126
// can simply add up the first and second components.
127
//
128
// Thus we actually want to compute N(v.x,v.y) = a*(-(dx/|PS|)*f'(|PX|), -(dy/|PS|)*f'(|PX|), 1) and keep adding
129
// the first two components. (a is the horizontal part)
130

    
131
  public static void enable()
132
    {
133
    addEffect(EffectName.DISTORT,
134

    
135
        "vec2 center = vUniforms[effect+1].yz; \n"
136
      + "vec2 ps = center-v.xy; \n"
137
      + "vec3 force = vUniforms[effect].xyz; \n"
138
      + "float d = degree(vUniforms[effect+2],center,ps); \n"
139
      + "float denom = dot(ps+(1.0-d)*force.xy,ps); \n"
140
      + "float one_over_denom = 1.0/(denom-0.001*(sign(denom)-1.0)); \n"          // = denom==0 ? 1000:1/denom;
141

    
142
       //v.z += force.z*d;                                                        // cone
143
       //b = -(force.z*(1.0-d))*one_over_denom;                                   //
144

    
145
       //v.z += force.z*d*d*(3.0-2.0*d);                                          // thin bubble
146
       //b = -(6.0*force.z*d*(1.0-d)*(1.0-d))*one_over_denom;                     //
147

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

    
151
      + "v.xy += d*force.xy; \n"
152
      + "n.xy += n.z*b*ps;"
153
      );
154
    }
155
  }
156

    
157

    
(22-22/26)