Project

General

Profile

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

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

1 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 Leszek Koltunski                                                               //
3
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 125cee3d Leszek Koltunski
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 125cee3d Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// Distorted 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
// 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 46b572b5 Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.library.effect;
21
22
import org.distorted.library.type.Data1D;
23
import org.distorted.library.type.Data3D;
24
import org.distorted.library.type.Data4D;
25
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27 faa3ff56 Leszek Koltunski
/**
28
 * 'Swirl' part of the Mesh, i.e rotate part of it around a point.
29 5e96393c Leszek Koltunski
 *
30
 * Note: this is not a fully 3D effect as the swirling happens entirely within the XY plane - z
31
 * coordinates of all vertices remain unaffected. If someone wants a fully 3D swirling, we'd
32
 * probably need to write another VertixEffectSwirl3D with two additional parameters: latitude and
33
 * longitude angles defining the plane swirling is working on.
34 faa3ff56 Leszek Koltunski
 */
35 125cee3d Leszek Koltunski
public class VertexEffectSwirl extends VertexEffect
36
  {
37 f046b159 Leszek Koltunski
  private static final EffectName NAME = EffectName.SWIRL;
38
39 0dd98279 Leszek Koltunski
  private Data1D mSwirl;
40
  private Data3D mCenter;
41
  private Data4D mRegion;
42
43 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
44 6bb59aad Leszek Koltunski
/**
45 faa3ff56 Leszek Koltunski
 * Only for use by the library itself.
46 6bb59aad Leszek Koltunski
 *
47 faa3ff56 Leszek Koltunski
 * @y.exclude
48 6bb59aad Leszek Koltunski
 */
49 15aa7d94 Leszek Koltunski
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
50
    {
51 b24e4719 Leszek Koltunski
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
52
    mRegion.get(uniforms,index+REGION_OFFSET,currentDuration,step);
53 f046b159 Leszek Koltunski
    boolean ret = mSwirl.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
54 15aa7d94 Leszek Koltunski
55 f046b159 Leszek Koltunski
    uniforms[index+VALUES_OFFSET] = (float)(Math.PI*uniforms[index]/180);
56 15aa7d94 Leszek Koltunski
57
    return ret;
58 125cee3d Leszek Koltunski
    }
59 7cd24173 leszek
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
// Let d be the degree of the current vertex V with respect to center of the effect S and Region vRegion.
62
// This effect rotates the current vertex V by vInterpolated.x radians clockwise around the circle dilated
63
// by (1-d) around the center of the effect S.
64 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65 f046b159 Leszek Koltunski
66
  static String code()
67 7cd24173 leszek
    {
68 f046b159 Leszek Koltunski
    return
69 7cd24173 leszek
70 353f7580 Leszek Koltunski
        "vec3 center = vUniforms[effect+1].yzw;                             \n"
71
      + "vec3 PS = center-v.xyz;                                            \n"
72
      + "vec4 SO = vUniforms[effect+2];                                     \n"
73 0f10a0b6 Leszek Koltunski
      + "float deg1 = degree(SO,PS);                                        \n"
74 353f7580 Leszek Koltunski
      + "float alpha = vUniforms[effect].x;                                 \n"
75
      + "float sinA = sin(alpha);                                           \n"
76
      + "float cosA = cos(alpha);                                           \n"
77 7cd24173 leszek
78 4aa38649 Leszek Koltunski
      + "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.
79 0f10a0b6 Leszek Koltunski
      + "vec4 SG = (1.0-deg1)*SO;                                           \n" // coordinates of the dilated circle P is going to get rotated around
80
      + "float d2 = max(0.0,degree(SG,PS2));                                \n" // make it a max(0,deg) because otherwise when center=left edge of the
81 353f7580 Leszek Koltunski
                                                                                // object some points end up with d2<0 and they disappear off view.
82 f046b159 Leszek Koltunski
      + "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?
83
    }
84
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
// PUBLIC API
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
/**
89
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
90
 */
91
  public static void enable()
92
    {
93
    addEffect( NAME, code() );
94 7cd24173 leszek
    }
95 faa3ff56 Leszek Koltunski
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
/**
98
 * Rotate part of the Mesh around the Center of the Effect by a certain angle.
99
 *
100
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
101
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
102
 * @param region Region that masks the Effect.
103
 */
104
  public VertexEffectSwirl(Data1D swirl, Data3D center, Data4D region)
105
    {
106 f046b159 Leszek Koltunski
    super(NAME);
107 faa3ff56 Leszek Koltunski
    mSwirl  = swirl;
108
    mCenter = center;
109 dd89c7f4 Leszek Koltunski
    mRegion = (region==null ? MAX_REGION : region);
110 faa3ff56 Leszek Koltunski
    }
111
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
/**
114
 * Rotate the whole Mesh around the Center of the Effect by a certain angle.
115
 *
116
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
117
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
118
 */
119
  public VertexEffectSwirl(Data1D swirl, Data3D center)
120
    {
121 f046b159 Leszek Koltunski
    super(NAME);
122 faa3ff56 Leszek Koltunski
    mSwirl  = swirl;
123
    mCenter = center;
124 dd89c7f4 Leszek Koltunski
    mRegion = MAX_REGION;
125 faa3ff56 Leszek Koltunski
    }
126 125cee3d Leszek Koltunski
  }