Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// 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
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.effect;
22

    
23
import org.distorted.library.type.Data1D;
24
import org.distorted.library.type.Data3D;
25
import org.distorted.library.type.Data4D;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
/**
29
 * 'Swirl' part of the Mesh, i.e rotate part of it around a point.
30
 *
31
 * Note: this is not a fully 3D effect as the swirling happens entirely within the XY plane - z
32
 * coordinates of all vertices remain unaffected. If someone wants a fully 3D swirling, we'd
33
 * probably need to write another VertixEffectSwirl3D with two additional parameters: latitude and
34
 * longitude angles defining the plane swirling is working on.
35
 */
36
public class VertexEffectSwirl extends VertexEffect
37
  {
38
  private static final EffectName NAME = EffectName.SWIRL;
39

    
40
  private final Data1D mSwirl;
41
  private final Data3D mCenter;
42
  private final Data4D mRegion;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
/**
46
 * Only for use by the library itself.
47
 *
48
 * @y.exclude
49
 */
50
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
51
    {
52
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
53
    mRegion.get(uniforms,index+REGION_OFFSET,currentDuration,step);
54
    boolean ret = mSwirl.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
55

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

    
58
    return ret;
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
// Let d be the degree of the current vertex V with respect to center of the effect S and Region vRegion.
63
// This effect rotates the current vertex V by vInterpolated.x radians clockwise around the circle dilated
64
// by (1-d) around the center of the effect S.
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  static String code()
68
    {
69
    return
70

    
71
        "vec3 center = vUniforms[effect+1].yzw;                             \n"
72
      + "vec3 PS = center-v.xyz;                                            \n"
73
      + "vec4 SO = vUniforms[effect+2];                                     \n"
74
      + "float deg1 = degree(SO,PS);                                        \n"
75
      + "float alpha = vUniforms[effect].x;                                 \n"
76
      + "float sinA = sin(alpha);                                           \n"
77
      + "float cosA = cos(alpha);                                           \n"
78

    
79
      + "vec3 PS2 = vec3( PS.x*cosA+PS.y*sinA,-PS.x*sinA+PS.y*cosA, PS.z ); \n" // vector PS rotated by A radians clockwise around center.
80
      + "vec4 SG = (1.0-deg1)*SO;                                           \n" // coordinates of the dilated circle P is going to get rotated around
81
      + "float d2 = max(0.0,degree(SG,PS2));                                \n" // make it a max(0,deg) because otherwise when center=left edge of the
82
                                                                                // object some points end up with d2<0 and they disappear off view.
83
      + "v.xy += deg1 * (PS.xy - PS2.xy/(1.0-d2));                          \n";// if d2=1 (i.e P=center) we should have P unchanged. How to do it?
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
// PUBLIC API
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
/**
90
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
91
 */
92
  public static void enable()
93
    {
94
    addEffect( NAME, code() );
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
/**
99
 * Rotate part of the Mesh around the Center of the Effect by a certain angle.
100
 *
101
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
102
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
103
 * @param region Region that masks the Effect.
104
 */
105
  public VertexEffectSwirl(Data1D swirl, Data3D center, Data4D region)
106
    {
107
    super(NAME);
108
    mSwirl  = swirl;
109
    mCenter = center;
110
    mRegion = (region==null ? MAX_REGION : region);
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
/**
115
 * Rotate the whole Mesh around the Center of the Effect by a certain angle.
116
 *
117
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
118
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
119
 */
120
  public VertexEffectSwirl(Data1D swirl, Data3D center)
121
    {
122
    super(NAME);
123
    mSwirl  = swirl;
124
    mCenter = center;
125
    mRegion = MAX_REGION;
126
    }
127
  }
128

    
(33-33/34)