JNoise.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "JNoise.h"
  2. #include "FactorizeNoise.h"
  3. #include "FastNoiseWrapper.h"
  4. #include "MultiplyNoise.h"
  5. #include "RandNoise.h"
  6. #include "NegateNoise.h"
  7. #include "FlattenNoise.h"
  8. #include "ScaleNoise.h"
  9. using namespace Framework;
  10. Noise* JNoise::parseNoise(JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
  11. {
  12. Text type = zConfig->asObject()->zValue("type")->asString()->getString();
  13. if (type.istGleich("random"))
  14. {
  15. JFloatExpression* seedExpression
  16. = JExpressionParser::parseFloatExpression(
  17. zConfig->asObject()->zValue("seed"));
  18. float seed = seedExpression->getValue(zMemory);
  19. return new RandNoise((int)(round(seed)));
  20. }
  21. else if (type.istGleich("factorize"))
  22. {
  23. double factor
  24. = zConfig->asObject()->zValue("factorA")->asNumber()->getNumber();
  25. Noise* a = parseNoise(zConfig->asObject()->zValue("noiseA"), zMemory);
  26. Noise* b = parseNoise(zConfig->asObject()->zValue("noiseB"), zMemory);
  27. return new FactorizeNoise(a, b, factor);
  28. }
  29. else if (type.istGleich("multiply"))
  30. {
  31. Noise* a = parseNoise(zConfig->asObject()->zValue("base"), zMemory);
  32. Noise* b
  33. = parseNoise(zConfig->asObject()->zValue("multiplier"), zMemory);
  34. return new MultiplyNoise(a, b);
  35. }
  36. else if (type.istGleich("negate"))
  37. {
  38. Noise* a = parseNoise(zConfig->asObject()->zValue("noise"), zMemory);
  39. return new NegateNoise(a);
  40. }
  41. else if (type.istGleich("flatten"))
  42. {
  43. Noise* a = parseNoise(zConfig->asObject()->zValue("noise"), zMemory);
  44. double factor
  45. = zConfig->asObject()->zValue("factor")->asNumber()->getNumber();
  46. double addition
  47. = zConfig->asObject()->zValue("addition")->asNumber()->getNumber();
  48. return new FlattenNoise(a, factor, addition);
  49. }
  50. else if (type.istGleich("scale"))
  51. {
  52. Noise* a = parseNoise(zConfig->asObject()->zValue("noise"), zMemory);
  53. double factor
  54. = zConfig->asObject()->zValue("factor")->asNumber()->getNumber();
  55. return new ScaleNoise(a, factor);
  56. }
  57. else
  58. {
  59. JFloatExpression* seedExpression
  60. = JExpressionParser::parseFloatExpression(
  61. zConfig->asObject()->zValue("seed"));
  62. float seed = seedExpression->getValue(zMemory);
  63. FastNoiseLite* noise = new FastNoiseLite((int)(round(seed)));
  64. if (type.istGleich("Cellular"))
  65. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Cellular);
  66. else if (type.istGleich("ValueCubic"))
  67. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  68. else if (type.istGleich("Perlin"))
  69. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Perlin);
  70. else if (type.istGleich("OpenSimplex2S"))
  71. noise->SetNoiseType(
  72. FastNoiseLite::NoiseType::NoiseType_OpenSimplex2S);
  73. else if (type.istGleich("OpenSimplex2"))
  74. noise->SetNoiseType(
  75. FastNoiseLite::NoiseType::NoiseType_OpenSimplex2);
  76. else if (type.istGleich("Value"))
  77. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Value);
  78. if (zConfig->asObject()->hasValue("rotationType3D"))
  79. {
  80. Text value = zConfig->asObject()
  81. ->zValue("rotationType3D")
  82. ->asString()
  83. ->getString();
  84. if (value.istGleich("None"))
  85. {
  86. noise->SetRotationType3D(
  87. FastNoiseLite::RotationType3D::RotationType3D_None);
  88. }
  89. else if (value.istGleich("ImproveXYPlanes"))
  90. {
  91. noise->SetRotationType3D(FastNoiseLite::RotationType3D::
  92. RotationType3D_ImproveXYPlanes);
  93. }
  94. else if (value.istGleich("ImproveXZPlanes"))
  95. {
  96. noise->SetRotationType3D(FastNoiseLite::RotationType3D::
  97. RotationType3D_ImproveXZPlanes);
  98. }
  99. }
  100. if (zConfig->asObject()->hasValue("frequency"))
  101. {
  102. noise->SetFrequency((float)zConfig->asObject()
  103. ->zValue("frequency")
  104. ->asNumber()
  105. ->getNumber());
  106. }
  107. if (zConfig->asObject()->hasValue("fractalType"))
  108. {
  109. Text value = zConfig->asObject()
  110. ->zValue("fractalType")
  111. ->asString()
  112. ->getString();
  113. if (value.istGleich("None"))
  114. {
  115. noise->SetFractalType(
  116. FastNoiseLite::FractalType::FractalType_None);
  117. }
  118. else if (value.istGleich("FBm"))
  119. {
  120. noise->SetFractalType(
  121. FastNoiseLite::FractalType::FractalType_FBm);
  122. }
  123. else if (value.istGleich("Ridged"))
  124. {
  125. noise->SetFractalType(
  126. FastNoiseLite::FractalType::FractalType_Ridged);
  127. }
  128. else if (value.istGleich("PingPong"))
  129. {
  130. noise->SetFractalType(
  131. FastNoiseLite::FractalType::FractalType_PingPong);
  132. }
  133. else if (value.istGleich("DomainWarpProgressive"))
  134. {
  135. noise->SetFractalType(FastNoiseLite::FractalType::
  136. FractalType_DomainWarpProgressive);
  137. }
  138. else if (value.istGleich("DomainWarpIndependent"))
  139. {
  140. noise->SetFractalType(FastNoiseLite::FractalType::
  141. FractalType_DomainWarpIndependent);
  142. }
  143. }
  144. if (zConfig->asObject()->hasValue("fractalOctaves"))
  145. {
  146. noise->SetFractalOctaves((int)round(zConfig->asObject()
  147. ->zValue("fractalOctaves")
  148. ->asNumber()
  149. ->getNumber()));
  150. }
  151. if (zConfig->asObject()->hasValue("fractalLacunarity"))
  152. {
  153. noise->SetFractalLacunarity((float)zConfig->asObject()
  154. ->zValue("fractalLacunarity")
  155. ->asNumber()
  156. ->getNumber());
  157. }
  158. if (zConfig->asObject()->hasValue("fractalGain"))
  159. {
  160. noise->SetFractalGain((float)zConfig->asObject()
  161. ->zValue("fractalGain")
  162. ->asNumber()
  163. ->getNumber());
  164. }
  165. if (zConfig->asObject()->hasValue("cellularDistanceFunction"))
  166. {
  167. Text value = zConfig->asObject()
  168. ->zValue("cellularDistanceFunction")
  169. ->asString()
  170. ->getString();
  171. if (value.istGleich("Hybrid"))
  172. {
  173. noise->SetCellularDistanceFunction(
  174. FastNoiseLite::CellularDistanceFunction::
  175. CellularDistanceFunction_Hybrid);
  176. }
  177. else if (value.istGleich("Manhattan"))
  178. {
  179. noise->SetCellularDistanceFunction(
  180. FastNoiseLite::CellularDistanceFunction::
  181. CellularDistanceFunction_Manhattan);
  182. }
  183. else if (value.istGleich("EuclideanSq"))
  184. {
  185. noise->SetCellularDistanceFunction(
  186. FastNoiseLite::CellularDistanceFunction::
  187. CellularDistanceFunction_EuclideanSq);
  188. }
  189. else if (value.istGleich("Euclidean"))
  190. {
  191. noise->SetCellularDistanceFunction(
  192. FastNoiseLite::CellularDistanceFunction::
  193. CellularDistanceFunction_Euclidean);
  194. }
  195. }
  196. if (zConfig->asObject()->hasValue("cellularReturnType"))
  197. {
  198. Text value = zConfig->asObject()
  199. ->zValue("cellularReturnType")
  200. ->asString()
  201. ->getString();
  202. if (value.istGleich("CellValue"))
  203. {
  204. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  205. CellularReturnType_CellValue);
  206. }
  207. else if (value.istGleich("Distance"))
  208. {
  209. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  210. CellularReturnType_Distance);
  211. }
  212. else if (value.istGleich("Distance2"))
  213. {
  214. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  215. CellularReturnType_Distance2);
  216. }
  217. else if (value.istGleich("Distance2Add"))
  218. {
  219. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  220. CellularReturnType_Distance2Add);
  221. }
  222. else if (value.istGleich("Distance2Sub"))
  223. {
  224. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  225. CellularReturnType_Distance2Sub);
  226. }
  227. else if (value.istGleich("Distance2Mul"))
  228. {
  229. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  230. CellularReturnType_Distance2Mul);
  231. }
  232. else if (value.istGleich("Distance2Div"))
  233. {
  234. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  235. CellularReturnType_Distance2Div);
  236. }
  237. }
  238. if (zConfig->asObject()->hasValue("cellularJitter"))
  239. {
  240. noise->SetCellularJitter((float)zConfig->asObject()
  241. ->zValue("cellularJitter")
  242. ->asNumber()
  243. ->getNumber());
  244. }
  245. if (zConfig->asObject()->hasValue("domainWarpType"))
  246. {
  247. Text value = zConfig->asObject()
  248. ->zValue("domainWarpType")
  249. ->asString()
  250. ->getString();
  251. if (value.istGleich("OpenSimplex2Reduced"))
  252. {
  253. noise->SetDomainWarpType(FastNoiseLite::DomainWarpType::
  254. DomainWarpType_OpenSimplex2Reduced);
  255. }
  256. else if (value.istGleich("OpenSimplex2"))
  257. {
  258. noise->SetDomainWarpType(
  259. FastNoiseLite::DomainWarpType::DomainWarpType_OpenSimplex2);
  260. }
  261. else if (value.istGleich("BasicGrid"))
  262. {
  263. noise->SetDomainWarpType(
  264. FastNoiseLite::DomainWarpType::DomainWarpType_BasicGrid);
  265. }
  266. }
  267. if (zConfig->asObject()->hasValue("domainWarpAmp"))
  268. {
  269. noise->SetDomainWarpAmp((float)zConfig->asObject()
  270. ->zValue("domainWarpAmp")
  271. ->asNumber()
  272. ->getNumber());
  273. }
  274. FastNoiseWrapper* result
  275. = new FastNoiseWrapper(noise, (int)(round(seed)));
  276. if (zConfig->asObject()->hasValue("multiplier"))
  277. {
  278. result->setMultiplier((float)zConfig->asObject()
  279. ->zValue("multiplier")
  280. ->asNumber()
  281. ->getNumber());
  282. }
  283. return result;
  284. }
  285. return 0;
  286. }
  287. JSON::Validator::JSONValidator* JNoise::getValidator(bool optional)
  288. {
  289. auto validator1 = JSON::Validator::JSONValidator::buildForObject();
  290. auto validator2 = JSON::Validator::JSONValidator::buildForObject();
  291. auto validator3 = JSON::Validator::JSONValidator::buildForObject();
  292. auto validator4 = JSON::Validator::JSONValidator::buildForObject();
  293. auto validator5 = JSON::Validator::JSONValidator::buildForObject();
  294. auto validator6 = JSON::Validator::JSONValidator::buildForObject();
  295. auto validator7 = JSON::Validator::JSONValidator::buildForObject();
  296. if (optional)
  297. {
  298. validator1 = validator1->whichIsOptional();
  299. validator2 = validator2->whichIsOptional();
  300. validator3 = validator3->whichIsOptional();
  301. validator4 = validator4->whichIsOptional();
  302. validator5 = validator5->whichIsOptional();
  303. validator6 = validator6->whichIsOptional();
  304. validator7 = validator7->whichIsOptional();
  305. }
  306. JSON::Validator::JSONValidator* refs
  307. = JSON::Validator::JSONValidator::buildForOneOf()
  308. ->typeSpecifiedByAttribute("type")
  309. ->addAcceptedType(
  310. JSON::Validator::JSONValidator::buildForObjectReference(
  311. "jn_random"))
  312. ->addAcceptedType(
  313. JSON::Validator::JSONValidator::buildForObjectReference(
  314. "jn_factorize"))
  315. ->addAcceptedType(
  316. JSON::Validator::JSONValidator::buildForObjectReference(
  317. "jn_multiply"))
  318. ->addAcceptedType(
  319. JSON::Validator::JSONValidator::buildForObjectReference(
  320. "jn_negate"))
  321. ->addAcceptedType(
  322. JSON::Validator::JSONValidator::buildForObjectReference(
  323. "jn_flatten"))
  324. ->addAcceptedType(
  325. JSON::Validator::JSONValidator::buildForObjectReference(
  326. "jn_scale"))
  327. ->addAcceptedType(
  328. JSON::Validator::JSONValidator::buildForObjectReference(
  329. "jn_fastNoiseLite"))
  330. ->finishOneOf();
  331. return JSON::Validator::JSONValidator::buildForOneOf()
  332. ->typeSpecifiedByAttribute("type")
  333. ->addAcceptedType(validator1->setObjectReferenceId("jn_random")
  334. ->withRequiredString("type")
  335. ->withExactMatch("random")
  336. ->finishString()
  337. ->withRequiredAttribute("seed",
  338. JExpressionParser::getFloatValidator())
  339. ->finishObject())
  340. ->addAcceptedType(validator2->setObjectReferenceId("jn_factorize")
  341. ->withRequiredString("type")
  342. ->withExactMatch("factorize")
  343. ->finishString()
  344. ->withRequiredNumber("factorA")
  345. ->whichIsGreaterThen(0)
  346. ->whichIsLessThen(1)
  347. ->finishNumber()
  348. ->withRequiredAttribute("noiseA",
  349. dynamic_cast<JSON::Validator::JSONValidator*>(
  350. refs->getThis()))
  351. ->withRequiredAttribute("noiseB",
  352. dynamic_cast<JSON::Validator::JSONValidator*>(
  353. refs->getThis()))
  354. ->finishObject())
  355. ->addAcceptedType(validator3->setObjectReferenceId("jn_multiply")
  356. ->withRequiredString("type")
  357. ->withExactMatch("multiply")
  358. ->finishString()
  359. ->withRequiredAttribute("base",
  360. dynamic_cast<JSON::Validator::JSONValidator*>(
  361. refs->getThis()))
  362. ->withRequiredAttribute("multiplier",
  363. dynamic_cast<JSON::Validator::JSONValidator*>(
  364. refs->getThis()))
  365. ->finishObject())
  366. ->addAcceptedType(validator4->setObjectReferenceId("jn_negate")
  367. ->withRequiredString("type")
  368. ->withExactMatch("negate")
  369. ->finishString()
  370. ->withRequiredAttribute("noise",
  371. dynamic_cast<JSON::Validator::JSONValidator*>(
  372. refs->getThis()))
  373. ->finishObject())
  374. ->addAcceptedType(validator5->setObjectReferenceId("jn_flatten")
  375. ->withRequiredString("type")
  376. ->withExactMatch("flatten")
  377. ->finishString()
  378. ->withRequiredAttribute("noise",
  379. dynamic_cast<JSON::Validator::JSONValidator*>(
  380. refs->getThis()))
  381. ->withRequiredNumber("factor")
  382. ->withDefault(1.0)
  383. ->finishNumber()
  384. ->withRequiredNumber("addition")
  385. ->withDefault(0.0)
  386. ->finishNumber()
  387. ->finishObject())
  388. ->addAcceptedType(validator6->setObjectReferenceId("jn_scale")
  389. ->withRequiredString("type")
  390. ->withExactMatch("scale")
  391. ->finishString()
  392. ->withRequiredAttribute("noise",
  393. dynamic_cast<JSON::Validator::JSONValidator*>(
  394. refs->getThis()))
  395. ->withRequiredNumber("factor")
  396. ->finishNumber()
  397. ->finishObject())
  398. ->addAcceptedType(
  399. validator7->setObjectReferenceId("jn_fastNoiseLite")
  400. ->withRequiredString("type")
  401. ->whichIsOneOf({"Cellular",
  402. "ValueCubic",
  403. "Perlin",
  404. "OpenSimplex2S",
  405. "OpenSimplex2",
  406. "Value"})
  407. ->finishString()
  408. ->withRequiredAttribute(
  409. "seed", JExpressionParser::getFloatValidator())
  410. ->withRequiredString("rotationType3D")
  411. ->whichIsOptional()
  412. ->whichIsOneOf({"None", "ImproveXYPlanes", "ImproveXZPlanes"})
  413. ->finishString()
  414. ->withRequiredNumber("frequency")
  415. ->whichIsOptional()
  416. ->finishNumber()
  417. ->withRequiredString("fractalType")
  418. ->whichIsOptional()
  419. ->whichIsOneOf({"None",
  420. "FBm",
  421. "Ridged",
  422. "PingPong",
  423. "DomainWarpProgressive",
  424. "DomainWarpIndependent"})
  425. ->finishString()
  426. ->withRequiredNumber("fractalOctaves")
  427. ->whichIsOptional()
  428. ->finishNumber()
  429. ->withRequiredNumber("fractalLacunarity")
  430. ->whichIsOptional()
  431. ->finishNumber()
  432. ->withRequiredNumber("fractalGain")
  433. ->whichIsOptional()
  434. ->finishNumber()
  435. ->withRequiredString("cellularDistanceFunction")
  436. ->whichIsOptional()
  437. ->whichIsOneOf(
  438. {"Hybrid", "Manhattan", "EuclideanSq", "Euclidean"})
  439. ->finishString()
  440. ->withRequiredString("cellularReturnType")
  441. ->whichIsOptional()
  442. ->whichIsOneOf({"CellValue",
  443. "Distance",
  444. "Distance2",
  445. "Distance2Add",
  446. "Distance2Sub",
  447. "Distance2Mul",
  448. "Distance2Div"})
  449. ->finishString()
  450. ->withRequiredNumber("cellularJitter")
  451. ->whichIsOptional()
  452. ->finishNumber()
  453. ->withRequiredString("domainWarpType")
  454. ->whichIsOptional()
  455. ->whichIsOneOf(
  456. {"BasicGrid", "OpenSimplex2", "OpenSimplex2Reduced"})
  457. ->finishString()
  458. ->withRequiredNumber("domainWarpAmp")
  459. ->whichIsOptional()
  460. ->finishNumber()
  461. ->withRequiredNumber("multiplier")
  462. ->whichIsOptional()
  463. ->whichIsGreaterThen(0)
  464. ->finishNumber()
  465. ->finishObject())
  466. ->finishOneOf();
  467. }