Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectSwirl.java @ 0f10a0b6

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

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
/**
28
 * 'Swirl' part of the Mesh, i.e rotate part of it around a point.
29
 *
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
 */
35
public class VertexEffectSwirl extends VertexEffect
36
  {
37
  private Data1D mSwirl;
38
  private Data3D mCenter;
39
  private Data4D mRegion;
40

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

    
53
    uniforms[index] = (float)(Math.PI*uniforms[index]/180);
54

    
55
    return ret;
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
// PUBLIC API
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
///////////////////////////////////////////////////////////////////////////////////////////////////
65
/**
66
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
67
 */
68
  public static void enable()
69
    {
70
    addEffect(EffectName.SWIRL,
71

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

    
80
      + "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.
81
      + "vec4 SG = (1.0-deg1)*SO;                                           \n" // coordinates of the dilated circle P is going to get rotated around
82
      + "float d2 = max(0.0,degree(SG,PS2));                                \n" // make it a max(0,deg) because otherwise when center=left edge of the
83
                                                                                // object some points end up with d2<0 and they disappear off view.
84
      + "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?
85
      );
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
/**
90
 * Rotate part of the Mesh around the Center of the Effect by a certain angle.
91
 *
92
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
93
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
94
 * @param region Region that masks the Effect.
95
 */
96
  public VertexEffectSwirl(Data1D swirl, Data3D center, Data4D region)
97
    {
98
    super(EffectName.SWIRL);
99
    mSwirl  = swirl;
100
    mCenter = center;
101
    mRegion = (region==null ? MAX_REGION : region);
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
/**
106
 * Rotate the whole Mesh around the Center of the Effect by a certain angle.
107
 *
108
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
109
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
110
 */
111
  public VertexEffectSwirl(Data1D swirl, Data3D center)
112
    {
113
    super(EffectName.SWIRL);
114
    mSwirl  = swirl;
115
    mCenter = center;
116
    mRegion = MAX_REGION;
117
    }
118
  }
119

    
(30-30/31)