123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- #include "JNoise.h"
- #include "FastNoiseWrapper.h"
- #include "RandNoise.h"
- using namespace Framework;
- Noise* JNoise::parseNoisse(JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
- {
- Text type = zConfig->asObject()->zValue("type")->asString()->getString();
- if (type.istGleich("random"))
- {
- JFloatExpression* seedExpression
- = JExpressionParser::parseFloatExpression(
- zConfig->asObject()->zValue("seed"));
- float seed = seedExpression->getValue(zMemory);
- return new RandNoise((int)(round(seed)));
- }
- else
- {
- JFloatExpression* seedExpression
- = JExpressionParser::parseFloatExpression(
- zConfig->asObject()->zValue("seed"));
- float seed = seedExpression->getValue(zMemory);
- FastNoiseLite* noise = new FastNoiseLite((int)(round(seed)));
- if (type.istGleich("Cellular"))
- noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Cellular);
- else if (type.istGleich("ValueCubic"))
- noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_ValueCubic);
- else if (type.istGleich("Perlin"))
- noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Perlin);
- else if (type.istGleich("OpenSimplex2S"))
- noise->SetNoiseType(
- FastNoiseLite::NoiseType::NoiseType_OpenSimplex2S);
- else if (type.istGleich("OpenSimplex2"))
- noise->SetNoiseType(
- FastNoiseLite::NoiseType::NoiseType_OpenSimplex2);
- else if (type.istGleich("Value"))
- noise->SetNoiseType(FastNoiseLite::NoiseType::NoiseType_Value);
- if (zConfig->asObject()->hasValue("rotationType3D"))
- {
- Text value = zConfig->asObject()
- ->zValue("rotationType3D")
- ->asString()
- ->getString();
- if (value.istGleich("None"))
- {
- noise->SetRotationType3D(
- FastNoiseLite::RotationType3D::RotationType3D_None);
- }
- else if (value.istGleich("ImproveXYPlanes"))
- {
- noise->SetRotationType3D(FastNoiseLite::RotationType3D::
- RotationType3D_ImproveXYPlanes);
- }
- else if (value.istGleich("ImproveXZPlanes"))
- {
- noise->SetRotationType3D(FastNoiseLite::RotationType3D::
- RotationType3D_ImproveXZPlanes);
- }
- }
- if (zConfig->asObject()->hasValue("frequency"))
- {
- noise->SetFrequency((float)zConfig->asObject()
- ->zValue("frequency")
- ->asNumber()
- ->getNumber());
- }
- if (zConfig->asObject()->hasValue("fractalType"))
- {
- Text value = zConfig->asObject()
- ->zValue("fractalType")
- ->asString()
- ->getString();
- if (value.istGleich("None"))
- {
- noise->SetFractalType(
- FastNoiseLite::FractalType::FractalType_None);
- }
- else if (value.istGleich("FBm"))
- {
- noise->SetFractalType(
- FastNoiseLite::FractalType::FractalType_FBm);
- }
- else if (value.istGleich("Ridged"))
- {
- noise->SetFractalType(
- FastNoiseLite::FractalType::FractalType_Ridged);
- }
- else if (value.istGleich("PingPong"))
- {
- noise->SetFractalType(
- FastNoiseLite::FractalType::FractalType_PingPong);
- }
- else if (value.istGleich("DomainWarpProgressive"))
- {
- noise->SetFractalType(FastNoiseLite::FractalType::
- FractalType_DomainWarpProgressive);
- }
- else if (value.istGleich("DomainWarpIndependent"))
- {
- noise->SetFractalType(FastNoiseLite::FractalType::
- FractalType_DomainWarpIndependent);
- }
- }
- if (zConfig->asObject()->hasValue("cellularDistanceFunction"))
- {
- Text value = zConfig->asObject()
- ->zValue("cellularDistanceFunction")
- ->asString()
- ->getString();
- if (value.istGleich("Hybrid"))
- {
- noise->SetCellularDistanceFunction(
- FastNoiseLite::CellularDistanceFunction::
- CellularDistanceFunction_Hybrid);
- }
- else if (value.istGleich("Manhattan"))
- {
- noise->SetCellularDistanceFunction(
- FastNoiseLite::CellularDistanceFunction::
- CellularDistanceFunction_Manhattan);
- }
- else if (value.istGleich("EuclideanSq"))
- {
- noise->SetCellularDistanceFunction(
- FastNoiseLite::CellularDistanceFunction::
- CellularDistanceFunction_EuclideanSq);
- }
- else if (value.istGleich("Euclidean"))
- {
- noise->SetCellularDistanceFunction(
- FastNoiseLite::CellularDistanceFunction::
- CellularDistanceFunction_Euclidean);
- }
- }
- if (zConfig->asObject()->hasValue("cellularReturnType"))
- {
- Text value = zConfig->asObject()
- ->zValue("cellularReturnType")
- ->asString()
- ->getString();
- if (value.istGleich("CellValue"))
- {
- noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
- CellularReturnType_CellValue);
- }
- else if (value.istGleich("Distance"))
- {
- noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
- CellularReturnType_Distance);
- }
- else if (value.istGleich("Distance2"))
- {
- noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
- CellularReturnType_Distance2);
- }
- else if (value.istGleich("Distance2Add"))
- {
- noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
- CellularReturnType_Distance2Add);
- }
- else if (value.istGleich("Distance2Sub"))
- {
- noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
- CellularReturnType_Distance2Sub);
- }
- else if (value.istGleich("Distance2Mul"))
- {
- noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
- CellularReturnType_Distance2Mul);
- }
- else if (value.istGleich("Distance2Div"))
- {
- noise->SetCellularReturnType(FastNoiseLite::CellularReturnType::
- CellularReturnType_Distance2Div);
- }
- }
- if (zConfig->asObject()->hasValue("cellularJitter"))
- {
- noise->SetCellularJitter((float)zConfig->asObject()
- ->zValue("cellularJitter")
- ->asNumber()
- ->getNumber());
- }
- if (zConfig->asObject()->hasValue("domainWarpType"))
- {
- Text value = zConfig->asObject()
- ->zValue("domainWarpType")
- ->asString()
- ->getString();
- if (value.istGleich("OpenSimplex2Reduced"))
- {
- noise->SetDomainWarpType(FastNoiseLite::DomainWarpType::
- DomainWarpType_OpenSimplex2Reduced);
- }
- else if (value.istGleich("OpenSimplex2"))
- {
- noise->SetDomainWarpType(
- FastNoiseLite::DomainWarpType::DomainWarpType_OpenSimplex2);
- }
- else if (value.istGleich("BasicGrid"))
- {
- noise->SetDomainWarpType(
- FastNoiseLite::DomainWarpType::DomainWarpType_BasicGrid);
- }
- }
- if (zConfig->asObject()->hasValue("domainWarpAmp"))
- {
- noise->SetDomainWarpAmp((float)zConfig->asObject()
- ->zValue("domainWarpAmp")
- ->asNumber()
- ->getNumber());
- }
- return new FastNoiseWrapper(noise, (int)(round(seed)));
- }
- return 0;
- }
- JSON::Validator::JSONValidator* JNoise::getValidator(bool optional)
- {
- auto validator1 = JSON::Validator::JSONValidator::buildForObject();
- auto validator2 = JSON::Validator::JSONValidator::buildForObject();
- if (optional)
- {
- validator1 = validator1->whichIsOptional();
- validator2 = validator2->whichIsOptional();
- }
- return JSON::Validator::JSONValidator::buildForOneOf()
- ->typeSpecifiedByAttribute("type")
- ->addAcceptedType(validator1->withRequiredString("type")
- ->withExactMatch("random")
- ->finishString()
- ->withRequiredAttribute("seed",
- JExpressionParser::getFloatValidator())
- ->finishObject())
- ->addAcceptedType(
- validator2->withRequiredString("type")
- ->whichIsOneOf({"Cellular",
- "ValueCubic",
- "Perlin",
- "OpenSimplex2S",
- "OpenSimplex2",
- "Value"})
- ->finishString()
- ->withRequiredAttribute(
- "seed", JExpressionParser::getFloatValidator())
- ->withRequiredString("rotationType3D")
- ->whichIsOptional()
- ->whichIsOneOf({"None", "ImproveXYPlanes", "ImproveXZPlanes"})
- ->finishString()
- ->withRequiredNumber("frequency")
- ->whichIsOptional()
- ->finishNumber()
- ->withRequiredString("fractalType")
- ->whichIsOptional()
- ->whichIsOneOf({"None",
- "FBm",
- "Ridged",
- "PingPong",
- "DomainWarpProgressive",
- "DomainWarpIndependent"})
- ->finishString()
- ->withRequiredString("cellularDistanceFunction")
- ->whichIsOptional()
- ->whichIsOneOf(
- {"Hybrid", "Manhattan", "EuclideanSq", "Euclidean"})
- ->finishString()
- ->withRequiredString("cellularReturnType")
- ->whichIsOptional()
- ->whichIsOneOf({"CellValue",
- "Distance",
- "Distance2",
- "Distance2Add",
- "Distance2Sub",
- "Distance2Mul",
- "Distance2Div"})
- ->finishString()
- ->withRequiredNumber("cellularJitter")
- ->whichIsOptional()
- ->finishNumber()
- ->withRequiredString("domainWarpType")
- ->whichIsOptional()
- ->whichIsOneOf(
- {"BasicGrid", "OpenSimplex2", "OpenSimplex2Reduced"})
- ->finishString()
- ->withRequiredNumber("domainWarpAmp")
- ->whichIsOptional()
- ->finishNumber()
- ->finishObject())
- ->finishOneOf();
- }
|