#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)(seed + 0.5f)); } else { JFloatExpression* seedExpression = JExpressionParser::parseFloatExpression( zConfig->asObject()->zValue("seed")); float seed = seedExpression->getValue(zMemory); FastNoiseLite* noise = new FastNoiseLite((int)(seed + 0.5f)); 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)(seed + 0.5f)); } 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(); }