Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectShear.java @ 78ff6ea9

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.Data3D;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/**
26
 * Shear the Mesh.
27
 */
28
public class VertexEffectShear extends VertexEffect
29
  {
30
  private static final EffectName NAME = EffectName.VERTEX_SHEAR;
31

    
32
  private final Data3D mShear, mCenter;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
/**
36
 * Only for use by the library itself.
37
 *
38
 * @y.exclude
39
 */
40
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
41
    {
42
    mCenter.get(uniforms,index+CENTER_OFFSET,currentDuration,step);
43
    return mShear.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
44
    }
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
// For explanation (esp. about the way we modify vectors n and i) see MatrixEffectShear.
48

    
49
  static String code()
50
    {
51
    return
52

    
53
      "float sx = vUniforms[effect].x;                  \n"
54
    + "float sy = vUniforms[effect].y;                  \n"
55
    + "float sz = vUniforms[effect].z;                  \n"
56
    + "vec3 center = vUniforms[effect+1].yzw;           \n"
57
    + "float tmp   = sx*sy + 1.0;                       \n"
58

    
59
    + "v -= center;                                     \n"
60

    
61
    + "float new_vx = tmp*v.x + sx*v.y;                 \n"
62
    + "float new_vy = sy*v.x + v.y;                     \n"
63
    + "float new_vz = sz*v.y + v.z;                     \n"
64

    
65
    + "v.x = new_vx;                                    \n"
66
    + "v.y = new_vy;                                    \n"
67
    + "v.z = new_vz;                                    \n"
68

    
69
    + "v += center;                                     \n"
70

    
71
    + "float new_nx = n.x - sy*n.y + sy*sz*n.z;         \n"
72
    + "float new_ny =-sx*n.x + tmp*(n.y - sz*n.z);      \n"
73
    + "n.x = new_nx;                                    \n"
74
    + "n.y = new_ny;                                    \n"
75

    
76
    + "n = normalize(n);";
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
// PUBLIC API
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
/**
83
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
84
 */
85
  public static void enable()
86
    {
87
    addEffect( NAME, code() );
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
/**
92
 * Shear the Mesh.
93
 *
94
 * @param shear   The 3-tuple of shear factors. The first controls level
95
 *                of shearing in the X-axis, second - Y-axis and the third -
96
 *                Z-axis. Each is the tangent of the shear angle, i.e 0 -
97
 *                no shear, 1 - shear by 45 degrees (tan(45deg)=1) etc.
98
 * @param center  Center of shearing, i.e. the point which stays unmoved.
99
 */
100
  public VertexEffectShear(Data3D shear, Data3D center)
101
    {
102
    super(NAME);
103
    mShear = shear;
104
    mCenter = center;
105
    }
106
  }
(29-29/32)