JNoise.cpp 19 KB

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