Project

General

Profile

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

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

1 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 15aa7d94 Leszek Koltunski
import org.distorted.library.type.Static4D;
26 125cee3d Leszek Koltunski
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28 faa3ff56 Leszek Koltunski
/**
29
 * 'Swirl' part of the Mesh, i.e rotate part of it around a point.
30 5e96393c Leszek Koltunski
 *
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 faa3ff56 Leszek Koltunski
 */
36 125cee3d Leszek Koltunski
public class VertexEffectSwirl extends VertexEffect
37
  {
38 0dd98279 Leszek Koltunski
  private Data1D mSwirl;
39
  private Data3D mCenter;
40
  private Data4D mRegion;
41
42 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
43 6bb59aad Leszek Koltunski
/**
44 faa3ff56 Leszek Koltunski
 * Only for use by the library itself.
45 6bb59aad Leszek Koltunski
 *
46 faa3ff56 Leszek Koltunski
 * @y.exclude
47 6bb59aad Leszek Koltunski
 */
48 15aa7d94 Leszek Koltunski
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
49
    {
50 b24e4719 Leszek Koltunski
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
51
    mRegion.get(uniforms,index+REGION_OFFSET,currentDuration,step);
52 0dd98279 Leszek Koltunski
    boolean ret = mSwirl.get(uniforms,index,currentDuration,step);
53 15aa7d94 Leszek Koltunski
54 82d6f93a Leszek Koltunski
    uniforms[index  ] = (float)(Math.PI*uniforms[index]/180);
55 b24e4719 Leszek Koltunski
    uniforms[index+REGION_OFFSET+1] =-uniforms[index+REGION_OFFSET+1];  // region's y
56 15aa7d94 Leszek Koltunski
57
    return ret;
58 125cee3d Leszek Koltunski
    }
59 7cd24173 leszek
60 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61
// PUBLIC API
62 7cd24173 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
67
/**
68
 * Have to call this before the shaders get compiled (i.e before Distorted.onCreate()) for the Effect to work.
69
 */
70 7cd24173 leszek
  public static void enable()
71
    {
72
    addEffect(EffectName.SWIRL,
73
74 4aa38649 Leszek Koltunski
        "vec3 center  = vUniforms[effect+1].yzw; \n"
75
      + "vec3 PS = center-v.xyz; \n"
76 7cd24173 leszek
      + "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 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.
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 7cd24173 leszek
      );
90
    }
91 faa3ff56 Leszek Koltunski
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 1f2cb152 Leszek Koltunski
    mRegion = (region==null ? new Static4D(0,0,0, Float.MAX_VALUE) : region);
106 faa3ff56 Leszek Koltunski
    }
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 4aa38649 Leszek Koltunski
    mRegion = new Static4D(0,0,0, Float.MAX_VALUE);
121 faa3ff56 Leszek Koltunski
    }
122 125cee3d Leszek Koltunski
  }