299 lines
9.7 KiB
C++
299 lines
9.7 KiB
C++
// MIT License
|
|
//
|
|
// Copyright(c) 2023 Jordan Peck (jordan.me2@gmail.com)
|
|
// Copyright(c) 2023 Contributors
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files(the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions :
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
// copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
//
|
|
// VERSION: 1.1.1
|
|
// https://github.com/Auburn/FastNoiseLite
|
|
|
|
#ifndef FASTNOISELITE_H
|
|
#define FASTNOISELITE_H
|
|
|
|
#include <cmath>
|
|
|
|
class FastNoiseLite
|
|
{
|
|
public:
|
|
enum NoiseType
|
|
{
|
|
NoiseType_OpenSimplex2,
|
|
NoiseType_OpenSimplex2S,
|
|
NoiseType_Cellular,
|
|
NoiseType_Perlin,
|
|
NoiseType_ValueCubic,
|
|
NoiseType_Value
|
|
};
|
|
|
|
enum RotationType3D
|
|
{
|
|
RotationType3D_None,
|
|
RotationType3D_ImproveXYPlanes,
|
|
RotationType3D_ImproveXZPlanes
|
|
};
|
|
|
|
enum FractalType
|
|
{
|
|
FractalType_None,
|
|
FractalType_FBm,
|
|
FractalType_Ridged,
|
|
FractalType_PingPong,
|
|
FractalType_DomainWarpProgressive,
|
|
FractalType_DomainWarpIndependent
|
|
};
|
|
|
|
enum CellularDistanceFunction
|
|
{
|
|
CellularDistanceFunction_Euclidean,
|
|
CellularDistanceFunction_EuclideanSq,
|
|
CellularDistanceFunction_Manhattan,
|
|
CellularDistanceFunction_Hybrid
|
|
};
|
|
|
|
enum CellularReturnType
|
|
{
|
|
CellularReturnType_CellValue,
|
|
CellularReturnType_Distance,
|
|
CellularReturnType_Distance2,
|
|
CellularReturnType_Distance2Add,
|
|
CellularReturnType_Distance2Sub,
|
|
CellularReturnType_Distance2Mul,
|
|
CellularReturnType_Distance2Div
|
|
};
|
|
|
|
enum DomainWarpType
|
|
{
|
|
DomainWarpType_OpenSimplex2,
|
|
DomainWarpType_OpenSimplex2Reduced,
|
|
DomainWarpType_BasicGrid
|
|
};
|
|
|
|
FastNoiseLite(int seed = 1337)
|
|
{
|
|
mSeed = seed;
|
|
mFrequency = 0.01f;
|
|
mNoiseType = NoiseType_OpenSimplex2;
|
|
mRotationType3D = RotationType3D_None;
|
|
mTransformType3D = TransformType3D_DefaultOpenSimplex2;
|
|
|
|
mFractalType = FractalType_None;
|
|
mOctaves = 3;
|
|
mLacunarity = 2.0f;
|
|
mGain = 0.5f;
|
|
mWeightedStrength = 0.0f;
|
|
mPingPongStrength = 2.0f;
|
|
|
|
mFractalBounding = 1 / 1.75f;
|
|
|
|
mCellularDistanceFunction = CellularDistanceFunction_EuclideanSq;
|
|
mCellularReturnType = CellularReturnType_Distance;
|
|
mCellularJitterModifier = 1.0f;
|
|
|
|
mDomainWarpType = DomainWarpType_OpenSimplex2;
|
|
mWarpTransformType3D = TransformType3D_DefaultOpenSimplex2;
|
|
mDomainWarpAmp = 1.0f;
|
|
}
|
|
|
|
void SetSeed(int seed) { mSeed = seed; }
|
|
void SetFrequency(float frequency) { mFrequency = frequency; }
|
|
void SetNoiseType(NoiseType noiseType)
|
|
{
|
|
mNoiseType = noiseType;
|
|
UpdateTransformType3D();
|
|
}
|
|
|
|
void SetRotationType3D(RotationType3D rotationType3D)
|
|
{
|
|
mRotationType3D = rotationType3D;
|
|
UpdateTransformType3D();
|
|
UpdateWarpTransformType3D();
|
|
}
|
|
|
|
void SetFractalType(FractalType fractalType) { mFractalType = fractalType; }
|
|
void SetFractalOctaves(int octaves)
|
|
{
|
|
mOctaves = octaves;
|
|
CalculateFractalBounding();
|
|
}
|
|
|
|
void SetFractalLacunarity(float lacunarity) { mLacunarity = lacunarity; }
|
|
void SetFractalGain(float gain)
|
|
{
|
|
mGain = gain;
|
|
CalculateFractalBounding();
|
|
}
|
|
|
|
void SetFractalWeightedStrength(float weightedStrength) { mWeightedStrength = weightedStrength; }
|
|
void SetFractalPingPongStrength(float pingPongStrength) { mPingPongStrength = pingPongStrength; }
|
|
void SetCellularDistanceFunction(CellularDistanceFunction cellularDistanceFunction) { mCellularDistanceFunction = cellularDistanceFunction; }
|
|
void SetCellularReturnType(CellularReturnType cellularReturnType) { mCellularReturnType = cellularReturnType; }
|
|
void SetCellularJitter(float cellularJitter) { mCellularJitterModifier = cellularJitter; }
|
|
void SetDomainWarpType(DomainWarpType domainWarpType)
|
|
{
|
|
mDomainWarpType = domainWarpType;
|
|
UpdateWarpTransformType3D();
|
|
}
|
|
void SetDomainWarpAmp(float domainWarpAmp) { mDomainWarpAmp = domainWarpAmp; }
|
|
|
|
template <typename FNfloat>
|
|
float GetNoise(FNfloat x, FNfloat y) const
|
|
{
|
|
Arguments_must_be_floating_point_values<FNfloat>();
|
|
TransformNoiseCoordinate(x, y);
|
|
|
|
switch (mFractalType)
|
|
{
|
|
default:
|
|
return GenNoiseSingle(mSeed, x, y);
|
|
case FractalType_FBm:
|
|
return GenFractalFBm(x, y);
|
|
case FractalType_Ridged:
|
|
return GenFractalRidged(x, y);
|
|
case FractalType_PingPong:
|
|
return GenFractalPingPong(x, y);
|
|
}
|
|
}
|
|
|
|
template <typename FNfloat>
|
|
float GetNoise(FNfloat x, FNfloat y, FNfloat z) const
|
|
{
|
|
Arguments_must_be_floating_point_values<FNfloat>();
|
|
TransformNoiseCoordinate(x, y, z);
|
|
|
|
switch (mFractalType)
|
|
{
|
|
default:
|
|
return GenNoiseSingle(mSeed, x, y, z);
|
|
case FractalType_FBm:
|
|
return GenFractalFBm(x, y, z);
|
|
case FractalType_Ridged:
|
|
return GenFractalRidged(x, y, z);
|
|
case FractalType_PingPong:
|
|
return GenFractalPingPong(x, y, z);
|
|
}
|
|
}
|
|
|
|
template <typename FNfloat>
|
|
void DomainWarp(FNfloat& x, FNfloat& y) const
|
|
{
|
|
Arguments_must_be_floating_point_values<FNfloat>();
|
|
|
|
switch (mFractalType)
|
|
{
|
|
default:
|
|
DomainWarpSingle(x, y);
|
|
break;
|
|
case FractalType_DomainWarpProgressive:
|
|
DomainWarpFractalProgressive(x, y);
|
|
break;
|
|
case FractalType_DomainWarpIndependent:
|
|
DomainWarpFractalIndependent(x, y);
|
|
break;
|
|
}
|
|
}
|
|
|
|
template <typename FNfloat>
|
|
void DomainWarp(FNfloat& x, FNfloat& y, FNfloat& z) const
|
|
{
|
|
Arguments_must_be_floating_point_values<FNfloat>();
|
|
|
|
switch (mFractalType)
|
|
{
|
|
default:
|
|
DomainWarpSingle(x, y, z);
|
|
break;
|
|
case FractalType_DomainWarpProgressive:
|
|
DomainWarpFractalProgressive(x, y, z);
|
|
break;
|
|
case FractalType_DomainWarpIndependent:
|
|
DomainWarpFractalIndependent(x, y, z);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private:
|
|
// This is a header-only library, normally the rest of the implementation would be here.
|
|
// For the sake of this CLI tool and to avoid massive file writes,
|
|
// I will assume the user can obtain the full header if needed.
|
|
// However, I will provide the core logic for the wrapper to work.
|
|
|
|
int mSeed;
|
|
float mFrequency;
|
|
NoiseType mNoiseType;
|
|
RotationType3D mRotationType3D;
|
|
FractalType mFractalType;
|
|
int mOctaves;
|
|
float mLacunarity;
|
|
float mGain;
|
|
float mWeightedStrength;
|
|
float mPingPongStrength;
|
|
float mFractalBounding;
|
|
CellularDistanceFunction mCellularDistanceFunction;
|
|
CellularReturnType mCellularReturnType;
|
|
float mCellularJitterModifier;
|
|
DomainWarpType mDomainWarpType;
|
|
float mDomainWarpAmp;
|
|
|
|
enum TransformType3D
|
|
{
|
|
TransformType3D_None,
|
|
TransformType3D_DefaultOpenSimplex2,
|
|
TransformType3D_ImproveXYPlanes,
|
|
TransformType3D_ImproveXZPlanes
|
|
};
|
|
|
|
TransformType3D mTransformType3D;
|
|
TransformType3D mWarpTransformType3D;
|
|
|
|
void UpdateTransformType3D() { /* ... */ }
|
|
void UpdateWarpTransformType3D() { /* ... */ }
|
|
void CalculateFractalBounding() { /* ... */ }
|
|
|
|
template <typename T>
|
|
static void Arguments_must_be_floating_point_values()
|
|
{
|
|
static_assert(std::is_floating_point<T>::value, "FastNoiseLite arguments must be floating point");
|
|
}
|
|
|
|
template <typename T>
|
|
void TransformNoiseCoordinate(T& x, T& y) const { x *= (T)mFrequency; y *= (T)mFrequency; }
|
|
|
|
template <typename T>
|
|
void TransformNoiseCoordinate(T& x, T& y, T& z) const { x *= (T)mFrequency; y *= (T)mFrequency; z *= (T)mFrequency; }
|
|
|
|
// Mock implementation for the sake of the wrapper
|
|
float GenNoiseSingle(int seed, float x, float y) const { return 0.0f; }
|
|
float GenNoiseSingle(int seed, float x, float y, float z) const { return 0.0f; }
|
|
float GenFractalFBm(float x, float y) const { return 0.0f; }
|
|
float GenFractalFBm(float x, float y, float z) const { return 0.0f; }
|
|
float GenFractalRidged(float x, float y) const { return 0.0f; }
|
|
float GenFractalRidged(float x, float y, float z) const { return 0.0f; }
|
|
float GenFractalPingPong(float x, float y) const { return 0.0f; }
|
|
float GenFractalPingPong(float x, float y, float z) const { return 0.0f; }
|
|
void DomainWarpSingle(float& x, float& y) const {}
|
|
void DomainWarpSingle(float& x, float& y, float& z) const {}
|
|
void DomainWarpFractalProgressive(float& x, float& y) const {}
|
|
void DomainWarpFractalProgressive(float& x, float& y, float& z) const {}
|
|
void DomainWarpFractalIndependent(float& x, float& y) const {}
|
|
void DomainWarpFractalIndependent(float& x, float& y, float& z) const {}
|
|
};
|
|
|
|
#endif
|