JNoise.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include "JNoise.h"
  2. #include "FastNoiseWrapper.h"
  3. #include "RandNoise.h"
  4. using namespace Framework;
  5. Noise* JNoise::parseNoisse(JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
  6. {
  7. Text type = zConfig->asObject()->zValue("type")->asString()->getString();
  8. if (type.istGleich("random"))
  9. {
  10. JFloatExpression* seedExpression
  11. = JExpressionParser::parseFloatExpression(
  12. zConfig->asObject()->zValue("seed"));
  13. float seed = seedExpression->getValue(zMemory);
  14. return new RandNoise((int)(seed + 0.5f));
  15. }
  16. else
  17. {
  18. JFloatExpression* seedExpression
  19. = JExpressionParser::parseFloatExpression(
  20. zConfig->asObject()->zValue("seed"));
  21. float seed = seedExpression->getValue(zMemory);
  22. FastNoiseLite* noise = new FastNoiseLite((int)(seed + 0.5f));
  23. if (type.istGleich("Cellular"))
  24. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Cellular);
  25. else if (type.istGleich("ValueCubic"))
  26. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
  27. else if (type.istGleich("Perlin"))
  28. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Perlin);
  29. else if (type.istGleich("OpenSimplex2S"))
  30. noise->SetNoiseType(
  31. FastNoiseLite::NoiseType::NoiseType_OpenSimplex2S);
  32. else if (type.istGleich("OpenSimplex2"))
  33. noise->SetNoiseType(
  34. FastNoiseLite::NoiseType::NoiseType_OpenSimplex2);
  35. else if (type.istGleich("Value"))
  36. noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Value);
  37. if (zConfig->asObject()->hasValue("rotationType3D"))
  38. {
  39. Text value = zConfig->asObject()
  40. ->zValue("rotationType3D")
  41. ->asString()
  42. ->getString();
  43. if (value.istGleich("None"))
  44. {
  45. noise->SetRotationType3D(
  46. FastNoiseLite::RotationType3D::RotationType3D_None);
  47. }
  48. else if (value.istGleich("ImproveXYPlanes"))
  49. {
  50. noise->SetRotationType3D(FastNoiseLite::RotationType3D::
  51. RotationType3D_ImproveXYPlanes);
  52. }
  53. else if (value.istGleich("ImproveXZPlanes"))
  54. {
  55. noise->SetRotationType3D(FastNoiseLite::RotationType3D::
  56. RotationType3D_ImproveXZPlanes);
  57. }
  58. }
  59. if (zConfig->asObject()->hasValue("frequency"))
  60. {
  61. noise->SetFrequency((float)zConfig->asObject()
  62. ->zValue("frequency")
  63. ->asNumber()
  64. ->getNumber());
  65. }
  66. if (zConfig->asObject()->hasValue("fractalType"))
  67. {
  68. Text value = zConfig->asObject()
  69. ->zValue("fractalType")
  70. ->asString()
  71. ->getString();
  72. if (value.istGleich("None"))
  73. {
  74. noise->SetFractalType(
  75. FastNoiseLite::FractalType::FractalType_None);
  76. }
  77. else if (value.istGleich("FBm"))
  78. {
  79. noise->SetFractalType(
  80. FastNoiseLite::FractalType::FractalType_FBm);
  81. }
  82. else if (value.istGleich("Ridged"))
  83. {
  84. noise->SetFractalType(
  85. FastNoiseLite::FractalType::FractalType_Ridged);
  86. }
  87. else if (value.istGleich("PingPong"))
  88. {
  89. noise->SetFractalType(
  90. FastNoiseLite::FractalType::FractalType_PingPong);
  91. }
  92. else if (value.istGleich("DomainWarpProgressive"))
  93. {
  94. noise->SetFractalType(FastNoiseLite::FractalType::
  95. FractalType_DomainWarpProgressive);
  96. }
  97. else if (value.istGleich("DomainWarpIndependent"))
  98. {
  99. noise->SetFractalType(FastNoiseLite::FractalType::
  100. FractalType_DomainWarpIndependent);
  101. }
  102. }
  103. if (zConfig->asObject()->hasValue("cellularDistanceFunction"))
  104. {
  105. Text value = zConfig->asObject()
  106. ->zValue("cellularDistanceFunction")
  107. ->asString()
  108. ->getString();
  109. if (value.istGleich("Hybrid"))
  110. {
  111. noise->SetCellularDistanceFunction(
  112. FastNoiseLite::CellularDistanceFunction::
  113. CellularDistanceFunction_Hybrid);
  114. }
  115. else if (value.istGleich("Manhattan"))
  116. {
  117. noise->SetCellularDistanceFunction(
  118. FastNoiseLite::CellularDistanceFunction::
  119. CellularDistanceFunction_Manhattan);
  120. }
  121. else if (value.istGleich("EuclideanSq"))
  122. {
  123. noise->SetCellularDistanceFunction(
  124. FastNoiseLite::CellularDistanceFunction::
  125. CellularDistanceFunction_EuclideanSq);
  126. }
  127. else if (value.istGleich("Euclidean"))
  128. {
  129. noise->SetCellularDistanceFunction(
  130. FastNoiseLite::CellularDistanceFunction::
  131. CellularDistanceFunction_Euclidean);
  132. }
  133. }
  134. if (zConfig->asObject()->hasValue("cellularReturnType"))
  135. {
  136. Text value = zConfig->asObject()
  137. ->zValue("cellularReturnType")
  138. ->asString()
  139. ->getString();
  140. if (value.istGleich("CellValue"))
  141. {
  142. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  143. CellularReturnType_CellValue);
  144. }
  145. else if (value.istGleich("Distance"))
  146. {
  147. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  148. CellularReturnType_Distance);
  149. }
  150. else if (value.istGleich("Distance2"))
  151. {
  152. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  153. CellularReturnType_Distance2);
  154. }
  155. else if (value.istGleich("Distance2Add"))
  156. {
  157. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  158. CellularReturnType_Distance2Add);
  159. }
  160. else if (value.istGleich("Distance2Sub"))
  161. {
  162. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  163. CellularReturnType_Distance2Sub);
  164. }
  165. else if (value.istGleich("Distance2Mul"))
  166. {
  167. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  168. CellularReturnType_Distance2Mul);
  169. }
  170. else if (value.istGleich("Distance2Div"))
  171. {
  172. noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
  173. CellularReturnType_Distance2Div);
  174. }
  175. }
  176. if (zConfig->asObject()->hasValue("cellularJitter"))
  177. {
  178. noise->SetCellularJitter((float)zConfig->asObject()
  179. ->zValue("cellularJitter")
  180. ->asNumber()
  181. ->getNumber());
  182. }
  183. if (zConfig->asObject()->hasValue("domainWarpType"))
  184. {
  185. Text value = zConfig->asObject()
  186. ->zValue("domainWarpType")
  187. ->asString()
  188. ->getString();
  189. if (value.istGleich("OpenSimplex2Reduced"))
  190. {
  191. noise->SetDomainWarpType(FastNoiseLite::DomainWarpType::
  192. DomainWarpType_OpenSimplex2Reduced);
  193. }
  194. else if (value.istGleich("OpenSimplex2"))
  195. {
  196. noise->SetDomainWarpType(
  197. FastNoiseLite::DomainWarpType::DomainWarpType_OpenSimplex2);
  198. }
  199. else if (value.istGleich("BasicGrid"))
  200. {
  201. noise->SetDomainWarpType(
  202. FastNoiseLite::DomainWarpType::DomainWarpType_BasicGrid);
  203. }
  204. }
  205. if (zConfig->asObject()->hasValue("domainWarpAmp"))
  206. {
  207. noise->SetDomainWarpAmp((float)zConfig->asObject()
  208. ->zValue("domainWarpAmp")
  209. ->asNumber()
  210. ->getNumber());
  211. }
  212. return new FastNoiseWrapper(noise, (int)(seed + 0.5f));
  213. }
  214. return 0;
  215. }
  216. JSON::Validator::JSONValidator* JNoise::getValidator(bool optional)
  217. {
  218. auto validator1 = JSON::Validator::JSONValidator::buildForObject();
  219. auto validator2 = JSON::Validator::JSONValidator::buildForObject();
  220. if (optional)
  221. {
  222. validator1 = validator1->whichIsOptional();
  223. validator2 = validator2->whichIsOptional();
  224. }
  225. return JSON::Validator::JSONValidator::buildForOneOf()
  226. ->typeSpecifiedByAttribute("type")
  227. ->addAcceptedType(validator1->withRequiredString("type")
  228. ->withExactMatch("random")
  229. ->finishString()
  230. ->withRequiredAttribute("seed",
  231. JExpressionParser::getFloatValidator())
  232. ->finishObject())
  233. ->addAcceptedType(
  234. validator2->withRequiredString("type")
  235. ->whichIsOneOf({"Cellular",
  236. "ValueCubic",
  237. "Perlin",
  238. "OpenSimplex2S",
  239. "OpenSimplex2",
  240. "Value"})
  241. ->finishString()
  242. ->withRequiredAttribute(
  243. "seed", JExpressionParser::getFloatValidator())
  244. ->withRequiredString("rotationType3D")
  245. ->whichIsOptional()
  246. ->whichIsOneOf({"None", "ImproveXYPlanes", "ImproveXZPlanes"})
  247. ->finishString()
  248. ->withRequiredNumber("frequency")
  249. ->whichIsOptional()
  250. ->finishNumber()
  251. ->withRequiredString("fractalType")
  252. ->whichIsOptional()
  253. ->whichIsOneOf({"None",
  254. "FBm",
  255. "Ridged",
  256. "PingPong",
  257. "DomainWarpProgressive",
  258. "DomainWarpIndependent"})
  259. ->finishString()
  260. ->withRequiredString("cellularDistanceFunction")
  261. ->whichIsOptional()
  262. ->whichIsOneOf(
  263. {"Hybrid", "Manhattan", "EuclideanSq", "Euclidean"})
  264. ->finishString()
  265. ->withRequiredString("cellularReturnType")
  266. ->whichIsOptional()
  267. ->whichIsOneOf({"CellValue",
  268. "Distance",
  269. "Distance2",
  270. "Distance2Add",
  271. "Distance2Sub",
  272. "Distance2Mul",
  273. "Distance2Div"})
  274. ->finishString()
  275. ->withRequiredNumber("cellularJitter")
  276. ->whichIsOptional()
  277. ->finishNumber()
  278. ->withRequiredString("domainWarpType")
  279. ->whichIsOptional()
  280. ->whichIsOneOf(
  281. {"BasicGrid", "OpenSimplex2", "OpenSimplex2Reduced"})
  282. ->finishString()
  283. ->withRequiredNumber("domainWarpAmp")
  284. ->whichIsOptional()
  285. ->finishNumber()
  286. ->finishObject())
  287. ->finishOneOf();
  288. }