Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectSwirl.java @ b24e4719

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

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

    
49
    uniforms[index  ] = (float)(Math.PI*uniforms[index]/180);
50
    uniforms[index+REGION_OFFSET+1] =-uniforms[index+REGION_OFFSET+1];  // region's y
51

    
52
    return ret;
53
    }
54

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

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

    
75
      + "float alpha = vUniforms[effect].x; \n"
76
      + "float sinA = sin(alpha); \n"
77
      + "float cosA = cos(alpha); \n"
78

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

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

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

    
(25-25/26)