Project

General

Profile

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

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

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.Data3D;
23
import org.distorted.library.type.Data4D;
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
/**
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
 *
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
 */
35
public class VertexEffectPinch extends VertexEffect
36
  {
37
  private static final EffectName NAME = EffectName.PINCH;
38

    
39
  private Data3D 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+VALUES_OFFSET,currentDuration,step);
54

    
55
    uniforms[index+VALUES_OFFSET+1] = (float)(Math.PI*uniforms[index+1]/180);
56
    uniforms[index+VALUES_OFFSET+2] = (float)(Math.PI*uniforms[index+2]/180);
57

    
58
    return ret;
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
// 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
// when h>1 we are pushing points away from S: P' = P + (1/h-1)*dist(line to P)
65
// (where 'line' above passes through the center point and goes along the vector)
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  static String code()
69
    {
70
    return
71

    
72
        "vec3 ps = vUniforms[effect+1].yzw -v;               \n"
73
      + "float h         = vUniforms[effect].x;              \n"
74
      + "float latitude  = vUniforms[effect].y;              \n"
75
      + "float longitude = vUniforms[effect].z;              \n"
76
      + "float deg = degree(vUniforms[effect+2],ps);         \n"
77
      + "float t = deg * (1.0-h)/max(1.0,h);                 \n"
78
      + "float sinLAT = sin(latitude);                       \n"
79
      + "float cosLAT = cos(latitude);                       \n"
80
      + "float sinLON = sin(longitude);                      \n"
81
      + "float cosLON = cos(longitude);                      \n"
82
      + "vec3 dir = vec3(sinLON*cosLAT,sinLAT,cosLON*cosLAT);\n"
83
      + "float dot_dir_ps = dot(dir,ps);                     \n"
84
      + "v += t*dot_dir_ps*dir;                              \n"
85

    
86
      + "const float A = 1.6;                                \n" // max amount of change in normal vector when pulling
87
      + "const float B = 10.0;                               \n" // when pushing
88
      + "vec3 n_ps = dot(ps,n)*ps;                           \n" // n_ps = (component of n that's parallel to ps) * |ps|^2 (=dot(ps,ps))
89
      + "float dot_ps = dot(ps,ps);                          \n"
90
      + "float sign_ps= sign(dot_ps);                        \n"
91
      + "n_ps = sign_ps*n_ps/(dot_ps-(sign_ps-1.0));         \n" // uff! now n_ps is the parallel to ps component of n, even if ps==0
92
      + "float move = deg*(h-1.0)/(h/B+1.0/A);               \n" // move(0)=-A*deg, move(1)=0, move(inf)=B*deg
93
      + "n += move * abs(dot(n,dir)) * n_ps;                 \n"
94
      + "n = normalize(n);";
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// PUBLIC API
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
/**
101
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
102
 */
103
  public static void enable()
104
    {
105
    addEffect( NAME, code() );
106
    }
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
 * @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
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
116
 * @param region Region that masks the Effect.
117
 */
118
  public VertexEffectPinch(Data3D pinch, Data3D center, Data4D region)
119
    {
120
    super(NAME);
121
    mPinch  = pinch;
122
    mCenter = center;
123
    mRegion = (region==null ? MAX_REGION : region);
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
/**
128
 * Pull all points around the center of the Effect towards a line passing through the center
129
 * (that's if degree>=1) or push them away from the line (degree<=1)
130
 *
131
 * @param pinch  The current degree of the Effect + (latitude,longitude) pair of angles defining the
132
 *               point towards which (and its antipode) the pinching power is strongest.
133
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
134
 */
135
  public VertexEffectPinch(Data3D pinch, Data3D center)
136
    {
137
    super(NAME);
138
    mPinch  = pinch;
139
    mCenter = center;
140
    mRegion = MAX_REGION;
141
    }
142
  }
(24-24/31)