Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectSwirl.java @ 7bebb196

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
import org.distorted.library.type.Static4D;
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 Data1D mSwirl;
39
  private Data3D mCenter;
40
  private Data4D mRegion;
41

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

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

    
56
    return ret;
57
    }
58

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

    
73
        "vec3 center  = vUniforms[effect+1].yzw; \n"
74
      + "vec3 PS = center-v.xyz; \n"
75
      + "vec4 SO = vUniforms[effect+2]; \n"
76
      + "float d1_circle = degree_region(SO,PS); \n"
77
      + "float d1_bitmap = degree_bitmap(center,PS); \n"
78

    
79
      + "float alpha = vUniforms[effect].x; \n"
80
      + "float sinA = sin(alpha); \n"
81
      + "float cosA = cos(alpha); \n"
82

    
83
      + "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.
84
      + "vec4 SG = (1.0-d1_circle)*SO; \n"                                      // coordinates of the dilated circle P is going to get rotated around
85
      + "float d2 = max(0.0,degree(SG,center,PS2)); \n"                         // make it a max(0,deg) because otherwise when center=left edge of the
86
                                                                                // bitmap some points end up with d2<0 and they disappear off view.
87
      + "v.xy += min(d1_circle,d1_bitmap)*(PS.xy - PS2.xy/(1.0-d2)); \n"        // if d2=1 (i.e P=center) we should have P unchanged. How to do it?
88
      );
89
    }
90

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

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

    
(25-25/26)