1 |
125cee3d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2017 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
46b572b5
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
125cee3d
|
Leszek Koltunski
|
// //
|
6 |
46b572b5
|
Leszek Koltunski
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
125cee3d
|
Leszek Koltunski
|
// 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 |
46b572b5
|
Leszek Koltunski
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
125cee3d
|
Leszek Koltunski
|
// 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 |
46b572b5
|
Leszek Koltunski
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
125cee3d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.library.effect;
|
21 |
|
|
|
22 |
|
|
import org.distorted.library.type.Data1D;
|
23 |
|
|
import org.distorted.library.type.Data3D;
|
24 |
|
|
import org.distorted.library.type.Data4D;
|
25 |
|
|
|
26 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
27 |
faa3ff56
|
Leszek Koltunski
|
/**
|
28 |
|
|
* Pull all points around the center of the Effect towards the center point (if degree>=1) or push them
|
29 |
|
|
* away from it (degree<=1).
|
30 |
|
|
*/
|
31 |
125cee3d
|
Leszek Koltunski
|
public class VertexEffectSink extends VertexEffect
|
32 |
|
|
{
|
33 |
0dd98279
|
Leszek Koltunski
|
private Data1D mSink;
|
34 |
|
|
private Data3D mCenter;
|
35 |
|
|
private Data4D mRegion;
|
36 |
|
|
|
37 |
125cee3d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38 |
6bb59aad
|
Leszek Koltunski
|
/**
|
39 |
faa3ff56
|
Leszek Koltunski
|
* Only for use by the library itself.
|
40 |
6bb59aad
|
Leszek Koltunski
|
*
|
41 |
faa3ff56
|
Leszek Koltunski
|
* @y.exclude
|
42 |
6bb59aad
|
Leszek Koltunski
|
*/
|
43 |
15aa7d94
|
Leszek Koltunski
|
public boolean compute(float[] uniforms, int index, long currentDuration, long step )
|
44 |
|
|
{
|
45 |
b24e4719
|
Leszek Koltunski
|
mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
|
46 |
|
|
mRegion.get(uniforms,index+REGION_OFFSET,currentDuration,step);
|
47 |
7bebb196
|
Leszek Koltunski
|
return mSink.get(uniforms,index,currentDuration,step);
|
48 |
125cee3d
|
Leszek Koltunski
|
}
|
49 |
7cd24173
|
leszek
|
|
50 |
faa3ff56
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51 |
|
|
// PUBLIC API
|
52 |
7cd24173
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53 |
|
|
// Pull P=(v.x,v.y) towards center of the effect with P' = P + (1-h)*dist(S-P)
|
54 |
|
|
// when h>1 we are pushing points away from S: P' = P + (1/h-1)*dist(S-P)
|
55 |
faa3ff56
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
56 |
|
|
/**
|
57 |
7602a827
|
Leszek Koltunski
|
* Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
|
58 |
faa3ff56
|
Leszek Koltunski
|
*/
|
59 |
7cd24173
|
leszek
|
public static void enable()
|
60 |
|
|
{
|
61 |
|
|
addEffect(EffectName.SINK,
|
62 |
|
|
|
63 |
5f76092f
|
Leszek Koltunski
|
"vec3 center = vUniforms[effect+1].yzw; \n"
|
64 |
|
|
+ "vec3 ps = center-v; \n"
|
65 |
|
|
+ "float h = vUniforms[effect].x; \n"
|
66 |
|
|
+ "float deg = degree(vUniforms[effect+2],center,ps);\n"
|
67 |
|
|
+ "float t = deg * (1.0-h)/max(1.0,h); \n"
|
68 |
|
|
+ "v += t*ps; \n"
|
69 |
7cd24173
|
leszek
|
|
70 |
5f76092f
|
Leszek Koltunski
|
+ "const float A = 1.6; \n" // max amount of change in normal vector when pulling
|
71 |
|
|
+ "const float B = 10.0; \n" // when pushing
|
72 |
569ea22c
|
Leszek Koltunski
|
+ "vec3 n_ps = dot(ps,n)*ps; \n" // n_ps = (component of n that's parallel to ps) * |ps|^2 (=dot(ps,ps))
|
73 |
5f76092f
|
Leszek Koltunski
|
+ "float dot_ps = dot(ps,ps); \n"
|
74 |
|
|
+ "float sign_ps= sign(dot_ps); \n"
|
75 |
569ea22c
|
Leszek Koltunski
|
+ "n_ps = (sign_ps*n_ps)/(dot_ps-(sign_ps-1.0)); \n" // uff! now n_ps is the parallel to ps component of n, even if ps==0
|
76 |
5f76092f
|
Leszek Koltunski
|
+ "float move = deg*(h-1.0)/(h/B+1.0/A); \n" // move(0)=-A*deg, move(1)=0, move(inf)=B*deg
|
77 |
569ea22c
|
Leszek Koltunski
|
+ "n += move*n_ps; \n"
|
78 |
5f76092f
|
Leszek Koltunski
|
+ "n = normalize(n);"
|
79 |
7cd24173
|
leszek
|
);
|
80 |
|
|
}
|
81 |
faa3ff56
|
Leszek Koltunski
|
|
82 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
83 |
|
|
/**
|
84 |
|
|
* Pull all points around the center of the Effect towards the center point (if degree>=1) or push them
|
85 |
|
|
* away from it (degree<=1)
|
86 |
|
|
*
|
87 |
|
|
* @param sink The current degree of the Effect.
|
88 |
|
|
* @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
|
89 |
|
|
* @param region Region that masks the Effect.
|
90 |
|
|
*/
|
91 |
|
|
public VertexEffectSink(Data1D sink, Data3D center, Data4D region)
|
92 |
|
|
{
|
93 |
|
|
super(EffectName.SINK);
|
94 |
|
|
mSink = sink;
|
95 |
|
|
mCenter = center;
|
96 |
dd89c7f4
|
Leszek Koltunski
|
mRegion = (region==null ? MAX_REGION : region);
|
97 |
faa3ff56
|
Leszek Koltunski
|
}
|
98 |
|
|
|
99 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100 |
|
|
/**
|
101 |
|
|
* Pull all points around the center of the Effect towards the center point(if degree>=1) or push them
|
102 |
|
|
* away from it (degree<=1)
|
103 |
|
|
*
|
104 |
|
|
* @param sink The current degree of the Effect.
|
105 |
|
|
* @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
|
106 |
|
|
*/
|
107 |
|
|
public VertexEffectSink(Data1D sink, Data3D center)
|
108 |
|
|
{
|
109 |
|
|
super(EffectName.SINK);
|
110 |
|
|
mSink = sink;
|
111 |
|
|
mCenter = center;
|
112 |
dd89c7f4
|
Leszek Koltunski
|
mRegion = MAX_REGION;
|
113 |
faa3ff56
|
Leszek Koltunski
|
}
|
114 |
125cee3d
|
Leszek Koltunski
|
}
|