Project

General

Profile

Download (5.61 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / effect / VertexEffectPinch.java @ 8fdeecd7

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

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
/**
28
 * Pull all points around the center of the Effect towards a line passing through the center
29
 * (that's if degree>=1) or push them away from the line (degree<=1).
30
 *
31
 * Note: this is not a fully 3D effect - the pinching is always within the XY plane; z coordinates
32
 * of all vertices remain unaffected. If someone wants a fully 3D Pinch effect, we'd probably need
33
 * to write another VertexEffectPinch3D with 2 more arguments (latitude and longitude angles defining
34
 * the plane the pinching works on).
35
 */
36
public class VertexEffectPinch extends VertexEffect
37
  {
38
  private Data2D mPinch;
39
  private Data3D mCenter;
40
  private Data4D mRegion;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
/**
44
 * Only for use by the library itself.
45
 *
46
 * @y.exclude
47
 */
48
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
49
    {
50
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
51
    mRegion.get(uniforms,index+REGION_OFFSET,currentDuration,step);
52
    boolean ret = mPinch.get(uniforms,index,currentDuration,step);
53

    
54
    uniforms[index+1] = (float)(Math.PI*uniforms[index+1]/180);
55

    
56
    return ret;
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
// PUBLIC API
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
// Pull P=(v.x,v.y) towards the line that
63
// a) passes through the center of the effect
64
// b) forms angle defined in the 2nd interpolated value with the X-axis
65
// with P' = P + (1-h)*dist(line to P)
66
// when h>1 we are pushing points away from S: P' = P + (1/h-1)*dist(line to P)
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
/**
69
 * Have to call this before the shaders get compiled (i.e before Distorted.onCreate()) for the Effect to work.
70
 */
71
  public static void enable()
72
    {
73
    addEffect(EffectName.PINCH,
74

    
75
        "vec3 center = vUniforms[effect+1].yzw; \n"
76
      + "vec3 ps = center-v; \n"
77
      + "float h = vUniforms[effect].x; \n"
78
      + "float t = degree(vUniforms[effect+2],center,ps) * (1.0-h)/max(1.0,h); \n"
79
      + "float angle = vUniforms[effect].y; \n"
80
      + "vec2 dir = vec2(sin(angle),-cos(angle)); \n"
81
      + "v.xy += t*dot(ps.xy,dir)*dir;"
82
      );
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
/**
87
 * Pull all points around the center of the Effect towards a line passing through the center
88
 * (that's if degree>=1) or push them away from the line (degree<=1)
89
 *
90
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
91
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
92
 * @param region Region that masks the Effect.
93
 */
94
  public VertexEffectPinch(Data2D pinch, Data3D center, Data4D region)
95
    {
96
    super(EffectName.PINCH);
97
    mPinch  = pinch;
98
    mCenter = center;
99
    mRegion = (region==null ? MAX_REGION : region);
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
/**
104
 * Pull all points around the center of the Effect towards a line passing through the center
105
 * (that's if degree>=1) or push them away from the line (degree<=1)
106
 *
107
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
108
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
109
 */
110
  public VertexEffectPinch(Data2D pinch, Data3D center)
111
    {
112
    super(EffectName.PINCH);
113
    mPinch  = pinch;
114
    mCenter = center;
115
    mRegion = MAX_REGION;
116
    }
117
  }
(23-23/26)