1/****************************************************************************
2**
3** Copyright (C) 2014 NVIDIA Corporation.
4** Copyright (C) 2019 The Qt Company Ltd.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of Qt 3D Studio.
8**
9** $QT_BEGIN_LICENSE:GPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3 or (at your option) any later version
21** approved by the KDE Free Qt Foundation. The licenses are as published by
22** the Free Software Foundation and appearing in the file LICENSE.GPL3
23** included in the packaging of this file. Please review the following
24** information to ensure the GNU General Public License requirements will
25** be met: https://www.gnu.org/licenses/gpl-3.0.html.
26**
27** $QT_END_LICENSE$
28**
29****************************************************************************/
30
31vec4 miNoise( in vec3 xyz )
32{
33  const float div = 1.0 / 256.0;
34
35  ivec3 itmp = ivec3( xyz );
36  ivec3 ixyz[3] = ivec3[]( itmp & 0xFF, itmp+1 & 0xFF, itmp+2 & 0xFF );
37  vec3 fxyz = xyz - floor( xyz );
38
39  vec3 dux, ux;
40  dux.z = fxyz.x * div;
41  dux.y = div - 2.0 * dux.z;
42  dux.x = dux.z - div;
43  ux.z = 0.5 * fxyz.x * dux.z;
44  ux.y = dux.z + 0.5 * ( div - fxyz.x * dux.z );
45  ux.x = ux.z - dux.z + 0.5 * div;
46
47  vec3 duy, uy;
48  duy.z = fxyz.y;
49  duy.y = 1.0 - 2.0 * duy.z;
50  duy.x = duy.z - 1.0;
51  uy.z = 0.5 * square( duy.z );
52  uy.y = duy.z + 0.5 - square( duy.z );
53  uy.x = uy.z - duy.z + 0.5;
54
55  float duz[3] = float[]( fxyz.z - 1.0, 1.0 - 2.0 * fxyz.z, fxyz.z );
56  float uz[3]  = float[]( 0.5 * square( fxyz.z ) - fxyz.z + 0.5, fxyz.z + 0.5 - square( fxyz.z ), 0.5 * square( fxyz.z ) );
57
58  int xx = random255X( ixyz[0].x );
59  int yx = random255Y( ixyz[0].y );
60  int zx = random255Z( ixyz[0].z );
61  int xy = random255X( ixyz[1].x );
62  int yy = random255Y( ixyz[1].y );
63  int zy = random255Z( ixyz[1].z );
64
65  int xxxy = xx ^ xy;
66  int yxxy = yx ^ xy;
67  int zxxy = zx ^ xy;
68  int xxyy = xx ^ yy;
69  int yxyy = yx ^ yy;
70  int zxyy = zx ^ yy;
71  int xxzy = xx ^ zy;
72  int yxzy = yx ^ zy;
73  int zxzy = zx ^ zy;
74
75  int ixyzxx = ixyz[0].x ^ ixyz[1].x;
76  int ixyzyx = ixyz[0].y ^ ixyz[1].x;
77  int ixyzzx = ixyz[0].z ^ ixyz[1].x;
78  int ixyzxy = ixyz[0].x ^ ixyz[1].y;
79  int ixyzyy = ixyz[0].y ^ ixyz[1].y;
80  int ixyzzy = ixyz[0].z ^ ixyz[1].y;
81  int ixyzxz = ixyz[0].x ^ ixyz[1].z;
82  int ixyzyz = ixyz[0].y ^ ixyz[1].z;
83  int ixyzzz = ixyz[0].z ^ ixyz[1].z;
84
85
86  vec4 ret = vec4( 0.0, 0.0, 0.0, 0.0 );
87
88  for ( int i=0 ; i<3 ; i++ )
89  {
90    int iz = random255Z( ixyz[2][i] );
91
92    mat3x3 nf = mat3x3( vec3( float( random255W( xxxy ^ iz ^ random255W( ixyzxx ^ ixyz[2][i] ) ) )
93                            , float( random255W( yxxy ^ iz ^ random255W( ixyzyx ^ ixyz[2][i] ) ) )
94                            , float( random255W( zxxy ^ iz ^ random255W( ixyzzx ^ ixyz[2][i] ) ) ) )
95                      , vec3( float( random255W( xxyy ^ iz ^ random255W( ixyzxy ^ ixyz[2][i] ) ) )
96                            , float( random255W( yxyy ^ iz ^ random255W( ixyzyy ^ ixyz[2][i] ) ) )
97                            , float( random255W( zxyy ^ iz ^ random255W( ixyzzy ^ ixyz[2][i] ) ) ) )
98                      , vec3( float( random255W( xxzy ^ iz ^ random255W( ixyzxz ^ ixyz[2][i] ) ) )
99                            , float( random255W( yxzy ^ iz ^ random255W( ixyzyz ^ ixyz[2][i] ) ) )
100                            , float( random255W( zxzy ^ iz ^ random255W( ixyzzz ^ ixyz[2][i] ) ) ) ) );
101
102    float fxdz = dot( uy, dux * nf );
103    vec3 dy = ux * nf;
104    float dz = dot( uy, dy );
105
106    ret.x += uz[i] * fxdz;
107    ret.y += uz[i] * dot( duy, dy );
108    ret.z += duz[i] * dz;
109    ret.w += uz[i] * dz;
110  }
111  return( ret );
112}
113
114