1 |
125cee3d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
cc62b34a
|
Leszek Koltunski
|
// Copyright 2017 Leszek Koltunski leszek@koltunski.pl //
|
3 |
125cee3d
|
Leszek Koltunski
|
// //
|
4 |
46b572b5
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
125cee3d
|
Leszek Koltunski
|
// //
|
6 |
cc62b34a
|
Leszek Koltunski
|
// This library is free software; you can redistribute it and/or //
|
7 |
|
|
// modify it under the terms of the GNU Lesser General Public //
|
8 |
|
|
// License as published by the Free Software Foundation; either //
|
9 |
|
|
// version 2.1 of the License, or (at your option) any later version. //
|
10 |
125cee3d
|
Leszek Koltunski
|
// //
|
11 |
cc62b34a
|
Leszek Koltunski
|
// This library 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 |
cc62b34a
|
Leszek Koltunski
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
|
14 |
|
|
// Lesser General Public License for more details. //
|
15 |
125cee3d
|
Leszek Koltunski
|
// //
|
16 |
cc62b34a
|
Leszek Koltunski
|
// You should have received a copy of the GNU Lesser General Public //
|
17 |
|
|
// License along with this library; if not, write to the Free Software //
|
18 |
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19 |
125cee3d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20 |
|
|
|
21 |
|
|
package org.distorted.library.effect;
|
22 |
|
|
|
23 |
|
|
import org.distorted.library.type.Data1D;
|
24 |
|
|
import org.distorted.library.type.Data3D;
|
25 |
|
|
import org.distorted.library.type.Data4D;
|
26 |
|
|
|
27 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28 |
faa3ff56
|
Leszek Koltunski
|
/**
|
29 |
|
|
* Pull all points around the center of the Effect towards the center point (if degree>=1) or push them
|
30 |
|
|
* away from it (degree<=1).
|
31 |
|
|
*/
|
32 |
125cee3d
|
Leszek Koltunski
|
public class VertexEffectSink extends VertexEffect
|
33 |
|
|
{
|
34 |
f046b159
|
Leszek Koltunski
|
private static final EffectName NAME = EffectName.SINK;
|
35 |
|
|
|
36 |
0e1d3d2e
|
Leszek Koltunski
|
private final Data1D mSink;
|
37 |
|
|
private final Data3D mCenter;
|
38 |
|
|
private final Data4D mRegion;
|
39 |
0dd98279
|
Leszek Koltunski
|
|
40 |
125cee3d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41 |
6bb59aad
|
Leszek Koltunski
|
/**
|
42 |
faa3ff56
|
Leszek Koltunski
|
* Only for use by the library itself.
|
43 |
6bb59aad
|
Leszek Koltunski
|
*
|
44 |
faa3ff56
|
Leszek Koltunski
|
* @y.exclude
|
45 |
6bb59aad
|
Leszek Koltunski
|
*/
|
46 |
15aa7d94
|
Leszek Koltunski
|
public boolean compute(float[] uniforms, int index, long currentDuration, long step )
|
47 |
|
|
{
|
48 |
b24e4719
|
Leszek Koltunski
|
mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
|
49 |
|
|
mRegion.get(uniforms,index+REGION_OFFSET,currentDuration,step);
|
50 |
f046b159
|
Leszek Koltunski
|
return mSink.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
|
51 |
125cee3d
|
Leszek Koltunski
|
}
|
52 |
7cd24173
|
leszek
|
|
53 |
faa3ff56
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54 |
c226920c
|
Leszek Koltunski
|
// used to be simple:
|
55 |
|
|
// float t = deg * (1.0-h)/max(1.0,h);
|
56 |
|
|
// Changed to the four complicated lines because the previous Sink effect fails to fully inflate the
|
57 |
|
|
// Masterball.
|
58 |
|
|
// Current function inflates and deflates much more as H is at the extremes (close to 0 and inf)
|
59 |
f046b159
|
Leszek Koltunski
|
|
60 |
|
|
static String code()
|
61 |
7cd24173
|
leszek
|
{
|
62 |
f046b159
|
Leszek Koltunski
|
return
|
63 |
7cd24173
|
leszek
|
|
64 |
0f10a0b6
|
Leszek Koltunski
|
"vec3 ps = vUniforms[effect+1].yzw - v; \n"
|
65 |
5f76092f
|
Leszek Koltunski
|
+ "float h = vUniforms[effect].x; \n"
|
66 |
0f10a0b6
|
Leszek Koltunski
|
+ "float deg = degree(vUniforms[effect+2],ps); \n"
|
67 |
c226920c
|
Leszek Koltunski
|
+ "float tmp = (1.0-h)*deg; \n"
|
68 |
|
|
+ "float t1 = tmp*(deg+1.0)/h; \n"
|
69 |
|
|
+ "float t2 = tmp/(0.618*(0.618+deg)); \n"
|
70 |
|
|
+ "float t = t2 + (t1-t2)*((sign(h-1.0)+1.0)/2.0); \n"
|
71 |
|
|
|
72 |
5f76092f
|
Leszek Koltunski
|
+ "v += t*ps; \n"
|
73 |
7cd24173
|
leszek
|
|
74 |
5f76092f
|
Leszek Koltunski
|
+ "const float A = 1.6; \n" // max amount of change in normal vector when pulling
|
75 |
|
|
+ "const float B = 10.0; \n" // when pushing
|
76 |
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))
|
77 |
5f76092f
|
Leszek Koltunski
|
+ "float dot_ps = dot(ps,ps); \n"
|
78 |
|
|
+ "float sign_ps= sign(dot_ps); \n"
|
79 |
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
|
80 |
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
|
81 |
569ea22c
|
Leszek Koltunski
|
+ "n += move*n_ps; \n"
|
82 |
f046b159
|
Leszek Koltunski
|
+ "n = normalize(n);";
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86 |
|
|
// PUBLIC API
|
87 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88 |
|
|
// Pull P=(v.x,v.y) towards center of the effect with P' = P + (1-h)*dist(S-P)
|
89 |
|
|
// when h>1 we are pushing points away from S: P' = P + (1/h-1)*dist(S-P)
|
90 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
91 |
|
|
/**
|
92 |
|
|
* Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
|
93 |
|
|
*/
|
94 |
|
|
public static void enable()
|
95 |
|
|
{
|
96 |
|
|
addEffect(NAME, code() );
|
97 |
7cd24173
|
leszek
|
}
|
98 |
faa3ff56
|
Leszek Koltunski
|
|
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 |
|
|
* @param region Region that masks the Effect.
|
107 |
|
|
*/
|
108 |
|
|
public VertexEffectSink(Data1D sink, Data3D center, Data4D region)
|
109 |
|
|
{
|
110 |
f046b159
|
Leszek Koltunski
|
super(NAME);
|
111 |
faa3ff56
|
Leszek Koltunski
|
mSink = sink;
|
112 |
|
|
mCenter = center;
|
113 |
dd89c7f4
|
Leszek Koltunski
|
mRegion = (region==null ? MAX_REGION : region);
|
114 |
faa3ff56
|
Leszek Koltunski
|
}
|
115 |
|
|
|
116 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117 |
|
|
/**
|
118 |
|
|
* Pull all points around the center of the Effect towards the center point(if degree>=1) or push them
|
119 |
|
|
* away from it (degree<=1)
|
120 |
|
|
*
|
121 |
|
|
* @param sink The current degree of the Effect.
|
122 |
|
|
* @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
|
123 |
|
|
*/
|
124 |
|
|
public VertexEffectSink(Data1D sink, Data3D center)
|
125 |
|
|
{
|
126 |
f046b159
|
Leszek Koltunski
|
super(NAME);
|
127 |
faa3ff56
|
Leszek Koltunski
|
mSink = sink;
|
128 |
|
|
mCenter = center;
|
129 |
dd89c7f4
|
Leszek Koltunski
|
mRegion = MAX_REGION;
|
130 |
faa3ff56
|
Leszek Koltunski
|
}
|
131 |
125cee3d
|
Leszek Koltunski
|
}
|