1 |
c1f892ed
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2023 Leszek Koltunski leszek@koltunski.pl //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Distorted. //
|
5 |
|
|
// //
|
6 |
|
|
// 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 |
|
|
// //
|
11 |
|
|
// This library 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 GNU //
|
14 |
|
|
// Lesser General Public License for more details. //
|
15 |
|
|
// //
|
16 |
|
|
// 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 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20 |
|
|
|
21 |
|
|
package org.distorted.library.effect;
|
22 |
|
|
|
23 |
|
|
import org.distorted.library.type.Data3D;
|
24 |
|
|
import org.distorted.library.type.Data5D;
|
25 |
|
|
|
26 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
27 |
|
|
|
28 |
|
|
/**
|
29 |
cbcf2374
|
leszek
|
* Pull (degree>1) or push away from (degree<1) all points of an infinite 'pipe' shaped region.
|
30 |
c1f892ed
|
leszek
|
*/
|
31 |
|
|
public class VertexEffectPipe extends VertexEffect
|
32 |
|
|
{
|
33 |
|
|
private static final EffectName NAME = EffectName.PIPE;
|
34 |
|
|
|
35 |
|
|
private final Data5D mPipe;
|
36 |
|
|
private final Data3D mCenter;
|
37 |
|
|
|
38 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39 |
|
|
/**
|
40 |
|
|
* Only for use by the library itself.
|
41 |
|
|
*
|
42 |
|
|
* @y.exclude
|
43 |
|
|
*/
|
44 |
|
|
public boolean compute(float[] uniforms, int index, long currentDuration, long step )
|
45 |
|
|
{
|
46 |
|
|
mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
|
47 |
|
|
return mPipe.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51 |
|
|
// here, if one tests this with the 'Generic' examples app - and chooses the 'Cubes 3x3x3' mesh,
|
52 |
|
|
// then adds an 'empty' PIPE effect - then two corners of the mesh turn bright.
|
53 |
|
|
// It turns out this is because the points at those corners are very close to being on the
|
54 |
|
|
// line defined by 'center' and 'vect' (so 'ps' is very small)
|
55 |
|
|
// Then by means of some miracle we have
|
56 |
|
|
// dot_ps>0, (sign_ps-1.0)==0.0 and dot_ps-(sign_ps-1.0) == 0.0 !! so n_ps - undef.
|
57 |
|
|
|
58 |
|
|
static String code()
|
59 |
|
|
{
|
60 |
|
|
return
|
61 |
|
|
|
62 |
|
|
"vec3 pc = vUniforms[effect+1].yzw - v; \n"
|
63 |
|
|
+ "vec3 vect = vUniforms[effect].yzw; \n"
|
64 |
|
|
+ "vec3 ps = pc - dot(pc,vect)*vect; \n"
|
65 |
|
|
+ "float radius = vUniforms[effect+1].x; \n"
|
66 |
|
|
+ "float deg = max( 0.0, 1.0 - length(ps)/radius ); \n"
|
67 |
|
|
|
68 |
|
|
+ "float h = vUniforms[effect].x; \n" // from this point on, having calculated
|
69 |
|
|
+ "float tmp = (1.0-h)*deg; \n" // ps and deg, proceed just like in Sink
|
70 |
|
|
+ "float t1 = tmp*(deg+1.0)/h; \n"
|
71 |
|
|
+ "float t2 = tmp/(0.618*(0.618+deg)); \n"
|
72 |
|
|
+ "float t = t2 + (t1-t2)*((sign(h-1.0)+1.0)/2.0); \n"
|
73 |
|
|
|
74 |
|
|
+ "v += t*ps; \n"
|
75 |
|
|
|
76 |
|
|
+ "const float A = 1.6; \n"
|
77 |
|
|
+ "const float B = 10.0; \n"
|
78 |
|
|
+ "vec3 n_ps = dot(ps,n)*ps; \n"
|
79 |
|
|
+ "float dot_ps = dot(ps,ps); \n"
|
80 |
|
|
+ "float sign_ps= sign(dot_ps); \n"
|
81 |
|
|
+ "n_ps = (sign_ps*n_ps)/(dot_ps-(sign_ps-1.0)); \n"
|
82 |
|
|
+ "float move = deg*(h-1.0)/(h/B+1.0/A); \n"
|
83 |
|
|
+ "n += move*n_ps; \n"
|
84 |
|
|
+ "n = normalize(n);";
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88 |
|
|
// PUBLIC API
|
89 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
90 |
|
|
/**
|
91 |
|
|
* Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
|
92 |
|
|
*/
|
93 |
|
|
public static void enable()
|
94 |
|
|
{
|
95 |
|
|
addEffect(NAME, code() );
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99 |
|
|
/**
|
100 |
|
|
* Pull (degree>1) or push away from (degree<1) all points of an infinite 'pipe' shaped region. We
|
101 |
|
|
* pull (or push) each point in space towards a point in the center of the pipe which is closest to it.
|
102 |
|
|
* Points located outside of the pipe are not affected (thus this acts kind of like a 'region')
|
103 |
|
|
*
|
104 |
|
|
* @param pipe 5-dimensional data describing the effect and kind of including the region in it as well.
|
105 |
|
|
* 1st: the degree of pull (similar to the one in the sink effect)
|
106 |
|
|
* (2nd,3rd,4th) -> the vector along which the 'pipe' points to.
|
107 |
|
|
* 5th: the radius of the pipe.
|
108 |
|
|
* @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
|
109 |
|
|
*/
|
110 |
|
|
public VertexEffectPipe(Data5D pipe, Data3D center)
|
111 |
|
|
{
|
112 |
|
|
super(NAME);
|
113 |
|
|
mPipe = pipe;
|
114 |
|
|
mCenter = center;
|
115 |
|
|
}
|
116 |
|
|
}
|