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 |
0f10a0b6
|
Leszek Koltunski
|
import org.distorted.library.type.Data1D;
|
23 |
125cee3d
|
Leszek Koltunski
|
import org.distorted.library.type.Data3D;
|
24 |
|
|
import org.distorted.library.type.Data4D;
|
25 |
|
|
|
26 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
27 |
faa3ff56
|
Leszek Koltunski
|
/**
|
28 |
|
|
* Deform the Mesh by applying a 3D vector of force.
|
29 |
|
|
*/
|
30 |
125cee3d
|
Leszek Koltunski
|
public class VertexEffectDeform extends VertexEffect
|
31 |
|
|
{
|
32 |
f046b159
|
Leszek Koltunski
|
private static final EffectName NAME = EffectName.DEFORM;
|
33 |
|
|
|
34 |
0dd98279
|
Leszek Koltunski
|
private Data3D mVector, mCenter;
|
35 |
0f10a0b6
|
Leszek Koltunski
|
private Data1D mRadius;
|
36 |
0dd98279
|
Leszek Koltunski
|
private Data4D mRegion;
|
37 |
|
|
|
38 |
125cee3d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39 |
6bb59aad
|
Leszek Koltunski
|
/**
|
40 |
faa3ff56
|
Leszek Koltunski
|
* Only for use by the library itself.
|
41 |
6bb59aad
|
Leszek Koltunski
|
*
|
42 |
faa3ff56
|
Leszek Koltunski
|
* @y.exclude
|
43 |
6bb59aad
|
Leszek Koltunski
|
*/
|
44 |
15aa7d94
|
Leszek Koltunski
|
public boolean compute(float[] uniforms, int index, long currentDuration, long step )
|
45 |
|
|
{
|
46 |
f046b159
|
Leszek Koltunski
|
mCenter.get(uniforms,index+CENTER_OFFSET ,currentDuration,step);
|
47 |
|
|
mRegion.get(uniforms,index+REGION_OFFSET ,currentDuration,step);
|
48 |
|
|
mRadius.get(uniforms,index+VALUES_OFFSET+3,currentDuration,step);
|
49 |
|
|
|
50 |
|
|
return mVector.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
|
51 |
125cee3d
|
Leszek Koltunski
|
}
|
52 |
7cd24173
|
leszek
|
|
53 |
faa3ff56
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54 |
|
|
// PUBLIC API
|
55 |
7cd24173
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
56 |
|
|
// Deform the whole shape of the Object by force V. Algorithm is as follows:
|
57 |
|
|
//
|
58 |
|
|
// Suppose we apply force (Vx,Vy) at point (Cx,Cy) (i.e. the center of the effect). Then, first of all,
|
59 |
|
|
// divide the rectangle into 4 smaller rectangles along the 1 horizontal + 1 vertical lines that pass
|
60 |
|
|
// through (Cx,Cy). Now suppose we have already understood the following case:
|
61 |
|
|
//
|
62 |
|
|
// A vertical (0,Vy) force applied to a rectangle (WxH) in size, at center which is the top-left corner
|
63 |
|
|
// of the rectangle. (*)
|
64 |
|
|
//
|
65 |
|
|
// If we understand (*), then we understand everything, because in order to compute the movement of the
|
66 |
|
|
// whole rectangle we can apply (*) 8 times: for each one of the 4 sub-rectangles, apply (*) twice,
|
67 |
|
|
// once for the vertical component of the force vector, the second time for the horizontal one.
|
68 |
|
|
//
|
69 |
|
|
// Let's then compute (*):
|
70 |
|
|
// 1) the top-left point will move by exactly (0,Vy)
|
71 |
|
|
// 2) we arbitrarily decide that the top-right point will move by (|Vy|/(|Vy|+A*W))*Vy, where A is some
|
72 |
|
|
// arbitrary constant (const float A below). The F(V,W) = (|Vy|/(|Vy|+A*W)) comes from the following:
|
73 |
|
|
// a) we want F(V,0) = 1
|
74 |
|
|
// b) we want lim V->inf (F) = 1
|
75 |
|
|
// c) we actually want F() to only depend on W/V, which we have here.
|
76 |
|
|
// 3) then the top edge of the rectangle will move along the line Vy*G(x), where G(x) = (1 - (A*W/(|Vy|+A*W))*(x/W)^2)
|
77 |
|
|
// 4) Now we decide that the left edge of the rectangle will move along Vy*H(y), where H(y) = (1 - |y|/(|Vy|+C*|y|))
|
78 |
|
|
// where C is again an arbitrary constant. Again, H(y) comes from the requirement that no matter how
|
79 |
|
|
// strong we push the left edge of the rectangle up or down, it can never 'go over itself', but its
|
80 |
|
|
// length will approach 0 if squeezed very hard.
|
81 |
|
|
// 5) The last point we need to compute is the left-right motion of the top-right corner (i.e. if we push
|
82 |
|
|
// the top-left corner up very hard, we want to have the top-right corner not only move up, but also to
|
83 |
|
|
// the left at least a little bit).
|
84 |
|
|
// We arbitrarily decide that, in addition to moving up-down by Vy*F(V,W), the corner will also move
|
85 |
|
|
// left-right by I(V,W) = B*W*F(V,W), where B is again an arbitrary constant.
|
86 |
|
|
// 6) combining 3), 4) and 5) together, we arrive at a movement of an arbitrary point (x,y) away from the
|
87 |
|
|
// top-left corner:
|
88 |
|
|
// X(x,y) = -B*x * (|Vy|/(|Vy|+A*W)) * (1-(y/H)^2) (**)
|
89 |
|
|
// Y(x,y) = Vy * (1 - |y|/(|Vy|+C*|y|)) * (1 - (A*W/(|Vy|+A*W))*(x/W)^2) (**)
|
90 |
|
|
//
|
91 |
|
|
// We notice that formulas (**) have been construed so that it is possible to continously mirror them
|
92 |
|
|
// left-right and up-down (i.e. apply not only to the 'bottom-right' rectangle of the 4 subrectangles
|
93 |
|
|
// but to all 4 of them!).
|
94 |
|
|
//
|
95 |
|
|
// Constants:
|
96 |
|
|
// a) A : valid values: (0,infinity). 'Bendiness' if the surface - the higher A is, the more the surface
|
97 |
|
|
// bends. A<=0 destroys the system.
|
98 |
|
|
// b) B : valid values: <-1,1>. The amount side edges get 'sucked' inwards when we pull the middle of the
|
99 |
|
|
// top edge up. B=0 --> not at all, B=1: a looot. B=-0.5: the edges will actually be pushed outwards
|
100 |
|
|
// quite a bit. One can also set it to <-1 or >1, but it will look a bit ridiculous.
|
101 |
|
|
// c) C : valid values: <1,infinity). The derivative of the H(y) function at 0, i.e. the rate of 'squeeze'
|
102 |
|
|
// surface gets along the force line. C=1: our point gets pulled very closely to points above it
|
103 |
|
|
// even when we apply only small vertical force to it. The higher C is, the more 'uniform' movement
|
104 |
|
|
// along the force line is.
|
105 |
|
|
// 0<=C<1 looks completely ridiculous and C<0 destroys the system.
|
106 |
faa3ff56
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107 |
0f10a0b6
|
Leszek Koltunski
|
// 2020-05-03: replaced vec3 'u_Bounding' with a uniform 'vUniforms[effect].w' (i.e. mRadius)
|
108 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109 |
f046b159
|
Leszek Koltunski
|
|
110 |
|
|
static String code()
|
111 |
7cd24173
|
leszek
|
{
|
112 |
f046b159
|
Leszek Koltunski
|
return
|
113 |
7cd24173
|
leszek
|
|
114 |
5f76092f
|
Leszek Koltunski
|
"const vec3 ONE = vec3(1.0,1.0,1.0); \n"
|
115 |
|
|
+ "const float A = 0.5; \n"
|
116 |
|
|
+ "const float B = 0.2; \n"
|
117 |
|
|
+ "const float C = 5.0; \n"
|
118 |
7cd24173
|
leszek
|
|
119 |
5f76092f
|
Leszek Koltunski
|
+ "vec3 center = vUniforms[effect+1].yzw; \n"
|
120 |
|
|
+ "vec3 ps = center-v; \n"
|
121 |
|
|
+ "vec3 aPS = abs(ps); \n"
|
122 |
0f10a0b6
|
Leszek Koltunski
|
+ "vec3 maxps = vUniforms[effect].w + abs(center); \n"
|
123 |
|
|
+ "float d = degree(vUniforms[effect+2],ps); \n"
|
124 |
5f76092f
|
Leszek Koltunski
|
+ "vec3 force = vUniforms[effect].xyz * d; \n"
|
125 |
|
|
+ "vec3 aForce = abs(force); \n"
|
126 |
|
|
+ "float denom = dot(ps+(1.0-d)*force,ps); \n"
|
127 |
|
|
+ "float one_over_denom = 1.0/(denom-0.001*(sign(denom)-1.0)); \n"
|
128 |
|
|
+ "vec3 Aw = A*maxps; \n"
|
129 |
|
|
+ "vec3 quot = ps / maxps; \n"
|
130 |
|
|
+ "quot = quot*quot; \n" // ( (x/W)^2 , (y/H)^2 ) where x,y are distances from V to center
|
131 |
7cd24173
|
leszek
|
|
132 |
5f76092f
|
Leszek Koltunski
|
+ "float denomV = 1.0 / (aForce.y + Aw.x); \n"
|
133 |
|
|
+ "float denomH = 1.0 / (aForce.x + Aw.y); \n"
|
134 |
7cd24173
|
leszek
|
|
135 |
5f76092f
|
Leszek Koltunski
|
+ "vec3 vertCorr= ONE - aPS / ( aForce+C*aPS + (ONE-sign(aForce)) ); \n" // avoid division by 0 when force and PS both are 0
|
136 |
7cd24173
|
leszek
|
|
137 |
5f76092f
|
Leszek Koltunski
|
+ "float mvXvert = -B * ps.x * aForce.y * (1.0-quot.y) * denomV; \n" // impact the vertical component of the force vector has on horizontal movement
|
138 |
|
|
+ "float mvYhorz = -B * ps.y * aForce.x * (1.0-quot.x) * denomH; \n" // impact the horizontal component of the force vector has on vertical movement
|
139 |
|
|
+ "float mvYvert = force.y * (1.0-quot.x*Aw.x*denomV) * vertCorr.y; \n" // impact the vertical component of the force vector has on vertical movement
|
140 |
|
|
+ "float mvXhorz = force.x * (1.0-quot.y*Aw.y*denomH) * vertCorr.x; \n" // impact the horizontal component of the force vector has on horizontal movement
|
141 |
7cd24173
|
leszek
|
|
142 |
5f76092f
|
Leszek Koltunski
|
+ "v.x += (mvXvert+mvXhorz); \n"
|
143 |
|
|
+ "v.y += (mvYvert+mvYhorz); \n"
|
144 |
7cd24173
|
leszek
|
|
145 |
5f76092f
|
Leszek Koltunski
|
+ "v.z += force.z*d*d*(3.0*d*d -8.0*d +6.0); \n" // thick bubble
|
146 |
7cd24173
|
leszek
|
+ "float b = -(12.0*force.z*d*(1.0-d)*(1.0-d)*(1.0-d))*one_over_denom; \n"
|
147 |
|
|
|
148 |
f046b159
|
Leszek Koltunski
|
+ "n.xy += n.z*b*ps.xy;";
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152 |
|
|
/**
|
153 |
|
|
* Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
|
154 |
|
|
*/
|
155 |
|
|
public static void enable()
|
156 |
|
|
{
|
157 |
|
|
addEffect( NAME, code() );
|
158 |
7cd24173
|
leszek
|
}
|
159 |
faa3ff56
|
Leszek Koltunski
|
|
160 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
161 |
|
|
/**
|
162 |
|
|
* Deform the whole Mesh with a (possibly changing in time) vector of force applied to
|
163 |
6b816678
|
Leszek Koltunski
|
* a (possibly changing in time) point on the Mesh.
|
164 |
faa3ff56
|
Leszek Koltunski
|
*
|
165 |
6b816678
|
Leszek Koltunski
|
* @param vector Vector of force that deforms the Mesh.
|
166 |
0f10a0b6
|
Leszek Koltunski
|
* @param radius How 'bendy' the object is. Don';t set this to 0. Typically set this to the object's
|
167 |
|
|
* mesh size (i.e. more or less X of the rightmost vertex - X of the leftmost vertex)
|
168 |
faa3ff56
|
Leszek Koltunski
|
* @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
|
169 |
|
|
* @param region Region that masks the Effect.
|
170 |
|
|
*/
|
171 |
0f10a0b6
|
Leszek Koltunski
|
public VertexEffectDeform(Data3D vector, Data1D radius, Data3D center, Data4D region)
|
172 |
faa3ff56
|
Leszek Koltunski
|
{
|
173 |
f046b159
|
Leszek Koltunski
|
super(NAME);
|
174 |
faa3ff56
|
Leszek Koltunski
|
mVector = vector;
|
175 |
0f10a0b6
|
Leszek Koltunski
|
mRadius = radius;
|
176 |
faa3ff56
|
Leszek Koltunski
|
mCenter = center;
|
177 |
dd89c7f4
|
Leszek Koltunski
|
mRegion = (region==null ? MAX_REGION : region);
|
178 |
faa3ff56
|
Leszek Koltunski
|
}
|
179 |
|
|
|
180 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181 |
|
|
/**
|
182 |
|
|
* Deform the whole Mesh with a (possibly changing in time) vector of force applied to
|
183 |
6b816678
|
Leszek Koltunski
|
* a (possibly changing in time) point on the Mesh.
|
184 |
faa3ff56
|
Leszek Koltunski
|
*
|
185 |
6b816678
|
Leszek Koltunski
|
* @param vector Vector of force that deforms the Mesh.
|
186 |
0f10a0b6
|
Leszek Koltunski
|
* @param radius How 'bendy' the object is. Don't set this to 0. Typically set this to the object's
|
187 |
|
|
* mesh size (i.e. more or less X of the rightmost vertex - X of the leftmost vertex)
|
188 |
faa3ff56
|
Leszek Koltunski
|
* @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
|
189 |
|
|
*/
|
190 |
0f10a0b6
|
Leszek Koltunski
|
public VertexEffectDeform(Data3D vector, Data1D radius, Data3D center)
|
191 |
faa3ff56
|
Leszek Koltunski
|
{
|
192 |
f046b159
|
Leszek Koltunski
|
super(NAME);
|
193 |
faa3ff56
|
Leszek Koltunski
|
mVector = vector;
|
194 |
0f10a0b6
|
Leszek Koltunski
|
mRadius = radius;
|
195 |
faa3ff56
|
Leszek Koltunski
|
mCenter = center;
|
196 |
dd89c7f4
|
Leszek Koltunski
|
mRegion = MAX_REGION;
|
197 |
faa3ff56
|
Leszek Koltunski
|
}
|
198 |
125cee3d
|
Leszek Koltunski
|
}
|