Project

General

Profile

« Previous | Next » 

Revision c1f892ed

Added by Leszek Koltunski over 1 year ago

New 'PIPE' vertex effect.

View differences:

src/main/java/org/distorted/library/effect/EffectName.java
57 57
  SWIRL            ( EffectType.VERTEX  ,   new float[] {0.0f}           , 1, 4,     3    , VertexEffectSwirl.class        ),
58 58
  WAVE             ( EffectType.VERTEX  ,   new float[] {0.0f}           , 5, 4,     3    , VertexEffectWave.class         ),
59 59
  DISAPPEAR        ( EffectType.VERTEX  ,   new float[] {}               , 0, 0,     0    , VertexEffectDisappear.class    ),
60
  PIPE             ( EffectType.VERTEX  ,   new float[] {1,0f}           , 5, 0,     3    , VertexEffectPipe.class         ),
60 61

  
61 62
  VERTEX_MOVE      ( EffectType.VERTEX  ,   new float[] {0.0f,0.0f,0.0f} , 3, 0,     0    , VertexEffectMove.class         ),
62 63
  VERTEX_QUATERNION( EffectType.VERTEX  ,   new float[] {0.0f,0.0f,0.0f} , 4, 0,     3    , VertexEffectQuaternion.class   ),
src/main/java/org/distorted/library/effect/VertexEffectPipe.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
package org.distorted.library.effect;
22

  
23
import org.distorted.library.type.Data3D;
24
import org.distorted.library.type.Data5D;
25

  
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

  
28
/**
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
public class VertexEffectPipe extends VertexEffect
33
  {
34
  private static final EffectName NAME = EffectName.PIPE;
35

  
36
  private final Data5D mPipe;
37
  private final Data3D mCenter;
38

  
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
/**
41
 * Only for use by the library itself.
42
 *
43
 * @y.exclude
44
 */
45
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
46
    {
47
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
48
    return mPipe.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
49
    }
50

  
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
// here, if one tests this with the 'Generic' examples app - and chooses the 'Cubes 3x3x3' mesh,
53
// then adds an 'empty' PIPE effect - then two corners of the mesh turn bright.
54
// It turns out this is because the points at those corners are very close to being on the
55
// line defined by 'center' and 'vect' (so 'ps' is very small)
56
// Then by means of some miracle we have
57
// dot_ps>0, (sign_ps-1.0)==0.0 and dot_ps-(sign_ps-1.0) == 0.0 !! so n_ps - undef.
58

  
59
  static String code()
60
    {
61
    return
62

  
63
        "vec3 pc = vUniforms[effect+1].yzw - v;            \n"
64
      + "vec3 vect = vUniforms[effect].yzw;                \n"
65
      + "vec3 ps = pc - dot(pc,vect)*vect;                 \n"
66
      + "float radius = vUniforms[effect+1].x;             \n"
67
      + "float deg = max( 0.0, 1.0 - length(ps)/radius );  \n"
68

  
69
      + "float h = vUniforms[effect].x;                    \n" // from this point on, having calculated
70
      + "float tmp = (1.0-h)*deg;                          \n" // ps and deg, proceed just like in Sink
71
      + "float t1 = tmp*(deg+1.0)/h;                       \n"
72
      + "float t2 = tmp/(0.618*(0.618+deg));               \n"
73
      + "float t = t2 + (t1-t2)*((sign(h-1.0)+1.0)/2.0);   \n"
74

  
75
      + "v += t*ps;                                        \n"
76

  
77
      + "const float A = 1.6;                              \n"
78
      + "const float B = 10.0;                             \n"
79
      + "vec3 n_ps = dot(ps,n)*ps;                         \n"
80
      + "float dot_ps = dot(ps,ps);                        \n"
81
      + "float sign_ps= sign(dot_ps);                      \n"
82
      + "n_ps = (sign_ps*n_ps)/(dot_ps-(sign_ps-1.0));     \n"
83
      + "float move = deg*(h-1.0)/(h/B+1.0/A);             \n"
84
      + "n += move*n_ps;                                   \n"
85
      + "n = normalize(n);";
86
    }
87

  
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
// PUBLIC API
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
/**
92
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
93
 */
94
  public static void enable()
95
    {
96
    addEffect(NAME, code() );
97
    }
98

  
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
/**
101
 * Pull (degree>1) or push away from (degree<1) all points of an infinite 'pipe' shaped region. We
102
 * pull (or push) each point in space towards a point in the center of the pipe which is closest to it.
103
 * Points located outside of the pipe are not affected (thus this acts kind of like a 'region')
104
 *
105
 * @param pipe   5-dimensional data describing the effect and kind of including the region in it as well.
106
 *               1st: the degree of pull (similar to the one in the sink effect)
107
 *               (2nd,3rd,4th) -> the vector along which the 'pipe' points to.
108
 *               5th: the radius of the pipe.
109
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
110
 */
111
  public VertexEffectPipe(Data5D pipe, Data3D center)
112
    {
113
    super(NAME);
114
    mPipe   = pipe;
115
    mCenter = center;
116
    }
117
  }
src/main/java/org/distorted/library/effect/VertexEffectSink.java
62 62
    return
63 63

  
64 64
        "vec3 ps = vUniforms[effect+1].yzw - v;            \n"
65
      + "float h = vUniforms[effect].x;                    \n"
66 65
      + "float deg = degree(vUniforms[effect+2],ps);       \n"
66

  
67
      + "float h = vUniforms[effect].x;                    \n"
67 68
      + "float tmp = (1.0-h)*deg;                          \n"
68 69
      + "float t1 = tmp*(deg+1.0)/h;                       \n"
69 70
      + "float t2 = tmp/(0.618*(0.618+deg));               \n"

Also available in: Unified diff