Project

General

Profile

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

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

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
public class VertexEffectSwirl extends VertexEffect
30
  {
31
  private Data1D mSwirl;
32
  private Data3D mCenter;
33
  private Data4D mRegion;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
/**
37
 * Rotate part of the Object around the Center of the Effect by a certain angle.
38
 *
39
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
40
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
41
 * @param region Region that masks the Effect.
42
 */
43
  public VertexEffectSwirl(Data1D swirl, Data3D center, Data4D region)
44
    {
45
    super(EffectName.SWIRL);
46
    mSwirl  = swirl;
47
    mCenter = center;
48
    mRegion = (region==null ? new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE) : region);
49
    }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
/**
53
 * Rotate the whole Object around the Center of the Effect by a certain angle.
54
 *
55
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
56
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
57
 */
58
  public VertexEffectSwirl(Data1D swirl, Data3D center)
59
    {
60
    super(EffectName.SWIRL);
61
    mSwirl  = swirl;
62
    mCenter = center;
63
    mRegion = new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE);
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
69
    {
70
    mCenter.get(uniforms,index+5,currentDuration,step);
71
    mRegion.get(uniforms,index+8,currentDuration,step);
72
    boolean ret = mSwirl.get(uniforms,index,currentDuration,step);
73

    
74
    uniforms[index  ] = (float)(Math.PI*uniforms[index]/180);
75
    uniforms[index+9] =-uniforms[index+9];
76

    
77
    return ret;
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
// Let d be the degree of the current vertex V with respect to center of the effect S and Region vRegion.
82
// This effect rotates the current vertex V by vInterpolated.x radians clockwise around the circle dilated
83
// by (1-d) around the center of the effect S.
84

    
85
  public static void enable()
86
    {
87
    addEffect(EffectName.SWIRL,
88

    
89
        "vec2 center  = vUniforms[effect+1].yz; \n"
90
      + "vec2 PS = center-v.xy; \n"
91
      + "vec4 SO = vUniforms[effect+2]; \n"
92
      + "float d1_circle = degree_region(SO,PS); \n"
93
      + "float d1_bitmap = degree_bitmap(center,PS); \n"
94

    
95
      + "float alpha = vUniforms[effect].x; \n"
96
      + "float sinA = sin(alpha); \n"
97
      + "float cosA = cos(alpha); \n"
98

    
99
      + "vec2 PS2 = vec2( PS.x*cosA+PS.y*sinA,-PS.x*sinA+PS.y*cosA ); \n" // vector PS rotated by A radians clockwise around center.
100
      + "vec4 SG = (1.0-d1_circle)*SO; \n"                                // coordinates of the dilated circle P is going to get rotated around
101
      + "float d2 = max(0.0,degree(SG,center,PS2)); \n"                   // make it a max(0,deg) because otherwise when center=left edge of the
102
                                                                          // bitmap some points end up with d2<0 and they disappear off view.
103
      + "v.xy += min(d1_circle,d1_bitmap)*(PS - PS2/(1.0-d2)); \n"        // if d2=1 (i.e P=center) we should have P unchanged. How to do it?
104
      );
105
    }
106
  }
107

    
(24-24/25)