1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 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
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
/**
|
27
|
* Shear the Mesh.
|
28
|
*/
|
29
|
public class VertexEffectShear extends VertexEffect
|
30
|
{
|
31
|
private static final EffectName NAME = EffectName.VERTEX_SHEAR;
|
32
|
|
33
|
private final Data3D mShear, mCenter;
|
34
|
|
35
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36
|
/**
|
37
|
* Only for use by the library itself.
|
38
|
*
|
39
|
* @y.exclude
|
40
|
*/
|
41
|
public boolean compute(float[] uniforms, int index, long currentDuration, long step )
|
42
|
{
|
43
|
mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
|
44
|
return mShear.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
|
45
|
}
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
// For explanation (esp. about the way we modify vectors n and i) see MatrixEffectShear.
|
49
|
|
50
|
static String code()
|
51
|
{
|
52
|
return
|
53
|
|
54
|
"float sx = vUniforms[effect].x; \n"
|
55
|
+ "float sy = vUniforms[effect].y; \n"
|
56
|
+ "float sz = vUniforms[effect].z; \n"
|
57
|
+ "vec3 center = vUniforms[effect+1].yzw; \n"
|
58
|
+ "float tmp = sx*sy + 1.0; \n"
|
59
|
|
60
|
+ "v -= center; \n"
|
61
|
|
62
|
+ "float new_vx = tmp*v.x + sx*v.y; \n"
|
63
|
+ "float new_vy = sy*v.x + v.y; \n"
|
64
|
+ "float new_vz = sz*v.y + v.z; \n"
|
65
|
|
66
|
+ "v.x = new_vx; \n"
|
67
|
+ "v.y = new_vy; \n"
|
68
|
+ "v.z = new_vz; \n"
|
69
|
|
70
|
+ "v += center; \n"
|
71
|
|
72
|
+ "float new_nx = n.x - sy*n.y + sy*sz*n.z; \n"
|
73
|
+ "float new_ny =-sx*n.x + tmp*(n.y - sz*n.z); \n"
|
74
|
+ "n.x = new_nx; \n"
|
75
|
+ "n.y = new_ny; \n"
|
76
|
|
77
|
+ "n = normalize(n);";
|
78
|
}
|
79
|
|
80
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81
|
// PUBLIC API
|
82
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
83
|
/**
|
84
|
* Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
|
85
|
*/
|
86
|
public static void enable()
|
87
|
{
|
88
|
addEffect( NAME, code() );
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
/**
|
93
|
* Shear the Mesh.
|
94
|
*
|
95
|
* @param shear The 3-tuple of shear factors. The first controls level
|
96
|
* of shearing in the X-axis, second - Y-axis and the third -
|
97
|
* Z-axis. Each is the tangent of the shear angle, i.e 0 -
|
98
|
* no shear, 1 - shear by 45 degrees (tan(45deg)=1) etc.
|
99
|
* @param center Center of shearing, i.e. the point which stays unmoved.
|
100
|
*/
|
101
|
public VertexEffectShear(Data3D shear, Data3D center)
|
102
|
{
|
103
|
super(NAME);
|
104
|
mShear = shear;
|
105
|
mCenter = center;
|
106
|
}
|
107
|
}
|