Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectSink.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
 * Pull all points around the center of the Effect towards the center point (if degree>=1) or push them
30
 * away from it (degree<=1).
31
 */
32 125cee3d Leszek Koltunski
public class VertexEffectSink extends VertexEffect
33
  {
34 0dd98279 Leszek Koltunski
  private Data1D mSink;
35
  private Data3D mCenter;
36
  private Data4D mRegion;
37
38 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
39 6bb59aad Leszek Koltunski
/**
40 faa3ff56 Leszek Koltunski
 * Only for use by the library itself.
41 6bb59aad Leszek Koltunski
 *
42 faa3ff56 Leszek Koltunski
 * @y.exclude
43 6bb59aad Leszek Koltunski
 */
44 15aa7d94 Leszek Koltunski
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
45
    {
46 b24e4719 Leszek Koltunski
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
47
    mRegion.get(uniforms,index+REGION_OFFSET,currentDuration,step);
48 0dd98279 Leszek Koltunski
    boolean ret = mSink.get(uniforms,index,currentDuration,step);
49 15aa7d94 Leszek Koltunski
50 b24e4719 Leszek Koltunski
    uniforms[index+REGION_OFFSET+1] =-uniforms[index+REGION_OFFSET+1];  // region's y
51 82d6f93a Leszek Koltunski
52 15aa7d94 Leszek Koltunski
    return ret;
53 125cee3d Leszek Koltunski
    }
54 7cd24173 leszek
55 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
56
// PUBLIC API
57 7cd24173 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
58
// Pull P=(v.x,v.y) towards center of the effect with P' = P + (1-h)*dist(S-P)
59
// when h>1 we are pushing points away from S: P' = P + (1/h-1)*dist(S-P)
60 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61
/**
62
 * Have to call this before the shaders get compiled (i.e before Distorted.onCreate()) for the Effect to work.
63
 */
64 7cd24173 leszek
  public static void enable()
65
    {
66
    addEffect(EffectName.SINK,
67
68 4aa38649 Leszek Koltunski
        "vec3 center = vUniforms[effect+1].yzw; \n"
69 5e96393c Leszek Koltunski
      + "vec3 ps = center-v; \n"
70 7cd24173 leszek
      + "float h = vUniforms[effect].x; \n"
71
      + "float t = degree(vUniforms[effect+2],center,ps) * (1.0-h)/max(1.0,h); \n"
72
73 5e96393c Leszek Koltunski
      + "v += t*ps;"
74 7cd24173 leszek
      );
75
    }
76 faa3ff56 Leszek Koltunski
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
/**
79
 * Pull all points around the center of the Effect towards the center point (if degree>=1) or push them
80
 * away from it (degree<=1)
81
 *
82
 * @param sink   The current degree of the Effect.
83
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
84
 * @param region Region that masks the Effect.
85
 */
86
  public VertexEffectSink(Data1D sink, Data3D center, Data4D region)
87
    {
88
    super(EffectName.SINK);
89
    mSink   = sink;
90
    mCenter = center;
91 1f2cb152 Leszek Koltunski
    mRegion = (region==null ? new Static4D(0,0,0, Float.MAX_VALUE) : region);
92 faa3ff56 Leszek Koltunski
    }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
/**
96
 * Pull all points around the center of the Effect towards the center point(if degree>=1) or push them
97
 * away from it (degree<=1)
98
 *
99
 * @param sink   The current degree of the Effect.
100
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
101
 */
102
  public VertexEffectSink(Data1D sink, Data3D center)
103
    {
104
    super(EffectName.SINK);
105
    mSink   = sink;
106
    mCenter = center;
107 4aa38649 Leszek Koltunski
    mRegion = new Static4D(0,0,0, Float.MAX_VALUE);
108 faa3ff56 Leszek Koltunski
    }
109 125cee3d Leszek Koltunski
  }