FastNoiseLite.h 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586
  1. // MIT License
  2. //
  3. // Copyright(c) 2020 Jordan Peck (jordan.me2@gmail.com)
  4. // Copyright(c) 2020 Contributors
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files(the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions :
  12. //
  13. // The above copyright notice and this permission notice shall be included in all
  14. // copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. //
  24. // .'',;:cldxkO00KKXXNNWWWNNXKOkxdollcc::::::;:::ccllloooolllllllllooollc:,'... ...........',;cldxkO000Okxdlc::;;;,,;;;::cclllllll
  25. // ..',;:ldxO0KXXNNNNNNNNXXK0kxdolcc::::::;;;,,,,,,;;;;;;;;;;:::cclllllc:;'.... ...........',;:ldxO0KXXXK0Okxdolc::;;;;::cllodddddo
  26. // ...',:loxO0KXNNNNNXXKK0Okxdolc::;::::::::;;;,,'''''.....''',;:clllllc:;,'............''''''''',;:loxO0KXNNNNNXK0Okxdollccccllodxxxxxxd
  27. // ....';:ldkO0KXXXKK00Okxdolcc:;;;;;::cclllcc:;;,''..... ....',;clooddolcc:;;;;,,;;;;;::::;;;;;;:cloxk0KXNWWWWWWNXKK0Okxddoooddxxkkkkkxx
  28. // .....';:ldxkOOOOOkxxdolcc:;;;,,,;;:cllooooolcc:;'... ..,:codxkkkxddooollloooooooollcc:::::clodkO0KXNWWWWWWNNXK00Okxxxxxxxxkkkkxxx
  29. // . ....';:cloddddo___________,,,,;;:clooddddoolc:,... ..,:ldx__00OOOkkk___kkkkkkxxdollc::::cclodkO0KXXNNNNNNXXK0OOkxxxxxxxxxxxxddd
  30. // .......',;:cccc:| |,,,;;:cclooddddoll:;'.. ..';cox| \KKK000| |KK00OOkxdocc___;::clldxxkO0KKKKK00Okkxdddddddddddddddoo
  31. // .......'',,,,,''| ________|',,;;::cclloooooolc:;'......___:ldk| \KK000| |XKKK0Okxolc| |;;::cclodxxkkkkxxdoolllcclllooodddooooo
  32. // ''......''''....| | ....'',,,,;;;::cclloooollc:;,''.'| |oxk| \OOO0| |KKK00Oxdoll|___|;;;;;::ccllllllcc::;;,,;;;:cclloooooooo
  33. // ;;,''.......... | |_____',,;;;____:___cllo________.___| |___| \xkk| |KK_______ool___:::;________;;;_______...'',;;:ccclllloo
  34. // c:;,''......... | |:::/ ' |lo/ | | \dx| |0/ \d| |cc/ |'/ \......',,;;:ccllo
  35. // ol:;,'..........| _____|ll/ __ |o/ ______|____ ___| | \o| |/ ___ \| |o/ ______|/ ___ \ .......'',;:clo
  36. // dlc;,...........| |::clooo| / | |x\___ \KXKKK0| |dol| |\ \| | | | | |d\___ \..| | / / ....',:cl
  37. // xoc;'... .....'| |llodddd| \__| |_____\ \KKK0O| |lc:| |'\ | |___| | |_____\ \.| |_/___/... ...',;:c
  38. // dlc;'... ....',;| |oddddddo\ | |Okkx| |::;| |..\ |\ /| | | \ |... ....',;:c
  39. // ol:,'.......',:c|___|xxxddollc\_____,___|_________/ddoll|___|,,,|___|...\_____|:\ ______/l|___|_________/...\________|'........',;::cc
  40. // c:;'.......';:codxxkkkkxxolc::;::clodxkOO0OOkkxdollc::;;,,''''',,,,''''''''''',,'''''',;:loxkkOOkxol:;,'''',,;:ccllcc:;,'''''',;::ccll
  41. // ;,'.......',:codxkOO0OOkxdlc:;,,;;:cldxxkkxxdolc:;;,,''.....'',;;:::;;,,,'''''........,;cldkO0KK0Okdoc::;;::cloodddoolc:;;;;;::ccllooo
  42. // .........',;:lodxOO0000Okdoc:,,',,;:clloddoolc:;,''.......'',;:clooollc:;;,,''.......',:ldkOKXNNXX0Oxdolllloddxxxxxxdolccccccllooodddd
  43. // . .....';:cldxkO0000Okxol:;,''',,;::cccc:;,,'.......'',;:cldxxkkxxdolc:;;,'.......';coxOKXNWWWNXKOkxddddxxkkkkkkxdoollllooddxxxxkkk
  44. // ....',;:codxkO000OOxdoc:;,''',,,;;;;,''.......',,;:clodkO00000Okxolc::;,,''..',;:ldxOKXNWWWNNK0OkkkkkkkkkkkxxddooooodxxkOOOOO000
  45. // ....',;;clodxkkOOOkkdolc:;,,,,,,,,'..........,;:clodxkO0KKXKK0Okxdolcc::;;,,,;;:codkO0XXNNNNXKK0OOOOOkkkkxxdoollloodxkO0KKKXXXXX
  46. //
  47. // VERSION: 1.0.1
  48. // https://github.com/Auburn/FastNoise
  49. #ifndef FASTNOISELITE_H
  50. #define FASTNOISELITE_H
  51. #include <cmath>
  52. class FastNoiseLite
  53. {
  54. public:
  55. enum NoiseType
  56. {
  57. NoiseType_OpenSimplex2,
  58. NoiseType_OpenSimplex2S,
  59. NoiseType_Cellular,
  60. NoiseType_Perlin,
  61. NoiseType_ValueCubic,
  62. NoiseType_Value
  63. };
  64. enum RotationType3D
  65. {
  66. RotationType3D_None,
  67. RotationType3D_ImproveXYPlanes,
  68. RotationType3D_ImproveXZPlanes
  69. };
  70. enum FractalType
  71. {
  72. FractalType_None,
  73. FractalType_FBm,
  74. FractalType_Ridged,
  75. FractalType_PingPong,
  76. FractalType_DomainWarpProgressive,
  77. FractalType_DomainWarpIndependent
  78. };
  79. enum CellularDistanceFunction
  80. {
  81. CellularDistanceFunction_Euclidean,
  82. CellularDistanceFunction_EuclideanSq,
  83. CellularDistanceFunction_Manhattan,
  84. CellularDistanceFunction_Hybrid
  85. };
  86. enum CellularReturnType
  87. {
  88. CellularReturnType_CellValue,
  89. CellularReturnType_Distance,
  90. CellularReturnType_Distance2,
  91. CellularReturnType_Distance2Add,
  92. CellularReturnType_Distance2Sub,
  93. CellularReturnType_Distance2Mul,
  94. CellularReturnType_Distance2Div
  95. };
  96. enum DomainWarpType
  97. {
  98. DomainWarpType_OpenSimplex2,
  99. DomainWarpType_OpenSimplex2Reduced,
  100. DomainWarpType_BasicGrid
  101. };
  102. /// <summary>
  103. /// Create new FastNoise object with optional seed
  104. /// </summary>
  105. FastNoiseLite(int seed = 1337)
  106. {
  107. mSeed = seed;
  108. mFrequency = 0.01f;
  109. mNoiseType = NoiseType_OpenSimplex2;
  110. mRotationType3D = RotationType3D_None;
  111. mTransformType3D = TransformType3D_DefaultOpenSimplex2;
  112. mFractalType = FractalType_None;
  113. mOctaves = 3;
  114. mLacunarity = 2.0f;
  115. mGain = 0.5f;
  116. mWeightedStrength = 0.0f;
  117. mPingPongStength = 2.0f;
  118. mFractalBounding = 1 / 1.75f;
  119. mCellularDistanceFunction = CellularDistanceFunction_EuclideanSq;
  120. mCellularReturnType = CellularReturnType_Distance;
  121. mCellularJitterModifier = 1.0f;
  122. mDomainWarpType = DomainWarpType_OpenSimplex2;
  123. mWarpTransformType3D = TransformType3D_DefaultOpenSimplex2;
  124. mDomainWarpAmp = 1.0f;
  125. }
  126. /// <summary>
  127. /// Sets seed used for all noise types
  128. /// </summary>
  129. /// <remarks>
  130. /// Default: 1337
  131. /// </remarks>
  132. void SetSeed(int seed) { mSeed = seed; }
  133. /// <summary>
  134. /// Sets frequency for all noise types
  135. /// </summary>
  136. /// <remarks>
  137. /// Default: 0.01
  138. /// </remarks>
  139. void SetFrequency(float frequency) { mFrequency = frequency; }
  140. /// <summary>
  141. /// Sets noise algorithm used for GetNoise(...)
  142. /// </summary>
  143. /// <remarks>
  144. /// Default: OpenSimplex2
  145. /// </remarks>
  146. void SetNoiseType(NoiseType noiseType)
  147. {
  148. mNoiseType = noiseType;
  149. UpdateTransformType3D();
  150. }
  151. /// <summary>
  152. /// Sets domain rotation type for 3D Noise and 3D DomainWarp.
  153. /// Can aid in reducing directional artifacts when sampling a 2D plane in 3D
  154. /// </summary>
  155. /// <remarks>
  156. /// Default: None
  157. /// </remarks>
  158. void SetRotationType3D(RotationType3D rotationType3D)
  159. {
  160. mRotationType3D = rotationType3D;
  161. UpdateTransformType3D();
  162. UpdateWarpTransformType3D();
  163. }
  164. /// <summary>
  165. /// Sets method for combining octaves in all fractal noise types
  166. /// </summary>
  167. /// <remarks>
  168. /// Default: None
  169. /// Note: FractalType_DomainWarp... only affects DomainWarp(...)
  170. /// </remarks>
  171. void SetFractalType(FractalType fractalType) { mFractalType = fractalType; }
  172. /// <summary>
  173. /// Sets octave count for all fractal noise types
  174. /// </summary>
  175. /// <remarks>
  176. /// Default: 3
  177. /// </remarks>
  178. void SetFractalOctaves(int octaves)
  179. {
  180. mOctaves = octaves;
  181. CalculateFractalBounding();
  182. }
  183. /// <summary>
  184. /// Sets octave lacunarity for all fractal noise types
  185. /// </summary>
  186. /// <remarks>
  187. /// Default: 2.0
  188. /// </remarks>
  189. void SetFractalLacunarity(float lacunarity) { mLacunarity = lacunarity; }
  190. /// <summary>
  191. /// Sets octave gain for all fractal noise types
  192. /// </summary>
  193. /// <remarks>
  194. /// Default: 0.5
  195. /// </remarks>
  196. void SetFractalGain(float gain)
  197. {
  198. mGain = gain;
  199. CalculateFractalBounding();
  200. }
  201. /// <summary>
  202. /// Sets octave weighting for all none DomainWarp fratal types
  203. /// </summary>
  204. /// <remarks>
  205. /// Default: 0.0
  206. /// Note: Keep between 0...1 to maintain -1...1 output bounding
  207. /// </remarks>
  208. void SetFractalWeightedStrength(float weightedStrength) { mWeightedStrength = weightedStrength; }
  209. /// <summary>
  210. /// Sets strength of the fractal ping pong effect
  211. /// </summary>
  212. /// <remarks>
  213. /// Default: 2.0
  214. /// </remarks>
  215. void SetFractalPingPongStrength(float pingPongStrength) { mPingPongStength = pingPongStrength; }
  216. /// <summary>
  217. /// Sets distance function used in cellular noise calculations
  218. /// </summary>
  219. /// <remarks>
  220. /// Default: Distance
  221. /// </remarks>
  222. void SetCellularDistanceFunction(CellularDistanceFunction cellularDistanceFunction) { mCellularDistanceFunction = cellularDistanceFunction; }
  223. /// <summary>
  224. /// Sets return type from cellular noise calculations
  225. /// </summary>
  226. /// <remarks>
  227. /// Default: EuclideanSq
  228. /// </remarks>
  229. void SetCellularReturnType(CellularReturnType cellularReturnType) { mCellularReturnType = cellularReturnType; }
  230. /// <summary>
  231. /// Sets the maximum distance a cellular point can move from it's grid position
  232. /// </summary>
  233. /// <remarks>
  234. /// Default: 1.0
  235. /// Note: Setting this higher than 1 will cause artifacts
  236. /// </remarks>
  237. void SetCellularJitter(float cellularJitter) { mCellularJitterModifier = cellularJitter; }
  238. /// <summary>
  239. /// Sets the warp algorithm when using DomainWarp(...)
  240. /// </summary>
  241. /// <remarks>
  242. /// Default: OpenSimplex2
  243. /// </remarks>
  244. void SetDomainWarpType(DomainWarpType domainWarpType)
  245. {
  246. mDomainWarpType = domainWarpType;
  247. UpdateWarpTransformType3D();
  248. }
  249. /// <summary>
  250. /// Sets the maximum warp distance from original position when using DomainWarp(...)
  251. /// </summary>
  252. /// <remarks>
  253. /// Default: 1.0
  254. /// </remarks>
  255. void SetDomainWarpAmp(float domainWarpAmp) { mDomainWarpAmp = domainWarpAmp; }
  256. /// <summary>
  257. /// 2D noise at given position using current settings
  258. /// </summary>
  259. /// <returns>
  260. /// Noise output bounded between -1...1
  261. /// </returns>
  262. template <typename FNfloat>
  263. float GetNoise(FNfloat x, FNfloat y)
  264. {
  265. Arguments_must_be_floating_point_values<FNfloat>();
  266. TransformNoiseCoordinate(x, y);
  267. switch (mFractalType)
  268. {
  269. default:
  270. return GenNoiseSingle(mSeed, x, y);
  271. case FractalType_FBm:
  272. return GenFractalFBm(x, y);
  273. case FractalType_Ridged:
  274. return GenFractalRidged(x, y);
  275. case FractalType_PingPong:
  276. return GenFractalPingPong(x, y);
  277. }
  278. }
  279. /// <summary>
  280. /// 3D noise at given position using current settings
  281. /// </summary>
  282. /// <returns>
  283. /// Noise output bounded between -1...1
  284. /// </returns>
  285. template <typename FNfloat>
  286. float GetNoise(FNfloat x, FNfloat y, FNfloat z)
  287. {
  288. Arguments_must_be_floating_point_values<FNfloat>();
  289. TransformNoiseCoordinate(x, y, z);
  290. switch (mFractalType)
  291. {
  292. default:
  293. return GenNoiseSingle(mSeed, x, y, z);
  294. case FractalType_FBm:
  295. return GenFractalFBm(x, y, z);
  296. case FractalType_Ridged:
  297. return GenFractalRidged(x, y, z);
  298. case FractalType_PingPong:
  299. return GenFractalPingPong(x, y, z);
  300. }
  301. }
  302. /// <summary>
  303. /// 2D warps the input position using current domain warp settings
  304. /// </summary>
  305. /// <example>
  306. /// Example usage with GetNoise
  307. /// <code>DomainWarp(x, y)
  308. /// noise = GetNoise(x, y)</code>
  309. /// </example>
  310. template <typename FNfloat>
  311. void DomainWarp(FNfloat& x, FNfloat& y)
  312. {
  313. Arguments_must_be_floating_point_values<FNfloat>();
  314. switch (mFractalType)
  315. {
  316. default:
  317. DomainWarpSingle(x, y);
  318. break;
  319. case FractalType_DomainWarpProgressive:
  320. DomainWarpFractalProgressive(x, y);
  321. break;
  322. case FractalType_DomainWarpIndependent:
  323. DomainWarpFractalIndependent(x, y);
  324. break;
  325. }
  326. }
  327. /// <summary>
  328. /// 3D warps the input position using current domain warp settings
  329. /// </summary>
  330. /// <example>
  331. /// Example usage with GetNoise
  332. /// <code>DomainWarp(x, y, z)
  333. /// noise = GetNoise(x, y, z)</code>
  334. /// </example>
  335. template <typename FNfloat>
  336. void DomainWarp(FNfloat& x, FNfloat& y, FNfloat& z)
  337. {
  338. Arguments_must_be_floating_point_values<FNfloat>();
  339. switch (mFractalType)
  340. {
  341. default:
  342. DomainWarpSingle(x, y, z);
  343. break;
  344. case FractalType_DomainWarpProgressive:
  345. DomainWarpFractalProgressive(x, y, z);
  346. break;
  347. case FractalType_DomainWarpIndependent:
  348. DomainWarpFractalIndependent(x, y, z);
  349. break;
  350. }
  351. }
  352. private:
  353. template <typename T>
  354. struct Arguments_must_be_floating_point_values;
  355. enum TransformType3D
  356. {
  357. TransformType3D_None,
  358. TransformType3D_ImproveXYPlanes,
  359. TransformType3D_ImproveXZPlanes,
  360. TransformType3D_DefaultOpenSimplex2
  361. };
  362. int mSeed;
  363. float mFrequency;
  364. NoiseType mNoiseType;
  365. RotationType3D mRotationType3D;
  366. TransformType3D mTransformType3D;
  367. FractalType mFractalType;
  368. int mOctaves;
  369. float mLacunarity;
  370. float mGain;
  371. float mWeightedStrength;
  372. float mPingPongStength;
  373. float mFractalBounding;
  374. CellularDistanceFunction mCellularDistanceFunction;
  375. CellularReturnType mCellularReturnType;
  376. float mCellularJitterModifier;
  377. DomainWarpType mDomainWarpType;
  378. TransformType3D mWarpTransformType3D;
  379. float mDomainWarpAmp;
  380. template <typename T>
  381. struct Lookup
  382. {
  383. static const T Gradients2D[];
  384. static const T Gradients3D[];
  385. static const T RandVecs2D[];
  386. static const T RandVecs3D[];
  387. };
  388. static float FastMin(float a, float b) { return a < b ? a : b; }
  389. static float FastMax(float a, float b) { return a > b ? a : b; }
  390. static float FastAbs(float f) { return f < 0 ? -f : f; }
  391. static float FastSqrt(float f) { return sqrtf(f); }
  392. template <typename FNfloat>
  393. static int FastFloor(FNfloat f) { return f >= 0 ? (int)f : (int)f - 1; }
  394. template <typename FNfloat>
  395. static int FastRound(FNfloat f) { return f >= 0 ? (int)(f + 0.5f) : (int)(f - 0.5f); }
  396. static float Lerp(float a, float b, float t) { return a + t * (b - a); }
  397. static float InterpHermite(float t) { return t * t * (3 - 2 * t); }
  398. static float InterpQuintic(float t) { return t * t * t * (t * (t * 6 - 15) + 10); }
  399. static float CubicLerp(float a, float b, float c, float d, float t)
  400. {
  401. float p = (d - c) - (a - b);
  402. return t * t * t * p + t * t * ((a - b) - p) + t * (c - a) + b;
  403. }
  404. static float PingPong(float t)
  405. {
  406. t -= (float)(int)(t * 0.5f) * 2.f;
  407. return t < 1 ? t : 2 - t;
  408. }
  409. void CalculateFractalBounding()
  410. {
  411. float gain = FastAbs(mGain);
  412. float amp = gain;
  413. float ampFractal = 1.0f;
  414. for (int i = 1; i < mOctaves; i++)
  415. {
  416. ampFractal += amp;
  417. amp *= gain;
  418. }
  419. mFractalBounding = 1 / ampFractal;
  420. }
  421. // Hashing
  422. static const int PrimeX = 501125321;
  423. static const int PrimeY = 1136930381;
  424. static const int PrimeZ = 1720413743;
  425. static int Hash(int seed, int xPrimed, int yPrimed)
  426. {
  427. int hash = seed ^ xPrimed ^ yPrimed;
  428. hash *= 0x27d4eb2d;
  429. return hash;
  430. }
  431. static int Hash(int seed, int xPrimed, int yPrimed, int zPrimed)
  432. {
  433. int hash = seed ^ xPrimed ^ yPrimed ^ zPrimed;
  434. hash *= 0x27d4eb2d;
  435. return hash;
  436. }
  437. static float ValCoord(int seed, int xPrimed, int yPrimed)
  438. {
  439. int hash = Hash(seed, xPrimed, yPrimed);
  440. hash *= hash;
  441. hash ^= hash << 19;
  442. return (float)hash * (1.f / 2147483648.0f);
  443. }
  444. static float ValCoord(int seed, int xPrimed, int yPrimed, int zPrimed)
  445. {
  446. int hash = Hash(seed, xPrimed, yPrimed, zPrimed);
  447. hash *= hash;
  448. hash ^= hash << 19;
  449. return (float)hash * (1.f / 2147483648.0f);
  450. }
  451. float GradCoord(int seed, int xPrimed, int yPrimed, float xd, float yd)
  452. {
  453. int hash = Hash(seed, xPrimed, yPrimed);
  454. hash ^= hash >> 15;
  455. hash &= 127 << 1;
  456. float xg = Lookup<float>::Gradients2D[hash];
  457. float yg = Lookup<float>::Gradients2D[hash | 1];
  458. return xd * xg + yd * yg;
  459. }
  460. float GradCoord(int seed, int xPrimed, int yPrimed, int zPrimed, float xd, float yd, float zd)
  461. {
  462. int hash = Hash(seed, xPrimed, yPrimed, zPrimed);
  463. hash ^= hash >> 15;
  464. hash &= 63 << 2;
  465. float xg = Lookup<float>::Gradients3D[hash];
  466. float yg = Lookup<float>::Gradients3D[hash | 1];
  467. float zg = Lookup<float>::Gradients3D[hash | 2];
  468. return xd * xg + yd * yg + zd * zg;
  469. }
  470. void GradCoordOut(int seed, int xPrimed, int yPrimed, float& xo, float& yo)
  471. {
  472. int hash = Hash(seed, xPrimed, yPrimed) & (255 << 1);
  473. xo = Lookup<float>::RandVecs2D[hash];
  474. yo = Lookup<float>::RandVecs2D[hash | 1];
  475. }
  476. void GradCoordOut(int seed, int xPrimed, int yPrimed, int zPrimed, float& xo, float& yo, float& zo)
  477. {
  478. int hash = Hash(seed, xPrimed, yPrimed, zPrimed) & (255 << 2);
  479. xo = Lookup<float>::RandVecs3D[hash];
  480. yo = Lookup<float>::RandVecs3D[hash | 1];
  481. zo = Lookup<float>::RandVecs3D[hash | 2];
  482. }
  483. void GradCoordDual(int seed, int xPrimed, int yPrimed, float xd, float yd, float& xo, float& yo)
  484. {
  485. int hash = Hash(seed, xPrimed, yPrimed);
  486. int index1 = hash & (127 << 1);
  487. int index2 = (hash >> 7) & (255 << 1);
  488. float xg = Lookup<float>::Gradients2D[index1];
  489. float yg = Lookup<float>::Gradients2D[index1 | 1];
  490. float value = xd * xg + yd * yg;
  491. float xgo = Lookup<float>::RandVecs2D[index2];
  492. float ygo = Lookup<float>::RandVecs2D[index2 | 1];
  493. xo = value * xgo;
  494. yo = value * ygo;
  495. }
  496. void GradCoordDual(int seed, int xPrimed, int yPrimed, int zPrimed, float xd, float yd, float zd, float& xo, float& yo, float& zo)
  497. {
  498. int hash = Hash(seed, xPrimed, yPrimed, zPrimed);
  499. int index1 = hash & (63 << 2);
  500. int index2 = (hash >> 6) & (255 << 2);
  501. float xg = Lookup<float>::Gradients3D[index1];
  502. float yg = Lookup<float>::Gradients3D[index1 | 1];
  503. float zg = Lookup<float>::Gradients3D[index1 | 2];
  504. float value = xd * xg + yd * yg + zd * zg;
  505. float xgo = Lookup<float>::RandVecs3D[index2];
  506. float ygo = Lookup<float>::RandVecs3D[index2 | 1];
  507. float zgo = Lookup<float>::RandVecs3D[index2 | 2];
  508. xo = value * xgo;
  509. yo = value * ygo;
  510. zo = value * zgo;
  511. }
  512. // Generic noise gen
  513. template <typename FNfloat>
  514. float GenNoiseSingle(int seed, FNfloat x, FNfloat y)
  515. {
  516. switch (mNoiseType)
  517. {
  518. case NoiseType_OpenSimplex2:
  519. return SingleSimplex(seed, x, y);
  520. case NoiseType_OpenSimplex2S:
  521. return SingleOpenSimplex2S(seed, x, y);
  522. case NoiseType_Cellular:
  523. return SingleCellular(seed, x, y);
  524. case NoiseType_Perlin:
  525. return SinglePerlin(seed, x, y);
  526. case NoiseType_ValueCubic:
  527. return SingleValueCubic(seed, x, y);
  528. case NoiseType_Value:
  529. return SingleValue(seed, x, y);
  530. default:
  531. return 0;
  532. }
  533. }
  534. template <typename FNfloat>
  535. float GenNoiseSingle(int seed, FNfloat x, FNfloat y, FNfloat z)
  536. {
  537. switch (mNoiseType)
  538. {
  539. case NoiseType_OpenSimplex2:
  540. return SingleOpenSimplex2(seed, x, y, z);
  541. case NoiseType_OpenSimplex2S:
  542. return SingleOpenSimplex2S(seed, x, y, z);
  543. case NoiseType_Cellular:
  544. return SingleCellular(seed, x, y, z);
  545. case NoiseType_Perlin:
  546. return SinglePerlin(seed, x, y, z);
  547. case NoiseType_ValueCubic:
  548. return SingleValueCubic(seed, x, y, z);
  549. case NoiseType_Value:
  550. return SingleValue(seed, x, y, z);
  551. default:
  552. return 0;
  553. }
  554. }
  555. // Noise Coordinate Transforms (frequency, and possible skew or rotation)
  556. template <typename FNfloat>
  557. void TransformNoiseCoordinate(FNfloat& x, FNfloat& y)
  558. {
  559. x *= mFrequency;
  560. y *= mFrequency;
  561. switch (mNoiseType)
  562. {
  563. case NoiseType_OpenSimplex2:
  564. case NoiseType_OpenSimplex2S:
  565. {
  566. const FNfloat SQRT3 = (FNfloat)1.7320508075688772935274463415059;
  567. const FNfloat F2 = 0.5f * (SQRT3 - 1);
  568. FNfloat t = (x + y) * F2;
  569. x += t;
  570. y += t;
  571. }
  572. break;
  573. default:
  574. break;
  575. }
  576. }
  577. template <typename FNfloat>
  578. void TransformNoiseCoordinate(FNfloat& x, FNfloat& y, FNfloat& z)
  579. {
  580. x *= mFrequency;
  581. y *= mFrequency;
  582. z *= mFrequency;
  583. switch (mTransformType3D)
  584. {
  585. case TransformType3D_ImproveXYPlanes:
  586. {
  587. FNfloat xy = x + y;
  588. FNfloat s2 = xy * -(FNfloat)0.211324865405187;
  589. z *= (FNfloat)0.577350269189626;
  590. x += s2 - z;
  591. y = y + s2 - z;
  592. z += xy * (FNfloat)0.577350269189626;
  593. }
  594. break;
  595. case TransformType3D_ImproveXZPlanes:
  596. {
  597. FNfloat xz = x + z;
  598. FNfloat s2 = xz * -(FNfloat)0.211324865405187;
  599. y *= (FNfloat)0.577350269189626;
  600. x += s2 - y;
  601. z += s2 - y;
  602. y += xz * (FNfloat)0.577350269189626;
  603. }
  604. break;
  605. case TransformType3D_DefaultOpenSimplex2:
  606. {
  607. const FNfloat R3 = (FNfloat)(2.0 / 3.0);
  608. FNfloat r = (x + y + z) * R3; // Rotation, not skew
  609. x = r - x;
  610. y = r - y;
  611. z = r - z;
  612. }
  613. break;
  614. default:
  615. break;
  616. }
  617. }
  618. void UpdateTransformType3D()
  619. {
  620. switch (mRotationType3D)
  621. {
  622. case RotationType3D_ImproveXYPlanes:
  623. mTransformType3D = TransformType3D_ImproveXYPlanes;
  624. break;
  625. case RotationType3D_ImproveXZPlanes:
  626. mTransformType3D = TransformType3D_ImproveXZPlanes;
  627. break;
  628. default:
  629. switch (mNoiseType)
  630. {
  631. case NoiseType_OpenSimplex2:
  632. case NoiseType_OpenSimplex2S:
  633. mTransformType3D = TransformType3D_DefaultOpenSimplex2;
  634. break;
  635. default:
  636. mTransformType3D = TransformType3D_None;
  637. break;
  638. }
  639. break;
  640. }
  641. }
  642. // Domain Warp Coordinate Transforms
  643. template <typename FNfloat>
  644. void TransformDomainWarpCoordinate(FNfloat& x, FNfloat& y)
  645. {
  646. switch (mDomainWarpType)
  647. {
  648. case DomainWarpType_OpenSimplex2:
  649. case DomainWarpType_OpenSimplex2Reduced:
  650. {
  651. const FNfloat SQRT3 = (FNfloat)1.7320508075688772935274463415059;
  652. const FNfloat F2 = 0.5f * (SQRT3 - 1);
  653. FNfloat t = (x + y) * F2;
  654. x += t;
  655. y += t;
  656. }
  657. break;
  658. default:
  659. break;
  660. }
  661. }
  662. template <typename FNfloat>
  663. void TransformDomainWarpCoordinate(FNfloat& x, FNfloat& y, FNfloat& z)
  664. {
  665. switch (mWarpTransformType3D)
  666. {
  667. case TransformType3D_ImproveXYPlanes:
  668. {
  669. FNfloat xy = x + y;
  670. FNfloat s2 = xy * -(FNfloat)0.211324865405187;
  671. z *= (FNfloat)0.577350269189626;
  672. x += s2 - z;
  673. y = y + s2 - z;
  674. z += xy * (FNfloat)0.577350269189626;
  675. }
  676. break;
  677. case TransformType3D_ImproveXZPlanes:
  678. {
  679. FNfloat xz = x + z;
  680. FNfloat s2 = xz * -(FNfloat)0.211324865405187;
  681. y *= (FNfloat)0.577350269189626;
  682. x += s2 - y;
  683. z += s2 - y;
  684. y += xz * (FNfloat)0.577350269189626;
  685. }
  686. break;
  687. case TransformType3D_DefaultOpenSimplex2:
  688. {
  689. const FNfloat R3 = (FNfloat)(2.0 / 3.0);
  690. FNfloat r = (x + y + z) * R3; // Rotation, not skew
  691. x = r - x;
  692. y = r - y;
  693. z = r - z;
  694. }
  695. break;
  696. default:
  697. break;
  698. }
  699. }
  700. void UpdateWarpTransformType3D()
  701. {
  702. switch (mRotationType3D)
  703. {
  704. case RotationType3D_ImproveXYPlanes:
  705. mWarpTransformType3D = TransformType3D_ImproveXYPlanes;
  706. break;
  707. case RotationType3D_ImproveXZPlanes:
  708. mWarpTransformType3D = TransformType3D_ImproveXZPlanes;
  709. break;
  710. default:
  711. switch (mDomainWarpType)
  712. {
  713. case DomainWarpType_OpenSimplex2:
  714. case DomainWarpType_OpenSimplex2Reduced:
  715. mWarpTransformType3D = TransformType3D_DefaultOpenSimplex2;
  716. break;
  717. default:
  718. mWarpTransformType3D = TransformType3D_None;
  719. break;
  720. }
  721. break;
  722. }
  723. }
  724. // Fractal FBm
  725. template <typename FNfloat>
  726. float GenFractalFBm(FNfloat x, FNfloat y)
  727. {
  728. int seed = mSeed;
  729. float sum = 0;
  730. float amp = mFractalBounding;
  731. for (int i = 0; i < mOctaves; i++)
  732. {
  733. float noise = GenNoiseSingle(seed++, x, y);
  734. sum += noise * amp;
  735. amp *= Lerp(1.0f, FastMin(noise + 1, 2) * 0.5f, mWeightedStrength);
  736. x *= mLacunarity;
  737. y *= mLacunarity;
  738. amp *= mGain;
  739. }
  740. return sum;
  741. }
  742. template <typename FNfloat>
  743. float GenFractalFBm(FNfloat x, FNfloat y, FNfloat z)
  744. {
  745. int seed = mSeed;
  746. float sum = 0;
  747. float amp = mFractalBounding;
  748. for (int i = 0; i < mOctaves; i++)
  749. {
  750. float noise = GenNoiseSingle(seed++, x, y, z);
  751. sum += noise * amp;
  752. amp *= Lerp(1.0f, (noise + 1) * 0.5f, mWeightedStrength);
  753. x *= mLacunarity;
  754. y *= mLacunarity;
  755. z *= mLacunarity;
  756. amp *= mGain;
  757. }
  758. return sum;
  759. }
  760. // Fractal Ridged
  761. template <typename FNfloat>
  762. float GenFractalRidged(FNfloat x, FNfloat y)
  763. {
  764. int seed = mSeed;
  765. float sum = 0;
  766. float amp = mFractalBounding;
  767. for (int i = 0; i < mOctaves; i++)
  768. {
  769. float noise = FastAbs(GenNoiseSingle(seed++, x, y));
  770. sum += (noise * -2 + 1) * amp;
  771. amp *= Lerp(1.0f, 1 - noise, mWeightedStrength);
  772. x *= mLacunarity;
  773. y *= mLacunarity;
  774. amp *= mGain;
  775. }
  776. return sum;
  777. }
  778. template <typename FNfloat>
  779. float GenFractalRidged(FNfloat x, FNfloat y, FNfloat z)
  780. {
  781. int seed = mSeed;
  782. float sum = 0;
  783. float amp = mFractalBounding;
  784. for (int i = 0; i < mOctaves; i++)
  785. {
  786. float noise = FastAbs(GenNoiseSingle(seed++, x, y, z));
  787. sum += (noise * -2 + 1) * amp;
  788. amp *= Lerp(1.0f, 1 - noise, mWeightedStrength);
  789. x *= mLacunarity;
  790. y *= mLacunarity;
  791. z *= mLacunarity;
  792. amp *= mGain;
  793. }
  794. return sum;
  795. }
  796. // Fractal PingPong
  797. template <typename FNfloat>
  798. float GenFractalPingPong(FNfloat x, FNfloat y)
  799. {
  800. int seed = mSeed;
  801. float sum = 0;
  802. float amp = mFractalBounding;
  803. for (int i = 0; i < mOctaves; i++)
  804. {
  805. float noise = PingPong((GenNoiseSingle(seed++, x, y) + 1) * mPingPongStength);
  806. sum += (noise - 0.5f) * 2 * amp;
  807. amp *= Lerp(1.0f, noise, mWeightedStrength);
  808. x *= mLacunarity;
  809. y *= mLacunarity;
  810. amp *= mGain;
  811. }
  812. return sum;
  813. }
  814. template <typename FNfloat>
  815. float GenFractalPingPong(FNfloat x, FNfloat y, FNfloat z)
  816. {
  817. int seed = mSeed;
  818. float sum = 0;
  819. float amp = mFractalBounding;
  820. for (int i = 0; i < mOctaves; i++)
  821. {
  822. float noise = PingPong((GenNoiseSingle(seed++, x, y, z) + 1) * mPingPongStength);
  823. sum += (noise - 0.5f) * 2 * amp;
  824. amp *= Lerp(1.0f, noise, mWeightedStrength);
  825. x *= mLacunarity;
  826. y *= mLacunarity;
  827. z *= mLacunarity;
  828. amp *= mGain;
  829. }
  830. return sum;
  831. }
  832. // Simplex/OpenSimplex2 Noise
  833. template <typename FNfloat>
  834. float SingleSimplex(int seed, FNfloat x, FNfloat y)
  835. {
  836. // 2D OpenSimplex2 case uses the same algorithm as ordinary Simplex.
  837. const float SQRT3 = 1.7320508075688772935274463415059f;
  838. const float G2 = (3 - SQRT3) / 6;
  839. /*
  840. * --- Skew moved to TransformNoiseCoordinate method ---
  841. * const FNfloat F2 = 0.5f * (SQRT3 - 1);
  842. * FNfloat s = (x + y) * F2;
  843. * x += s; y += s;
  844. */
  845. int i = FastFloor(x);
  846. int j = FastFloor(y);
  847. float xi = (float)(x - i);
  848. float yi = (float)(y - j);
  849. float t = (xi + yi) * G2;
  850. float x0 = (float)(xi - t);
  851. float y0 = (float)(yi - t);
  852. i *= PrimeX;
  853. j *= PrimeY;
  854. float n0, n1, n2;
  855. float a = 0.5f - x0 * x0 - y0 * y0;
  856. if (a <= 0) n0 = 0;
  857. else
  858. {
  859. n0 = (a * a) * (a * a) * GradCoord(seed, i, j, x0, y0);
  860. }
  861. float c = (float)(2 * (1 - 2 * G2) * (1 / G2 - 2)) * t + ((float)(-2 * (1 - 2 * G2) * (1 - 2 * G2)) + a);
  862. if (c <= 0) n2 = 0;
  863. else
  864. {
  865. float x2 = x0 + (2 * (float)G2 - 1);
  866. float y2 = y0 + (2 * (float)G2 - 1);
  867. n2 = (c * c) * (c * c) * GradCoord(seed, i + PrimeX, j + PrimeY, x2, y2);
  868. }
  869. if (y0 > x0)
  870. {
  871. float x1 = x0 + (float)G2;
  872. float y1 = y0 + ((float)G2 - 1);
  873. float b = 0.5f - x1 * x1 - y1 * y1;
  874. if (b <= 0) n1 = 0;
  875. else
  876. {
  877. n1 = (b * b) * (b * b) * GradCoord(seed, i, j + PrimeY, x1, y1);
  878. }
  879. }
  880. else
  881. {
  882. float x1 = x0 + ((float)G2 - 1);
  883. float y1 = y0 + (float)G2;
  884. float b = 0.5f - x1 * x1 - y1 * y1;
  885. if (b <= 0) n1 = 0;
  886. else
  887. {
  888. n1 = (b * b) * (b * b) * GradCoord(seed, i + PrimeX, j, x1, y1);
  889. }
  890. }
  891. return (n0 + n1 + n2) * 99.83685446303647f;
  892. }
  893. template <typename FNfloat>
  894. float SingleOpenSimplex2(int seed, FNfloat x, FNfloat y, FNfloat z)
  895. {
  896. // 3D OpenSimplex2 case uses two offset rotated cube grids.
  897. /*
  898. * --- Rotation moved to TransformNoiseCoordinate method ---
  899. * const FNfloat R3 = (FNfloat)(2.0 / 3.0);
  900. * FNfloat r = (x + y + z) * R3; // Rotation, not skew
  901. * x = r - x; y = r - y; z = r - z;
  902. */
  903. int i = FastRound(x);
  904. int j = FastRound(y);
  905. int k = FastRound(z);
  906. float x0 = (float)(x - i);
  907. float y0 = (float)(y - j);
  908. float z0 = (float)(z - k);
  909. int xNSign = (int)(-1.0f - x0) | 1;
  910. int yNSign = (int)(-1.0f - y0) | 1;
  911. int zNSign = (int)(-1.0f - z0) | 1;
  912. float ax0 = (float)xNSign * -x0;
  913. float ay0 = (float)yNSign * -y0;
  914. float az0 = (float)zNSign * -z0;
  915. i *= PrimeX;
  916. j *= PrimeY;
  917. k *= PrimeZ;
  918. float value = 0;
  919. float a = (0.6f - x0 * x0) - (y0 * y0 + z0 * z0);
  920. for (int l = 0; ; l++)
  921. {
  922. if (a > 0)
  923. {
  924. value += (a * a) * (a * a) * GradCoord(seed, i, j, k, x0, y0, z0);
  925. }
  926. float b = a + 1;
  927. int i1 = i;
  928. int j1 = j;
  929. int k1 = k;
  930. float x1 = x0;
  931. float y1 = y0;
  932. float z1 = z0;
  933. if (ax0 >= ay0 && ax0 >= az0)
  934. {
  935. x1 += (float)xNSign;
  936. b -= (float)(xNSign * 2) * x1;
  937. i1 -= xNSign * PrimeX;
  938. }
  939. else if (ay0 > ax0 && ay0 >= az0)
  940. {
  941. y1 += (float)yNSign;
  942. b -= (float)(yNSign * 2) * y1;
  943. j1 -= yNSign * PrimeY;
  944. }
  945. else
  946. {
  947. z1 += (float)zNSign;
  948. b -= (float)(zNSign * 2) * z1;
  949. k1 -= zNSign * PrimeZ;
  950. }
  951. if (b > 0)
  952. {
  953. value += (b * b) * (b * b) * GradCoord(seed, i1, j1, k1, x1, y1, z1);
  954. }
  955. if (l == 1) break;
  956. ax0 = 0.5f - ax0;
  957. ay0 = 0.5f - ay0;
  958. az0 = 0.5f - az0;
  959. x0 = (float)xNSign * ax0;
  960. y0 = (float)yNSign * ay0;
  961. z0 = (float)zNSign * az0;
  962. a += (0.75f - ax0) - (ay0 + az0);
  963. i += (xNSign >> 1) & PrimeX;
  964. j += (yNSign >> 1) & PrimeY;
  965. k += (zNSign >> 1) & PrimeZ;
  966. xNSign = -xNSign;
  967. yNSign = -yNSign;
  968. zNSign = -zNSign;
  969. seed = ~seed;
  970. }
  971. return value * 32.69428253173828125f;
  972. }
  973. // OpenSimplex2S Noise
  974. template <typename FNfloat>
  975. float SingleOpenSimplex2S(int seed, FNfloat x, FNfloat y)
  976. {
  977. // 2D OpenSimplex2S case is a modified 2D simplex noise.
  978. const FNfloat SQRT3 = (FNfloat)1.7320508075688772935274463415059;
  979. const FNfloat G2 = (3 - SQRT3) / 6;
  980. /*
  981. * --- Skew moved to TransformNoiseCoordinate method ---
  982. * const FNfloat F2 = 0.5f * (SQRT3 - 1);
  983. * FNfloat s = (x + y) * F2;
  984. * x += s; y += s;
  985. */
  986. int i = FastFloor(x);
  987. int j = FastFloor(y);
  988. float xi = (float)(x - i);
  989. float yi = (float)(y - j);
  990. i *= PrimeX;
  991. j *= PrimeY;
  992. int i1 = i + PrimeX;
  993. int j1 = j + PrimeY;
  994. float t = (xi + yi) * (float)G2;
  995. float x0 = xi - t;
  996. float y0 = yi - t;
  997. float a0 = (2.0f / 3.0f) - x0 * x0 - y0 * y0;
  998. float value = (a0 * a0) * (a0 * a0) * GradCoord(seed, i, j, x0, y0);
  999. float a1 = (float)(2 * (1 - 2 * G2) * (1 / G2 - 2)) * t + ((float)(-2 * (1 - 2 * G2) * (1 - 2 * G2)) + a0);
  1000. float x1 = x0 - (float)(1 - 2 * G2);
  1001. float y1 = y0 - (float)(1 - 2 * G2);
  1002. value += (a1 * a1) * (a1 * a1) * GradCoord(seed, i1, j1, x1, y1);
  1003. // Nested conditionals were faster than compact bit logic/arithmetic.
  1004. float xmyi = xi - yi;
  1005. if (t > G2)
  1006. {
  1007. if (xi + xmyi > 1)
  1008. {
  1009. float x2 = x0 + (float)(3 * G2 - 2);
  1010. float y2 = y0 + (float)(3 * G2 - 1);
  1011. float a2 = (2.0f / 3.0f) - x2 * x2 - y2 * y2;
  1012. if (a2 > 0)
  1013. {
  1014. value += (a2 * a2) * (a2 * a2) * GradCoord(seed, i + (PrimeX << 1), j + PrimeY, x2, y2);
  1015. }
  1016. }
  1017. else
  1018. {
  1019. float x2 = x0 + (float)G2;
  1020. float y2 = y0 + (float)(G2 - 1);
  1021. float a2 = (2.0f / 3.0f) - x2 * x2 - y2 * y2;
  1022. if (a2 > 0)
  1023. {
  1024. value += (a2 * a2) * (a2 * a2) * GradCoord(seed, i, j + PrimeY, x2, y2);
  1025. }
  1026. }
  1027. if (yi - xmyi > 1)
  1028. {
  1029. float x3 = x0 + (float)(3 * G2 - 1);
  1030. float y3 = y0 + (float)(3 * G2 - 2);
  1031. float a3 = (2.0f / 3.0f) - x3 * x3 - y3 * y3;
  1032. if (a3 > 0)
  1033. {
  1034. value += (a3 * a3) * (a3 * a3) * GradCoord(seed, i + PrimeX, j + (PrimeY << 1), x3, y3);
  1035. }
  1036. }
  1037. else
  1038. {
  1039. float x3 = x0 + (float)(G2 - 1);
  1040. float y3 = y0 + (float)G2;
  1041. float a3 = (2.0f / 3.0f) - x3 * x3 - y3 * y3;
  1042. if (a3 > 0)
  1043. {
  1044. value += (a3 * a3) * (a3 * a3) * GradCoord(seed, i + PrimeX, j, x3, y3);
  1045. }
  1046. }
  1047. }
  1048. else
  1049. {
  1050. if (xi + xmyi < 0)
  1051. {
  1052. float x2 = x0 + (float)(1 - G2);
  1053. float y2 = y0 - (float)G2;
  1054. float a2 = (2.0f / 3.0f) - x2 * x2 - y2 * y2;
  1055. if (a2 > 0)
  1056. {
  1057. value += (a2 * a2) * (a2 * a2) * GradCoord(seed, i - PrimeX, j, x2, y2);
  1058. }
  1059. }
  1060. else
  1061. {
  1062. float x2 = x0 + (float)(G2 - 1);
  1063. float y2 = y0 + (float)G2;
  1064. float a2 = (2.0f / 3.0f) - x2 * x2 - y2 * y2;
  1065. if (a2 > 0)
  1066. {
  1067. value += (a2 * a2) * (a2 * a2) * GradCoord(seed, i + PrimeX, j, x2, y2);
  1068. }
  1069. }
  1070. if (yi < xmyi)
  1071. {
  1072. float x2 = x0 - (float)G2;
  1073. float y2 = y0 - (float)(G2 - 1);
  1074. float a2 = (2.0f / 3.0f) - x2 * x2 - y2 * y2;
  1075. if (a2 > 0)
  1076. {
  1077. value += (a2 * a2) * (a2 * a2) * GradCoord(seed, i, j - PrimeY, x2, y2);
  1078. }
  1079. }
  1080. else
  1081. {
  1082. float x2 = x0 + (float)G2;
  1083. float y2 = y0 + (float)(G2 - 1);
  1084. float a2 = (2.0f / 3.0f) - x2 * x2 - y2 * y2;
  1085. if (a2 > 0)
  1086. {
  1087. value += (a2 * a2) * (a2 * a2) * GradCoord(seed, i, j + PrimeY, x2, y2);
  1088. }
  1089. }
  1090. }
  1091. return value * 18.24196194486065f;
  1092. }
  1093. template <typename FNfloat>
  1094. float SingleOpenSimplex2S(int seed, FNfloat x, FNfloat y, FNfloat z)
  1095. {
  1096. // 3D OpenSimplex2S case uses two offset rotated cube grids.
  1097. /*
  1098. * --- Rotation moved to TransformNoiseCoordinate method ---
  1099. * const FNfloat R3 = (FNfloat)(2.0 / 3.0);
  1100. * FNfloat r = (x + y + z) * R3; // Rotation, not skew
  1101. * x = r - x; y = r - y; z = r - z;
  1102. */
  1103. int i = FastFloor(x);
  1104. int j = FastFloor(y);
  1105. int k = FastFloor(z);
  1106. float xi = (float)(x - i);
  1107. float yi = (float)(y - j);
  1108. float zi = (float)(z - k);
  1109. i *= PrimeX;
  1110. j *= PrimeY;
  1111. k *= PrimeZ;
  1112. int seed2 = seed + 1293373;
  1113. int xNMask = (int)(-0.5f - xi);
  1114. int yNMask = (int)(-0.5f - yi);
  1115. int zNMask = (int)(-0.5f - zi);
  1116. float x0 = xi + (float)xNMask;
  1117. float y0 = yi + (float)yNMask;
  1118. float z0 = zi + (float)zNMask;
  1119. float a0 = 0.75f - x0 * x0 - y0 * y0 - z0 * z0;
  1120. float value = (a0 * a0) * (a0 * a0) * GradCoord(seed,
  1121. i + (xNMask & PrimeX), j + (yNMask & PrimeY), k + (zNMask & PrimeZ), x0, y0, z0);
  1122. float x1 = xi - 0.5f;
  1123. float y1 = yi - 0.5f;
  1124. float z1 = zi - 0.5f;
  1125. float a1 = 0.75f - x1 * x1 - y1 * y1 - z1 * z1;
  1126. value += (a1 * a1) * (a1 * a1) * GradCoord(seed2,
  1127. i + PrimeX, j + PrimeY, k + PrimeZ, x1, y1, z1);
  1128. float xAFlipMask0 = (float)((xNMask | 1) << 1) * x1;
  1129. float yAFlipMask0 = (float)((yNMask | 1) << 1) * y1;
  1130. float zAFlipMask0 = (float)((zNMask | 1) << 1) * z1;
  1131. float xAFlipMask1 = (float)(-2 - (xNMask << 2)) * x1 - 1.0f;
  1132. float yAFlipMask1 = (float)(-2 - (yNMask << 2)) * y1 - 1.0f;
  1133. float zAFlipMask1 = (float)(-2 - (zNMask << 2)) * z1 - 1.0f;
  1134. bool skip5 = false;
  1135. float a2 = xAFlipMask0 + a0;
  1136. if (a2 > 0)
  1137. {
  1138. float x2 = x0 - (float)(xNMask | 1);
  1139. float y2 = y0;
  1140. float z2 = z0;
  1141. value += (a2 * a2) * (a2 * a2) * GradCoord(seed,
  1142. i + (~xNMask & PrimeX), j + (yNMask & PrimeY), k + (zNMask & PrimeZ), x2, y2, z2);
  1143. }
  1144. else
  1145. {
  1146. float a3 = yAFlipMask0 + zAFlipMask0 + a0;
  1147. if (a3 > 0)
  1148. {
  1149. float x3 = x0;
  1150. float y3 = y0 - (float)(yNMask | 1);
  1151. float z3 = z0 - (float)(zNMask | 1);
  1152. value += (a3 * a3) * (a3 * a3) * GradCoord(seed,
  1153. i + (xNMask & PrimeX), j + (~yNMask & PrimeY), k + (~zNMask & PrimeZ), x3, y3, z3);
  1154. }
  1155. float a4 = xAFlipMask1 + a1;
  1156. if (a4 > 0)
  1157. {
  1158. float x4 = (float)(xNMask | 1) + x1;
  1159. float y4 = y1;
  1160. float z4 = z1;
  1161. value += (a4 * a4) * (a4 * a4) * GradCoord(seed2,
  1162. i + (xNMask & (PrimeX * 2)), j + PrimeY, k + PrimeZ, x4, y4, z4);
  1163. skip5 = true;
  1164. }
  1165. }
  1166. bool skip9 = false;
  1167. float a6 = yAFlipMask0 + a0;
  1168. if (a6 > 0)
  1169. {
  1170. float x6 = x0;
  1171. float y6 = y0 - (float)(yNMask | 1);
  1172. float z6 = z0;
  1173. value += (a6 * a6) * (a6 * a6) * GradCoord(seed,
  1174. i + (xNMask & PrimeX), j + (~yNMask & PrimeY), k + (zNMask & PrimeZ), x6, y6, z6);
  1175. }
  1176. else
  1177. {
  1178. float a7 = xAFlipMask0 + zAFlipMask0 + a0;
  1179. if (a7 > 0)
  1180. {
  1181. float x7 = x0 - (float)(xNMask | 1);
  1182. float y7 = y0;
  1183. float z7 = z0 - (float)(zNMask | 1);
  1184. value += (a7 * a7) * (a7 * a7) * GradCoord(seed,
  1185. i + (~xNMask & PrimeX), j + (yNMask & PrimeY), k + (~zNMask & PrimeZ), x7, y7, z7);
  1186. }
  1187. float a8 = yAFlipMask1 + a1;
  1188. if (a8 > 0)
  1189. {
  1190. float x8 = x1;
  1191. float y8 = (float)(yNMask | 1) + y1;
  1192. float z8 = z1;
  1193. value += (a8 * a8) * (a8 * a8) * GradCoord(seed2,
  1194. i + PrimeX, j + (yNMask & (PrimeY << 1)), k + PrimeZ, x8, y8, z8);
  1195. skip9 = true;
  1196. }
  1197. }
  1198. bool skipD = false;
  1199. float aA = zAFlipMask0 + a0;
  1200. if (aA > 0)
  1201. {
  1202. float xA = x0;
  1203. float yA = y0;
  1204. float zA = z0 - (float)(zNMask | 1);
  1205. value += (aA * aA) * (aA * aA) * GradCoord(seed,
  1206. i + (xNMask & PrimeX), j + (yNMask & PrimeY), k + (~zNMask & PrimeZ), xA, yA, zA);
  1207. }
  1208. else
  1209. {
  1210. float aB = xAFlipMask0 + yAFlipMask0 + a0;
  1211. if (aB > 0)
  1212. {
  1213. float xB = x0 - (float)(xNMask | 1);
  1214. float yB = y0 - (float)(yNMask | 1);
  1215. float zB = z0;
  1216. value += (aB * aB) * (aB * aB) * GradCoord(seed,
  1217. i + (~xNMask & PrimeX), j + (~yNMask & PrimeY), k + (zNMask & PrimeZ), xB, yB, zB);
  1218. }
  1219. float aC = zAFlipMask1 + a1;
  1220. if (aC > 0)
  1221. {
  1222. float xC = x1;
  1223. float yC = y1;
  1224. float zC = (float)(zNMask | 1) + z1;
  1225. value += (aC * aC) * (aC * aC) * GradCoord(seed2,
  1226. i + PrimeX, j + PrimeY, k + (zNMask & (PrimeZ << 1)), xC, yC, zC);
  1227. skipD = true;
  1228. }
  1229. }
  1230. if (!skip5)
  1231. {
  1232. float a5 = yAFlipMask1 + zAFlipMask1 + a1;
  1233. if (a5 > 0)
  1234. {
  1235. float x5 = x1;
  1236. float y5 = (float)(yNMask | 1) + y1;
  1237. float z5 = (float)(zNMask | 1) + z1;
  1238. value += (a5 * a5) * (a5 * a5) * GradCoord(seed2,
  1239. i + PrimeX, j + (yNMask & (PrimeY << 1)), k + (zNMask & (PrimeZ << 1)), x5, y5, z5);
  1240. }
  1241. }
  1242. if (!skip9)
  1243. {
  1244. float a9 = xAFlipMask1 + zAFlipMask1 + a1;
  1245. if (a9 > 0)
  1246. {
  1247. float x9 = (float)(xNMask | 1) + x1;
  1248. float y9 = y1;
  1249. float z9 = (float)(zNMask | 1) + z1;
  1250. value += (a9 * a9) * (a9 * a9) * GradCoord(seed2,
  1251. i + (xNMask & (PrimeX * 2)), j + PrimeY, k + (zNMask & (PrimeZ << 1)), x9, y9, z9);
  1252. }
  1253. }
  1254. if (!skipD)
  1255. {
  1256. float aD = xAFlipMask1 + yAFlipMask1 + a1;
  1257. if (aD > 0)
  1258. {
  1259. float xD = (float)(xNMask | 1) + x1;
  1260. float yD = (float)(yNMask | 1) + y1;
  1261. float zD = z1;
  1262. value += (aD * aD) * (aD * aD) * GradCoord(seed2,
  1263. i + (xNMask & (PrimeX << 1)), j + (yNMask & (PrimeY << 1)), k + PrimeZ, xD, yD, zD);
  1264. }
  1265. }
  1266. return value * 9.046026385208288f;
  1267. }
  1268. // Cellular Noise
  1269. template <typename FNfloat>
  1270. float SingleCellular(int seed, FNfloat x, FNfloat y)
  1271. {
  1272. int xr = FastRound(x);
  1273. int yr = FastRound(y);
  1274. float distance0 = 1e10f;
  1275. float distance1 = 1e10f;
  1276. int closestHash = 0;
  1277. float cellularJitter = 0.43701595f * mCellularJitterModifier;
  1278. int xPrimed = (xr - 1) * PrimeX;
  1279. int yPrimedBase = (yr - 1) * PrimeY;
  1280. switch (mCellularDistanceFunction)
  1281. {
  1282. default:
  1283. case CellularDistanceFunction_Euclidean:
  1284. case CellularDistanceFunction_EuclideanSq:
  1285. for (int xi = xr - 1; xi <= xr + 1; xi++)
  1286. {
  1287. int yPrimed = yPrimedBase;
  1288. for (int yi = yr - 1; yi <= yr + 1; yi++)
  1289. {
  1290. int hash = Hash(seed, xPrimed, yPrimed);
  1291. int idx = hash & (255 << 1);
  1292. float vecX = (float)(xi - x) + Lookup<float>::RandVecs2D[idx] * cellularJitter;
  1293. float vecY = (float)(yi - y) + Lookup<float>::RandVecs2D[idx | 1] * cellularJitter;
  1294. float newDistance = vecX * vecX + vecY * vecY;
  1295. distance1 = FastMax(FastMin(distance1, newDistance), distance0);
  1296. if (newDistance < distance0)
  1297. {
  1298. distance0 = newDistance;
  1299. closestHash = hash;
  1300. }
  1301. yPrimed += PrimeY;
  1302. }
  1303. xPrimed += PrimeX;
  1304. }
  1305. break;
  1306. case CellularDistanceFunction_Manhattan:
  1307. for (int xi = xr - 1; xi <= xr + 1; xi++)
  1308. {
  1309. int yPrimed = yPrimedBase;
  1310. for (int yi = yr - 1; yi <= yr + 1; yi++)
  1311. {
  1312. int hash = Hash(seed, xPrimed, yPrimed);
  1313. int idx = hash & (255 << 1);
  1314. float vecX = (float)(xi - x) + Lookup<float>::RandVecs2D[idx] * cellularJitter;
  1315. float vecY = (float)(yi - y) + Lookup<float>::RandVecs2D[idx | 1] * cellularJitter;
  1316. float newDistance = FastAbs(vecX) + FastAbs(vecY);
  1317. distance1 = FastMax(FastMin(distance1, newDistance), distance0);
  1318. if (newDistance < distance0)
  1319. {
  1320. distance0 = newDistance;
  1321. closestHash = hash;
  1322. }
  1323. yPrimed += PrimeY;
  1324. }
  1325. xPrimed += PrimeX;
  1326. }
  1327. break;
  1328. case CellularDistanceFunction_Hybrid:
  1329. for (int xi = xr - 1; xi <= xr + 1; xi++)
  1330. {
  1331. int yPrimed = yPrimedBase;
  1332. for (int yi = yr - 1; yi <= yr + 1; yi++)
  1333. {
  1334. int hash = Hash(seed, xPrimed, yPrimed);
  1335. int idx = hash & (255 << 1);
  1336. float vecX = (float)(xi - x) + Lookup<float>::RandVecs2D[idx] * cellularJitter;
  1337. float vecY = (float)(yi - y) + Lookup<float>::RandVecs2D[idx | 1] * cellularJitter;
  1338. float newDistance = (FastAbs(vecX) + FastAbs(vecY)) + (vecX * vecX + vecY * vecY);
  1339. distance1 = FastMax(FastMin(distance1, newDistance), distance0);
  1340. if (newDistance < distance0)
  1341. {
  1342. distance0 = newDistance;
  1343. closestHash = hash;
  1344. }
  1345. yPrimed += PrimeY;
  1346. }
  1347. xPrimed += PrimeX;
  1348. }
  1349. break;
  1350. }
  1351. if (mCellularDistanceFunction == CellularDistanceFunction_Euclidean && mCellularReturnType >= CellularReturnType_Distance)
  1352. {
  1353. distance0 = FastSqrt(distance0);
  1354. if (mCellularReturnType >= CellularReturnType_Distance2)
  1355. {
  1356. distance1 = FastSqrt(distance1);
  1357. }
  1358. }
  1359. switch (mCellularReturnType)
  1360. {
  1361. case CellularReturnType_CellValue:
  1362. return closestHash * (1 / 2147483648.0f);
  1363. case CellularReturnType_Distance:
  1364. return distance0 - 1;
  1365. case CellularReturnType_Distance2:
  1366. return distance1 - 1;
  1367. case CellularReturnType_Distance2Add:
  1368. return (distance1 + distance0) * 0.5f - 1;
  1369. case CellularReturnType_Distance2Sub:
  1370. return distance1 - distance0 - 1;
  1371. case CellularReturnType_Distance2Mul:
  1372. return distance1 * distance0 * 0.5f - 1;
  1373. case CellularReturnType_Distance2Div:
  1374. return distance0 / distance1 - 1;
  1375. default:
  1376. return 0;
  1377. }
  1378. }
  1379. template <typename FNfloat>
  1380. float SingleCellular(int seed, FNfloat x, FNfloat y, FNfloat z)
  1381. {
  1382. int xr = FastRound(x);
  1383. int yr = FastRound(y);
  1384. int zr = FastRound(z);
  1385. float distance0 = 1e10f;
  1386. float distance1 = 1e10f;
  1387. int closestHash = 0;
  1388. float cellularJitter = 0.39614353f * mCellularJitterModifier;
  1389. int xPrimed = (xr - 1) * PrimeX;
  1390. int yPrimedBase = (yr - 1) * PrimeY;
  1391. int zPrimedBase = (zr - 1) * PrimeZ;
  1392. switch (mCellularDistanceFunction)
  1393. {
  1394. case CellularDistanceFunction_Euclidean:
  1395. case CellularDistanceFunction_EuclideanSq:
  1396. for (int xi = xr - 1; xi <= xr + 1; xi++)
  1397. {
  1398. int yPrimed = yPrimedBase;
  1399. for (int yi = yr - 1; yi <= yr + 1; yi++)
  1400. {
  1401. int zPrimed = zPrimedBase;
  1402. for (int zi = zr - 1; zi <= zr + 1; zi++)
  1403. {
  1404. int hash = Hash(seed, xPrimed, yPrimed, zPrimed);
  1405. int idx = hash & (255 << 2);
  1406. float vecX = (float)(xi - x) + Lookup<float>::RandVecs3D[idx] * cellularJitter;
  1407. float vecY = (float)(yi - y) + Lookup<float>::RandVecs3D[idx | 1] * cellularJitter;
  1408. float vecZ = (float)(zi - z) + Lookup<float>::RandVecs3D[idx | 2] * cellularJitter;
  1409. float newDistance = vecX * vecX + vecY * vecY + vecZ * vecZ;
  1410. distance1 = FastMax(FastMin(distance1, newDistance), distance0);
  1411. if (newDistance < distance0)
  1412. {
  1413. distance0 = newDistance;
  1414. closestHash = hash;
  1415. }
  1416. zPrimed += PrimeZ;
  1417. }
  1418. yPrimed += PrimeY;
  1419. }
  1420. xPrimed += PrimeX;
  1421. }
  1422. break;
  1423. case CellularDistanceFunction_Manhattan:
  1424. for (int xi = xr - 1; xi <= xr + 1; xi++)
  1425. {
  1426. int yPrimed = yPrimedBase;
  1427. for (int yi = yr - 1; yi <= yr + 1; yi++)
  1428. {
  1429. int zPrimed = zPrimedBase;
  1430. for (int zi = zr - 1; zi <= zr + 1; zi++)
  1431. {
  1432. int hash = Hash(seed, xPrimed, yPrimed, zPrimed);
  1433. int idx = hash & (255 << 2);
  1434. float vecX = (float)(xi - x) + Lookup<float>::RandVecs3D[idx] * cellularJitter;
  1435. float vecY = (float)(yi - y) + Lookup<float>::RandVecs3D[idx | 1] * cellularJitter;
  1436. float vecZ = (float)(zi - z) + Lookup<float>::RandVecs3D[idx | 2] * cellularJitter;
  1437. float newDistance = FastAbs(vecX) + FastAbs(vecY) + FastAbs(vecZ);
  1438. distance1 = FastMax(FastMin(distance1, newDistance), distance0);
  1439. if (newDistance < distance0)
  1440. {
  1441. distance0 = newDistance;
  1442. closestHash = hash;
  1443. }
  1444. zPrimed += PrimeZ;
  1445. }
  1446. yPrimed += PrimeY;
  1447. }
  1448. xPrimed += PrimeX;
  1449. }
  1450. break;
  1451. case CellularDistanceFunction_Hybrid:
  1452. for (int xi = xr - 1; xi <= xr + 1; xi++)
  1453. {
  1454. int yPrimed = yPrimedBase;
  1455. for (int yi = yr - 1; yi <= yr + 1; yi++)
  1456. {
  1457. int zPrimed = zPrimedBase;
  1458. for (int zi = zr - 1; zi <= zr + 1; zi++)
  1459. {
  1460. int hash = Hash(seed, xPrimed, yPrimed, zPrimed);
  1461. int idx = hash & (255 << 2);
  1462. float vecX = (float)(xi - x) + Lookup<float>::RandVecs3D[idx] * cellularJitter;
  1463. float vecY = (float)(yi - y) + Lookup<float>::RandVecs3D[idx | 1] * cellularJitter;
  1464. float vecZ = (float)(zi - z) + Lookup<float>::RandVecs3D[idx | 2] * cellularJitter;
  1465. float newDistance = (FastAbs(vecX) + FastAbs(vecY) + FastAbs(vecZ)) + (vecX * vecX + vecY * vecY + vecZ * vecZ);
  1466. distance1 = FastMax(FastMin(distance1, newDistance), distance0);
  1467. if (newDistance < distance0)
  1468. {
  1469. distance0 = newDistance;
  1470. closestHash = hash;
  1471. }
  1472. zPrimed += PrimeZ;
  1473. }
  1474. yPrimed += PrimeY;
  1475. }
  1476. xPrimed += PrimeX;
  1477. }
  1478. break;
  1479. default:
  1480. break;
  1481. }
  1482. if (mCellularDistanceFunction == CellularDistanceFunction_Euclidean && mCellularReturnType >= CellularReturnType_Distance)
  1483. {
  1484. distance0 = FastSqrt(distance0);
  1485. if (mCellularReturnType >= CellularReturnType_Distance2)
  1486. {
  1487. distance1 = FastSqrt(distance1);
  1488. }
  1489. }
  1490. switch (mCellularReturnType)
  1491. {
  1492. case CellularReturnType_CellValue:
  1493. return (float)closestHash * (1.f / 2147483648.0f);
  1494. case CellularReturnType_Distance:
  1495. return distance0 - 1;
  1496. case CellularReturnType_Distance2:
  1497. return distance1 - 1;
  1498. case CellularReturnType_Distance2Add:
  1499. return (distance1 + distance0) * 0.5f - 1;
  1500. case CellularReturnType_Distance2Sub:
  1501. return distance1 - distance0 - 1;
  1502. case CellularReturnType_Distance2Mul:
  1503. return distance1 * distance0 * 0.5f - 1;
  1504. case CellularReturnType_Distance2Div:
  1505. return distance0 / distance1 - 1;
  1506. default:
  1507. return 0;
  1508. }
  1509. }
  1510. // Perlin Noise
  1511. template <typename FNfloat>
  1512. float SinglePerlin(int seed, FNfloat x, FNfloat y)
  1513. {
  1514. int x0 = FastFloor(x);
  1515. int y0 = FastFloor(y);
  1516. float xd0 = (float)(x - x0);
  1517. float yd0 = (float)(y - y0);
  1518. float xd1 = xd0 - 1;
  1519. float yd1 = yd0 - 1;
  1520. float xs = InterpQuintic(xd0);
  1521. float ys = InterpQuintic(yd0);
  1522. x0 *= PrimeX;
  1523. y0 *= PrimeY;
  1524. int x1 = x0 + PrimeX;
  1525. int y1 = y0 + PrimeY;
  1526. float xf0 = Lerp(GradCoord(seed, x0, y0, xd0, yd0), GradCoord(seed, x1, y0, xd1, yd0), xs);
  1527. float xf1 = Lerp(GradCoord(seed, x0, y1, xd0, yd1), GradCoord(seed, x1, y1, xd1, yd1), xs);
  1528. return Lerp(xf0, xf1, ys) * 1.4247691104677813f;
  1529. }
  1530. template <typename FNfloat>
  1531. float SinglePerlin(int seed, FNfloat x, FNfloat y, FNfloat z)
  1532. {
  1533. int x0 = FastFloor(x);
  1534. int y0 = FastFloor(y);
  1535. int z0 = FastFloor(z);
  1536. float xd0 = (float)(x - x0);
  1537. float yd0 = (float)(y - y0);
  1538. float zd0 = (float)(z - z0);
  1539. float xd1 = xd0 - 1;
  1540. float yd1 = yd0 - 1;
  1541. float zd1 = zd0 - 1;
  1542. float xs = InterpQuintic(xd0);
  1543. float ys = InterpQuintic(yd0);
  1544. float zs = InterpQuintic(zd0);
  1545. x0 *= PrimeX;
  1546. y0 *= PrimeY;
  1547. z0 *= PrimeZ;
  1548. int x1 = x0 + PrimeX;
  1549. int y1 = y0 + PrimeY;
  1550. int z1 = z0 + PrimeZ;
  1551. float xf00 = Lerp(GradCoord(seed, x0, y0, z0, xd0, yd0, zd0), GradCoord(seed, x1, y0, z0, xd1, yd0, zd0), xs);
  1552. float xf10 = Lerp(GradCoord(seed, x0, y1, z0, xd0, yd1, zd0), GradCoord(seed, x1, y1, z0, xd1, yd1, zd0), xs);
  1553. float xf01 = Lerp(GradCoord(seed, x0, y0, z1, xd0, yd0, zd1), GradCoord(seed, x1, y0, z1, xd1, yd0, zd1), xs);
  1554. float xf11 = Lerp(GradCoord(seed, x0, y1, z1, xd0, yd1, zd1), GradCoord(seed, x1, y1, z1, xd1, yd1, zd1), xs);
  1555. float yf0 = Lerp(xf00, xf10, ys);
  1556. float yf1 = Lerp(xf01, xf11, ys);
  1557. return Lerp(yf0, yf1, zs) * 0.964921414852142333984375f;
  1558. }
  1559. // Value Cubic Noise
  1560. template <typename FNfloat>
  1561. float SingleValueCubic(int seed, FNfloat x, FNfloat y)
  1562. {
  1563. int x1 = FastFloor(x);
  1564. int y1 = FastFloor(y);
  1565. float xs = (float)(x - x1);
  1566. float ys = (float)(y - y1);
  1567. x1 *= PrimeX;
  1568. y1 *= PrimeY;
  1569. int x0 = x1 - PrimeX;
  1570. int y0 = y1 - PrimeY;
  1571. int x2 = x1 + PrimeX;
  1572. int y2 = y1 + PrimeY;
  1573. int x3 = x1 + (int)((long)PrimeX << 1);
  1574. int y3 = y1 + (int)((long)PrimeY << 1);
  1575. return CubicLerp(
  1576. CubicLerp(ValCoord(seed, x0, y0), ValCoord(seed, x1, y0), ValCoord(seed, x2, y0), ValCoord(seed, x3, y0),
  1577. xs),
  1578. CubicLerp(ValCoord(seed, x0, y1), ValCoord(seed, x1, y1), ValCoord(seed, x2, y1), ValCoord(seed, x3, y1),
  1579. xs),
  1580. CubicLerp(ValCoord(seed, x0, y2), ValCoord(seed, x1, y2), ValCoord(seed, x2, y2), ValCoord(seed, x3, y2),
  1581. xs),
  1582. CubicLerp(ValCoord(seed, x0, y3), ValCoord(seed, x1, y3), ValCoord(seed, x2, y3), ValCoord(seed, x3, y3),
  1583. xs),
  1584. ys) * (1 / (1.5f * 1.5f));
  1585. }
  1586. template <typename FNfloat>
  1587. float SingleValueCubic(int seed, FNfloat x, FNfloat y, FNfloat z)
  1588. {
  1589. int x1 = FastFloor(x);
  1590. int y1 = FastFloor(y);
  1591. int z1 = FastFloor(z);
  1592. float xs = (float)(x - x1);
  1593. float ys = (float)(y - y1);
  1594. float zs = (float)(z - z1);
  1595. x1 *= PrimeX;
  1596. y1 *= PrimeY;
  1597. z1 *= PrimeZ;
  1598. int x0 = x1 - PrimeX;
  1599. int y0 = y1 - PrimeY;
  1600. int z0 = z1 - PrimeZ;
  1601. int x2 = x1 + PrimeX;
  1602. int y2 = y1 + PrimeY;
  1603. int z2 = z1 + PrimeZ;
  1604. int x3 = x1 + (int)((long)PrimeX << 1);
  1605. int y3 = y1 + (int)((long)PrimeY << 1);
  1606. int z3 = z1 + (int)((long)PrimeZ << 1);
  1607. return CubicLerp(
  1608. CubicLerp(
  1609. CubicLerp(ValCoord(seed, x0, y0, z0), ValCoord(seed, x1, y0, z0), ValCoord(seed, x2, y0, z0), ValCoord(seed, x3, y0, z0), xs),
  1610. CubicLerp(ValCoord(seed, x0, y1, z0), ValCoord(seed, x1, y1, z0), ValCoord(seed, x2, y1, z0), ValCoord(seed, x3, y1, z0), xs),
  1611. CubicLerp(ValCoord(seed, x0, y2, z0), ValCoord(seed, x1, y2, z0), ValCoord(seed, x2, y2, z0), ValCoord(seed, x3, y2, z0), xs),
  1612. CubicLerp(ValCoord(seed, x0, y3, z0), ValCoord(seed, x1, y3, z0), ValCoord(seed, x2, y3, z0), ValCoord(seed, x3, y3, z0), xs),
  1613. ys),
  1614. CubicLerp(
  1615. CubicLerp(ValCoord(seed, x0, y0, z1), ValCoord(seed, x1, y0, z1), ValCoord(seed, x2, y0, z1), ValCoord(seed, x3, y0, z1), xs),
  1616. CubicLerp(ValCoord(seed, x0, y1, z1), ValCoord(seed, x1, y1, z1), ValCoord(seed, x2, y1, z1), ValCoord(seed, x3, y1, z1), xs),
  1617. CubicLerp(ValCoord(seed, x0, y2, z1), ValCoord(seed, x1, y2, z1), ValCoord(seed, x2, y2, z1), ValCoord(seed, x3, y2, z1), xs),
  1618. CubicLerp(ValCoord(seed, x0, y3, z1), ValCoord(seed, x1, y3, z1), ValCoord(seed, x2, y3, z1), ValCoord(seed, x3, y3, z1), xs),
  1619. ys),
  1620. CubicLerp(
  1621. CubicLerp(ValCoord(seed, x0, y0, z2), ValCoord(seed, x1, y0, z2), ValCoord(seed, x2, y0, z2), ValCoord(seed, x3, y0, z2), xs),
  1622. CubicLerp(ValCoord(seed, x0, y1, z2), ValCoord(seed, x1, y1, z2), ValCoord(seed, x2, y1, z2), ValCoord(seed, x3, y1, z2), xs),
  1623. CubicLerp(ValCoord(seed, x0, y2, z2), ValCoord(seed, x1, y2, z2), ValCoord(seed, x2, y2, z2), ValCoord(seed, x3, y2, z2), xs),
  1624. CubicLerp(ValCoord(seed, x0, y3, z2), ValCoord(seed, x1, y3, z2), ValCoord(seed, x2, y3, z2), ValCoord(seed, x3, y3, z2), xs),
  1625. ys),
  1626. CubicLerp(
  1627. CubicLerp(ValCoord(seed, x0, y0, z3), ValCoord(seed, x1, y0, z3), ValCoord(seed, x2, y0, z3), ValCoord(seed, x3, y0, z3), xs),
  1628. CubicLerp(ValCoord(seed, x0, y1, z3), ValCoord(seed, x1, y1, z3), ValCoord(seed, x2, y1, z3), ValCoord(seed, x3, y1, z3), xs),
  1629. CubicLerp(ValCoord(seed, x0, y2, z3), ValCoord(seed, x1, y2, z3), ValCoord(seed, x2, y2, z3), ValCoord(seed, x3, y2, z3), xs),
  1630. CubicLerp(ValCoord(seed, x0, y3, z3), ValCoord(seed, x1, y3, z3), ValCoord(seed, x2, y3, z3), ValCoord(seed, x3, y3, z3), xs),
  1631. ys),
  1632. zs) * (1 / (1.5f * 1.5f * 1.5f));
  1633. }
  1634. // Value Noise
  1635. template <typename FNfloat>
  1636. float SingleValue(int seed, FNfloat x, FNfloat y)
  1637. {
  1638. int x0 = FastFloor(x);
  1639. int y0 = FastFloor(y);
  1640. float xs = InterpHermite((float)(x - x0));
  1641. float ys = InterpHermite((float)(y - y0));
  1642. x0 *= PrimeX;
  1643. y0 *= PrimeY;
  1644. int x1 = x0 + PrimeX;
  1645. int y1 = y0 + PrimeY;
  1646. float xf0 = Lerp(ValCoord(seed, x0, y0), ValCoord(seed, x1, y0), xs);
  1647. float xf1 = Lerp(ValCoord(seed, x0, y1), ValCoord(seed, x1, y1), xs);
  1648. return Lerp(xf0, xf1, ys);
  1649. }
  1650. template <typename FNfloat>
  1651. float SingleValue(int seed, FNfloat x, FNfloat y, FNfloat z)
  1652. {
  1653. int x0 = FastFloor(x);
  1654. int y0 = FastFloor(y);
  1655. int z0 = FastFloor(z);
  1656. float xs = InterpHermite((float)(x - x0));
  1657. float ys = InterpHermite((float)(y - y0));
  1658. float zs = InterpHermite((float)(z - z0));
  1659. x0 *= PrimeX;
  1660. y0 *= PrimeY;
  1661. z0 *= PrimeZ;
  1662. int x1 = x0 + PrimeX;
  1663. int y1 = y0 + PrimeY;
  1664. int z1 = z0 + PrimeZ;
  1665. float xf00 = Lerp(ValCoord(seed, x0, y0, z0), ValCoord(seed, x1, y0, z0), xs);
  1666. float xf10 = Lerp(ValCoord(seed, x0, y1, z0), ValCoord(seed, x1, y1, z0), xs);
  1667. float xf01 = Lerp(ValCoord(seed, x0, y0, z1), ValCoord(seed, x1, y0, z1), xs);
  1668. float xf11 = Lerp(ValCoord(seed, x0, y1, z1), ValCoord(seed, x1, y1, z1), xs);
  1669. float yf0 = Lerp(xf00, xf10, ys);
  1670. float yf1 = Lerp(xf01, xf11, ys);
  1671. return Lerp(yf0, yf1, zs);
  1672. }
  1673. // Domain Warp
  1674. template <typename FNfloat>
  1675. void DoSingleDomainWarp(int seed, float amp, float freq, FNfloat x, FNfloat y, FNfloat& xr, FNfloat& yr)
  1676. {
  1677. switch (mDomainWarpType)
  1678. {
  1679. case DomainWarpType_OpenSimplex2:
  1680. SingleDomainWarpSimplexGradient(seed, amp * 38.283687591552734375f, freq, x, y, xr, yr, false);
  1681. break;
  1682. case DomainWarpType_OpenSimplex2Reduced:
  1683. SingleDomainWarpSimplexGradient(seed, amp * 16.0f, freq, x, y, xr, yr, true);
  1684. break;
  1685. case DomainWarpType_BasicGrid:
  1686. SingleDomainWarpBasicGrid(seed, amp, freq, x, y, xr, yr);
  1687. break;
  1688. }
  1689. }
  1690. template <typename FNfloat>
  1691. void DoSingleDomainWarp(int seed, float amp, float freq, FNfloat x, FNfloat y, FNfloat z, FNfloat& xr, FNfloat& yr, FNfloat& zr)
  1692. {
  1693. switch (mDomainWarpType)
  1694. {
  1695. case DomainWarpType_OpenSimplex2:
  1696. SingleDomainWarpOpenSimplex2Gradient(seed, amp * 32.69428253173828125f, freq, x, y, z, xr, yr, zr, false);
  1697. break;
  1698. case DomainWarpType_OpenSimplex2Reduced:
  1699. SingleDomainWarpOpenSimplex2Gradient(seed, amp * 7.71604938271605f, freq, x, y, z, xr, yr, zr, true);
  1700. break;
  1701. case DomainWarpType_BasicGrid:
  1702. SingleDomainWarpBasicGrid(seed, amp, freq, x, y, z, xr, yr, zr);
  1703. break;
  1704. }
  1705. }
  1706. // Domain Warp Single Wrapper
  1707. template <typename FNfloat>
  1708. void DomainWarpSingle(FNfloat& x, FNfloat& y)
  1709. {
  1710. int seed = mSeed;
  1711. float amp = mDomainWarpAmp * mFractalBounding;
  1712. float freq = mFrequency;
  1713. FNfloat xs = x;
  1714. FNfloat ys = y;
  1715. TransformDomainWarpCoordinate(xs, ys);
  1716. DoSingleDomainWarp(seed, amp, freq, xs, ys, x, y);
  1717. }
  1718. template <typename FNfloat>
  1719. void DomainWarpSingle(FNfloat& x, FNfloat& y, FNfloat& z)
  1720. {
  1721. int seed = mSeed;
  1722. float amp = mDomainWarpAmp * mFractalBounding;
  1723. float freq = mFrequency;
  1724. FNfloat xs = x;
  1725. FNfloat ys = y;
  1726. FNfloat zs = z;
  1727. TransformDomainWarpCoordinate(xs, ys, zs);
  1728. DoSingleDomainWarp(seed, amp, freq, xs, ys, zs, x, y, z);
  1729. }
  1730. // Domain Warp Fractal Progressive
  1731. template <typename FNfloat>
  1732. void DomainWarpFractalProgressive(FNfloat& x, FNfloat& y)
  1733. {
  1734. int seed = mSeed;
  1735. float amp = mDomainWarpAmp * mFractalBounding;
  1736. float freq = mFrequency;
  1737. for (int i = 0; i < mOctaves; i++)
  1738. {
  1739. FNfloat xs = x;
  1740. FNfloat ys = y;
  1741. TransformDomainWarpCoordinate(xs, ys);
  1742. DoSingleDomainWarp(seed, amp, freq, xs, ys, x, y);
  1743. seed++;
  1744. amp *= mGain;
  1745. freq *= mLacunarity;
  1746. }
  1747. }
  1748. template <typename FNfloat>
  1749. void DomainWarpFractalProgressive(FNfloat& x, FNfloat& y, FNfloat& z)
  1750. {
  1751. int seed = mSeed;
  1752. float amp = mDomainWarpAmp * mFractalBounding;
  1753. float freq = mFrequency;
  1754. for (int i = 0; i < mOctaves; i++)
  1755. {
  1756. FNfloat xs = x;
  1757. FNfloat ys = y;
  1758. FNfloat zs = z;
  1759. TransformDomainWarpCoordinate(xs, ys, zs);
  1760. DoSingleDomainWarp(seed, amp, freq, xs, ys, zs, x, y, z);
  1761. seed++;
  1762. amp *= mGain;
  1763. freq *= mLacunarity;
  1764. }
  1765. }
  1766. // Domain Warp Fractal Independant
  1767. template <typename FNfloat>
  1768. void DomainWarpFractalIndependent(FNfloat& x, FNfloat& y)
  1769. {
  1770. FNfloat xs = x;
  1771. FNfloat ys = y;
  1772. TransformDomainWarpCoordinate(xs, ys);
  1773. int seed = mSeed;
  1774. float amp = mDomainWarpAmp * mFractalBounding;
  1775. float freq = mFrequency;
  1776. for (int i = 0; i < mOctaves; i++)
  1777. {
  1778. DoSingleDomainWarp(seed, amp, freq, xs, ys, x, y);
  1779. seed++;
  1780. amp *= mGain;
  1781. freq *= mLacunarity;
  1782. }
  1783. }
  1784. template <typename FNfloat>
  1785. void DomainWarpFractalIndependent(FNfloat& x, FNfloat& y, FNfloat& z)
  1786. {
  1787. FNfloat xs = x;
  1788. FNfloat ys = y;
  1789. FNfloat zs = z;
  1790. TransformDomainWarpCoordinate(xs, ys, zs);
  1791. int seed = mSeed;
  1792. float amp = mDomainWarpAmp * mFractalBounding;
  1793. float freq = mFrequency;
  1794. for (int i = 0; i < mOctaves; i++)
  1795. {
  1796. DoSingleDomainWarp(seed, amp, freq, xs, ys, zs, x, y, z);
  1797. seed++;
  1798. amp *= mGain;
  1799. freq *= mLacunarity;
  1800. }
  1801. }
  1802. // Domain Warp Basic Grid
  1803. template <typename FNfloat>
  1804. void SingleDomainWarpBasicGrid(int seed, float warpAmp, float frequency, FNfloat x, FNfloat y, FNfloat& xr, FNfloat& yr)
  1805. {
  1806. FNfloat xf = x * frequency;
  1807. FNfloat yf = y * frequency;
  1808. int x0 = FastFloor(xf);
  1809. int y0 = FastFloor(yf);
  1810. float xs = InterpHermite((float)(xf - x0));
  1811. float ys = InterpHermite((float)(yf - y0));
  1812. x0 *= PrimeX;
  1813. y0 *= PrimeY;
  1814. int x1 = x0 + PrimeX;
  1815. int y1 = y0 + PrimeY;
  1816. int hash0 = Hash(seed, x0, y0) & (255 << 1);
  1817. int hash1 = Hash(seed, x1, y0) & (255 << 1);
  1818. float lx0x = Lerp(Lookup<float>::RandVecs2D[hash0], Lookup<float>::RandVecs2D[hash1], xs);
  1819. float ly0x = Lerp(Lookup<float>::RandVecs2D[hash0 | 1], Lookup<float>::RandVecs2D[hash1 | 1], xs);
  1820. hash0 = Hash(seed, x0, y1) & (255 << 1);
  1821. hash1 = Hash(seed, x1, y1) & (255 << 1);
  1822. float lx1x = Lerp(Lookup<float>::RandVecs2D[hash0], Lookup<float>::RandVecs2D[hash1], xs);
  1823. float ly1x = Lerp(Lookup<float>::RandVecs2D[hash0 | 1], Lookup<float>::RandVecs2D[hash1 | 1], xs);
  1824. xr += Lerp(lx0x, lx1x, ys) * warpAmp;
  1825. yr += Lerp(ly0x, ly1x, ys) * warpAmp;
  1826. }
  1827. template <typename FNfloat>
  1828. void SingleDomainWarpBasicGrid(int seed, float warpAmp, float frequency, FNfloat x, FNfloat y, FNfloat z, FNfloat& xr, FNfloat& yr, FNfloat& zr)
  1829. {
  1830. FNfloat xf = x * frequency;
  1831. FNfloat yf = y * frequency;
  1832. FNfloat zf = z * frequency;
  1833. int x0 = FastFloor(xf);
  1834. int y0 = FastFloor(yf);
  1835. int z0 = FastFloor(zf);
  1836. float xs = InterpHermite((float)(xf - x0));
  1837. float ys = InterpHermite((float)(yf - y0));
  1838. float zs = InterpHermite((float)(zf - z0));
  1839. x0 *= PrimeX;
  1840. y0 *= PrimeY;
  1841. z0 *= PrimeZ;
  1842. int x1 = x0 + PrimeX;
  1843. int y1 = y0 + PrimeY;
  1844. int z1 = z0 + PrimeZ;
  1845. int hash0 = Hash(seed, x0, y0, z0) & (255 << 2);
  1846. int hash1 = Hash(seed, x1, y0, z0) & (255 << 2);
  1847. float lx0x = Lerp(Lookup<float>::RandVecs3D[hash0], Lookup<float>::RandVecs3D[hash1], xs);
  1848. float ly0x = Lerp(Lookup<float>::RandVecs3D[hash0 | 1], Lookup<float>::RandVecs3D[hash1 | 1], xs);
  1849. float lz0x = Lerp(Lookup<float>::RandVecs3D[hash0 | 2], Lookup<float>::RandVecs3D[hash1 | 2], xs);
  1850. hash0 = Hash(seed, x0, y1, z0) & (255 << 2);
  1851. hash1 = Hash(seed, x1, y1, z0) & (255 << 2);
  1852. float lx1x = Lerp(Lookup<float>::RandVecs3D[hash0], Lookup<float>::RandVecs3D[hash1], xs);
  1853. float ly1x = Lerp(Lookup<float>::RandVecs3D[hash0 | 1], Lookup<float>::RandVecs3D[hash1 | 1], xs);
  1854. float lz1x = Lerp(Lookup<float>::RandVecs3D[hash0 | 2], Lookup<float>::RandVecs3D[hash1 | 2], xs);
  1855. float lx0y = Lerp(lx0x, lx1x, ys);
  1856. float ly0y = Lerp(ly0x, ly1x, ys);
  1857. float lz0y = Lerp(lz0x, lz1x, ys);
  1858. hash0 = Hash(seed, x0, y0, z1) & (255 << 2);
  1859. hash1 = Hash(seed, x1, y0, z1) & (255 << 2);
  1860. lx0x = Lerp(Lookup<float>::RandVecs3D[hash0], Lookup<float>::RandVecs3D[hash1], xs);
  1861. ly0x = Lerp(Lookup<float>::RandVecs3D[hash0 | 1], Lookup<float>::RandVecs3D[hash1 | 1], xs);
  1862. lz0x = Lerp(Lookup<float>::RandVecs3D[hash0 | 2], Lookup<float>::RandVecs3D[hash1 | 2], xs);
  1863. hash0 = Hash(seed, x0, y1, z1) & (255 << 2);
  1864. hash1 = Hash(seed, x1, y1, z1) & (255 << 2);
  1865. lx1x = Lerp(Lookup<float>::RandVecs3D[hash0], Lookup<float>::RandVecs3D[hash1], xs);
  1866. ly1x = Lerp(Lookup<float>::RandVecs3D[hash0 | 1], Lookup<float>::RandVecs3D[hash1 | 1], xs);
  1867. lz1x = Lerp(Lookup<float>::RandVecs3D[hash0 | 2], Lookup<float>::RandVecs3D[hash1 | 2], xs);
  1868. xr += Lerp(lx0y, Lerp(lx0x, lx1x, ys), zs) * warpAmp;
  1869. yr += Lerp(ly0y, Lerp(ly0x, ly1x, ys), zs) * warpAmp;
  1870. zr += Lerp(lz0y, Lerp(lz0x, lz1x, ys), zs) * warpAmp;
  1871. }
  1872. // Domain Warp Simplex/OpenSimplex2
  1873. template <typename FNfloat>
  1874. void SingleDomainWarpSimplexGradient(int seed, float warpAmp, float frequency, FNfloat x, FNfloat y, FNfloat& xr, FNfloat& yr, bool outGradOnly)
  1875. {
  1876. const float SQRT3 = 1.7320508075688772935274463415059f;
  1877. const float G2 = (3 - SQRT3) / 6;
  1878. x *= frequency;
  1879. y *= frequency;
  1880. /*
  1881. * --- Skew moved to TransformNoiseCoordinate method ---
  1882. * const FNfloat F2 = 0.5f * (SQRT3 - 1);
  1883. * FNfloat s = (x + y) * F2;
  1884. * x += s; y += s;
  1885. */
  1886. int i = FastFloor(x);
  1887. int j = FastFloor(y);
  1888. float xi = (float)(x - i);
  1889. float yi = (float)(y - j);
  1890. float t = (xi + yi) * G2;
  1891. float x0 = (float)(xi - t);
  1892. float y0 = (float)(yi - t);
  1893. i *= PrimeX;
  1894. j *= PrimeY;
  1895. float vx, vy;
  1896. vx = vy = 0;
  1897. float a = 0.5f - x0 * x0 - y0 * y0;
  1898. if (a > 0)
  1899. {
  1900. float aaaa = (a * a) * (a * a);
  1901. float xo, yo;
  1902. if (outGradOnly)
  1903. GradCoordOut(seed, i, j, xo, yo);
  1904. else
  1905. GradCoordDual(seed, i, j, x0, y0, xo, yo);
  1906. vx += aaaa * xo;
  1907. vy += aaaa * yo;
  1908. }
  1909. float c = (float)(2 * (1 - 2 * G2) * (1 / G2 - 2)) * t + ((float)(-2 * (1 - 2 * G2) * (1 - 2 * G2)) + a);
  1910. if (c > 0)
  1911. {
  1912. float x2 = x0 + (2 * (float)G2 - 1);
  1913. float y2 = y0 + (2 * (float)G2 - 1);
  1914. float cccc = (c * c) * (c * c);
  1915. float xo, yo;
  1916. if (outGradOnly)
  1917. GradCoordOut(seed, i + PrimeX, j + PrimeY, xo, yo);
  1918. else
  1919. GradCoordDual(seed, i + PrimeX, j + PrimeY, x2, y2, xo, yo);
  1920. vx += cccc * xo;
  1921. vy += cccc * yo;
  1922. }
  1923. if (y0 > x0)
  1924. {
  1925. float x1 = x0 + (float)G2;
  1926. float y1 = y0 + ((float)G2 - 1);
  1927. float b = 0.5f - x1 * x1 - y1 * y1;
  1928. if (b > 0)
  1929. {
  1930. float bbbb = (b * b) * (b * b);
  1931. float xo, yo;
  1932. if (outGradOnly)
  1933. GradCoordOut(seed, i, j + PrimeY, xo, yo);
  1934. else
  1935. GradCoordDual(seed, i, j + PrimeY, x1, y1, xo, yo);
  1936. vx += bbbb * xo;
  1937. vy += bbbb * yo;
  1938. }
  1939. }
  1940. else
  1941. {
  1942. float x1 = x0 + ((float)G2 - 1);
  1943. float y1 = y0 + (float)G2;
  1944. float b = 0.5f - x1 * x1 - y1 * y1;
  1945. if (b > 0)
  1946. {
  1947. float bbbb = (b * b) * (b * b);
  1948. float xo, yo;
  1949. if (outGradOnly)
  1950. GradCoordOut(seed, i + PrimeX, j, xo, yo);
  1951. else
  1952. GradCoordDual(seed, i + PrimeX, j, x1, y1, xo, yo);
  1953. vx += bbbb * xo;
  1954. vy += bbbb * yo;
  1955. }
  1956. }
  1957. xr += vx * warpAmp;
  1958. yr += vy * warpAmp;
  1959. }
  1960. template <typename FNfloat>
  1961. void SingleDomainWarpOpenSimplex2Gradient(int seed, float warpAmp, float frequency, FNfloat x, FNfloat y, FNfloat z, FNfloat& xr, FNfloat& yr, FNfloat& zr, bool outGradOnly)
  1962. {
  1963. x *= frequency;
  1964. y *= frequency;
  1965. z *= frequency;
  1966. /*
  1967. * --- Rotation moved to TransformDomainWarpCoordinate method ---
  1968. * const FNfloat R3 = (FNfloat)(2.0 / 3.0);
  1969. * FNfloat r = (x + y + z) * R3; // Rotation, not skew
  1970. * x = r - x; y = r - y; z = r - z;
  1971. */
  1972. int i = FastRound(x);
  1973. int j = FastRound(y);
  1974. int k = FastRound(z);
  1975. float x0 = (float)x - i;
  1976. float y0 = (float)y - j;
  1977. float z0 = (float)z - k;
  1978. int xNSign = (int)(-x0 - 1.0f) | 1;
  1979. int yNSign = (int)(-y0 - 1.0f) | 1;
  1980. int zNSign = (int)(-z0 - 1.0f) | 1;
  1981. float ax0 = xNSign * -x0;
  1982. float ay0 = yNSign * -y0;
  1983. float az0 = zNSign * -z0;
  1984. i *= PrimeX;
  1985. j *= PrimeY;
  1986. k *= PrimeZ;
  1987. float vx, vy, vz;
  1988. vx = vy = vz = 0;
  1989. float a = (0.6f - x0 * x0) - (y0 * y0 + z0 * z0);
  1990. for (int l = 0; l < 2; l++)
  1991. {
  1992. if (a > 0)
  1993. {
  1994. float aaaa = (a * a) * (a * a);
  1995. float xo, yo, zo;
  1996. if (outGradOnly)
  1997. GradCoordOut(seed, i, j, k, xo, yo, zo);
  1998. else
  1999. GradCoordDual(seed, i, j, k, x0, y0, z0, xo, yo, zo);
  2000. vx += aaaa * xo;
  2001. vy += aaaa * yo;
  2002. vz += aaaa * zo;
  2003. }
  2004. float b = a + 1;
  2005. int i1 = i;
  2006. int j1 = j;
  2007. int k1 = k;
  2008. float x1 = x0;
  2009. float y1 = y0;
  2010. float z1 = z0;
  2011. if (ax0 >= ay0 && ax0 >= az0)
  2012. {
  2013. x1 += xNSign;
  2014. b -= xNSign * 2 * x1;
  2015. i1 -= xNSign * PrimeX;
  2016. }
  2017. else if (ay0 > ax0 && ay0 >= az0)
  2018. {
  2019. y1 += yNSign;
  2020. b -= yNSign * 2 * y1;
  2021. j1 -= yNSign * PrimeY;
  2022. }
  2023. else
  2024. {
  2025. z1 += zNSign;
  2026. b -= zNSign * 2 * z1;
  2027. k1 -= zNSign * PrimeZ;
  2028. }
  2029. if (b > 0)
  2030. {
  2031. float bbbb = (b * b) * (b * b);
  2032. float xo, yo, zo;
  2033. if (outGradOnly)
  2034. GradCoordOut(seed, i1, j1, k1, xo, yo, zo);
  2035. else
  2036. GradCoordDual(seed, i1, j1, k1, x1, y1, z1, xo, yo, zo);
  2037. vx += bbbb * xo;
  2038. vy += bbbb * yo;
  2039. vz += bbbb * zo;
  2040. }
  2041. if (l == 1) break;
  2042. ax0 = 0.5f - ax0;
  2043. ay0 = 0.5f - ay0;
  2044. az0 = 0.5f - az0;
  2045. x0 = xNSign * ax0;
  2046. y0 = yNSign * ay0;
  2047. z0 = zNSign * az0;
  2048. a += (0.75f - ax0) - (ay0 + az0);
  2049. i += (xNSign >> 1) & PrimeX;
  2050. j += (yNSign >> 1) & PrimeY;
  2051. k += (zNSign >> 1) & PrimeZ;
  2052. xNSign = -xNSign;
  2053. yNSign = -yNSign;
  2054. zNSign = -zNSign;
  2055. seed += 1293373;
  2056. }
  2057. xr += vx * warpAmp;
  2058. yr += vy * warpAmp;
  2059. zr += vz * warpAmp;
  2060. }
  2061. };
  2062. template <>
  2063. struct FastNoiseLite::Arguments_must_be_floating_point_values<float> {};
  2064. template <>
  2065. struct FastNoiseLite::Arguments_must_be_floating_point_values<double> {};
  2066. template <>
  2067. struct FastNoiseLite::Arguments_must_be_floating_point_values<long double> {};
  2068. template <typename T>
  2069. const T FastNoiseLite::Lookup<T>::Gradients2D[] =
  2070. {
  2071. 0.130526192220052f, 0.99144486137381f, 0.38268343236509f, 0.923879532511287f, 0.608761429008721f, 0.793353340291235f, 0.793353340291235f, 0.608761429008721f,
  2072. 0.923879532511287f, 0.38268343236509f, 0.99144486137381f, 0.130526192220051f, 0.99144486137381f, -0.130526192220051f, 0.923879532511287f, -0.38268343236509f,
  2073. 0.793353340291235f, -0.60876142900872f, 0.608761429008721f, -0.793353340291235f, 0.38268343236509f, -0.923879532511287f, 0.130526192220052f, -0.99144486137381f,
  2074. -0.130526192220052f, -0.99144486137381f, -0.38268343236509f, -0.923879532511287f, -0.608761429008721f, -0.793353340291235f, -0.793353340291235f, -0.608761429008721f,
  2075. -0.923879532511287f, -0.38268343236509f, -0.99144486137381f, -0.130526192220052f, -0.99144486137381f, 0.130526192220051f, -0.923879532511287f, 0.38268343236509f,
  2076. -0.793353340291235f, 0.608761429008721f, -0.608761429008721f, 0.793353340291235f, -0.38268343236509f, 0.923879532511287f, -0.130526192220052f, 0.99144486137381f,
  2077. 0.130526192220052f, 0.99144486137381f, 0.38268343236509f, 0.923879532511287f, 0.608761429008721f, 0.793353340291235f, 0.793353340291235f, 0.608761429008721f,
  2078. 0.923879532511287f, 0.38268343236509f, 0.99144486137381f, 0.130526192220051f, 0.99144486137381f, -0.130526192220051f, 0.923879532511287f, -0.38268343236509f,
  2079. 0.793353340291235f, -0.60876142900872f, 0.608761429008721f, -0.793353340291235f, 0.38268343236509f, -0.923879532511287f, 0.130526192220052f, -0.99144486137381f,
  2080. -0.130526192220052f, -0.99144486137381f, -0.38268343236509f, -0.923879532511287f, -0.608761429008721f, -0.793353340291235f, -0.793353340291235f, -0.608761429008721f,
  2081. -0.923879532511287f, -0.38268343236509f, -0.99144486137381f, -0.130526192220052f, -0.99144486137381f, 0.130526192220051f, -0.923879532511287f, 0.38268343236509f,
  2082. -0.793353340291235f, 0.608761429008721f, -0.608761429008721f, 0.793353340291235f, -0.38268343236509f, 0.923879532511287f, -0.130526192220052f, 0.99144486137381f,
  2083. 0.130526192220052f, 0.99144486137381f, 0.38268343236509f, 0.923879532511287f, 0.608761429008721f, 0.793353340291235f, 0.793353340291235f, 0.608761429008721f,
  2084. 0.923879532511287f, 0.38268343236509f, 0.99144486137381f, 0.130526192220051f, 0.99144486137381f, -0.130526192220051f, 0.923879532511287f, -0.38268343236509f,
  2085. 0.793353340291235f, -0.60876142900872f, 0.608761429008721f, -0.793353340291235f, 0.38268343236509f, -0.923879532511287f, 0.130526192220052f, -0.99144486137381f,
  2086. -0.130526192220052f, -0.99144486137381f, -0.38268343236509f, -0.923879532511287f, -0.608761429008721f, -0.793353340291235f, -0.793353340291235f, -0.608761429008721f,
  2087. -0.923879532511287f, -0.38268343236509f, -0.99144486137381f, -0.130526192220052f, -0.99144486137381f, 0.130526192220051f, -0.923879532511287f, 0.38268343236509f,
  2088. -0.793353340291235f, 0.608761429008721f, -0.608761429008721f, 0.793353340291235f, -0.38268343236509f, 0.923879532511287f, -0.130526192220052f, 0.99144486137381f,
  2089. 0.130526192220052f, 0.99144486137381f, 0.38268343236509f, 0.923879532511287f, 0.608761429008721f, 0.793353340291235f, 0.793353340291235f, 0.608761429008721f,
  2090. 0.923879532511287f, 0.38268343236509f, 0.99144486137381f, 0.130526192220051f, 0.99144486137381f, -0.130526192220051f, 0.923879532511287f, -0.38268343236509f,
  2091. 0.793353340291235f, -0.60876142900872f, 0.608761429008721f, -0.793353340291235f, 0.38268343236509f, -0.923879532511287f, 0.130526192220052f, -0.99144486137381f,
  2092. -0.130526192220052f, -0.99144486137381f, -0.38268343236509f, -0.923879532511287f, -0.608761429008721f, -0.793353340291235f, -0.793353340291235f, -0.608761429008721f,
  2093. -0.923879532511287f, -0.38268343236509f, -0.99144486137381f, -0.130526192220052f, -0.99144486137381f, 0.130526192220051f, -0.923879532511287f, 0.38268343236509f,
  2094. -0.793353340291235f, 0.608761429008721f, -0.608761429008721f, 0.793353340291235f, -0.38268343236509f, 0.923879532511287f, -0.130526192220052f, 0.99144486137381f,
  2095. 0.130526192220052f, 0.99144486137381f, 0.38268343236509f, 0.923879532511287f, 0.608761429008721f, 0.793353340291235f, 0.793353340291235f, 0.608761429008721f,
  2096. 0.923879532511287f, 0.38268343236509f, 0.99144486137381f, 0.130526192220051f, 0.99144486137381f, -0.130526192220051f, 0.923879532511287f, -0.38268343236509f,
  2097. 0.793353340291235f, -0.60876142900872f, 0.608761429008721f, -0.793353340291235f, 0.38268343236509f, -0.923879532511287f, 0.130526192220052f, -0.99144486137381f,
  2098. -0.130526192220052f, -0.99144486137381f, -0.38268343236509f, -0.923879532511287f, -0.608761429008721f, -0.793353340291235f, -0.793353340291235f, -0.608761429008721f,
  2099. -0.923879532511287f, -0.38268343236509f, -0.99144486137381f, -0.130526192220052f, -0.99144486137381f, 0.130526192220051f, -0.923879532511287f, 0.38268343236509f,
  2100. -0.793353340291235f, 0.608761429008721f, -0.608761429008721f, 0.793353340291235f, -0.38268343236509f, 0.923879532511287f, -0.130526192220052f, 0.99144486137381f,
  2101. 0.38268343236509f, 0.923879532511287f, 0.923879532511287f, 0.38268343236509f, 0.923879532511287f, -0.38268343236509f, 0.38268343236509f, -0.923879532511287f,
  2102. -0.38268343236509f, -0.923879532511287f, -0.923879532511287f, -0.38268343236509f, -0.923879532511287f, 0.38268343236509f, -0.38268343236509f, 0.923879532511287f,
  2103. };
  2104. template <typename T>
  2105. const T FastNoiseLite::Lookup<T>::RandVecs2D[] =
  2106. {
  2107. -0.2700222198f, -0.9628540911f, 0.3863092627f, -0.9223693152f, 0.04444859006f, -0.999011673f, -0.5992523158f, -0.8005602176f, -0.7819280288f, 0.6233687174f, 0.9464672271f, 0.3227999196f, -0.6514146797f, -0.7587218957f, 0.9378472289f, 0.347048376f,
  2108. -0.8497875957f, -0.5271252623f, -0.879042592f, 0.4767432447f, -0.892300288f, -0.4514423508f, -0.379844434f, -0.9250503802f, -0.9951650832f, 0.0982163789f, 0.7724397808f, -0.6350880136f, 0.7573283322f, -0.6530343002f, -0.9928004525f, -0.119780055f,
  2109. -0.0532665713f, 0.9985803285f, 0.9754253726f, -0.2203300762f, -0.7665018163f, 0.6422421394f, 0.991636706f, 0.1290606184f, -0.994696838f, 0.1028503788f, -0.5379205513f, -0.84299554f, 0.5022815471f, -0.8647041387f, 0.4559821461f, -0.8899889226f,
  2110. -0.8659131224f, -0.5001944266f, 0.0879458407f, -0.9961252577f, -0.5051684983f, 0.8630207346f, 0.7753185226f, -0.6315704146f, -0.6921944612f, 0.7217110418f, -0.5191659449f, -0.8546734591f, 0.8978622882f, -0.4402764035f, -0.1706774107f, 0.9853269617f,
  2111. -0.9353430106f, -0.3537420705f, -0.9992404798f, 0.03896746794f, -0.2882064021f, -0.9575683108f, -0.9663811329f, 0.2571137995f, -0.8759714238f, -0.4823630009f, -0.8303123018f, -0.5572983775f, 0.05110133755f, -0.9986934731f, -0.8558373281f, -0.5172450752f,
  2112. 0.09887025282f, 0.9951003332f, 0.9189016087f, 0.3944867976f, -0.2439375892f, -0.9697909324f, -0.8121409387f, -0.5834613061f, -0.9910431363f, 0.1335421355f, 0.8492423985f, -0.5280031709f, -0.9717838994f, -0.2358729591f, 0.9949457207f, 0.1004142068f,
  2113. 0.6241065508f, -0.7813392434f, 0.662910307f, 0.7486988212f, -0.7197418176f, 0.6942418282f, -0.8143370775f, -0.5803922158f, 0.104521054f, -0.9945226741f, -0.1065926113f, -0.9943027784f, 0.445799684f, -0.8951327509f, 0.105547406f, 0.9944142724f,
  2114. -0.992790267f, 0.1198644477f, -0.8334366408f, 0.552615025f, 0.9115561563f, -0.4111755999f, 0.8285544909f, -0.5599084351f, 0.7217097654f, -0.6921957921f, 0.4940492677f, -0.8694339084f, -0.3652321272f, -0.9309164803f, -0.9696606758f, 0.2444548501f,
  2115. 0.08925509731f, -0.996008799f, 0.5354071276f, -0.8445941083f, -0.1053576186f, 0.9944343981f, -0.9890284586f, 0.1477251101f, 0.004856104961f, 0.9999882091f, 0.9885598478f, 0.1508291331f, 0.9286129562f, -0.3710498316f, -0.5832393863f, -0.8123003252f,
  2116. 0.3015207509f, 0.9534596146f, -0.9575110528f, 0.2883965738f, 0.9715802154f, -0.2367105511f, 0.229981792f, 0.9731949318f, 0.955763816f, -0.2941352207f, 0.740956116f, 0.6715534485f, -0.9971513787f, -0.07542630764f, 0.6905710663f, -0.7232645452f,
  2117. -0.290713703f, -0.9568100872f, 0.5912777791f, -0.8064679708f, -0.9454592212f, -0.325740481f, 0.6664455681f, 0.74555369f, 0.6236134912f, 0.7817328275f, 0.9126993851f, -0.4086316587f, -0.8191762011f, 0.5735419353f, -0.8812745759f, -0.4726046147f,
  2118. 0.9953313627f, 0.09651672651f, 0.9855650846f, -0.1692969699f, -0.8495980887f, 0.5274306472f, 0.6174853946f, -0.7865823463f, 0.8508156371f, 0.52546432f, 0.9985032451f, -0.05469249926f, 0.1971371563f, -0.9803759185f, 0.6607855748f, -0.7505747292f,
  2119. -0.03097494063f, 0.9995201614f, -0.6731660801f, 0.739491331f, -0.7195018362f, -0.6944905383f, 0.9727511689f, 0.2318515979f, 0.9997059088f, -0.0242506907f, 0.4421787429f, -0.8969269532f, 0.9981350961f, -0.061043673f, -0.9173660799f, -0.3980445648f,
  2120. -0.8150056635f, -0.5794529907f, -0.8789331304f, 0.4769450202f, 0.0158605829f, 0.999874213f, -0.8095464474f, 0.5870558317f, -0.9165898907f, -0.3998286786f, -0.8023542565f, 0.5968480938f, -0.5176737917f, 0.8555780767f, -0.8154407307f, -0.5788405779f,
  2121. 0.4022010347f, -0.9155513791f, -0.9052556868f, -0.4248672045f, 0.7317445619f, 0.6815789728f, -0.5647632201f, -0.8252529947f, -0.8403276335f, -0.5420788397f, -0.9314281527f, 0.363925262f, 0.5238198472f, 0.8518290719f, 0.7432803869f, -0.6689800195f,
  2122. -0.985371561f, -0.1704197369f, 0.4601468731f, 0.88784281f, 0.825855404f, 0.5638819483f, 0.6182366099f, 0.7859920446f, 0.8331502863f, -0.553046653f, 0.1500307506f, 0.9886813308f, -0.662330369f, -0.7492119075f, -0.668598664f, 0.743623444f,
  2123. 0.7025606278f, 0.7116238924f, -0.5419389763f, -0.8404178401f, -0.3388616456f, 0.9408362159f, 0.8331530315f, 0.5530425174f, -0.2989720662f, -0.9542618632f, 0.2638522993f, 0.9645630949f, 0.124108739f, -0.9922686234f, -0.7282649308f, -0.6852956957f,
  2124. 0.6962500149f, 0.7177993569f, -0.9183535368f, 0.3957610156f, -0.6326102274f, -0.7744703352f, -0.9331891859f, -0.359385508f, -0.1153779357f, -0.9933216659f, 0.9514974788f, -0.3076565421f, -0.08987977445f, -0.9959526224f, 0.6678496916f, 0.7442961705f,
  2125. 0.7952400393f, -0.6062947138f, -0.6462007402f, -0.7631674805f, -0.2733598753f, 0.9619118351f, 0.9669590226f, -0.254931851f, -0.9792894595f, 0.2024651934f, -0.5369502995f, -0.8436138784f, -0.270036471f, -0.9628500944f, -0.6400277131f, 0.7683518247f,
  2126. -0.7854537493f, -0.6189203566f, 0.06005905383f, -0.9981948257f, -0.02455770378f, 0.9996984141f, -0.65983623f, 0.751409442f, -0.6253894466f, -0.7803127835f, -0.6210408851f, -0.7837781695f, 0.8348888491f, 0.5504185768f, -0.1592275245f, 0.9872419133f,
  2127. 0.8367622488f, 0.5475663786f, -0.8675753916f, -0.4973056806f, -0.2022662628f, -0.9793305667f, 0.9399189937f, 0.3413975472f, 0.9877404807f, -0.1561049093f, -0.9034455656f, 0.4287028224f, 0.1269804218f, -0.9919052235f, -0.3819600854f, 0.924178821f,
  2128. 0.9754625894f, 0.2201652486f, -0.3204015856f, -0.9472818081f, -0.9874760884f, 0.1577687387f, 0.02535348474f, -0.9996785487f, 0.4835130794f, -0.8753371362f, -0.2850799925f, -0.9585037287f, -0.06805516006f, -0.99768156f, -0.7885244045f, -0.6150034663f,
  2129. 0.3185392127f, -0.9479096845f, 0.8880043089f, 0.4598351306f, 0.6476921488f, -0.7619021462f, 0.9820241299f, 0.1887554194f, 0.9357275128f, -0.3527237187f, -0.8894895414f, 0.4569555293f, 0.7922791302f, 0.6101588153f, 0.7483818261f, 0.6632681526f,
  2130. -0.7288929755f, -0.6846276581f, 0.8729032783f, -0.4878932944f, 0.8288345784f, 0.5594937369f, 0.08074567077f, 0.9967347374f, 0.9799148216f, -0.1994165048f, -0.580730673f, -0.8140957471f, -0.4700049791f, -0.8826637636f, 0.2409492979f, 0.9705377045f,
  2131. 0.9437816757f, -0.3305694308f, -0.8927998638f, -0.4504535528f, -0.8069622304f, 0.5906030467f, 0.06258973166f, 0.9980393407f, -0.9312597469f, 0.3643559849f, 0.5777449785f, 0.8162173362f, -0.3360095855f, -0.941858566f, 0.697932075f, -0.7161639607f,
  2132. -0.002008157227f, -0.9999979837f, -0.1827294312f, -0.9831632392f, -0.6523911722f, 0.7578824173f, -0.4302626911f, -0.9027037258f, -0.9985126289f, -0.05452091251f, -0.01028102172f, -0.9999471489f, -0.4946071129f, 0.8691166802f, -0.2999350194f, 0.9539596344f,
  2133. 0.8165471961f, 0.5772786819f, 0.2697460475f, 0.962931498f, -0.7306287391f, -0.6827749597f, -0.7590952064f, -0.6509796216f, -0.907053853f, 0.4210146171f, -0.5104861064f, -0.8598860013f, 0.8613350597f, 0.5080373165f, 0.5007881595f, -0.8655698812f,
  2134. -0.654158152f, 0.7563577938f, -0.8382755311f, -0.545246856f, 0.6940070834f, 0.7199681717f, 0.06950936031f, 0.9975812994f, 0.1702942185f, -0.9853932612f, 0.2695973274f, 0.9629731466f, 0.5519612192f, -0.8338697815f, 0.225657487f, -0.9742067022f,
  2135. 0.4215262855f, -0.9068161835f, 0.4881873305f, -0.8727388672f, -0.3683854996f, -0.9296731273f, -0.9825390578f, 0.1860564427f, 0.81256471f, 0.5828709909f, 0.3196460933f, -0.9475370046f, 0.9570913859f, 0.2897862643f, -0.6876655497f, -0.7260276109f,
  2136. -0.9988770922f, -0.047376731f, -0.1250179027f, 0.992154486f, -0.8280133617f, 0.560708367f, 0.9324863769f, -0.3612051451f, 0.6394653183f, 0.7688199442f, -0.01623847064f, -0.9998681473f, -0.9955014666f, -0.09474613458f, -0.81453315f, 0.580117012f,
  2137. 0.4037327978f, -0.9148769469f, 0.9944263371f, 0.1054336766f, -0.1624711654f, 0.9867132919f, -0.9949487814f, -0.100383875f, -0.6995302564f, 0.7146029809f, 0.5263414922f, -0.85027327f, -0.5395221479f, 0.841971408f, 0.6579370318f, 0.7530729462f,
  2138. 0.01426758847f, -0.9998982128f, -0.6734383991f, 0.7392433447f, 0.639412098f, -0.7688642071f, 0.9211571421f, 0.3891908523f, -0.146637214f, -0.9891903394f, -0.782318098f, 0.6228791163f, -0.5039610839f, -0.8637263605f, -0.7743120191f, -0.6328039957f,
  2139. };
  2140. template <typename T>
  2141. const T FastNoiseLite::Lookup<T>::Gradients3D[] =
  2142. {
  2143. 0, 1, 1, 0, 0,-1, 1, 0, 0, 1,-1, 0, 0,-1,-1, 0,
  2144. 1, 0, 1, 0, -1, 0, 1, 0, 1, 0,-1, 0, -1, 0,-1, 0,
  2145. 1, 1, 0, 0, -1, 1, 0, 0, 1,-1, 0, 0, -1,-1, 0, 0,
  2146. 0, 1, 1, 0, 0,-1, 1, 0, 0, 1,-1, 0, 0,-1,-1, 0,
  2147. 1, 0, 1, 0, -1, 0, 1, 0, 1, 0,-1, 0, -1, 0,-1, 0,
  2148. 1, 1, 0, 0, -1, 1, 0, 0, 1,-1, 0, 0, -1,-1, 0, 0,
  2149. 0, 1, 1, 0, 0,-1, 1, 0, 0, 1,-1, 0, 0,-1,-1, 0,
  2150. 1, 0, 1, 0, -1, 0, 1, 0, 1, 0,-1, 0, -1, 0,-1, 0,
  2151. 1, 1, 0, 0, -1, 1, 0, 0, 1,-1, 0, 0, -1,-1, 0, 0,
  2152. 0, 1, 1, 0, 0,-1, 1, 0, 0, 1,-1, 0, 0,-1,-1, 0,
  2153. 1, 0, 1, 0, -1, 0, 1, 0, 1, 0,-1, 0, -1, 0,-1, 0,
  2154. 1, 1, 0, 0, -1, 1, 0, 0, 1,-1, 0, 0, -1,-1, 0, 0,
  2155. 0, 1, 1, 0, 0,-1, 1, 0, 0, 1,-1, 0, 0,-1,-1, 0,
  2156. 1, 0, 1, 0, -1, 0, 1, 0, 1, 0,-1, 0, -1, 0,-1, 0,
  2157. 1, 1, 0, 0, -1, 1, 0, 0, 1,-1, 0, 0, -1,-1, 0, 0,
  2158. 1, 1, 0, 0, 0,-1, 1, 0, -1, 1, 0, 0, 0,-1,-1, 0
  2159. };
  2160. template <typename T>
  2161. const T FastNoiseLite::Lookup<T>::RandVecs3D[] =
  2162. {
  2163. -0.7292736885f, -0.6618439697f, 0.1735581948f, 0, 0.790292081f, -0.5480887466f, -0.2739291014f, 0, 0.7217578935f, 0.6226212466f, -0.3023380997f, 0, 0.565683137f, -0.8208298145f, -0.0790000257f, 0, 0.760049034f, -0.5555979497f, -0.3370999617f, 0, 0.3713945616f, 0.5011264475f, 0.7816254623f, 0, -0.1277062463f, -0.4254438999f, -0.8959289049f, 0, -0.2881560924f, -0.5815838982f, 0.7607405838f, 0,
  2164. 0.5849561111f, -0.662820239f, -0.4674352136f, 0, 0.3307171178f, 0.0391653737f, 0.94291689f, 0, 0.8712121778f, -0.4113374369f, -0.2679381538f, 0, 0.580981015f, 0.7021915846f, 0.4115677815f, 0, 0.503756873f, 0.6330056931f, -0.5878203852f, 0, 0.4493712205f, 0.601390195f, 0.6606022552f, 0, -0.6878403724f, 0.09018890807f, -0.7202371714f, 0, -0.5958956522f, -0.6469350577f, 0.475797649f, 0,
  2165. -0.5127052122f, 0.1946921978f, -0.8361987284f, 0, -0.9911507142f, -0.05410276466f, -0.1212153153f, 0, -0.2149721042f, 0.9720882117f, -0.09397607749f, 0, -0.7518650936f, -0.5428057603f, 0.3742469607f, 0, 0.5237068895f, 0.8516377189f, -0.02107817834f, 0, 0.6333504779f, 0.1926167129f, -0.7495104896f, 0, -0.06788241606f, 0.3998305789f, 0.9140719259f, 0, -0.5538628599f, -0.4729896695f, -0.6852128902f, 0,
  2166. -0.7261455366f, -0.5911990757f, 0.3509933228f, 0, -0.9229274737f, -0.1782808786f, 0.3412049336f, 0, -0.6968815002f, 0.6511274338f, 0.3006480328f, 0, 0.9608044783f, -0.2098363234f, -0.1811724921f, 0, 0.06817146062f, -0.9743405129f, 0.2145069156f, 0, -0.3577285196f, -0.6697087264f, -0.6507845481f, 0, -0.1868621131f, 0.7648617052f, -0.6164974636f, 0, -0.6541697588f, 0.3967914832f, 0.6439087246f, 0,
  2167. 0.6993340405f, -0.6164538506f, 0.3618239211f, 0, -0.1546665739f, 0.6291283928f, 0.7617583057f, 0, -0.6841612949f, -0.2580482182f, -0.6821542638f, 0, 0.5383980957f, 0.4258654885f, 0.7271630328f, 0, -0.5026987823f, -0.7939832935f, -0.3418836993f, 0, 0.3202971715f, 0.2834415347f, 0.9039195862f, 0, 0.8683227101f, -0.0003762656404f, -0.4959995258f, 0, 0.791120031f, -0.08511045745f, 0.6057105799f, 0,
  2168. -0.04011016052f, -0.4397248749f, 0.8972364289f, 0, 0.9145119872f, 0.3579346169f, -0.1885487608f, 0, -0.9612039066f, -0.2756484276f, 0.01024666929f, 0, 0.6510361721f, -0.2877799159f, -0.7023778346f, 0, -0.2041786351f, 0.7365237271f, 0.644859585f, 0, -0.7718263711f, 0.3790626912f, 0.5104855816f, 0, -0.3060082741f, -0.7692987727f, 0.5608371729f, 0, 0.454007341f, -0.5024843065f, 0.7357899537f, 0,
  2169. 0.4816795475f, 0.6021208291f, -0.6367380315f, 0, 0.6961980369f, -0.3222197429f, 0.641469197f, 0, -0.6532160499f, -0.6781148932f, 0.3368515753f, 0, 0.5089301236f, -0.6154662304f, -0.6018234363f, 0, -0.1635919754f, -0.9133604627f, -0.372840892f, 0, 0.52408019f, -0.8437664109f, 0.1157505864f, 0, 0.5902587356f, 0.4983817807f, -0.6349883666f, 0, 0.5863227872f, 0.494764745f, 0.6414307729f, 0,
  2170. 0.6779335087f, 0.2341345225f, 0.6968408593f, 0, 0.7177054546f, -0.6858979348f, 0.120178631f, 0, -0.5328819713f, -0.5205125012f, 0.6671608058f, 0, -0.8654874251f, -0.0700727088f, -0.4960053754f, 0, -0.2861810166f, 0.7952089234f, 0.5345495242f, 0, -0.04849529634f, 0.9810836427f, -0.1874115585f, 0, -0.6358521667f, 0.6058348682f, 0.4781800233f, 0, 0.6254794696f, -0.2861619734f, 0.7258696564f, 0,
  2171. -0.2585259868f, 0.5061949264f, -0.8227581726f, 0, 0.02136306781f, 0.5064016808f, -0.8620330371f, 0, 0.200111773f, 0.8599263484f, 0.4695550591f, 0, 0.4743561372f, 0.6014985084f, -0.6427953014f, 0, 0.6622993731f, -0.5202474575f, -0.5391679918f, 0, 0.08084972818f, -0.6532720452f, 0.7527940996f, 0, -0.6893687501f, 0.0592860349f, 0.7219805347f, 0, -0.1121887082f, -0.9673185067f, 0.2273952515f, 0,
  2172. 0.7344116094f, 0.5979668656f, -0.3210532909f, 0, 0.5789393465f, -0.2488849713f, 0.7764570201f, 0, 0.6988182827f, 0.3557169806f, -0.6205791146f, 0, -0.8636845529f, -0.2748771249f, -0.4224826141f, 0, -0.4247027957f, -0.4640880967f, 0.777335046f, 0, 0.5257722489f, -0.8427017621f, 0.1158329937f, 0, 0.9343830603f, 0.316302472f, -0.1639543925f, 0, -0.1016836419f, -0.8057303073f, -0.5834887393f, 0,
  2173. -0.6529238969f, 0.50602126f, -0.5635892736f, 0, -0.2465286165f, -0.9668205684f, -0.06694497494f, 0, -0.9776897119f, -0.2099250524f, -0.007368825344f, 0, 0.7736893337f, 0.5734244712f, 0.2694238123f, 0, -0.6095087895f, 0.4995678998f, 0.6155736747f, 0, 0.5794535482f, 0.7434546771f, 0.3339292269f, 0, -0.8226211154f, 0.08142581855f, 0.5627293636f, 0, -0.510385483f, 0.4703667658f, 0.7199039967f, 0,
  2174. -0.5764971849f, -0.07231656274f, -0.8138926898f, 0, 0.7250628871f, 0.3949971505f, -0.5641463116f, 0, -0.1525424005f, 0.4860840828f, -0.8604958341f, 0, -0.5550976208f, -0.4957820792f, 0.667882296f, 0, -0.1883614327f, 0.9145869398f, 0.357841725f, 0, 0.7625556724f, -0.5414408243f, -0.3540489801f, 0, -0.5870231946f, -0.3226498013f, -0.7424963803f, 0, 0.3051124198f, 0.2262544068f, -0.9250488391f, 0,
  2175. 0.6379576059f, 0.577242424f, -0.5097070502f, 0, -0.5966775796f, 0.1454852398f, -0.7891830656f, 0, -0.658330573f, 0.6555487542f, -0.3699414651f, 0, 0.7434892426f, 0.2351084581f, 0.6260573129f, 0, 0.5562114096f, 0.8264360377f, -0.0873632843f, 0, -0.3028940016f, -0.8251527185f, 0.4768419182f, 0, 0.1129343818f, -0.985888439f, -0.1235710781f, 0, 0.5937652891f, -0.5896813806f, 0.5474656618f, 0,
  2176. 0.6757964092f, -0.5835758614f, -0.4502648413f, 0, 0.7242302609f, -0.1152719764f, 0.6798550586f, 0, -0.9511914166f, 0.0753623979f, -0.2992580792f, 0, 0.2539470961f, -0.1886339355f, 0.9486454084f, 0, 0.571433621f, -0.1679450851f, -0.8032795685f, 0, -0.06778234979f, 0.3978269256f, 0.9149531629f, 0, 0.6074972649f, 0.733060024f, -0.3058922593f, 0, -0.5435478392f, 0.1675822484f, 0.8224791405f, 0,
  2177. -0.5876678086f, -0.3380045064f, -0.7351186982f, 0, -0.7967562402f, 0.04097822706f, -0.6029098428f, 0, -0.1996350917f, 0.8706294745f, 0.4496111079f, 0, -0.02787660336f, -0.9106232682f, -0.4122962022f, 0, -0.7797625996f, -0.6257634692f, 0.01975775581f, 0, -0.5211232846f, 0.7401644346f, -0.4249554471f, 0, 0.8575424857f, 0.4053272873f, -0.3167501783f, 0, 0.1045223322f, 0.8390195772f, -0.5339674439f, 0,
  2178. 0.3501822831f, 0.9242524096f, -0.1520850155f, 0, 0.1987849858f, 0.07647613266f, 0.9770547224f, 0, 0.7845996363f, 0.6066256811f, -0.1280964233f, 0, 0.09006737436f, -0.9750989929f, -0.2026569073f, 0, -0.8274343547f, -0.542299559f, 0.1458203587f, 0, -0.3485797732f, -0.415802277f, 0.840000362f, 0, -0.2471778936f, -0.7304819962f, -0.6366310879f, 0, -0.3700154943f, 0.8577948156f, 0.3567584454f, 0,
  2179. 0.5913394901f, -0.548311967f, -0.5913303597f, 0, 0.1204873514f, -0.7626472379f, -0.6354935001f, 0, 0.616959265f, 0.03079647928f, 0.7863922953f, 0, 0.1258156836f, -0.6640829889f, -0.7369967419f, 0, -0.6477565124f, -0.1740147258f, -0.7417077429f, 0, 0.6217889313f, -0.7804430448f, -0.06547655076f, 0, 0.6589943422f, -0.6096987708f, 0.4404473475f, 0, -0.2689837504f, -0.6732403169f, -0.6887635427f, 0,
  2180. -0.3849775103f, 0.5676542638f, 0.7277093879f, 0, 0.5754444408f, 0.8110471154f, -0.1051963504f, 0, 0.9141593684f, 0.3832947817f, 0.131900567f, 0, -0.107925319f, 0.9245493968f, 0.3654593525f, 0, 0.377977089f, 0.3043148782f, 0.8743716458f, 0, -0.2142885215f, -0.8259286236f, 0.5214617324f, 0, 0.5802544474f, 0.4148098596f, -0.7008834116f, 0, -0.1982660881f, 0.8567161266f, -0.4761596756f, 0,
  2181. -0.03381553704f, 0.3773180787f, -0.9254661404f, 0, -0.6867922841f, -0.6656597827f, 0.2919133642f, 0, 0.7731742607f, -0.2875793547f, -0.5652430251f, 0, -0.09655941928f, 0.9193708367f, -0.3813575004f, 0, 0.2715702457f, -0.9577909544f, -0.09426605581f, 0, 0.2451015704f, -0.6917998565f, -0.6792188003f, 0, 0.977700782f, -0.1753855374f, 0.1155036542f, 0, -0.5224739938f, 0.8521606816f, 0.02903615945f, 0,
  2182. -0.7734880599f, -0.5261292347f, 0.3534179531f, 0, -0.7134492443f, -0.269547243f, 0.6467878011f, 0, 0.1644037271f, 0.5105846203f, -0.8439637196f, 0, 0.6494635788f, 0.05585611296f, 0.7583384168f, 0, -0.4711970882f, 0.5017280509f, -0.7254255765f, 0, -0.6335764307f, -0.2381686273f, -0.7361091029f, 0, -0.9021533097f, -0.270947803f, -0.3357181763f, 0, -0.3793711033f, 0.872258117f, 0.3086152025f, 0,
  2183. -0.6855598966f, -0.3250143309f, 0.6514394162f, 0, 0.2900942212f, -0.7799057743f, -0.5546100667f, 0, -0.2098319339f, 0.85037073f, 0.4825351604f, 0, -0.4592603758f, 0.6598504336f, -0.5947077538f, 0, 0.8715945488f, 0.09616365406f, -0.4807031248f, 0, -0.6776666319f, 0.7118504878f, -0.1844907016f, 0, 0.7044377633f, 0.312427597f, 0.637304036f, 0, -0.7052318886f, -0.2401093292f, -0.6670798253f, 0,
  2184. 0.081921007f, -0.7207336136f, -0.6883545647f, 0, -0.6993680906f, -0.5875763221f, -0.4069869034f, 0, -0.1281454481f, 0.6419895885f, 0.7559286424f, 0, -0.6337388239f, -0.6785471501f, -0.3714146849f, 0, 0.5565051903f, -0.2168887573f, -0.8020356851f, 0, -0.5791554484f, 0.7244372011f, -0.3738578718f, 0, 0.1175779076f, -0.7096451073f, 0.6946792478f, 0, -0.6134619607f, 0.1323631078f, 0.7785527795f, 0,
  2185. 0.6984635305f, -0.02980516237f, -0.715024719f, 0, 0.8318082963f, -0.3930171956f, 0.3919597455f, 0, 0.1469576422f, 0.05541651717f, -0.9875892167f, 0, 0.708868575f, -0.2690503865f, 0.6520101478f, 0, 0.2726053183f, 0.67369766f, -0.68688995f, 0, -0.6591295371f, 0.3035458599f, -0.6880466294f, 0, 0.4815131379f, -0.7528270071f, 0.4487723203f, 0, 0.9430009463f, 0.1675647412f, -0.2875261255f, 0,
  2186. 0.434802957f, 0.7695304522f, -0.4677277752f, 0, 0.3931996188f, 0.594473625f, 0.7014236729f, 0, 0.7254336655f, -0.603925654f, 0.3301814672f, 0, 0.7590235227f, -0.6506083235f, 0.02433313207f, 0, -0.8552768592f, -0.3430042733f, 0.3883935666f, 0, -0.6139746835f, 0.6981725247f, 0.3682257648f, 0, -0.7465905486f, -0.5752009504f, 0.3342849376f, 0, 0.5730065677f, 0.810555537f, -0.1210916791f, 0,
  2187. -0.9225877367f, -0.3475211012f, -0.167514036f, 0, -0.7105816789f, -0.4719692027f, -0.5218416899f, 0, -0.08564609717f, 0.3583001386f, 0.929669703f, 0, -0.8279697606f, -0.2043157126f, 0.5222271202f, 0, 0.427944023f, 0.278165994f, 0.8599346446f, 0, 0.5399079671f, -0.7857120652f, -0.3019204161f, 0, 0.5678404253f, -0.5495413974f, -0.6128307303f, 0, -0.9896071041f, 0.1365639107f, -0.04503418428f, 0,
  2188. -0.6154342638f, -0.6440875597f, 0.4543037336f, 0, 0.1074204368f, -0.7946340692f, 0.5975094525f, 0, -0.3595449969f, -0.8885529948f, 0.28495784f, 0, -0.2180405296f, 0.1529888965f, 0.9638738118f, 0, -0.7277432317f, -0.6164050508f, -0.3007234646f, 0, 0.7249729114f, -0.00669719484f, 0.6887448187f, 0, -0.5553659455f, -0.5336586252f, 0.6377908264f, 0, 0.5137558015f, 0.7976208196f, -0.3160000073f, 0,
  2189. -0.3794024848f, 0.9245608561f, -0.03522751494f, 0, 0.8229248658f, 0.2745365933f, -0.4974176556f, 0, -0.5404114394f, 0.6091141441f, 0.5804613989f, 0, 0.8036581901f, -0.2703029469f, 0.5301601931f, 0, 0.6044318879f, 0.6832968393f, 0.4095943388f, 0, 0.06389988817f, 0.9658208605f, -0.2512108074f, 0, 0.1087113286f, 0.7402471173f, -0.6634877936f, 0, -0.713427712f, -0.6926784018f, 0.1059128479f, 0,
  2190. 0.6458897819f, -0.5724548511f, -0.5050958653f, 0, -0.6553931414f, 0.7381471625f, 0.159995615f, 0, 0.3910961323f, 0.9188871375f, -0.05186755998f, 0, -0.4879022471f, -0.5904376907f, 0.6429111375f, 0, 0.6014790094f, 0.7707441366f, -0.2101820095f, 0, -0.5677173047f, 0.7511360995f, 0.3368851762f, 0, 0.7858573506f, 0.226674665f, 0.5753666838f, 0, -0.4520345543f, -0.604222686f, -0.6561857263f, 0,
  2191. 0.002272116345f, 0.4132844051f, -0.9105991643f, 0, -0.5815751419f, -0.5162925989f, 0.6286591339f, 0, -0.03703704785f, 0.8273785755f, 0.5604221175f, 0, -0.5119692504f, 0.7953543429f, -0.3244980058f, 0, -0.2682417366f, -0.9572290247f, -0.1084387619f, 0, -0.2322482736f, -0.9679131102f, -0.09594243324f, 0, 0.3554328906f, -0.8881505545f, 0.2913006227f, 0, 0.7346520519f, -0.4371373164f, 0.5188422971f, 0,
  2192. 0.9985120116f, 0.04659011161f, -0.02833944577f, 0, -0.3727687496f, -0.9082481361f, 0.1900757285f, 0, 0.91737377f, -0.3483642108f, 0.1925298489f, 0, 0.2714911074f, 0.4147529736f, -0.8684886582f, 0, 0.5131763485f, -0.7116334161f, 0.4798207128f, 0, -0.8737353606f, 0.18886992f, -0.4482350644f, 0, 0.8460043821f, -0.3725217914f, 0.3814499973f, 0, 0.8978727456f, -0.1780209141f, -0.4026575304f, 0,
  2193. 0.2178065647f, -0.9698322841f, -0.1094789531f, 0, -0.1518031304f, -0.7788918132f, -0.6085091231f, 0, -0.2600384876f, -0.4755398075f, -0.8403819825f, 0, 0.572313509f, -0.7474340931f, -0.3373418503f, 0, -0.7174141009f, 0.1699017182f, -0.6756111411f, 0, -0.684180784f, 0.02145707593f, -0.7289967412f, 0, -0.2007447902f, 0.06555605789f, -0.9774476623f, 0, -0.1148803697f, -0.8044887315f, 0.5827524187f, 0,
  2194. -0.7870349638f, 0.03447489231f, 0.6159443543f, 0, -0.2015596421f, 0.6859872284f, 0.6991389226f, 0, -0.08581082512f, -0.10920836f, -0.9903080513f, 0, 0.5532693395f, 0.7325250401f, -0.396610771f, 0, -0.1842489331f, -0.9777375055f, -0.1004076743f, 0, 0.0775473789f, -0.9111505856f, 0.4047110257f, 0, 0.1399838409f, 0.7601631212f, -0.6344734459f, 0, 0.4484419361f, -0.845289248f, 0.2904925424f, 0
  2195. };
  2196. #endif