|
kernelSource "//Based on https://github.com/Draradech/csworldgen/blob/master/simplexnoise.cpp\n//Ported by Mads Hagbarth Lund 2014\n//Modifications (fBm and Turbulence) by Erwan Leroy 2018 at Spin VFX\n\n/* Copyright (c) 2007-2012 Eliot Eshelman\n *\n * This program is free software: you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n *\n * This program is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n * GNU General Public License for more details.\n *\n * You should have received a copy of the GNU General Public License\n * along with this program. If not, see <http://www.gnu.org/licenses/>.\n *\n */\n\n\nint fastfloor( const float x ) \{ return x > 0 ? (int) x : (int) x - 1; \}\n\ninline float raw_noise_4d(const float4 coord)\n\{\n int simplex\[64]\[4] =\n \{\n \{0,1,2,3\},\{0,1,3,2\},\{0,0,0,0\},\{0,2,3,1\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{1,2,3,0\},\n \{0,2,1,3\},\{0,0,0,0\},\{0,3,1,2\},\{0,3,2,1\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{1,3,2,0\},\n \{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\n \{1,2,0,3\},\{0,0,0,0\},\{1,3,0,2\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{2,3,0,1\},\{2,3,1,0\},\n \{1,0,2,3\},\{1,0,3,2\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{2,0,3,1\},\{0,0,0,0\},\{2,1,3,0\},\n \{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\n \{2,0,1,3\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{3,0,1,2\},\{3,0,2,1\},\{0,0,0,0\},\{3,1,2,0\},\n \{2,1,0,3\},\{0,0,0,0\},\{0,0,0,0\},\{0,0,0,0\},\{3,1,0,2\},\{0,0,0,0\},\{3,2,0,1\},\{3,2,1,0\}\n \};\n\n int perm\[512] =\n \{\n 151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,\n 8,99,37,240,21,10,23,190,6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,\n 35,11,32,57,177,33,88,237,149,56,87,174,20,125,136,171,168,68,175,74,165,71,\n 134,139,48,27,166,77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,\n 55,46,245,40,244,102,143,54,65,25,63,161,1,216,80,73,209,76,132,187,208, 89,\n 18,169,200,196,135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,\n 250,124,123,5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,\n 189,28,42,223,183,170,213,119,248,152,2,44,154,163,70,221,153,101,155,167,43,\n 172,9,129,22,39,253,19,98,108,110,79,113,224,232,178,185,112,104,218,246,97,\n 228,251,34,242,193,238,210,144,12,191,179,162,241,81,51,145,235,249,14,239,\n 107,49,192,214,31,181,199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,\n 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,\n\n 151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,\n 8,99,37,240,21,10,23,190,6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,\n 35,11,32,57,177,33,88,237,149,56,87,174,20,125,136,171,168,68,175,74,165,71,\n 134,139,48,27,166,77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,\n 55,46,245,40,244,102,143,54,65,25,63,161,1,216,80,73,209,76,132,187,208, 89,\n 18,169,200,196,135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,\n 250,124,123,5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,\n 189,28,42,223,183,170,213,119,248,152,2,44,154,163,70,221,153,101,155,167,43,\n 172,9,129,22,39,253,19,98,108,110,79,113,224,232,178,185,112,104,218,246,97,\n 228,251,34,242,193,238,210,144,12,191,179,162,241,81,51,145,235,249,14,239,\n 107,49,192,214,31,181,199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,\n 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180\n \};\n\n int grad4\[32]\[4]=\n \{\n \{0,1,1,1\}, \{0,1,1,-1\}, \{0,1,-1,1\}, \{0,1,-1,-1\},\n \{0,-1,1,1\}, \{0,-1,1,-1\}, \{0,-1,-1,1\}, \{0,-1,-1,-1\},\n \{1,0,1,1\}, \{1,0,1,-1\}, \{1,0,-1,1\}, \{1,0,-1,-1\},\n \{-1,0,1,1\}, \{-1,0,1,-1\}, \{-1,0,-1,1\}, \{-1,0,-1,-1\},\n \{1,1,0,1\}, \{1,1,0,-1\}, \{1,-1,0,1\}, \{1,-1,0,-1\},\n \{-1,1,0,1\}, \{-1,1,0,-1\}, \{-1,-1,0,1\}, \{-1,-1,0,-1\},\n \{1,1,1,0\}, \{1,1,-1,0\}, \{1,-1,1,0\}, \{1,-1,-1,0\},\n \{-1,1,1,0\}, \{-1,1,-1,0\}, \{-1,-1,1,0\}, \{-1,-1,-1,0\}\n \};\n float x = coord.x;\n float y = coord.y;\n float z = coord.z;\n float w = coord.w;\n float F4 = (sqrt(5.0f)-1.0)/4.0;\n float G4 = (5.0-sqrt(5.0f))/20.0;\n float n0, n1, n2, n3, n4;\n float s = (x + y + z + w) * F4;\n int i = fastfloor(x + s);\n int j = fastfloor(y + s);\n int k = fastfloor(z + s);\n int l = fastfloor(w + s);\n float t = (i + j + k + l) * G4;\n float X0 = i - t;\n float Y0 = j - t;\n float Z0 = k - t;\n float W0 = l - t;\n float x0 = x - X0;\n float y0 = y - Y0;\n float z0 = z - Z0;\n float w0 = w - W0;\n int c1 = (x0 > y0) ? 32 : 0;\n int c2 = (x0 > z0) ? 16 : 0;\n int c3 = (y0 > z0) ? 8 : 0;\n int c4 = (x0 > w0) ? 4 : 0;\n int c5 = (y0 > w0) ? 2 : 0;\n int c6 = (z0 > w0) ? 1 : 0;\n int c = c1 + c2 + c3 + c4 + c5 + c6;\n int i1, j1, k1, l1;\n int i2, j2, k2, l2;\n int i3, j3, k3, l3;\n i1 = simplex\[c]\[0]>=3 ? 1 : 0;\n j1 = simplex\[c]\[1]>=3 ? 1 : 0;\n k1 = simplex\[c]\[2]>=3 ? 1 : 0;\n l1 = simplex\[c]\[3]>=3 ? 1 : 0;\n i2 = simplex\[c]\[0]>=2 ? 1 : 0;\n j2 = simplex\[c]\[1]>=2 ? 1 : 0;\n k2 = simplex\[c]\[2]>=2 ? 1 : 0;\n l2 = simplex\[c]\[3]>=2 ? 1 : 0;\n i3 = simplex\[c]\[0]>=1 ? 1 : 0;\n j3 = simplex\[c]\[1]>=1 ? 1 : 0;\n k3 = simplex\[c]\[2]>=1 ? 1 : 0;\n l3 = simplex\[c]\[3]>=1 ? 1 : 0;\n float x1 = x0 - i1 + G4;\n float y1 = y0 - j1 + G4;\n float z1 = z0 - k1 + G4;\n float w1 = w0 - l1 + G4;\n float x2 = x0 - i2 + 2.0*G4;\n float y2 = y0 - j2 + 2.0*G4;\n float z2 = z0 - k2 + 2.0*G4;\n float w2 = w0 - l2 + 2.0*G4;\n float x3 = x0 - i3 + 3.0*G4;\n float y3 = y0 - j3 + 3.0*G4;\n float z3 = z0 - k3 + 3.0*G4;\n float w3 = w0 - l3 + 3.0*G4;\n float x4 = x0 - 1.0 + 4.0*G4;\n float y4 = y0 - 1.0 + 4.0*G4;\n float z4 = z0 - 1.0 + 4.0*G4;\n float w4 = w0 - 1.0 + 4.0*G4;\n int ii = i & 255;\n int jj = j & 255;\n int kk = k & 255;\n int ll = l & 255;\n int gi0 = perm\[ii+perm\[jj+perm\[kk+perm\[ll]]]] % 32;\n int gi1 = perm\[ii+i1+perm\[jj+j1+perm\[kk+k1+perm\[ll+l1]]]] % 32;\n int gi2 = perm\[ii+i2+perm\[jj+j2+perm\[kk+k2+perm\[ll+l2]]]] % 32;\n int gi3 = perm\[ii+i3+perm\[jj+j3+perm\[kk+k3+perm\[ll+l3]]]] % 32;\n int gi4 = perm\[ii+1+perm\[jj+1+perm\[kk+1+perm\[ll+1]]]] % 32;\n\n float t0 = 0.6 - x0*x0 - y0*y0 - z0*z0 - w0*w0;\n\n if(t0<0) \{\n n0 = 0.0;\n \}\n else\n \{\n t0 *= t0;\n n0 = t0 * t0 * dot(float4(grad4\[gi0]\[0],grad4\[gi0]\[2],grad4\[gi0]\[3],grad4\[gi0]\[3]), float4(x0, y0, z0, w0));\n \}\n\n float t1 = 0.6 - x1*x1 - y1*y1 - z1*z1 - w1*w1;\n\n if(t1<0) \{\n n1 = 0.0;\n \}\n else \{\n t1 *= t1;\n n1 = t1 * t1 * dot(float4(grad4\[gi1]\[0],grad4\[gi1]\[2],grad4\[gi1]\[3],grad4\[gi1]\[3]), float4(x1, y1, z1, w1));\n \}\n\n float t2 = 0.6 - x2*x2 - y2*y2 - z2*z2 - w2*w2;\n\n if(t2<0) \{\n n2 = 0.0;\n \}\n else \{\n t2 *= t2;\n n2 = t2 * t2 * dot(float4(grad4\[gi2]\[0],grad4\[gi2]\[2],grad4\[gi2]\[3],grad4\[gi2]\[3]), float4(x2, y2, z2, w2));\n \}\n\n float t3 = 0.6 - x3*x3 - y3*y3 - z3*z3 - w3*w3;\n\n if(t3<0) \{\n n3 = 0.0;\n \}\n else \{\n t3 *= t3;\n n3 = t3 * t3 * dot(float4(grad4\[gi3]\[0],grad4\[gi3]\[2],grad4\[gi3]\[3],grad4\[gi3]\[3]), float4(x3, y3, z3, w3));\n \}\n\n float t4 = 0.6 - x4*x4 - y4*y4 - z4*z4 - w4*w4;\n\n if(t4<0) \{\n n4 = 0.0;\n \}\n else \{\n t4 *= t4;\n n4 = t4 * t4 * dot(float4(grad4\[gi4]\[0],grad4\[gi4]\[2],grad4\[gi4]\[3],grad4\[gi4]\[3]), float4(x4, y4, z4, w4));\n \}\n return 27.0 * (n0 + n1 + n2 + n3 + n4);\n\}\n\ninline float fBm_4d(const float octaves, const float lacunarity, const float gain, const float4 coord, float amplitude)\n\{\n float total = 0;\n float4 frequency = coord;\n for( int i=0; i < octaves; i++ ) \{\n total += (float)raw_noise_4d(frequency) * amplitude;\n frequency *= lacunarity;\n amplitude *= gain;\n \}\n return float(total)*0.5f+0.5f;\n\}\n\ninline float turbulence_4d(const float octaves, const float lacunarity, const float gain, const float4 coord, float amplitude)\n\{\n float total = 0;\n float4 frequency = coord;\n for( int i=0; i < octaves; i++ ) \{\n total += fabs((float)raw_noise_4d(frequency)) * amplitude;\n frequency *= lacunarity;\n amplitude *= gain;\n \}\n return float(total);\n\}\n\nkernel Noise4D : ImageComputationKernel<ePixelWise>\n\{\n Image<eRead, eAccessPoint, eEdgeClamped> src;\n Image<eWrite, eAccessPoint> dst;\nparam:\n int octaves;\n float lacunarity;\n float amplitude;\n float gain;\n int mode;\n\n void define() \{\n defineParam(octaves, \"octaves\", 8);\n defineParam(lacunarity, \"lacunarity\", 2.0f);\n defineParam(amplitude, \"amplitude\", 0.6f);\n defineParam(gain, \"gain\", 0.5f);\n \}\n void process() \{\n float sample;\n if (mode == 0)\{\n sample = (float)fBm_4d(float(octaves),lacunarity,gain,src(), amplitude);\n \} else \{\n sample = (float)turbulence_4d(float(octaves),lacunarity,gain,src(), amplitude);\n \}\n dst() = float4(sample,sample,sample,sample);\n\}\n\};" |