Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectSwirl.java @ 5e96393c

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
    uniforms[index+REGION_OFFSET+1] =-uniforms[index+REGION_OFFSET+1];  // region's y
56

    
57
    return ret;
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
// PUBLIC API
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
// Let d be the degree of the current vertex V with respect to center of the effect S and Region vRegion.
64
// This effect rotates the current vertex V by vInterpolated.x radians clockwise around the circle dilated
65
// by (1-d) around the center of the effect S.
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
/**
68
 * Have to call this before the shaders get compiled (i.e before Distorted.onCreate()) for the Effect to work.
69
 */
70
  public static void enable()
71
    {
72
    addEffect(EffectName.SWIRL,
73

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

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

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

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

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

    
(25-25/26)