Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectShear.java @ f046b159

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 Data3D mShear;
33
  private Data3D mCenter;
34

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

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
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

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

    
60
    + "float new_vx = (1.0+sx*sy)*v.x + sx*v.y;      \n"
61
    + "float new_vy = sy*v.x + v.y;                  \n"
62
    + "float new_vz = sz*v.y + v.z;                  \n"
63

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

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

    
70
    + "float new_nx = (1.0+sx*sy)*n.x + sx*n.y;      \n"
71
    + "float new_ny = sy*n.x + n.y;                  \n"
72
    + "float new_nz = sz*n.y + n.z;                  \n"
73

    
74
    + "n.x = new_nx;                                 \n"
75
    + "n.y = new_ny;                                 \n"
76
    + "n.z = new_nz;                                 \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
  }
(28-28/31)