Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectPinch.java @ 5e96393c

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
    uniforms[index+REGION_OFFSET+1] =-uniforms[index+REGION_OFFSET+1];  // region's y
57

    
58
    return ret;
59
    }
60

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

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

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

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