summaryrefslogtreecommitdiffstats
path: root/res/effectlib/perlinNoise.glsllib
blob: 347386c4d126339c890f63bf7526455cbcb9227b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/****************************************************************************
**
** Copyright (C) 2014 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

// check out implementation from GPU Gems 1 and 2

float fade( in float x )
{
  return( x * x * x * ( x * ( x * 6.0 - 15.0 ) + 10.0 ));
  //return( x * x * ( 3.0 - 2.0 * x ) );
}

vec2 fade( in vec2 x )
{
  return( x * x * x * ( x * ( x * 6.0 - 15.0 ) + 10.0 ));
  //return( x * x * ( 3.0 - 2.0 * x ) );
}

vec3 fade( in vec3 x )
{
  return( x * x * x * ( x * ( x * 6.0 - 15.0 ) + 10.0 ));
  //return( x * x * ( 3.0 - 2.0 * x ) );
}

vec4 fade( in vec4 x )
{
  return( x * x * x * ( x * ( x * 6.0 - 15.0 ) + 10.0 ));
  //return( x * x * ( 3.0 - 2.0 * x ) );
}

float influence( in int hash, in float x )
{
  return( x * texelFetch( randomGradient1D, ivec2(hash, 0), 0 ).x );
}

float influence( in int hash, in float x, in float y )
{
  return( dot( vec2( x, y ), texelFetch( randomGradient2D, ivec2(hash, 0), 0 ).xy ) );
}

float influence( in int hash, in float x, in float y, in float z )
{
  return( dot( vec3( x, y, z ), texelFetch( randomGradient3D, ivec2(hash, 0), 0 ).xyz ) );
}

float influence( in int hash, in float x, in float y, in float z, in float w )
{
  return( dot( vec4( x, y, z, w ), texelFetch( randomGradient4D, ivec2(hash, 0), 0 ) ) );
}

float perlinNoise( in float pos )
{
  float floorPos = floor( pos );
  int intPos = int( floorPos );
  float fracPos = pos - floorPos;

  return( mix( influence( random255X( intPos ), fracPos ), influence( random255X( intPos+1 ), fracPos - 1.0 ), fade( fracPos ) ) );
}

float perlinNoise( in vec2 pos )
{
  vec2 floorPos = floor( pos );
  vec2 fracPos = pos - floorPos;
  vec2 fadedPos = fade( fracPos );

  int ax = random255X( int(floorPos.x)     );
  int bx = random255X( int(floorPos.x) + 1 );
  int ay = random255Y( int(floorPos.y)     );
  int by = random255Y( int(floorPos.y) + 1 );

  return( mix( mix( influence( ax^ay, fracPos.x,        fracPos.y        )
                  , influence( bx^ay, fracPos.x - 1.0, fracPos.y        ), fadedPos.x )
             , mix( influence( ax^by, fracPos.x,        fracPos.y - 1.0 )
                  , influence( bx^by, fracPos.x - 1.0, fracPos.y - 1.0 ), fadedPos.x ), fadedPos.y ) );
}

float perlinNoise( in vec3 pos )
{
  vec3 floorPos = floor( pos );
  vec3 fracPos = pos - floorPos;
  vec3 fadedPos = fade( fracPos );

  int ax = random255X( int(floorPos.x)     );
  int bx = random255X( int(floorPos.x) + 1 );
  int ay = random255Y( int(floorPos.y)     );
  int by = random255Y( int(floorPos.y) + 1 );
  int az = random255Z( int(floorPos.z)     );
  int bz = random255Z( int(floorPos.z) + 1 );

  int axay = ax ^ ay;
  int bxay = bx ^ ay;
  int axby = ax ^ by;
  int bxby = bx ^ by;

  return( mix( mix( mix( influence( axay^az, fracPos.x,        fracPos.y,        fracPos.z        )
                       , influence( bxay^az, fracPos.x - 1.0, fracPos.y,        fracPos.z        ), fadedPos.x )
                  , mix( influence( axby^az, fracPos.x,        fracPos.y - 1.0, fracPos.z        )
                       , influence( bxby^az, fracPos.x - 1.0, fracPos.y - 1.0, fracPos.z        ), fadedPos.x ), fadedPos.y )
             , mix( mix( influence( axay^bz, fracPos.x,        fracPos.y       , fracPos.z - 1.0 )
                       , influence( bxay^bz, fracPos.x - 1.0, fracPos.y       , fracPos.z - 1.0 ), fadedPos.x )
                  , mix( influence( axby^bz, fracPos.x,        fracPos.y - 1.0, fracPos.z - 1.0 )
                       , influence( bxby^bz, fracPos.x - 1.0, fracPos.y - 1.0, fracPos.z - 1.0 ), fadedPos.x ), fadedPos.y ), fadedPos.z ) );
}

float perlinNoise( in vec4 pos )
{
  vec4 floorPos = floor( pos );
  vec4 fracPos = pos - floorPos;
  vec4 fadedPos = fade( fracPos );

  int ax = random255X( int(floorPos.x)     );
  int bx = random255X( int(floorPos.x) + 1 );
  int ay = random255Y( int(floorPos.y)     );
  int by = random255Y( int(floorPos.y) + 1 );
  int az = random255Z( int(floorPos.z)     );
  int bz = random255Z( int(floorPos.z) + 1 );
  int aw = random255W( int(floorPos.w)     );

  int axay = ax ^ ay;
  int bxay = bx ^ ay;
  int axby = ax ^ by;
  int bxby = bx ^ by;

  float result[2];
  for ( int i=0 ; i<2 ; i++ )
  {
    int azaw = az ^ aw;
    int bzaw = bz ^ aw;

    result[i] = mix( mix( mix( influence( axay^azaw, fracPos.x,        fracPos.y,        fracPos.z,        fracPos.w )
                             , influence( bxay^azaw, fracPos.x - 1.0, fracPos.y,        fracPos.z,        fracPos.w ), fadedPos.x )
                        , mix( influence( axby^azaw, fracPos.x,        fracPos.y - 1.0, fracPos.z,        fracPos.w )
                             , influence( bxby^azaw, fracPos.x - 1.0, fracPos.y - 1.0, fracPos.z,        fracPos.w ), fadedPos.x ), fadedPos.y )
                   , mix( mix( influence( axay^bzaw, fracPos.x,        fracPos.y,        fracPos.z - 1.0, fracPos.w )
                             , influence( bxay^bzaw, fracPos.x - 1.0, fracPos.y,        fracPos.z - 1.0, fracPos.w ), fadedPos.x )
                        , mix( influence( axby^bzaw, fracPos.x,        fracPos.y - 1.0, fracPos.z - 1.0, fracPos.w )
                             , influence( bxby^bzaw, fracPos.x - 1.0, fracPos.y - 1.0, fracPos.z - 1.0, fracPos.w ), fadedPos.x ), fadedPos.y ), fadedPos.z );
    aw = random255W( int(floorPos.w) + 1 );
    fracPos.w -= 1.0;
  }
  return( mix( result[0], result[1], fadedPos.w ) );
}

float summedPerlinNoise( in vec3 pos, in int terms, in bool absNoise )
{
  float sum = 0.0;
  float weight = 1.0;
  vec3 p = pos;
  while ( terms-- != 0 )
  {
    float noise = perlinNoise( p );
    sum += weight * ( absNoise ? abs(noise) : noise );
    p += p;
    weight *= 0.5;
  }
  return( sum );
}

float summedPerlinNoise( in vec4 pos, in int terms, in bool absNoise )
{
  float sum = 0.0;
  float weight = 1.0;
  vec4 p = pos;
  while ( terms-- != 0 )
  {
    float noise = perlinNoise( p );
    sum += weight * ( absNoise ? abs(noise) : noise );
    p += p;
    weight *= 0.5;
  }
  return( sum );
}

float perlinNoise( in vec3 pos, in float time, in int terms, in vec3 turbulenceWeight, in bool absoluteNoise, in bool applyMarble
                 , in bool applyDent, in float noiseBands, in float noiseThresholdHigh, in float noiseThresholdLow )
{
  float noise = ( time == 0.0 ) ? summedPerlinNoise( pos, terms, absoluteNoise ) : summedPerlinNoise( vec4( pos, time ), terms, absoluteNoise );
  if ( turbulenceWeight != vec3( 0.0, 0.0, 0.0 ) )
  {
    noise = sin( dot( pos, turbulenceWeight ) + noise );
  }
  if ( ! absoluteNoise )
  {
    noise = 0.5 * noise + 0.5;    // scale [-1,1] to [0,1]
  }
  if ( applyMarble )
  {
    noise = cos( pos.x + 5.0 * noise );    // classic Perlin marble function, with magic 5.0
  }
  if ( applyDent )
  {
    noise = cube( noise );
  }
  if ( noiseBands != 1.0 )
  {
    // Create banding/stripes by using the fraction component only
    noise *= noiseBands;
    noise -= floor( noise );
    noise += pow( 1.0 - noise, 20.0 );
  }
  if ( noiseThresholdLow < noiseThresholdHigh )
  {
    // clamp the noise
    noise = clamp( ( noise - noiseThresholdLow ) / ( noiseThresholdHigh - noiseThresholdLow ), 0.0, 1.0 );
  }
  return( noise );
}