mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-17 16:17:02 +08:00
refactor: extract biome logic into separate
This commit is contained in:
@@ -10,7 +10,6 @@ constexpr int MAX_CHARACTER = 128;
|
|||||||
constexpr float NORMAL_FOV = 70.0f;
|
constexpr float NORMAL_FOV = 70.0f;
|
||||||
|
|
||||||
constexpr int MAX_BIOME_SUM = 4;
|
constexpr int MAX_BIOME_SUM = 4;
|
||||||
constexpr float BIOME_NOISE_FREQUENCY = 0.003f;
|
|
||||||
|
|
||||||
constexpr float VERTICES_POS[6][6][3] = {
|
constexpr float VERTICES_POS[6][6][3] = {
|
||||||
// ===== front (z = +1) =====
|
// ===== front (z = +1) =====
|
||||||
|
|||||||
@@ -1,34 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <cmath>
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <functional>
|
#include <cmath>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
#include <Cubed/tools/log.hpp>
|
|
||||||
#include <Cubed/tools/cubed_assert.hpp>
|
#include <Cubed/tools/cubed_assert.hpp>
|
||||||
struct ChunkPos {
|
#include <Cubed/tools/log.hpp>
|
||||||
int x;
|
#include <Cubed/tools/perlin_noise.hpp>
|
||||||
int z;
|
|
||||||
|
|
||||||
bool operator==(const ChunkPos&) const = default;
|
|
||||||
struct Hash {
|
|
||||||
std::size_t operator()(const ChunkPos& pos) const{
|
|
||||||
std::size_t h1 = std::hash<int>{}(pos.x);
|
|
||||||
std::size_t h2 = std::hash<int>{}(pos.z);
|
|
||||||
return h1 ^ (h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
ChunkPos operator+(const ChunkPos& pos) const{
|
constexpr float BIOME_NOISE_FREQUENCY = 0.003f;
|
||||||
return ChunkPos{x + pos.x, z + pos.z};
|
|
||||||
}
|
|
||||||
|
|
||||||
ChunkPos& operator+=(const ChunkPos& pos) {
|
|
||||||
x += pos.x;
|
|
||||||
z += pos.z;
|
|
||||||
return *this;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
constexpr float PLAIN_FREQ = 0.5f;
|
constexpr float PLAIN_FREQ = 0.5f;
|
||||||
constexpr float FOREST_FREQ = 1.0f;
|
constexpr float FOREST_FREQ = 1.0f;
|
||||||
@@ -47,7 +27,7 @@ struct BiomeHeightRange {
|
|||||||
int amplitude;
|
int amplitude;
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr inline std::string get_biome_str(Biome biome) {
|
inline std::string get_biome_str(Biome biome) {
|
||||||
std::string str;
|
std::string str;
|
||||||
using enum Biome;
|
using enum Biome;
|
||||||
switch (biome) {
|
switch (biome) {
|
||||||
@@ -131,6 +111,45 @@ inline Biome safe_int_to_biome(int x) {
|
|||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline int get_interpolated_height(float world_x, float world_z, float temp, float humid) {
|
||||||
|
|
||||||
|
auto weight = [](float t, float h, float ct, float ch) -> float {
|
||||||
|
float dt = t - ct;
|
||||||
|
float dh = h - ch;
|
||||||
|
float dist = std::sqrt(dt*dt + dh*dh);
|
||||||
|
return std::max(0.0f, 0.5f - dist);
|
||||||
|
};
|
||||||
|
|
||||||
|
float w_mountain = weight(temp, humid, 0.25f, 0.15f);
|
||||||
|
float w_plain = weight(temp, humid, 0.50f, 0.40f);
|
||||||
|
float w_desert = weight(temp, humid, 0.75f, 0.15f);
|
||||||
|
float w_forest = weight(temp, humid, 0.75f, 0.75f);
|
||||||
|
// adjust transitions between chunks
|
||||||
|
float pow_n = 8.0f; // the larger n is, the purer the biome
|
||||||
|
w_mountain = std::pow(w_mountain, pow_n) * MOUNTAIN_FREQ;
|
||||||
|
w_plain = std::pow(w_plain, pow_n) * PLAIN_FREQ;
|
||||||
|
w_desert = std::pow(w_desert, pow_n) * DESERT_FREQ;
|
||||||
|
w_forest = std::pow(w_forest, pow_n) * FOREST_FREQ;
|
||||||
|
|
||||||
|
float total = w_mountain + w_plain + w_desert + w_forest;
|
||||||
|
w_mountain /= total; w_plain /= total; w_desert /= total; w_forest /= total;
|
||||||
|
|
||||||
|
auto sample_height = [&](Biome b) -> float {
|
||||||
|
auto range = get_biome_height_range(b);
|
||||||
|
auto [f1, f2, f3] = get_noise_frequencies_for_biome(b);
|
||||||
|
float n =
|
||||||
|
1.00f * PerlinNoise::noise(world_x * f1, 0.5f, world_z * f1) +
|
||||||
|
0.50f * PerlinNoise::noise(world_x * f2, 0.5f, world_z * f2) +
|
||||||
|
0.25f * PerlinNoise::noise(world_x * f3, 0.5f, world_z * f3);
|
||||||
|
n /= 1.75f;
|
||||||
|
return range.base_y + n * range.amplitude;
|
||||||
|
};
|
||||||
|
|
||||||
|
float h = w_mountain * sample_height(Biome::MOUNTAIN)
|
||||||
|
+ w_plain * sample_height(Biome::PLAIN)
|
||||||
|
+ w_desert * sample_height(Biome::DESERT)
|
||||||
|
+ w_forest * sample_height(Biome::FOREST);
|
||||||
|
return static_cast<int>(h);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -4,7 +4,8 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
#include <Cubed/config.hpp>
|
#include <Cubed/config.hpp>
|
||||||
#include <Cubed/gameplay/chunk_status.hpp>
|
#include <Cubed/gameplay/biome.hpp>
|
||||||
|
#include <Cubed/gameplay/chunk_pos.hpp>
|
||||||
#include <Cubed/gameplay/block.hpp>
|
#include <Cubed/gameplay/block.hpp>
|
||||||
|
|
||||||
class World;
|
class World;
|
||||||
|
|||||||
30
include/Cubed/gameplay/chunk_pos.hpp
Normal file
30
include/Cubed/gameplay/chunk_pos.hpp
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
|
||||||
|
#include <Cubed/tools/log.hpp>
|
||||||
|
#include <Cubed/tools/cubed_assert.hpp>
|
||||||
|
struct ChunkPos {
|
||||||
|
int x;
|
||||||
|
int z;
|
||||||
|
|
||||||
|
bool operator==(const ChunkPos&) const = default;
|
||||||
|
struct Hash {
|
||||||
|
std::size_t operator()(const ChunkPos& pos) const{
|
||||||
|
std::size_t h1 = std::hash<int>{}(pos.x);
|
||||||
|
std::size_t h2 = std::hash<int>{}(pos.z);
|
||||||
|
return h1 ^ (h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ChunkPos operator+(const ChunkPos& pos) const{
|
||||||
|
return ChunkPos{x + pos.x, z + pos.z};
|
||||||
|
}
|
||||||
|
|
||||||
|
ChunkPos& operator+=(const ChunkPos& pos) {
|
||||||
|
x += pos.x;
|
||||||
|
z += pos.z;
|
||||||
|
return *this;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <Cubed/AABB.hpp>
|
#include <Cubed/AABB.hpp>
|
||||||
#include <Cubed/config.hpp>
|
#include <Cubed/config.hpp>
|
||||||
#include <Cubed/gameplay/block.hpp>
|
#include <Cubed/gameplay/block.hpp>
|
||||||
#include <Cubed/gameplay/chunk_status.hpp>
|
#include <Cubed/gameplay/chunk_pos.hpp>
|
||||||
#include <Cubed/input.hpp>
|
#include <Cubed/input.hpp>
|
||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|||||||
@@ -2,5 +2,4 @@
|
|||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
namespace Math {
|
namespace Math {
|
||||||
void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes);
|
void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes);
|
||||||
int get_interpolated_height(float world_x, float world_z, float temp, float humid);
|
|
||||||
}
|
}
|
||||||
@@ -283,7 +283,7 @@ void Chunk::resolve_blocks() {
|
|||||||
|
|
||||||
float temp = PerlinNoise::noise(world_x * BIOME_NOISE_FREQUENCY, 0.0f, world_z * BIOME_NOISE_FREQUENCY);
|
float temp = PerlinNoise::noise(world_x * BIOME_NOISE_FREQUENCY, 0.0f, world_z * BIOME_NOISE_FREQUENCY);
|
||||||
float humid = PerlinNoise::noise(world_x * BIOME_NOISE_FREQUENCY, 1.0f, world_z * BIOME_NOISE_FREQUENCY);
|
float humid = PerlinNoise::noise(world_x * BIOME_NOISE_FREQUENCY, 1.0f, world_z * BIOME_NOISE_FREQUENCY);
|
||||||
int height = Math::get_interpolated_height(world_x, world_z, temp, humid);
|
int height = get_interpolated_height(world_x, world_z, temp, humid);
|
||||||
auto biome = get_biome_from_noise(temp, humid);
|
auto biome = get_biome_from_noise(temp, humid);
|
||||||
for (int y = 5; y < height - 5; y++) {
|
for (int y = 5; y < height - 5; y++) {
|
||||||
m_blocks[get_index(x, y, z)] = 3;
|
m_blocks[get_index(x, y, z)] = 3;
|
||||||
|
|||||||
@@ -2,12 +2,6 @@
|
|||||||
|
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
#include <Cubed/config.hpp>
|
|
||||||
#include <Cubed/gameplay/chunk_status.hpp>
|
|
||||||
#include <Cubed/tools/perlin_noise.hpp>
|
|
||||||
|
|
||||||
namespace Math {
|
namespace Math {
|
||||||
void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes) {
|
void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes) {
|
||||||
if (planes.size() != 6) {
|
if (planes.size() != 6) {
|
||||||
@@ -34,44 +28,5 @@ namespace Math {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_interpolated_height(float world_x, float world_z, float temp, float humid) {
|
|
||||||
|
|
||||||
auto weight = [](float t, float h, float ct, float ch) -> float {
|
|
||||||
float dt = t - ct;
|
|
||||||
float dh = h - ch;
|
|
||||||
float dist = std::sqrt(dt*dt + dh*dh);
|
|
||||||
return std::max(0.0f, 0.5f - dist);
|
|
||||||
};
|
|
||||||
|
|
||||||
float w_mountain = weight(temp, humid, 0.25f, 0.15f);
|
|
||||||
float w_plain = weight(temp, humid, 0.50f, 0.40f);
|
|
||||||
float w_desert = weight(temp, humid, 0.75f, 0.15f);
|
|
||||||
float w_forest = weight(temp, humid, 0.75f, 0.75f);
|
|
||||||
// adjust transitions between chunks
|
|
||||||
float pow_n = 8.0f; // the larger n is, the purer the biome
|
|
||||||
w_mountain = std::pow(w_mountain, pow_n) * MOUNTAIN_FREQ;
|
|
||||||
w_plain = std::pow(w_plain, pow_n) * PLAIN_FREQ;
|
|
||||||
w_desert = std::pow(w_desert, pow_n) * DESERT_FREQ;
|
|
||||||
w_forest = std::pow(w_forest, pow_n) * FOREST_FREQ;
|
|
||||||
|
|
||||||
float total = w_mountain + w_plain + w_desert + w_forest;
|
|
||||||
w_mountain /= total; w_plain /= total; w_desert /= total; w_forest /= total;
|
|
||||||
|
|
||||||
auto sample_height = [&](Biome b) -> float {
|
|
||||||
auto range = get_biome_height_range(b);
|
|
||||||
auto [f1, f2, f3] = get_noise_frequencies_for_biome(b);
|
|
||||||
float n =
|
|
||||||
1.00f * PerlinNoise::noise(world_x * f1, 0.5f, world_z * f1) +
|
|
||||||
0.50f * PerlinNoise::noise(world_x * f2, 0.5f, world_z * f2) +
|
|
||||||
0.25f * PerlinNoise::noise(world_x * f3, 0.5f, world_z * f3);
|
|
||||||
n /= 1.75f;
|
|
||||||
return range.base_y + n * range.amplitude;
|
|
||||||
};
|
|
||||||
|
|
||||||
float h = w_mountain * sample_height(Biome::MOUNTAIN)
|
|
||||||
+ w_plain * sample_height(Biome::PLAIN)
|
|
||||||
+ w_desert * sample_height(Biome::DESERT)
|
|
||||||
+ w_forest * sample_height(Biome::FOREST);
|
|
||||||
return static_cast<int>(h);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user