Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectPinch.java @ aa2f0486

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
public class VertexEffectPinch extends VertexEffect
30
  {
31
  private Data2D mPinch;
32
  private Data3D mCenter;
33
  private Data4D mRegion;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
/**
37
 * Pull all points around the center of the Effect towards a line passing through the center
38
 * (that's if degree>=1) or push them away from the line (degree<=1)
39
 *
40
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
41
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
42
 * @param region Region that masks the Effect.
43
 */
44
  public VertexEffectPinch(Data2D pinch, Data3D center, Data4D region)
45
    {
46
    super(EffectName.PINCH);
47
    mPinch  = pinch;
48
    mCenter = center;
49
    mRegion = (region==null ? new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE) : region);
50
    }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
/**
54
 * Pull all points around the center of the Effect towards a line passing through the center
55
 * (that's if degree>=1) or push them away from the line (degree<=1)
56
 *
57
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
58
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
59
 */
60
  public VertexEffectPinch(Data2D pinch, Data3D center)
61
    {
62
    super(EffectName.PINCH);
63
    mPinch  = pinch;
64
    mCenter = center;
65
    mRegion = new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE);
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
71
    {
72
    mCenter.get(uniforms,index+5,currentDuration,step);
73
    mRegion.get(uniforms,index+8,currentDuration,step);
74
    boolean ret = mPinch.get(uniforms,index,currentDuration,step);
75

    
76
    uniforms[index+1] = (float)(Math.PI*uniforms[index+1]/180);
77
    uniforms[index+9] =-uniforms[index+9];
78

    
79
    return ret;
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
// Pull P=(v.x,v.y) towards the line that
84
// a) passes through the center of the effect
85
// b) forms angle defined in the 2nd interpolated value with the X-axis
86
// with P' = P + (1-h)*dist(line to P)
87
// when h>1 we are pushing points away from S: P' = P + (1/h-1)*dist(line to P)
88

    
89
  public static void enable()
90
    {
91
    addEffect(EffectName.PINCH,
92

    
93
        "vec2 center = vUniforms[effect+1].yz; \n"
94
      + "vec2 ps = center-v.xy; \n"
95
      + "float h = vUniforms[effect].x; \n"
96
      + "float t = degree(vUniforms[effect+2],center,ps) * (1.0-h)/max(1.0,h); \n"
97
      + "float angle = vUniforms[effect].y; \n"
98
      + "vec2 dir = vec2(sin(angle),-cos(angle)); \n"
99
      + "v.xy += t*dot(ps,dir)*dir;"
100
      );
101
    }
102
  }
(22-22/25)