JNoise.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. JSON::Validator::JSONValidator* JNoise::getValidator(bool optional)
  289. {
  290. auto validator1 = JSON::Validator::JSONValidator::buildForObject();
  291. auto validator2 = JSON::Validator::JSONValidator::buildForObject();
  292. auto validator3 = JSON::Validator::JSONValidator::buildForObject();
  293. auto validator4 = JSON::Validator::JSONValidator::buildForObject();
  294. auto validator5 = JSON::Validator::JSONValidator::buildForObject();
  295. auto validator6 = JSON::Validator::JSONValidator::buildForObject();
  296. auto validator7 = JSON::Validator::JSONValidator::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. JSON::Validator::JSONValidator* refs
  308. = JSON::Validator::JSONValidator::buildForOneOf()
  309. ->typeSpecifiedByAttribute("type")
  310. ->addAcceptedType(
  311. JSON::Validator::JSONValidator::buildForObjectReference(
  312. "jn_random"))
  313. ->addAcceptedType(
  314. JSON::Validator::JSONValidator::buildForObjectReference(
  315. "jn_factorize"))
  316. ->addAcceptedType(
  317. JSON::Validator::JSONValidator::buildForObjectReference(
  318. "jn_multiply"))
  319. ->addAcceptedType(
  320. JSON::Validator::JSONValidator::buildForObjectReference(
  321. "jn_negate"))
  322. ->addAcceptedType(
  323. JSON::Validator::JSONValidator::buildForObjectReference(
  324. "jn_flatten"))
  325. ->addAcceptedType(
  326. JSON::Validator::JSONValidator::buildForObjectReference(
  327. "jn_scale"))
  328. ->addAcceptedType(
  329. JSON::Validator::JSONValidator::buildForObjectReference(
  330. "jn_fastNoiseLite"))
  331. ->finishOneOf();
  332. return JSON::Validator::JSONValidator::buildForOneOf()
  333. ->typeSpecifiedByAttribute("type")
  334. ->addAcceptedType(validator1->setObjectReferenceId("jn_random")
  335. ->withRequiredString("type")
  336. ->withExactMatch("random")
  337. ->finishString()
  338. ->withRequiredAttribute("seed",
  339. Game::INSTANCE->zTypeRegistry()
  340. ->getValidator<JFloatExpression>())
  341. ->finishObject())
  342. ->addAcceptedType(validator2->setObjectReferenceId("jn_factorize")
  343. ->withRequiredString("type")
  344. ->withExactMatch("factorize")
  345. ->finishString()
  346. ->withRequiredNumber("factorA")
  347. ->whichIsGreaterThen(0)
  348. ->whichIsLessThen(1)
  349. ->finishNumber()
  350. ->withRequiredAttribute("noiseA",
  351. dynamic_cast<JSON::Validator::JSONValidator*>(
  352. refs->getThis()))
  353. ->withRequiredAttribute("noiseB",
  354. dynamic_cast<JSON::Validator::JSONValidator*>(
  355. refs->getThis()))
  356. ->finishObject())
  357. ->addAcceptedType(validator3->setObjectReferenceId("jn_multiply")
  358. ->withRequiredString("type")
  359. ->withExactMatch("multiply")
  360. ->finishString()
  361. ->withRequiredAttribute("base",
  362. dynamic_cast<JSON::Validator::JSONValidator*>(
  363. refs->getThis()))
  364. ->withRequiredAttribute("multiplier",
  365. dynamic_cast<JSON::Validator::JSONValidator*>(
  366. refs->getThis()))
  367. ->finishObject())
  368. ->addAcceptedType(validator4->setObjectReferenceId("jn_negate")
  369. ->withRequiredString("type")
  370. ->withExactMatch("negate")
  371. ->finishString()
  372. ->withRequiredAttribute("noise",
  373. dynamic_cast<JSON::Validator::JSONValidator*>(
  374. refs->getThis()))
  375. ->finishObject())
  376. ->addAcceptedType(validator5->setObjectReferenceId("jn_flatten")
  377. ->withRequiredString("type")
  378. ->withExactMatch("flatten")
  379. ->finishString()
  380. ->withRequiredAttribute("noise",
  381. dynamic_cast<JSON::Validator::JSONValidator*>(
  382. refs->getThis()))
  383. ->withRequiredNumber("factor")
  384. ->withDefault(1.0)
  385. ->finishNumber()
  386. ->withRequiredNumber("addition")
  387. ->withDefault(0.0)
  388. ->finishNumber()
  389. ->finishObject())
  390. ->addAcceptedType(validator6->setObjectReferenceId("jn_scale")
  391. ->withRequiredString("type")
  392. ->withExactMatch("scale")
  393. ->finishString()
  394. ->withRequiredAttribute("noise",
  395. dynamic_cast<JSON::Validator::JSONValidator*>(
  396. refs->getThis()))
  397. ->withRequiredNumber("factor")
  398. ->finishNumber()
  399. ->finishObject())
  400. ->addAcceptedType(
  401. validator7->setObjectReferenceId("jn_fastNoiseLite")
  402. ->withRequiredString("type")
  403. ->whichIsOneOf({"Cellular",
  404. "ValueCubic",
  405. "Perlin",
  406. "OpenSimplex2S",
  407. "OpenSimplex2",
  408. "Value"})
  409. ->finishString()
  410. ->withRequiredAttribute("seed",
  411. Game::INSTANCE->zTypeRegistry()
  412. ->getValidator<JFloatExpression>())
  413. ->withRequiredString("rotationType3D")
  414. ->whichIsOptional()
  415. ->whichIsOneOf({"None", "ImproveXYPlanes", "ImproveXZPlanes"})
  416. ->finishString()
  417. ->withRequiredNumber("frequency")
  418. ->whichIsOptional()
  419. ->finishNumber()
  420. ->withRequiredString("fractalType")
  421. ->whichIsOptional()
  422. ->whichIsOneOf({"None",
  423. "FBm",
  424. "Ridged",
  425. "PingPong",
  426. "DomainWarpProgressive",
  427. "DomainWarpIndependent"})
  428. ->finishString()
  429. ->withRequiredNumber("fractalOctaves")
  430. ->whichIsOptional()
  431. ->finishNumber()
  432. ->withRequiredNumber("fractalLacunarity")
  433. ->whichIsOptional()
  434. ->finishNumber()
  435. ->withRequiredNumber("fractalGain")
  436. ->whichIsOptional()
  437. ->finishNumber()
  438. ->withRequiredString("cellularDistanceFunction")
  439. ->whichIsOptional()
  440. ->whichIsOneOf(
  441. {"Hybrid", "Manhattan", "EuclideanSq", "Euclidean"})
  442. ->finishString()
  443. ->withRequiredString("cellularReturnType")
  444. ->whichIsOptional()
  445. ->whichIsOneOf({"CellValue",
  446. "Distance",
  447. "Distance2",
  448. "Distance2Add",
  449. "Distance2Sub",
  450. "Distance2Mul",
  451. "Distance2Div"})
  452. ->finishString()
  453. ->withRequiredNumber("cellularJitter")
  454. ->whichIsOptional()
  455. ->finishNumber()
  456. ->withRequiredString("domainWarpType")
  457. ->whichIsOptional()
  458. ->whichIsOneOf(
  459. {"BasicGrid", "OpenSimplex2", "OpenSimplex2Reduced"})
  460. ->finishString()
  461. ->withRequiredNumber("domainWarpAmp")
  462. ->whichIsOptional()
  463. ->finishNumber()
  464. ->withRequiredNumber("multiplier")
  465. ->whichIsOptional()
  466. ->whichIsGreaterThen(0)
  467. ->finishNumber()
  468. ->finishObject())
  469. ->finishOneOf();
  470. }