Project

General

Profile

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

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

1 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 cc62b34a Leszek Koltunski
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3 125cee3d Leszek Koltunski
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 125cee3d Leszek Koltunski
//                                                                                               //
6 cc62b34a Leszek Koltunski
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10 125cee3d Leszek Koltunski
//                                                                                               //
11 cc62b34a Leszek Koltunski
// This library is distributed in the hope that it will be useful,                               //
12 125cee3d Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13 cc62b34a Leszek Koltunski
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15 125cee3d Leszek Koltunski
//                                                                                               //
16 cc62b34a Leszek Koltunski
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21
package org.distorted.library.effect;
22
23
import org.distorted.library.type.Data3D;
24
import org.distorted.library.type.Data4D;
25
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27 faa3ff56 Leszek Koltunski
/**
28
 * Pull all points around the center of the Effect towards a line passing through the center
29
 * (that's if degree>=1) or push them away from the line (degree<=1).
30 5e96393c Leszek Koltunski
 *
31
 * Note: this is not a fully 3D effect - the pinching is always within the XY plane; z coordinates
32
 * of all vertices remain unaffected. If someone wants a fully 3D Pinch effect, we'd probably need
33
 * to write another VertexEffectPinch3D with 2 more arguments (latitude and longitude angles defining
34
 * the plane the pinching works on).
35 faa3ff56 Leszek Koltunski
 */
36 125cee3d Leszek Koltunski
public class VertexEffectPinch extends VertexEffect
37
  {
38 f046b159 Leszek Koltunski
  private static final EffectName NAME = EffectName.PINCH;
39
40 0e1d3d2e Leszek Koltunski
  private final Data3D mPinch;
41
  private final Data3D mCenter;
42
  private final Data4D mRegion;
43 0dd98279 Leszek Koltunski
44 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
45 6bb59aad Leszek Koltunski
/**
46 faa3ff56 Leszek Koltunski
 * Only for use by the library itself.
47 6bb59aad Leszek Koltunski
 *
48 faa3ff56 Leszek Koltunski
 * @y.exclude
49 6bb59aad Leszek Koltunski
 */
50 15aa7d94 Leszek Koltunski
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
51
    {
52 b24e4719 Leszek Koltunski
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
53
    mRegion.get(uniforms,index+REGION_OFFSET,currentDuration,step);
54 f046b159 Leszek Koltunski
    boolean ret = mPinch.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
55 15aa7d94 Leszek Koltunski
56 f046b159 Leszek Koltunski
    uniforms[index+VALUES_OFFSET+1] = (float)(Math.PI*uniforms[index+1]/180);
57
    uniforms[index+VALUES_OFFSET+2] = (float)(Math.PI*uniforms[index+2]/180);
58 15aa7d94 Leszek Koltunski
59
    return ret;
60 125cee3d Leszek Koltunski
    }
61 7cd24173 leszek
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63 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
64
// the (latitude,longitude) pair of angles with P' = P + (1-h)*dist(line to P)
65 7cd24173 leszek
// when h>1 we are pushing points away from S: P' = P + (1/h-1)*dist(line to P)
66 aee0b581 Leszek Koltunski
// (where 'line' above passes through the center point and goes along the vector)
67 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68 f046b159 Leszek Koltunski
69
  static String code()
70 7cd24173 leszek
    {
71 f046b159 Leszek Koltunski
    return
72 7cd24173 leszek
73 0f10a0b6 Leszek Koltunski
        "vec3 ps = vUniforms[effect+1].yzw -v;               \n"
74 569ea22c Leszek Koltunski
      + "float h         = vUniforms[effect].x;              \n"
75
      + "float latitude  = vUniforms[effect].y;              \n"
76
      + "float longitude = vUniforms[effect].z;              \n"
77 0f10a0b6 Leszek Koltunski
      + "float deg = degree(vUniforms[effect+2],ps);         \n"
78 aee0b581 Leszek Koltunski
      + "float t = deg * (1.0-h)/max(1.0,h);                 \n"
79
      + "float sinLAT = sin(latitude);                       \n"
80
      + "float cosLAT = cos(latitude);                       \n"
81
      + "float sinLON = sin(longitude);                      \n"
82
      + "float cosLON = cos(longitude);                      \n"
83
      + "vec3 dir = vec3(sinLON*cosLAT,sinLAT,cosLON*cosLAT);\n"
84 569ea22c Leszek Koltunski
      + "float dot_dir_ps = dot(dir,ps);                     \n"
85
      + "v += t*dot_dir_ps*dir;                              \n"
86
87
      + "const float A = 1.6;                                \n" // max amount of change in normal vector when pulling
88
      + "const float B = 10.0;                               \n" // when pushing
89
      + "vec3 n_ps = dot(ps,n)*ps;                           \n" // n_ps = (component of n that's parallel to ps) * |ps|^2 (=dot(ps,ps))
90
      + "float dot_ps = dot(ps,ps);                          \n"
91
      + "float sign_ps= sign(dot_ps);                        \n"
92
      + "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
93
      + "float move = deg*(h-1.0)/(h/B+1.0/A);               \n" // move(0)=-A*deg, move(1)=0, move(inf)=B*deg
94
      + "n += move * abs(dot(n,dir)) * n_ps;                 \n"
95 f046b159 Leszek Koltunski
      + "n = normalize(n);";
96
    }
97
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
// PUBLIC API
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
/**
102
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
103
 */
104
  public static void enable()
105
    {
106
    addEffect( NAME, code() );
107 7cd24173 leszek
    }
108 faa3ff56 Leszek Koltunski
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
/**
111
 * Pull all points around the center of the Effect towards a line passing through the center
112
 * (that's if degree>=1) or push them away from the line (degree<=1)
113
 *
114 aee0b581 Leszek Koltunski
 * @param pinch  The current degree of the Effect + (latitude,longitude) pair of angles defining the
115
 *  *            point towards which (and its antipode) the pinching power is strongest.
116 faa3ff56 Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
117
 * @param region Region that masks the Effect.
118
 */
119 aee0b581 Leszek Koltunski
  public VertexEffectPinch(Data3D pinch, Data3D center, Data4D region)
120 faa3ff56 Leszek Koltunski
    {
121 f046b159 Leszek Koltunski
    super(NAME);
122 faa3ff56 Leszek Koltunski
    mPinch  = pinch;
123
    mCenter = center;
124 dd89c7f4 Leszek Koltunski
    mRegion = (region==null ? MAX_REGION : region);
125 faa3ff56 Leszek Koltunski
    }
126
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
/**
129
 * Pull all points around the center of the Effect towards a line passing through the center
130
 * (that's if degree>=1) or push them away from the line (degree<=1)
131
 *
132 aee0b581 Leszek Koltunski
 * @param pinch  The current degree of the Effect + (latitude,longitude) pair of angles defining the
133
 *               point towards which (and its antipode) the pinching power is strongest.
134 faa3ff56 Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
135
 */
136 aee0b581 Leszek Koltunski
  public VertexEffectPinch(Data3D pinch, Data3D center)
137 faa3ff56 Leszek Koltunski
    {
138 f046b159 Leszek Koltunski
    super(NAME);
139 faa3ff56 Leszek Koltunski
    mPinch  = pinch;
140
    mCenter = center;
141 dd89c7f4 Leszek Koltunski
    mRegion = MAX_REGION;
142 faa3ff56 Leszek Koltunski
    }
143 125cee3d Leszek Koltunski
  }