Project

General

Profile

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

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

1 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.Data3D;
23
import org.distorted.library.type.Data4D;
24
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26 faa3ff56 Leszek Koltunski
/**
27
 * Pull all points around the center of the Effect towards a line passing through the center
28
 * (that's if degree>=1) or push them away from the line (degree<=1).
29 5e96393c Leszek Koltunski
 *
30
 * Note: this is not a fully 3D effect - the pinching is always within the XY plane; z coordinates
31
 * of all vertices remain unaffected. If someone wants a fully 3D Pinch effect, we'd probably need
32
 * to write another VertexEffectPinch3D with 2 more arguments (latitude and longitude angles defining
33
 * the plane the pinching works on).
34 faa3ff56 Leszek Koltunski
 */
35 125cee3d Leszek Koltunski
public class VertexEffectPinch extends VertexEffect
36
  {
37 aee0b581 Leszek Koltunski
  private Data3D mPinch;
38 0dd98279 Leszek Koltunski
  private Data3D mCenter;
39
  private Data4D mRegion;
40
41 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42 6bb59aad Leszek Koltunski
/**
43 faa3ff56 Leszek Koltunski
 * Only for use by the library itself.
44 6bb59aad Leszek Koltunski
 *
45 faa3ff56 Leszek Koltunski
 * @y.exclude
46 6bb59aad Leszek Koltunski
 */
47 15aa7d94 Leszek Koltunski
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
48
    {
49 b24e4719 Leszek Koltunski
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
50
    mRegion.get(uniforms,index+REGION_OFFSET,currentDuration,step);
51 0dd98279 Leszek Koltunski
    boolean ret = mPinch.get(uniforms,index,currentDuration,step);
52 15aa7d94 Leszek Koltunski
53
    uniforms[index+1] = (float)(Math.PI*uniforms[index+1]/180);
54 aee0b581 Leszek Koltunski
    uniforms[index+2] = (float)(Math.PI*uniforms[index+2]/180);
55 15aa7d94 Leszek Koltunski
56
    return ret;
57 125cee3d Leszek Koltunski
    }
58 7cd24173 leszek
59 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
// PUBLIC API
61 7cd24173 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
62 aee0b581 Leszek Koltunski
// Pull P=(v.x,v.y) along a vector from the center of region sphere to a point on it defined by
63
// the (latitude,longitude) pair of angles with P' = P + (1-h)*dist(line to P)
64 7cd24173 leszek
// when h>1 we are pushing points away from S: P' = P + (1/h-1)*dist(line to P)
65 aee0b581 Leszek Koltunski
// (where 'line' above passes through the center point and goes along the vector)
66 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
67
/**
68
 * Have to call this before the shaders get compiled (i.e before Distorted.onCreate()) for the Effect to work.
69
 */
70 7cd24173 leszek
  public static void enable()
71
    {
72
    addEffect(EffectName.PINCH,
73
74 aee0b581 Leszek Koltunski
        "vec3 center = vUniforms[effect+1].yzw;              \n"
75
      + "vec3 ps = center-v;                                 \n"
76
      + "float h = vUniforms[effect].x;                      \n"
77
      + "float deg = degree(vUniforms[effect+2],center,ps);  \n"
78
      + "float t = deg * (1.0-h)/max(1.0,h);                 \n"
79
      + "float latitude = vUniforms[effect].y;               \n"
80
      + "float longitude = vUniforms[effect].z;              \n"
81
      + "float sinLAT = sin(latitude);                       \n"
82
      + "float cosLAT = cos(latitude);                       \n"
83
      + "float sinLON = sin(longitude);                      \n"
84
      + "float cosLON = cos(longitude);                      \n"
85
      + "vec3 dir = vec3(sinLON*cosLAT,sinLAT,cosLON*cosLAT);\n"
86
      + "v += t*dot(ps,dir)*dir;"
87 7cd24173 leszek
      );
88
    }
89 faa3ff56 Leszek Koltunski
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
/**
92
 * Pull all points around the center of the Effect towards a line passing through the center
93
 * (that's if degree>=1) or push them away from the line (degree<=1)
94
 *
95 aee0b581 Leszek Koltunski
 * @param pinch  The current degree of the Effect + (latitude,longitude) pair of angles defining the
96
 *  *            point towards which (and its antipode) the pinching power is strongest.
97 faa3ff56 Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
98
 * @param region Region that masks the Effect.
99
 */
100 aee0b581 Leszek Koltunski
  public VertexEffectPinch(Data3D pinch, Data3D center, Data4D region)
101 faa3ff56 Leszek Koltunski
    {
102
    super(EffectName.PINCH);
103
    mPinch  = pinch;
104
    mCenter = center;
105 dd89c7f4 Leszek Koltunski
    mRegion = (region==null ? MAX_REGION : region);
106 faa3ff56 Leszek Koltunski
    }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
/**
110
 * Pull all points around the center of the Effect towards a line passing through the center
111
 * (that's if degree>=1) or push them away from the line (degree<=1)
112
 *
113 aee0b581 Leszek Koltunski
 * @param pinch  The current degree of the Effect + (latitude,longitude) pair of angles defining the
114
 *               point towards which (and its antipode) the pinching power is strongest.
115 faa3ff56 Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
116
 */
117 aee0b581 Leszek Koltunski
  public VertexEffectPinch(Data3D pinch, Data3D center)
118 faa3ff56 Leszek Koltunski
    {
119
    super(EffectName.PINCH);
120
    mPinch  = pinch;
121
    mCenter = center;
122 dd89c7f4 Leszek Koltunski
    mRegion = MAX_REGION;
123 faa3ff56 Leszek Koltunski
    }
124 125cee3d Leszek Koltunski
  }