feat: add 4 biomes

This commit is contained in:
2026-04-17 19:55:47 +08:00
parent be176ff18a
commit 5723c17f94
23 changed files with 259 additions and 40 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 418 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 418 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 418 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 418 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 418 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 382 B

After

Width:  |  Height:  |  Size: 418 B

View File

@@ -1,6 +1,6 @@
#pragma once
constexpr int WORLD_SIZE_Y = 256;
constexpr int MAX_BLOCK_NUM = 4;
constexpr int MAX_BLOCK_NUM = 5;
constexpr int MAX_UI_NUM = 1;
constexpr int CHUCK_SIZE = 16;
@@ -9,7 +9,8 @@ constexpr int MAX_BLOCK_STATUS = 1;
constexpr int MAX_CHARACTER = 128;
constexpr float NORMAL_FOV = 70.0f;
constexpr int SEED = 999;
constexpr int MAX_BIOME_SUM = 4;
constexpr float BIOME_NOISE_FREQUENCY = 0.005f;
constexpr float VERTICES_POS[6][6][3] = {
// ===== front (z = +1) =====

View File

@@ -17,6 +17,7 @@ private:
static constexpr int SIZE_Y = WORLD_SIZE_Y;
static constexpr int SIZE_Z = CHUCK_SIZE;
Biome m_biome = Biome::PLAIN;
ChunkPos m_chunk_pos;
World& m_world;
// the index is a array of block id
@@ -28,6 +29,10 @@ private:
float height = 80;
void clear_dirty();
void resolve_biome();
void resolve_blocks();
public:
Chunk(World& world, ChunkPos chunk_pos);
~Chunk();
@@ -35,6 +40,9 @@ public:
Chunk& operator=(const Chunk&) = delete;
Chunk(Chunk&&);
Chunk& operator=(Chunk&&);
Biome get_biome() const;
const std::vector<uint8_t>& get_chunk_blocks() const;
static int get_index(int x, int y, int z);

View File

@@ -1,5 +1,9 @@
#pragma once
#include <functional>
#include <string>
#include <Cubed/tools/log.hpp>
#include <Cubed/tools/cubed_assert.hpp>
struct ChunkPos {
int x;
int z;
@@ -24,6 +28,90 @@ struct ChunkPos {
};
};
enum class Biome {
PLAIN = 0,
FOREST,
DESERT,
MOUNTAIN
};
struct BiomeHeightRange {
int base_y;
int amplitude;
};
constexpr inline std::string get_biome_str(Biome biome) {
std::string str;
using enum Biome;
switch (biome) {
case PLAIN:
str = "Plain";
break;
case FOREST:
str = "Forest";
break;
case DESERT:
str = "Desert";
break;
case MOUNTAIN:
str = "Mountain";
break;
}
return str;
};
inline Biome get_biome_from_noise(float temp, float humid) {
if (temp < 0.5f && humid < 0.5f) return Biome::MOUNTAIN;
if (temp < 0.5f && humid >= 0.5f) return Biome::PLAIN;
if (temp >= 0.5f && humid < 0.5f) return Biome::DESERT;
return Biome::FOREST;
}
constexpr inline std::array<float, 3> get_noise_frequencies_for_biome(Biome biome) {
using enum Biome;
switch (biome) {
case PLAIN:
return {0.003f, 0.008f, 0.018f};
case FOREST:
return {0.004f, 0.012f, 0.022f};
case DESERT:
return {0.003f, 0.008f, 0.018f};
case MOUNTAIN:
return {0.006f, 0.015f, 0.03f};
}
Logger::warn("Unknown Biome");
return {0.003f, 0.015f, 0.06f};
}
constexpr inline BiomeHeightRange get_biome_height_range(Biome biome) {
using enum Biome;
switch (biome) {
case PLAIN:
return {62, 4};
case FOREST:
return {64, 8};
case DESERT:
return {61, 8};
case MOUNTAIN:
return {70, 70};
}
Logger::warn("Unknown Biome");
return {62, 4};
}
inline Biome safe_int_to_biome(int x) {
using enum Biome;
static const std::unordered_map<int, Biome> INT_TO_BIOME_MAP {
{0, PLAIN},
{1, FOREST},
{2, DESERT},
{3, MOUNTAIN}
};
auto it = INT_TO_BIOME_MAP.find(x);
CUBED_ASSERT_MSG(it != INT_TO_BIOME_MAP.end(), ":Can't Find");
return it->second;
}

View File

@@ -27,7 +27,7 @@ private:
std::vector<glm::vec4> m_planes;
std::thread m_gen_thread;
std::mutex m_chunks_mutex;
mutable std::mutex m_chunks_mutex;
std::mutex m_gen_signal_mutex;
std::mutex m_new_chunk_queue_mutex;
std::mutex m_delete_vbo_mutex;
@@ -54,7 +54,10 @@ public:
bool can_move(const AABB& player_box) const;
//const BlockRenderData& get_block_render_data(int x, int y ,int z);
const std::optional<LookBlock>& get_look_block_pos(const std::string& name) const;
const Chunk* get_chunk(const ChunkPos& pos) const;
Player& get_player(const std::string& name);
void init_world();
bool is_aabb_in_frustum(const glm::vec3& center, const glm::vec3& half_extents);

View File

@@ -2,4 +2,5 @@
#include <glm/glm.hpp>
namespace Math {
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 biome_noise, float temp, float humid);
}

View File

@@ -21,6 +21,7 @@ void DebugCollector::init_text() {
Text cpu_text("cpu");
Text gpu_text("gpu");
Text opengl_version_text("opengl_version");
Text biome_text("biome");
version_text
.position(0.0f, 100.0f)
.scale(0.8f)
@@ -68,6 +69,10 @@ void DebugCollector::init_text() {
.text("OpenGL: " + std::to_string(GLVersion.major) + "." + std::to_string(GLVersion.minor))
.scale(0.7f)
.position(0.0f, 450.0f);
biome_text
.text("Biome: ")
.scale(0.8f)
.position(0.0f, 500.0f);
m_texts.insert({version_text.uuid(), std::move(version_text)});
m_texts.insert({fps_text.uuid(), std::move(fps_text)});
m_texts.insert({player_pos_text.uuid(), std::move(player_pos_text)});
@@ -77,6 +82,7 @@ void DebugCollector::init_text() {
m_texts.insert({cpu_text.uuid(), std::move(cpu_text)});
m_texts.insert({gpu_text.uuid(), std::move(gpu_text)});
m_texts.insert({opengl_version_text.uuid(), std::move(opengl_version_text)});
m_texts.insert({biome_text.uuid(), std::move(biome_text)});
}
std::unordered_map<std::size_t, Text>& DebugCollector::all_texts() {

View File

@@ -2,7 +2,10 @@
#include <Cubed/gameplay/world.hpp>
#include <Cubed/tools/cubed_assert.hpp>
#include <Cubed/tools/log.hpp>
#include <Cubed/tools/math_tools.hpp>
#include <Cubed/tools/perlin_noise.hpp>
#include <utility>
Chunk::Chunk(World& world, ChunkPos chunk_pos) :
m_world(world),
m_chunk_pos(chunk_pos)
@@ -23,7 +26,8 @@ Chunk::Chunk(Chunk&& other) :
m_world(other.m_world),
m_blocks(std::move(other.m_blocks)),
m_dirty(other.is_dirty()),
m_vertexs_data(std::move(other.m_vertexs_data))
m_vertexs_data(std::move(other.m_vertexs_data)),
m_biome(other.m_biome)
{
other.m_vbo = 0;
}
@@ -35,9 +39,14 @@ Chunk& Chunk::operator=(Chunk&& other) {
m_blocks = std::move(other.m_blocks);
m_dirty = other.is_dirty();
m_vertexs_data = std::move(other.m_vertexs_data);
m_biome = other.m_biome;
return *this;
}
Biome Chunk::get_biome() const {
return m_biome;
}
const std::vector<uint8_t>& Chunk::get_chunk_blocks() const{
return m_blocks;
}
@@ -212,41 +221,8 @@ const std::vector<Vertex>& Chunk::get_vertex_data() const{
}
void Chunk::init_chunk() {
m_blocks.assign(CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y, 0);
for (int x = 0; x < CHUCK_SIZE; x++) {
for (int y = 0; y < 5; y++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
m_blocks[get_index(x, y, z)] = 3;
}
}
}
for (int x = 0; x < CHUCK_SIZE; x++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
float world_x = static_cast<float>(x + m_chunk_pos.x * CHUCK_SIZE);
float world_z = static_cast<float>(z + m_chunk_pos.z * CHUCK_SIZE);
float noise =
0.5f * PerlinNoise::noise(world_x * 0.01f, world_z * 0.01f, 0.5f) +
0.25f * PerlinNoise::noise(world_x * 0.02f, world_z * 0.02f, 0.5f) +
0.125f * PerlinNoise::noise(world_x * 0.04f, world_z * 0.04f, 0.5f);
int y_max = height * noise;
for (int y = 5; y < y_max - 5; y++) {
m_blocks[get_index(x, y, z)] = 3;
}
for (int y = y_max - 5; y < y_max - 1; y++) {
m_blocks[get_index(x, y, z)] = 2;
}
for (int y = y_max - 1; y < y_max; y++) {
m_blocks[get_index(x, y, z)] = 1;
}
}
}
mark_dirty();
resolve_biome();
resolve_blocks();
}
void Chunk::upload_to_gpu() {
@@ -281,3 +257,72 @@ void Chunk::set_chunk_block(int index ,unsigned id) {
}
void Chunk::resolve_biome() {
float cx = (m_chunk_pos.x + 0.5f) * CHUCK_SIZE;
float cz = (m_chunk_pos.z + 0.5f) * CHUCK_SIZE;
float temp = PerlinNoise::noise(cx * BIOME_NOISE_FREQUENCY, 0.0f, cz * BIOME_NOISE_FREQUENCY);
float humid = PerlinNoise::noise(cx * BIOME_NOISE_FREQUENCY, 1.0f, cz * BIOME_NOISE_FREQUENCY);
m_biome = get_biome_from_noise(temp, humid);
}
void Chunk::resolve_blocks() {
m_blocks.assign(CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y, 0);
for (int x = 0; x < CHUCK_SIZE; x++) {
for (int y = 0; y < 5; y++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
m_blocks[get_index(x, y, z)] = 3;
}
}
}
for (int x = 0; x < CHUCK_SIZE; x++) {
for (int z = 0; z < CHUCK_SIZE; z++) {
float world_x = static_cast<float>(x + m_chunk_pos.x * CHUCK_SIZE);
float world_z = static_cast<float>(z + m_chunk_pos.z * CHUCK_SIZE);
float biome_noise = PerlinNoise::noise(
world_x * BIOME_NOISE_FREQUENCY,
0.5f,
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);
int height = Math::get_interpolated_height(world_x, world_z, biome_noise, temp, humid);
auto biome = get_biome_from_noise(temp, humid);
for (int y = 5; y < height - 5; y++) {
m_blocks[get_index(x, y, z)] = 3;
}
if (biome == Biome::MOUNTAIN) {
for (int y = height - 5; y < height - 1; y++) {
if (y > 101) {
m_blocks[get_index(x, y, z)] = 3;
} else {
m_blocks[get_index(x, y, z)] = 2;
}
}
if (height - 1 > 101) {
m_blocks[get_index(x, height - 1, z)] = 3;
} else {
m_blocks[get_index(x, height - 1, z)] = 1;
}
} else if (biome == Biome::DESERT) {
for (int y = height - 5; y < height; y++) {
m_blocks[get_index(x, y, z)] = 4;
}
} else {
for (int y = height - 5; y < height - 1; y++) {
m_blocks[get_index(x, y, z)] = 2;
}
for (int y = height - 1; y < height; y++) {
m_blocks[get_index(x, y, z)] = 1;
}
}
}
}
mark_dirty();
}

View File

@@ -235,6 +235,14 @@ void Player::check_player_chunk_transition() {
if (cur_pos != m_player_chunk_pos) {
m_world.need_gen();
m_player_chunk_pos = cur_pos;
auto chunk = m_world.get_chunk(cur_pos);
if (chunk == nullptr) {
DebugCollector::get().report("biome", "Biome: Unknown");
} else {
DebugCollector::get()
.report("biome", "Biome: " + get_biome_str(chunk->get_biome()));
}
}
}

View File

@@ -80,6 +80,15 @@ const std::optional<LookBlock>& World::get_look_block_pos(const std::string& nam
}
const Chunk* World::get_chunk(const ChunkPos& pos) const {
std::lock_guard lk(m_chunks_mutex);
auto it = m_chunks.find(pos);
if (it == m_chunks.end()) {
return nullptr;
}
return &it->second;
}
Player& World::get_player(const std::string& name){
auto it = m_players.find(HASH::str(name));
if (it == m_players.end()) {

View File

@@ -12,7 +12,8 @@ constexpr std::array<std::string_view, MAX_BLOCK_NUM> BLOCK_REISTER{
"air",
"grass_block",
"dirt",
"stone"
"stone",
"sand"
};

View File

@@ -1,6 +1,13 @@
#include <Cubed/tools/math_tools.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 {
void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes) {
if (planes.size() != 6) {
@@ -26,4 +33,46 @@ namespace Math {
p = glm::normalize(p);
}
}
int get_interpolated_height(float world_x, float world_z, float biome_noise, 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.25f);
float w_plain = weight(temp, humid, 0.25f, 0.75f);
float w_desert = weight(temp, humid, 0.75f, 0.25f);
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);
w_plain = std::pow(w_plain, pow_n);
w_desert = std::pow(w_desert, pow_n);
w_forest = std::pow(w_forest, pow_n);
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);
}
}