1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2017 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// 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
|
// Distorted 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 //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library.effect;
|
21
|
|
22
|
import org.distorted.library.type.Data2D;
|
23
|
import org.distorted.library.type.Data3D;
|
24
|
import org.distorted.library.type.Data4D;
|
25
|
import org.distorted.library.type.Static4D;
|
26
|
|
27
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28
|
/**
|
29
|
* Pull all points around the center of the Effect towards a line passing through the center
|
30
|
* (that's if degree>=1) or push them away from the line (degree<=1).
|
31
|
*
|
32
|
* Note: this is not a fully 3D effect - the pinching is always within the XY plane; z coordinates
|
33
|
* of all vertices remain unaffected. If someone wants a fully 3D Pinch effect, we'd probably need
|
34
|
* to write another VertexEffectPinch3D with 2 more arguments (latitude and longitude angles defining
|
35
|
* the plane the pinching works on).
|
36
|
*/
|
37
|
public class VertexEffectPinch extends VertexEffect
|
38
|
{
|
39
|
private Data2D mPinch;
|
40
|
private Data3D mCenter;
|
41
|
private Data4D mRegion;
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
/**
|
45
|
* Only for use by the library itself.
|
46
|
*
|
47
|
* @y.exclude
|
48
|
*/
|
49
|
public boolean compute(float[] uniforms, int index, long currentDuration, long step )
|
50
|
{
|
51
|
mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
|
52
|
mRegion.get(uniforms,index+REGION_OFFSET,currentDuration,step);
|
53
|
boolean ret = mPinch.get(uniforms,index,currentDuration,step);
|
54
|
|
55
|
uniforms[index+1] = (float)(Math.PI*uniforms[index+1]/180);
|
56
|
|
57
|
return ret;
|
58
|
}
|
59
|
|
60
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61
|
// PUBLIC API
|
62
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63
|
// Pull P=(v.x,v.y) towards the line that
|
64
|
// a) passes through the center of the effect
|
65
|
// b) forms angle defined in the 2nd interpolated value with the X-axis
|
66
|
// with P' = P + (1-h)*dist(line to P)
|
67
|
// when h>1 we are pushing points away from S: P' = P + (1/h-1)*dist(line to P)
|
68
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69
|
/**
|
70
|
* Have to call this before the shaders get compiled (i.e before Distorted.onCreate()) for the Effect to work.
|
71
|
*/
|
72
|
public static void enable()
|
73
|
{
|
74
|
addEffect(EffectName.PINCH,
|
75
|
|
76
|
"vec3 center = vUniforms[effect+1].yzw; \n"
|
77
|
+ "vec3 ps = center-v; \n"
|
78
|
+ "float h = vUniforms[effect].x; \n"
|
79
|
+ "float t = degree(vUniforms[effect+2],center,ps) * (1.0-h)/max(1.0,h); \n"
|
80
|
+ "float angle = vUniforms[effect].y; \n"
|
81
|
+ "vec2 dir = vec2(sin(angle),-cos(angle)); \n"
|
82
|
+ "v.xy += t*dot(ps.xy,dir)*dir;"
|
83
|
);
|
84
|
}
|
85
|
|
86
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87
|
/**
|
88
|
* Pull all points around the center of the Effect towards a line passing through the center
|
89
|
* (that's if degree>=1) or push them away from the line (degree<=1)
|
90
|
*
|
91
|
* @param pinch The current degree of the Effect + angle the line forms with X-axis
|
92
|
* @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
|
93
|
* @param region Region that masks the Effect.
|
94
|
*/
|
95
|
public VertexEffectPinch(Data2D pinch, Data3D center, Data4D region)
|
96
|
{
|
97
|
super(EffectName.PINCH);
|
98
|
mPinch = pinch;
|
99
|
mCenter = center;
|
100
|
mRegion = (region==null ? new Static4D(0,0,0, Float.MAX_VALUE) : region);
|
101
|
}
|
102
|
|
103
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
104
|
/**
|
105
|
* Pull all points around the center of the Effect towards a line passing through the center
|
106
|
* (that's if degree>=1) or push them away from the line (degree<=1)
|
107
|
*
|
108
|
* @param pinch The current degree of the Effect + angle the line forms with X-axis
|
109
|
* @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
|
110
|
*/
|
111
|
public VertexEffectPinch(Data2D pinch, Data3D center)
|
112
|
{
|
113
|
super(EffectName.PINCH);
|
114
|
mPinch = pinch;
|
115
|
mCenter = center;
|
116
|
mRegion = new Static4D(0,0,0, Float.MAX_VALUE);
|
117
|
}
|
118
|
}
|