Project

General

Profile

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

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

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

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

    
50
    uniforms[index+1] = (float)(Math.PI*uniforms[index+1]/180);
51
    uniforms[index+REGION_OFFSET+1] =-uniforms[index+REGION_OFFSET+1];  // region's y
52

    
53
    return ret;
54
    }
55

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

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

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

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