├── .gitattributes ├── .gitignore ├── License.txt ├── README.md ├── Resources └── Icon128.png ├── SimplexNoise.uplugin └── Source └── SimplexNoise ├── Private ├── SimplexNoiseBPLibrary.cpp ├── SimplexNoiseModule.cpp └── SimplexNoisePrivatePCH.h ├── Public ├── ISimplexNoise.h └── SimplexNoiseBPLibrary.h └── SimplexNoise.Build.cs /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | Archive/SimplexNoise_UE4.18.3.zip 49 | -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [2015] [Afan Olovcic - DevDad @ art-and-code.com] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | I only request that you mention me in the credits for your game in the way that feels most appropriate to you. 13 | 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimplexNoise 2 | 3 | ![SimplexNoise UE4 Plugin Screenshot](http://i.imgur.com/Fpw5mPX.png) 4 | 5 | * This is a clean, fast, modern and free Perlin Simplex noise function. 6 | * If we change float to double it could be even faster but there is no double type in Blueprint 7 | * All Public Functions are BlueprintCallable so they can be used in every blueprint 8 | 9 | From DevDad and Dedicated to you and Unreal Community. 10 | Code is now under MIT License, Use it free for what ever you want. 11 | I only request that you mention me in the credits for your game in the way that feels most appropriate to you. 12 | 13 | * SimplexNoise 1D,2D,3D & 4D 14 | * Scaled Version SimplexNoise 1D,2D,3D & 4D 15 | * InRange version SimplexNoise 1D,2D,3D & 4D 16 | * SimplexNoise Function retruns float value between 0 - 1 17 | * SimplexNoise Scaled retruns float value between 0 - scale factor 18 | * SimplexNoise In Range returns float value between minRange - maxRange 19 | * inFactor value added to all functions to set frequency of the noise [ Recomended values form: 0.1 - 0.00001 ] 20 | * GetSimpleNoise1D_EX (float x, float lacunarity, float persistance, int octaves, float inFactor, bool ZeroToOne) 21 | * GetSimpleNoise2D_EX (float x, float y, float lacunarity, float persistance, int octaves, float inFactor, bool ZeroToOne) 22 | * GetSimpleNoise3D_EX (float x, float y, float z, float lacunarity, float persistance, int octaves, float inFactor, bool ZeroToOne) 23 | * GetSimpleNoise4D_EX (float x, float y, float z, float w, float lacunarity, float persistance, int octaves, float inFactor, bool ZeroToOne) 24 | 25 | This algorithm was originally designed by Ken Perlin, but my code has been 26 | adapted and extended from the implementation written by Stefan Gustavson (stegu@itn.liu.se) 27 | and modified to fit to Unreal Engine 4 28 | -------------------------------------------------------------------------------- /Resources/Icon128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devdad/SimplexNoise/b57598706afd8fc4d50164da1ed58515595699b2/Resources/Icon128.png -------------------------------------------------------------------------------- /SimplexNoise.uplugin: -------------------------------------------------------------------------------- 1 | { 2 | "PluginFileVersion" : 1, 3 | 4 | "FriendlyName" : "Simplex Noise", 5 | "Version" : 1, 6 | "VersionName" : "1.2.0", 7 | "CreatedBy" : "DevDad", 8 | "CreatedByURL" : "https://art-and-code.com/", 9 | "EngineVersion":"4.25", 10 | "Description" : "SimplexNoise Blueprint Library to use in Unreal Engine 4", 11 | "Category" : "ArtAndCodeSuite.SimplexNoise", 12 | "EnabledByDefault" : true, 13 | 14 | "Modules" : 15 | [ 16 | { 17 | "Name" : "SimplexNoise", 18 | "Type" : "Runtime", 19 | "LoadingPhase" : "PreDefault", 20 | "WhitelistPlatforms" : [ "Win64", "Win32", "Mac", "Linux" ] 21 | }, 22 | 23 | 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /Source/SimplexNoise/Private/SimplexNoiseBPLibrary.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | SimplexNoise 1.2.0 3 | ----- 4 | DevDad - Afan Olovcic @ www.art-and-code.com - 08/12/2015 5 | 6 | This algorithm was originally designed by Ken Perlin, but my code has been 7 | adapted and extended from the implementation written by Stefan Gustavson (stegu@itn.liu.se) 8 | and modified to fit to Unreal Engine 4 9 | 10 | 11 | * This is a clean, fast, modern and free Perlin Simplex noise function. 12 | * If we change float to double it could be even faster but there is no double type in Blueprint 13 | * All Public Functions are BlueprintCallable so they can be used in every blueprint 14 | 15 | 16 | From DevDad and Dedicated to you and Unreal Community 17 | Use it free for what ever you want 18 | I only request that you mention me in the credits for your game in the way that feels most appropriate to you. 19 | 20 | */ 21 | 22 | 23 | #include "SimplexNoiseBPLibrary.h" 24 | #include "SimplexNoisePrivatePCH.h" 25 | 26 | // USimplexNoiseBPLibrary 27 | #define FASTFLOOR(x) ( ((x)>0) ? ((int)x) : (((int)x)-1) ) 28 | 29 | USimplexNoiseBPLibrary::USimplexNoiseBPLibrary(const class FObjectInitializer& PCIP) 30 | : Super(PCIP) 31 | { 32 | 33 | } 34 | 35 | 36 | unsigned char USimplexNoiseBPLibrary::perm[512] = { 151,160,137,91,90,15, 37 | 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, 38 | 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, 39 | 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, 40 | 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, 41 | 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, 42 | 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, 43 | 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, 44 | 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, 45 | 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, 46 | 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, 47 | 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, 48 | 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180, 49 | 151,160,137,91,90,15, 50 | 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, 51 | 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, 52 | 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, 53 | 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, 54 | 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, 55 | 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, 56 | 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, 57 | 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, 58 | 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, 59 | 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, 60 | 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, 61 | 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 62 | }; 63 | 64 | void USimplexNoiseBPLibrary::setNoiseSeed(const int32& newSeed) 65 | { 66 | FMath::RandInit(newSeed); 67 | for (uint16 it = 0; it < 256; ++it) 68 | { 69 | uint8 nextNum = FMath::RandRange(0, 255); 70 | USimplexNoiseBPLibrary::perm[it] = (unsigned char)nextNum; 71 | USimplexNoiseBPLibrary::perm[it + 256] = (unsigned char)nextNum; 72 | } 73 | } 74 | 75 | void USimplexNoiseBPLibrary::setNoiseFromStream(FRandomStream& RandStream) 76 | { 77 | for (uint16 it = 0; it < 256; ++it) 78 | { 79 | uint8 nextNum = RandStream.RandRange(0, 255); 80 | USimplexNoiseBPLibrary::perm[it] = (unsigned char)nextNum; 81 | USimplexNoiseBPLibrary::perm[it + 256] = (unsigned char)nextNum; 82 | } 83 | } 84 | 85 | static unsigned char simplex[64][4] = { 86 | { 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 }, 87 | { 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 }, 88 | { 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 }, 89 | { 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 }, 90 | { 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 }, 91 | { 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 }, 92 | { 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 }, 93 | { 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 } }; 94 | 95 | 96 | float USimplexNoiseBPLibrary::_grad(int hash, float x) 97 | { 98 | int h = hash & 15; 99 | float grad = 1.0 + (h & 7); // Gradient value 1.0, 2.0, ..., 8.0 100 | if (h & 8) grad = -grad; // Set a random sign for the gradient 101 | return (grad * x); // Multiply the gradient with the distance 102 | } 103 | 104 | 105 | float USimplexNoiseBPLibrary::_grad(int hash, float x, float y) 106 | { 107 | int h = hash & 7; // Convert low 3 bits of hash code 108 | float u = h < 4 ? x : y; // into 8 simple gradient directions, 109 | float v = h < 4 ? y : x; // and compute the dot product with (x,y). 110 | return ((h & 1) ? -u : u) + ((h & 2) ? -2.0f*v : 2.0f*v); 111 | } 112 | 113 | 114 | float USimplexNoiseBPLibrary::_grad(int hash, float x, float y, float z) 115 | { 116 | int h = hash & 15; // Convert low 4 bits of hash code into 12 simple 117 | float u = h < 8 ? x : y; // gradient directions, and compute dot product. 118 | float v = h < 4 ? y : h == 12 || h == 14 ? x : z; // Fix repeats at h = 12 to 15 119 | return ((h & 1) ? -u : u) + ((h & 2) ? -v : v); 120 | } 121 | 122 | 123 | float USimplexNoiseBPLibrary::_grad(int hash, float x, float y, float z, float t) 124 | { 125 | int h = hash & 31; // Convert low 5 bits of hash code into 32 simple 126 | float u = h < 24 ? x : y; // gradient directions, and compute dot product. 127 | float v = h < 16 ? y : z; 128 | float w = h < 8 ? z : t; 129 | return ((h & 1) ? -u : u) + ((h & 2) ? -v : v) + ((h & 4) ? -w : w); 130 | } 131 | 132 | 133 | float USimplexNoiseBPLibrary::_simplexNoise1D(float x) 134 | { 135 | int i0 = FASTFLOOR(x); 136 | int i1 = i0 + 1; 137 | float x0 = x - i0; 138 | float x1 = x0 - 1.0f; 139 | 140 | float n0, n1; 141 | 142 | float t0 = 1.0f - x0 * x0; 143 | // if(t0 < 0.0f) t0 = 0.0f; 144 | t0 *= t0; 145 | n0 = t0 * t0 * _grad(perm[i0 & 0xff], x0); 146 | 147 | float t1 = 1.0f - x1 * x1; 148 | // if(t1 < 0.0f) t1 = 0.0f; 149 | t1 *= t1; 150 | n1 = t1 * t1 * _grad(perm[i1 & 0xff], x1); 151 | // The maximum value of this noise is 8*(3/4)^4 = 2.53125 152 | // A factor of 0.395 would scale to fit exactly within [-1,1], but 153 | // we want to match PRMan's 1D noise, so we scale it down some more. 154 | return 0.25f * (n0 + n1); 155 | } 156 | 157 | 158 | float USimplexNoiseBPLibrary::_simplexNoise2D(float x, float y) 159 | { 160 | #define F2 0.366025403f // F2 = 0.5*(sqrt(3.0)-1.0) 161 | #define G2 0.211324865f // G2 = (3.0-Math.sqrt(3.0))/6.0 162 | 163 | float n0, n1, n2; // Noise contributions from the three corners 164 | 165 | // Skew the input space to determine which simplex cell we're in 166 | 167 | float s = (x + y) * F2; // Hairy factor for 2D 168 | float xs = x + s; 169 | float ys = y + s; 170 | int i = FASTFLOOR(xs); 171 | int j = FASTFLOOR(ys); 172 | 173 | float t = (float)(i + j) * G2; 174 | float X0 = i - t; // Unskew the cell origin back to (x,y) space 175 | float Y0 = j - t; 176 | float x0 = x - X0; // The x,y distances from the cell origin 177 | float y0 = y - Y0; 178 | 179 | // For the 2D case, the simplex shape is an equilateral triangle. 180 | // Determine which simplex we are in. 181 | int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords 182 | if (x0 > y0) { i1 = 1; j1 = 0; } // lower triangle, XY order: (0,0)->(1,0)->(1,1) 183 | else { i1 = 0; j1 = 1; } // upper triangle, YX order: (0,0)->(0,1)->(1,1) 184 | 185 | // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and 186 | // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where 187 | // c = (3-sqrt(3))/6 188 | 189 | float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords 190 | float y1 = y0 - j1 + G2; 191 | float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords 192 | float y2 = y0 - 1.0f + 2.0f * G2; 193 | 194 | // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds 195 | int ii = i & 0xff; 196 | int jj = j & 0xff; 197 | 198 | // Calculate the contribution from the three corners 199 | float t0 = 0.5f - x0 * x0 - y0 * y0; 200 | if (t0 < 0.0f) n0 = 0.0f; 201 | else { 202 | t0 *= t0; 203 | n0 = t0 * t0 * _grad(perm[ii + perm[jj]], x0, y0); 204 | } 205 | 206 | float t1 = 0.5f - x1 * x1 - y1 * y1; 207 | if (t1 < 0.0) n1 = 0.0; 208 | else { 209 | t1 *= t1; 210 | n1 = t1 * t1 * _grad(perm[ii + i1 + perm[jj + j1]], x1, y1); 211 | } 212 | 213 | float t2 = 0.5f - x2 * x2 - y2 * y2; 214 | if (t2 < 0.0) n2 = 0.0; 215 | else { 216 | t2 *= t2; 217 | n2 = t2 * t2 * _grad(perm[ii + 1 + perm[jj + 1]], x2, y2); 218 | } 219 | 220 | // Add contributions from each corner to get the final noise value. 221 | // The result is scaled to return values in the interval [-1,1] 222 | return 40.0f / 0.884343445f * (n0 + n1 + n2); //accurate to e-9 so that values scale to [-1, 1], same acc as F2 G2. 223 | } 224 | 225 | 226 | float USimplexNoiseBPLibrary::_simplexNoise3D(float x, float y, float z) 227 | { 228 | 229 | // Simple skewing factors for the 3D case 230 | #define F3 0.333333333f 231 | #define G3 0.166666667f 232 | 233 | float n0, n1, n2, n3; // Noise contributions from the four corners 234 | 235 | // Skew the input space to determine which simplex cell we're in 236 | float s = (x + y + z) * F3; // Very nice and simple skew factor for 3D 237 | float xs = x + s; 238 | float ys = y + s; 239 | float zs = z + s; 240 | int i = FASTFLOOR(xs); 241 | int j = FASTFLOOR(ys); 242 | int k = FASTFLOOR(zs); 243 | 244 | float t = (float)(i + j + k) * G3; 245 | float X0 = i - t; // Unskew the cell origin back to (x,y,z) space 246 | float Y0 = j - t; 247 | float Z0 = k - t; 248 | float x0 = x - X0; // The x,y,z distances from the cell origin 249 | float y0 = y - Y0; 250 | float z0 = z - Z0; 251 | 252 | // For the 3D case, the simplex shape is a slightly irregular tetrahedron. 253 | // Determine which simplex we are in. 254 | int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords 255 | int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords 256 | 257 | /* This code would benefit from a backport from the GLSL version! */ 258 | if (x0 >= y0) { 259 | if (y0 >= z0) 260 | { 261 | i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 1; k2 = 0; 262 | } // X Y Z order 263 | else if (x0 >= z0) { i1 = 1; j1 = 0; k1 = 0; i2 = 1; j2 = 0; k2 = 1; } // X Z Y order 264 | else { i1 = 0; j1 = 0; k1 = 1; i2 = 1; j2 = 0; k2 = 1; } // Z X Y order 265 | } 266 | else { // x0 y0) ? 32 : 0; 365 | int c2 = (x0 > z0) ? 16 : 0; 366 | int c3 = (y0 > z0) ? 8 : 0; 367 | int c4 = (x0 > w0) ? 4 : 0; 368 | int c5 = (y0 > w0) ? 2 : 0; 369 | int c6 = (z0 > w0) ? 1 : 0; 370 | int c = c1 + c2 + c3 + c4 + c5 + c6; 371 | 372 | int i1, j1, k1, l1; // The integer offsets for the second simplex corner 373 | int i2, j2, k2, l2; // The integer offsets for the third simplex corner 374 | int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner 375 | 376 | // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order. 377 | // Many values of c will never occur, since e.g. x>y>z>w makes x= 3 ? 1 : 0; 382 | j1 = simplex[c][1] >= 3 ? 1 : 0; 383 | k1 = simplex[c][2] >= 3 ? 1 : 0; 384 | l1 = simplex[c][3] >= 3 ? 1 : 0; 385 | // The number 2 in the "simplex" array is at the second largest coordinate. 386 | i2 = simplex[c][0] >= 2 ? 1 : 0; 387 | j2 = simplex[c][1] >= 2 ? 1 : 0; 388 | k2 = simplex[c][2] >= 2 ? 1 : 0; 389 | l2 = simplex[c][3] >= 2 ? 1 : 0; 390 | // The number 1 in the "simplex" array is at the second smallest coordinate. 391 | i3 = simplex[c][0] >= 1 ? 1 : 0; 392 | j3 = simplex[c][1] >= 1 ? 1 : 0; 393 | k3 = simplex[c][2] >= 1 ? 1 : 0; 394 | l3 = simplex[c][3] >= 1 ? 1 : 0; 395 | // The fifth corner has all coordinate offsets = 1, so no need to look that up. 396 | 397 | float x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords 398 | float y1 = y0 - j1 + G4; 399 | float z1 = z0 - k1 + G4; 400 | float w1 = w0 - l1 + G4; 401 | float x2 = x0 - i2 + 2.0f * G4; // Offsets for third corner in (x,y,z,w) coords 402 | float y2 = y0 - j2 + 2.0f * G4; 403 | float z2 = z0 - k2 + 2.0f * G4; 404 | float w2 = w0 - l2 + 2.0f * G4; 405 | float x3 = x0 - i3 + 3.0f * G4; // Offsets for fourth corner in (x,y,z,w) coords 406 | float y3 = y0 - j3 + 3.0f * G4; 407 | float z3 = z0 - k3 + 3.0f * G4; 408 | float w3 = w0 - l3 + 3.0f * G4; 409 | float x4 = x0 - 1.0f + 4.0f * G4; // Offsets for last corner in (x,y,z,w) coords 410 | float y4 = y0 - 1.0f + 4.0f * G4; 411 | float z4 = z0 - 1.0f + 4.0f * G4; 412 | float w4 = w0 - 1.0f + 4.0f * G4; 413 | 414 | // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds 415 | int ii = i & 0xff; 416 | int jj = j & 0xff; 417 | int kk = k & 0xff; 418 | int ll = l & 0xff; 419 | 420 | // Calculate the contribution from the five corners 421 | float t0 = 0.6f - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0; 422 | if (t0 < 0.0) n0 = 0.0; 423 | else { 424 | t0 *= t0; 425 | n0 = t0 * t0 * _grad(perm[ii + perm[jj + perm[kk + perm[ll]]]], x0, y0, z0, w0); 426 | } 427 | 428 | float t1 = 0.6f - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1; 429 | if (t1 < 0.0) n1 = 0.0; 430 | else { 431 | t1 *= t1; 432 | n1 = t1 * t1 * _grad(perm[ii + i1 + perm[jj + j1 + perm[kk + k1 + perm[ll + l1]]]], x1, y1, z1, w1); 433 | } 434 | 435 | float t2 = 0.6f - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2; 436 | if (t2 < 0.0) n2 = 0.0; 437 | else { 438 | t2 *= t2; 439 | n2 = t2 * t2 * _grad(perm[ii + i2 + perm[jj + j2 + perm[kk + k2 + perm[ll + l2]]]], x2, y2, z2, w2); 440 | } 441 | 442 | float t3 = 0.6f - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3; 443 | if (t3 < 0.0) n3 = 0.0; 444 | else { 445 | t3 *= t3; 446 | n3 = t3 * t3 * _grad(perm[ii + i3 + perm[jj + j3 + perm[kk + k3 + perm[ll + l3]]]], x3, y3, z3, w3); 447 | } 448 | 449 | float t4 = 0.6f - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4; 450 | if (t4 < 0.0) n4 = 0.0; 451 | else { 452 | t4 *= t4; 453 | n4 = t4 * t4 * _grad(perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]], x4, y4, z4, w4); 454 | } 455 | 456 | // Sum up and scale the result to cover the range [-1,1] 457 | return 27.0f * (n0 + n1 + n2 + n3 + n4); 458 | } 459 | 460 | int USimplexNoiseBPLibrary::_polygonise(FCell cell, float isolevel, FTriangle* triangles) 461 | { 462 | return 0; 463 | } 464 | 465 | // 1D Simplex Noise 466 | 467 | float USimplexNoiseBPLibrary::SimplexNoise1D(float x, float inFactor) 468 | { 469 | return (float)_simplexNoise1D(x * inFactor); 470 | } 471 | 472 | 473 | 474 | // 2D Simplex Noise 475 | 476 | float USimplexNoiseBPLibrary::SimplexNoise2D(float x, float y, float inFactor) 477 | { 478 | return (float)_simplexNoise2D(x * inFactor, y * inFactor); 479 | } 480 | 481 | 482 | 483 | 484 | // 3D Simplex Noise 485 | float USimplexNoiseBPLibrary::SimplexNoise3D(float x, float y, float z, float inFactor) 486 | { 487 | return (float)_simplexNoise3D(x * inFactor, y * inFactor, z* inFactor); 488 | } 489 | 490 | 491 | 492 | 493 | // 4D Simplex Noise 494 | float USimplexNoiseBPLibrary::SimplexNoise4D(float x, float y, float z, float w, float inFactor) 495 | { 496 | return (float)_simplexNoise4D(x * inFactor, y * inFactor, z * inFactor, w * inFactor); 497 | } 498 | 499 | // Scaled by float value 500 | 501 | float USimplexNoiseBPLibrary::SimplexNoiseScaled1D(float x, float scaleOut, float inFactor) 502 | { 503 | return _simplexNoise1D(x * inFactor) * scaleOut; 504 | } 505 | 506 | 507 | float USimplexNoiseBPLibrary::SimplexNoiseScaled2D(float x, float y, float scaleOut, float inFactor) 508 | { 509 | return _simplexNoise2D(x * inFactor, y * inFactor) * scaleOut; 510 | } 511 | 512 | 513 | float USimplexNoiseBPLibrary::SimplexNoiseScaled3D(float x, float y, float z, float scaleOut, float inFactor) 514 | { 515 | return _simplexNoise3D((x * inFactor), (y * inFactor), (z * inFactor)) * scaleOut; 516 | } 517 | 518 | 519 | float USimplexNoiseBPLibrary::SimplexNoiseScaled4D(float x, float y, float z, float w, float scaleOut, float inFactor) 520 | { 521 | return _simplexNoise4D(x * inFactor, y * inFactor, z * inFactor, w * inFactor) * scaleOut; 522 | }; 523 | 524 | // Return value in Range between two float numbers 525 | // Return Value is scaled by difference between rangeMin & rangeMax value 526 | 527 | 528 | float USimplexNoiseBPLibrary::SimplexNoiseInRange1D(float x, float rangeMin, float rangeMax, float inFactor) 529 | { 530 | if (rangeMax < rangeMin)rangeMax = rangeMin + 1.0f; // prevent negative numbers in that case we will return value between 0 - 1 531 | float nval = (SimplexNoise1D(x, inFactor) + 1) * 0.5f; 532 | return nval * (rangeMax - rangeMin) + rangeMin; 533 | } 534 | 535 | 536 | float USimplexNoiseBPLibrary::SimplexNoiseInRange2D(float x, float y, float rangeMin, float rangeMax, float inFactor) 537 | { 538 | if (rangeMax < rangeMin)rangeMax = rangeMin + 1.0f; // prevent negative numbers in that case we will return value between 0 - 1 539 | float nval = (SimplexNoise2D(x, y, inFactor) + 1) * 0.5f; 540 | return nval * (rangeMax - rangeMin) + rangeMin; 541 | } 542 | 543 | 544 | float USimplexNoiseBPLibrary::SimplexNoiseInRange3D(float x, float y, float z, float rangeMin, float rangeMax, float inFactor) 545 | { 546 | if (rangeMax < rangeMin)rangeMax = rangeMin + 1.0f; // prevent negative numbers in that case we will return value between 0 - 1 547 | float nval = (SimplexNoise3D(x, y, z, inFactor) + 1) * 0.5f; 548 | return nval * (rangeMax - rangeMin) + rangeMin; 549 | } 550 | 551 | 552 | float USimplexNoiseBPLibrary::SimplexNoiseInRange4D(float x, float y, float z, float w, float rangeMin, float rangeMax, float inFactor) 553 | { 554 | if (rangeMax < rangeMin)rangeMax = rangeMin + 1.0f; // prevent negative numbers in that case we will return value between 0 - 1 555 | float nval = (SimplexNoise4D(x, y, z, w, inFactor) + 1) * 0.5f; 556 | return nval * (rangeMax - rangeMin) + rangeMin; 557 | } 558 | 559 | // Get 1D Simplex Noise ( with lacunarity, persistance, octaves ) 560 | float USimplexNoiseBPLibrary::GetSimplexNoise1D_EX(float x, float lacunarity, float persistance, int octaves, float inFactor, bool ZeroToOne) 561 | { 562 | int i; 563 | float frequency = 1.0f; 564 | float amplitude = 1.0f; 565 | float sum = 0.0f; 566 | 567 | for (i = 0; i < octaves; i++) { 568 | sum += _simplexNoise1D(x * inFactor * frequency) * amplitude; 569 | frequency *= lacunarity; 570 | amplitude *= persistance; 571 | } 572 | 573 | return ZeroToOne ? sum * 0.5f + 0.5f : sum; 574 | } 575 | 576 | 577 | 578 | // Get 2D Simplex Noise ( with lacunarity, persistance, octaves ) 579 | 580 | float USimplexNoiseBPLibrary::GetSimplexNoise2D_EX(float x, float y, float lacunarity, float persistance, int octaves, float inFactor, bool ZeroToOne ) 581 | { 582 | int i; 583 | float frequency = 1.0f; 584 | float amplitude = 1.0f; 585 | float sum = 0.0f; 586 | 587 | for (i = 0; i < octaves; i++) { 588 | sum += _simplexNoise2D(x* inFactor * frequency, y* inFactor * frequency) * amplitude; 589 | frequency *= lacunarity; 590 | amplitude *= persistance; 591 | } 592 | 593 | return ZeroToOne ? sum * 0.5f + 0.5f : sum; 594 | } 595 | 596 | 597 | 598 | 599 | // Get 3D Simplex Noise ( with lacunarity, persistance, octaves ) 600 | float USimplexNoiseBPLibrary::GetSimplexNoise3D_EX(float x, float y, float z, float lacunarity, float persistance, int octaves, float inFactor, bool ZeroToOne) 601 | { 602 | int i; 603 | float frequency = 1.0f; 604 | float amplitude = 1.0f; 605 | float sum = 0.0f; 606 | 607 | for (i = 0; i < octaves; i++) { 608 | sum += _simplexNoise3D(x* inFactor * frequency, y* inFactor * frequency, z* inFactor * frequency) * amplitude; 609 | frequency *= lacunarity; 610 | amplitude *= persistance; 611 | } 612 | 613 | return ZeroToOne ? sum * 0.5f + 0.5f : sum; 614 | } 615 | 616 | 617 | 618 | 619 | // Get Get 4D Simplex Noise ( with lacunarity, persistance, octaves ) 620 | float USimplexNoiseBPLibrary::GetSimplexNoise4D_EX(float x, float y, float z, float w, float lacunarity, float persistance, int octaves, float inFactor, bool ZeroToOne) 621 | { 622 | int i; 623 | float frequency = 1.0f; 624 | float amplitude = 1.0f; 625 | float sum = 0.0f; 626 | 627 | for (i = 0; i < octaves; i++) { 628 | sum += _simplexNoise4D(x* inFactor * frequency, y* inFactor * frequency, z* inFactor * frequency, w* inFactor * frequency) * amplitude; 629 | frequency *= lacunarity; 630 | amplitude *= persistance; 631 | } 632 | 633 | return ZeroToOne ? sum * 0.5f + 0.5f : sum; 634 | } -------------------------------------------------------------------------------- /Source/SimplexNoise/Private/SimplexNoiseModule.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | SimplexNoise 1.2.0 3 | ----- 4 | DevDad - Afan Olovcic @ art-and-code.com - 08/12/2015 5 | */ 6 | #include "SimplexNoisePrivatePCH.h" 7 | 8 | //DEFINE_LOG_CATEGORY(Victory) 9 | 10 | IMPLEMENT_MODULE(FDefaultGameModuleImpl, SimplexNoise); -------------------------------------------------------------------------------- /Source/SimplexNoise/Private/SimplexNoisePrivatePCH.h: -------------------------------------------------------------------------------- 1 | /* 2 | SimplexNoise 1.2.0 3 | ----- 4 | DevDad - Afan Olovcic @ art-and-code.com - 08/12/2015 5 | */ 6 | #pragma once 7 | 8 | #include "Engine.h" 9 | #include "SimplexNoiseClasses.h" 10 | 11 | //DECLARE_LOG_CATEGORY_EXTERN(Victory, Log, All); 12 | -------------------------------------------------------------------------------- /Source/SimplexNoise/Public/ISimplexNoise.h: -------------------------------------------------------------------------------- 1 | /* 2 | SimplexNoise 1.2.0 3 | ----- 4 | DevDad @ art-and-code.com - 08/12/2015 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "ModuleManager.h" 10 | 11 | 12 | class ISimplexNoise : public IModuleInterface 13 | { 14 | 15 | public: 16 | 17 | static inline ISimplexNoise& Get() 18 | { 19 | return FModuleManager::LoadModuleChecked< ISimplexNoise >( "VictoryAI" ); 20 | } 21 | 22 | static inline bool IsAvailable() 23 | { 24 | return FModuleManager::Get().IsModuleLoaded( "SimplexNoise" ); 25 | } 26 | }; 27 | 28 | -------------------------------------------------------------------------------- /Source/SimplexNoise/Public/SimplexNoiseBPLibrary.h: -------------------------------------------------------------------------------- 1 | /* 2 | SimplexNoise 1.2.0 3 | ----- 4 | DevDad - Afan Olovcic @ www.art-and-code.com - 08/12/2015 5 | 6 | This algorithm was originally designed by Ken Perlin, but my code has been 7 | adapted and extended from the implementation written by Stefan Gustavson (stegu@itn.liu.se) 8 | and modified to fit to Unreal Engine 4 9 | 10 | 11 | * This is a clean, fast, modern and free Perlin Simplex noise function. 12 | * If we change float to double it could be even faster but there is no double type in Blueprint 13 | * All Public Functions are BlueprintCallable so they can be used in every blueprint 14 | 15 | 16 | From DevDad and Dedicated to you and Unreal Community 17 | Use it free for what ever you want 18 | I only request that you mention me in the credits for your game in the way that feels most appropriate to you. 19 | 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "Kismet/BlueprintFunctionLibrary.h" 25 | #include "SimplexNoiseBPLibrary.generated.h" 26 | 27 | USTRUCT() 28 | struct FPoint { 29 | GENERATED_BODY() 30 | UPROPERTY() 31 | float x; 32 | UPROPERTY() 33 | float y; 34 | UPROPERTY() 35 | float z; 36 | }; 37 | 38 | 39 | USTRUCT() 40 | struct FTriangle { 41 | GENERATED_BODY() 42 | UPROPERTY() 43 | FPoint point[3]; 44 | }; 45 | 46 | USTRUCT() 47 | struct FCell { 48 | GENERATED_BODY() 49 | UPROPERTY() 50 | FPoint point[8]; 51 | UPROPERTY() 52 | float val[8]; 53 | }; 54 | 55 | UCLASS() 56 | class SIMPLEXNOISE_API USimplexNoiseBPLibrary : public UBlueprintFunctionLibrary 57 | { 58 | GENERATED_UCLASS_BODY() 59 | private: 60 | static unsigned char perm[]; 61 | static float _grad(int hash, float x); 62 | static float _grad(int hash, float x, float y); 63 | static float _grad(int hash, float x, float y, float z); 64 | static float _grad(int hash, float x, float y, float z, float t); 65 | 66 | static float _simplexNoise1D(float x); 67 | static float _simplexNoise2D(float x, float y); 68 | static float _simplexNoise3D(float x, float y, float z); 69 | static float _simplexNoise4D(float x, float y, float z, float w); 70 | static int _polygonise(FCell cell, float isolevel, FTriangle* triangles); 71 | 72 | public: 73 | 74 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 75 | static void setNoiseSeed(const int32& newSeed); 76 | 77 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 78 | static void setNoiseFromStream(FRandomStream& RandStream); 79 | 80 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 81 | static float SimplexNoise1D(float x, float inFactor = 1.f); 82 | 83 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 84 | static float SimplexNoise2D(float x, float y, float inFactor = 1.f); 85 | 86 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 87 | static float SimplexNoise3D(float x, float y, float z, float inFactor = 1.f); 88 | 89 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 90 | static float SimplexNoise4D(float x, float y, float z, float w, float inFactor = 1.f); 91 | 92 | // Scaled by float value 93 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 94 | static float SimplexNoiseScaled1D(float x , float scaleOut = 1.f, float inFactor = 1.f); 95 | 96 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 97 | static float SimplexNoiseScaled2D(float x, float y, float scaleOut = 1.f, float inFactor = 1.f); 98 | 99 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 100 | static float SimplexNoiseScaled3D(float x, float y, float z, float scaleOut = 1.f, float inFactor = 1.f); 101 | 102 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 103 | static float SimplexNoiseScaled4D(float x, float y, float z, float w, float scaleOut = 1.f, float inFactor = 1.f); 104 | 105 | // Return value in Range between two float numbers 106 | // Return Value is scaled by difference between rangeMin & rangeMax value 107 | 108 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 109 | static float SimplexNoiseInRange1D(float x, float rangeMin, float rangeMax, float inFactor = 1.f); 110 | 111 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 112 | static float SimplexNoiseInRange2D(float x, float y, float rangeMin, float rangeMax, float inFactor = 1.f); 113 | 114 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 115 | static float SimplexNoiseInRange3D(float x, float y, float z, float rangeMin, float rangeMax, float inFactor = 1.f); 116 | 117 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 118 | static float SimplexNoiseInRange4D(float x, float y, float z, float w, float rangeMin, float rangeMax, float inFactor = 1.f); 119 | 120 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 121 | static float GetSimplexNoise1D_EX(float x, float lacunarity = 2.3f, float persistance = 0.6f, int octaves = 4, float inFactor = 1.0f, bool ZeroToOne = false); 122 | 123 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 124 | static float GetSimplexNoise2D_EX(float x, float y, float lacunarity = 2.3f, float persistance = 0.6f, int octaves = 4, float inFactor = 1.0f, bool ZeroToOne = false); 125 | 126 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 127 | static float GetSimplexNoise3D_EX(float x, float y, float z, float lacunarity = 2.3f, float persistance = 0.6f, int octaves = 4, float inFactor = 1.0f, bool ZeroToOne = false); 128 | 129 | UFUNCTION(BlueprintCallable, Category = "SimplexNoise") 130 | static float GetSimplexNoise4D_EX(float x, float y, float z, float w, float lacunarity = 2.3f, float persistance = 0.6f, int octaves = 4, float inFactor = 1.0f, bool ZeroToOne = false); 131 | 132 | }; 133 | 134 | -------------------------------------------------------------------------------- /Source/SimplexNoise/SimplexNoise.Build.cs: -------------------------------------------------------------------------------- 1 | /* 2 | SimplexNoise 1.2.0 3 | ----- 4 | DevDad - Afan Olovcic @ www.art-and-code.com - 08/12/2015 5 | */ 6 | using UnrealBuildTool; 7 | 8 | public class SimplexNoise : ModuleRules 9 | { 10 | public SimplexNoise(ReadOnlyTargetRules Target) : base(Target) //4.16+ Module Constructor 11 | //public SimplexNoise(TargetInfo Target) //4.15 Module Constructor 12 | { 13 | PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; 14 | //Private Paths 15 | PrivateIncludePaths.AddRange(new string[] { 16 | "SimplexNoise/Private" 17 | }); 18 | 19 | PublicDependencyModuleNames.AddRange( 20 | new string[] { 21 | "Core", 22 | "CoreUObject", 23 | "Engine" 24 | } 25 | ); 26 | } 27 | } 28 | --------------------------------------------------------------------------------