Tweaked current available parameters, and went further for a better generation

This commit is contained in:
Namonay
2025-05-30 20:03:21 +02:00
parent 0e60e16ee9
commit 1fcf111e43
2 changed files with 18 additions and 5 deletions

View File

@@ -1,3 +1,4 @@
#include "Maths/Vec3.h"
#include <Noise.h> #include <Noise.h>
#include <Block.h> #include <Block.h>
#include <cstdint> #include <cstdint>
@@ -5,11 +6,14 @@
constexpr float HEIGHT_COEFF = 255.0f; constexpr float HEIGHT_COEFF = 255.0f;
Noise::Noise(const std::uint32_t seed, float frequency, float amplitude, int octaves, float lacunarity, float persistance): seed(std::mt19937(seed)), frequency(frequency), amplitude(amplitude), octaves(octaves), lacunarity(lacunarity), persistance(persistance) constexpr std::uint32_t WATER_LEVEL = 20;
Noise::Noise(const std::uint32_t seed, float frequency, float amplitude, int octaves, float lacunarity, float persistance, int redistribution): seed(std::mt19937(seed)), frequency(frequency), amplitude(amplitude), octaves(octaves), lacunarity(lacunarity), persistance(persistance), redistribution(redistribution)
{ {
if(amplitude > 1.0f || amplitude < -1.0f) if(amplitude > 1.0f || amplitude < -1.0f)
Scop::FatalError("Amplitude value must be in [-1;1]"); Scop::FatalError("Amplitude value must be in [-1;1]");
if(redistribution <= 0)
Scop::FatalError("Redistribution cannot be a negative integer");
InitPermutation(); InitPermutation();
} }
@@ -72,6 +76,7 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t
} }
float normalized = total / maxValue; float normalized = total / maxValue;
normalized = std::clamp(normalized, 0.0f, 1.0f); normalized = std::clamp(normalized, 0.0f, 1.0f);
normalized = std::pow(normalized, redistribution);
return static_cast<int>(normalized * HEIGHT_COEFF); return static_cast<int>(normalized * HEIGHT_COEFF);
} }
@@ -173,13 +178,20 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t
//std::uint32_t height = std::abs(std::sin((float)pos.x / 20.0f) * std::cos((float)pos.y / 20.0f) * 60.0f) + 1; //std::uint32_t height = std::abs(std::sin((float)pos.x / 20.0f) * std::cos((float)pos.y / 20.0f) * 60.0f) + 1;
std::uint32_t height = Perlin2D(pos.x, pos.y); const std::uint32_t height = Perlin2D(pos.x, pos.y);
for(std::uint32_t y = 0; y < std::min(height, CHUNK_SIZE.y); y++) for(std::uint32_t y = 0; y < std::min(height, CHUNK_SIZE.y); y++)
{ {
// const std::uint32_t value = Perlin3D(pos.x, y, pos.y);
if(y > std::min(height, CHUNK_SIZE.y) - 2) if(y > std::min(height, CHUNK_SIZE.y) - 2)
data[y] = static_cast<std::uint32_t>(BlockType::Grass); data[y] = static_cast<std::uint32_t>(BlockType::Grass);
else else
data[y] = static_cast<std::uint32_t>(BlockType::Stone); data[y] = static_cast<std::uint32_t>(BlockType::Stone);
} }
for (std::uint32_t y = 0; y < WATER_LEVEL; y++)
{
if (data[y] == static_cast<std::uint32_t>(BlockType::Air))
data[y] = static_cast<std::uint32_t>(BlockType::Sand);
}
return data; return data;
} }

View File

@@ -11,7 +11,7 @@
class Noise class Noise
{ {
public: public:
Noise(const std::uint32_t seed = 42, float frequency = 0.05f, float amplitude = 0.9f, int octaves = 4, float lacunarity = 2.0f, float persistance = 0.5f); Noise(const std::uint32_t seed = 42, float frequency = 0.05f, float amplitude = 0.95f, int octaves = 4, float lacunarity = 2.4f, float persistance = 0.8f, int redistribution = 3);
[[nodiscard]] std::array<std::uint32_t, CHUNK_SIZE.y> GetHeight(Scop::Vec2i pos); [[nodiscard]] std::array<std::uint32_t, CHUNK_SIZE.y> GetHeight(Scop::Vec2i pos);
[[nodiscard]] const int Perlin2D(int x, int y) noexcept; [[nodiscard]] const int Perlin2D(int x, int y) noexcept;
@@ -26,6 +26,7 @@ class Noise
const int octaves; const int octaves;
const float lacunarity; const float lacunarity;
const float persistance; const float persistance;
const int redistribution;
void InitPermutation(void); void InitPermutation(void);
[[nodiscard]] const float Perlin2D(float x, float y) noexcept; [[nodiscard]] const float Perlin2D(float x, float y) noexcept;