mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
refactor: chunk interpolate (#6)
* refactor: rewrite blend_heightmap_boundaries * refactor: init_world * fix: unnatural biome boundary transition
This commit is contained in:
@@ -123,14 +123,14 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
|||||||
message(STATUS "Building with AddressSanitizer enabled for target: ${PROJECT_NAME}")
|
message(STATUS "Building with AddressSanitizer enabled for target: ${PROJECT_NAME}")
|
||||||
|
|
||||||
target_compile_options(${PROJECT_NAME} PRIVATE
|
target_compile_options(${PROJECT_NAME} PRIVATE
|
||||||
-fsanitize=address
|
#-fsanitize=address
|
||||||
#-fsanitize=thread
|
#-fsanitize=thread
|
||||||
-fno-omit-frame-pointer
|
-fno-omit-frame-pointer
|
||||||
-g
|
-g
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_options(${PROJECT_NAME} PRIVATE
|
target_link_options(${PROJECT_NAME} PRIVATE
|
||||||
-fsanitize=address
|
#-fsanitize=address
|
||||||
#-fsanitize=thread
|
#-fsanitize=thread
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Cubed/gameplay/chunk_pos.hpp"
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
|
|
||||||
constexpr int WORLD_SIZE_Y = 256;
|
constexpr int WORLD_SIZE_Y = 256;
|
||||||
constexpr int CHUCK_SIZE = 16;
|
constexpr int CHUNK_SIZE = 16;
|
||||||
constexpr int SEA_LEVEL = 64;
|
constexpr int SEA_LEVEL = 64;
|
||||||
|
|
||||||
constexpr int MAX_BLOCK_NUM = 8;
|
constexpr int MAX_BLOCK_NUM = 8;
|
||||||
@@ -22,10 +24,13 @@ constexpr float DEFAULT_MAX_RUN_SPEED = 7.0f;
|
|||||||
constexpr float DEFAULT_ACCELERATION = 10.0f;
|
constexpr float DEFAULT_ACCELERATION = 10.0f;
|
||||||
constexpr float DEFAULT_DECELERATION = 15.0f;
|
constexpr float DEFAULT_DECELERATION = 15.0f;
|
||||||
constexpr float DEFAULT_G = 22.5f;
|
constexpr float DEFAULT_G = 22.5f;
|
||||||
static constexpr int SIZE_X = CHUCK_SIZE;
|
static constexpr int SIZE_X = CHUNK_SIZE;
|
||||||
static constexpr int SIZE_Y = WORLD_SIZE_Y;
|
static constexpr int SIZE_Y = WORLD_SIZE_Y;
|
||||||
static constexpr int SIZE_Z = CHUCK_SIZE;
|
static constexpr int SIZE_Z = CHUNK_SIZE;
|
||||||
|
|
||||||
using HeightMapArray = std::array<std::array<float, CHUCK_SIZE>, CHUCK_SIZE>;
|
constexpr ChunkPos CHUNK_DIR[]{{1, 0}, {-1, 0}, {0, 1}, {0, -1},
|
||||||
|
{1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
|
||||||
|
|
||||||
|
using HeightMapArray = std::array<std::array<int, CHUNK_SIZE>, CHUNK_SIZE>;
|
||||||
|
|
||||||
} // namespace Cubed
|
} // namespace Cubed
|
||||||
@@ -42,6 +42,7 @@ private:
|
|||||||
PlayerProfile m_player_profile;
|
PlayerProfile m_player_profile;
|
||||||
TextEditing m_text_editing;
|
TextEditing m_text_editing;
|
||||||
bool m_need_save_config = false;
|
bool m_need_save_config = false;
|
||||||
|
bool m_gen_thread_running = true;
|
||||||
int m_theme = 0;
|
int m_theme = 0;
|
||||||
void show_about_table_bar();
|
void show_about_table_bar();
|
||||||
void show_biome_table_bar();
|
void show_biome_table_bar();
|
||||||
|
|||||||
@@ -23,12 +23,8 @@ struct BiomeNonAdjacent {
|
|||||||
static inline const std::vector<BiomeNonAdjacent> NON_ADJACENT{
|
static inline const std::vector<BiomeNonAdjacent> NON_ADJACENT{
|
||||||
{{BiomeType::PLAIN, {BiomeType::DESERT}, BiomeType::RIVER},
|
{{BiomeType::PLAIN, {BiomeType::DESERT}, BiomeType::RIVER},
|
||||||
{BiomeType::FOREST, {BiomeType::DESERT}, BiomeType::RIVER},
|
{BiomeType::FOREST, {BiomeType::DESERT}, BiomeType::RIVER},
|
||||||
{BiomeType::DESERT,
|
{BiomeType::DESERT, {BiomeType::FOREST}, BiomeType::RIVER},
|
||||||
{BiomeType::MOUNTAIN, BiomeType::FOREST},
|
{BiomeType::MOUNTAIN, {BiomeType::NONE}, BiomeType::RIVER}}};
|
||||||
BiomeType::RIVER},
|
|
||||||
{BiomeType::MOUNTAIN,
|
|
||||||
{BiomeType::DESERT, BiomeType::FOREST},
|
|
||||||
BiomeType::RIVER}}};
|
|
||||||
|
|
||||||
struct BaseBiomeParams {
|
struct BaseBiomeParams {
|
||||||
BiomeType biome;
|
BiomeType biome;
|
||||||
|
|||||||
@@ -12,5 +12,6 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void build_bottom();
|
void build_bottom();
|
||||||
|
void fill_water();
|
||||||
};
|
};
|
||||||
} // namespace Cubed
|
} // namespace Cubed
|
||||||
@@ -15,11 +15,10 @@ class World;
|
|||||||
// if want to use, do init_chunk(), gen_vertex_data() and
|
// if want to use, do init_chunk(), gen_vertex_data() and
|
||||||
class Chunk {
|
class Chunk {
|
||||||
private:
|
private:
|
||||||
static constexpr int SIZE_X = CHUCK_SIZE;
|
static constexpr int SIZE_X = CHUNK_SIZE;
|
||||||
static constexpr int SIZE_Y = WORLD_SIZE_Y;
|
static constexpr int SIZE_Y = WORLD_SIZE_Y;
|
||||||
static constexpr int SIZE_Z = CHUCK_SIZE;
|
static constexpr int SIZE_Z = CHUNK_SIZE;
|
||||||
|
|
||||||
using HeightMapArray = std::array<std::array<float, SIZE_Z>, SIZE_X>;
|
|
||||||
std::atomic<bool> m_dirty{false};
|
std::atomic<bool> m_dirty{false};
|
||||||
std::atomic<bool> m_need_upload{true};
|
std::atomic<bool> m_need_upload{true};
|
||||||
std::atomic<bool> m_is_on_gen_vertex_data{false};
|
std::atomic<bool> m_is_on_gen_vertex_data{false};
|
||||||
@@ -60,12 +59,13 @@ public:
|
|||||||
// Determine biome from temperature and humidity noise
|
// Determine biome from temperature and humidity noise
|
||||||
void gen_phase_one();
|
void gen_phase_one();
|
||||||
// Resolve biome adjacency conflicts with neighbor chunks
|
// Resolve biome adjacency conflicts with neighbor chunks
|
||||||
void gen_phase_two(const std::array<const Chunk*, 4>& adj_chunks);
|
void gen_phase_two(const std::array<const Chunk*, 8>& adj_chunks);
|
||||||
// Generate heightmap using biome-specific noise
|
// Generate heightmap using biome-specific noise
|
||||||
void gen_phase_three();
|
void gen_phase_three();
|
||||||
// Blend heightmap with neighbors for smooth transitions
|
// Blend heightmap with neighbors for smooth transitions
|
||||||
void gen_phase_four(
|
void gen_phase_four(
|
||||||
const std::array<std::optional<HeightMapArray>, 4>& neighbor_heightmap);
|
const std::array<std::optional<HeightMapArray>, 8>& neighbor_heightmap,
|
||||||
|
const std::array<BiomeType, 8>& neighbor_biome);
|
||||||
// Generate terrain blocks from heightmap and biome
|
// Generate terrain blocks from heightmap and biome
|
||||||
void gen_phase_five();
|
void gen_phase_five();
|
||||||
// Blend surface blocks at chunk borders with neighbors
|
// Blend surface blocks at chunk borders with neighbors
|
||||||
|
|||||||
@@ -25,12 +25,13 @@ public:
|
|||||||
void assign_chunk_biome();
|
void assign_chunk_biome();
|
||||||
// Adjust Biome
|
// Adjust Biome
|
||||||
void resolve_biome_adjacency_conflict(
|
void resolve_biome_adjacency_conflict(
|
||||||
const std::array<const Chunk*, 4>& adj_chunks);
|
const std::array<const Chunk*, 8>& adj_chunks);
|
||||||
// Generate Heightmap
|
// Generate Heightmap
|
||||||
void generate_heightmap();
|
void generate_heightmap();
|
||||||
// Adjust Height
|
// Adjust Height
|
||||||
void blend_heightmap_boundaries(
|
void blend_heightmap_boundaries(
|
||||||
const std::array<std::optional<HeightMapArray>, 4>& neighbor_heightmap);
|
const std::array<std::optional<HeightMapArray>, 8>& neighbor_heightmap,
|
||||||
|
const std::array<BiomeType, 8>& neighbor_biome);
|
||||||
// Generate Block
|
// Generate Block
|
||||||
void generate_terrain_blocks();
|
void generate_terrain_blocks();
|
||||||
// Adjust Block;
|
// Adjust Block;
|
||||||
@@ -39,10 +40,10 @@ public:
|
|||||||
neighbor_block);
|
neighbor_block);
|
||||||
// Generate Structure
|
// Generate Structure
|
||||||
void generate_vegetation();
|
void generate_vegetation();
|
||||||
|
BiomeType get_biome_at(float world_x, float world_z);
|
||||||
Chunk& chunk();
|
Chunk& chunk();
|
||||||
Random& random();
|
Random& random();
|
||||||
bool neighbor_river() const;
|
const std::array<BiomeType, 8>& neighbor_biome() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static inline std::atomic<bool> is_init{false};
|
static inline std::atomic<bool> is_init{false};
|
||||||
@@ -50,11 +51,9 @@ private:
|
|||||||
static inline std::atomic<bool> is_seed_change{false};
|
static inline std::atomic<bool> is_seed_change{false};
|
||||||
Chunk& m_chunk;
|
Chunk& m_chunk;
|
||||||
Random m_random;
|
Random m_random;
|
||||||
std::array<BiomeType, 4> neighbor_biome{BiomeType::NONE, BiomeType::NONE,
|
|
||||||
BiomeType::NONE, BiomeType::NONE};
|
|
||||||
std::unique_ptr<BiomeBuilder> m_biome_builder{nullptr};
|
std::unique_ptr<BiomeBuilder> m_biome_builder{nullptr};
|
||||||
bool is_neighbor_river = false;
|
bool is_cur_chunk_ins = false;
|
||||||
|
std::array<BiomeType, 8> m_neighbor_biome;
|
||||||
void make_biome_builder();
|
void make_biome_builder();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -28,9 +28,9 @@ private:
|
|||||||
using ConstChunkMap =
|
using ConstChunkMap =
|
||||||
std::unordered_map<ChunkPos, const Chunk*, ChunkPos::Hash>;
|
std::unordered_map<ChunkPos, const Chunk*, ChunkPos::Hash>;
|
||||||
using ChunkPosSet = std::unordered_set<ChunkPos, ChunkPos::Hash>;
|
using ChunkPosSet = std::unordered_set<ChunkPos, ChunkPos::Hash>;
|
||||||
|
using ChunkHashMap = std::unordered_map<ChunkPos, Chunk, ChunkPos::Hash>;
|
||||||
glm::vec3 m_gen_player_pos{0.0f, 0.0f, 0.0f};
|
glm::vec3 m_gen_player_pos{0.0f, 0.0f, 0.0f};
|
||||||
std::unordered_map<ChunkPos, Chunk, ChunkPos::Hash> m_chunks;
|
ChunkHashMap m_chunks;
|
||||||
std::unordered_map<std::size_t, Player> m_players;
|
std::unordered_map<std::size_t, Player> m_players;
|
||||||
std::vector<glm::vec4> m_planes;
|
std::vector<glm::vec4> m_planes;
|
||||||
|
|
||||||
@@ -63,11 +63,10 @@ private:
|
|||||||
void
|
void
|
||||||
build_neighbor_context_for_new_chunks(ConstChunkMap& new_chunks_neighbor,
|
build_neighbor_context_for_new_chunks(ConstChunkMap& new_chunks_neighbor,
|
||||||
ChunkPtrUpdateList& affected_neighbor,
|
ChunkPtrUpdateList& affected_neighbor,
|
||||||
const ChunkUpdateList& new_chunks);
|
const ChunkUpdateList& new_chunks,
|
||||||
|
ChunkHashMap& temp_neighbor);
|
||||||
void build_neighbor_context_for_affected_neighbors(ChunkPtrUpdateList&,
|
void build_neighbor_context_for_affected_neighbors(ChunkPtrUpdateList&,
|
||||||
ConstChunkMap&);
|
ConstChunkMap&);
|
||||||
void start_gen_thread();
|
|
||||||
void stop_gen_thread();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
World();
|
World();
|
||||||
@@ -105,6 +104,8 @@ public:
|
|||||||
float chunk_gen_fraction() const;
|
float chunk_gen_fraction() const;
|
||||||
int rendering_distance() const;
|
int rendering_distance() const;
|
||||||
void rendering_distance(int rendering_distance);
|
void rendering_distance(int rendering_distance);
|
||||||
|
void start_gen_thread();
|
||||||
|
void stop_gen_thread();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Cubed
|
} // namespace Cubed
|
||||||
|
|||||||
@@ -387,6 +387,14 @@ void DevPanel::show_world_tab_item() {
|
|||||||
if (ImGui::Button("Spawn Point")) {
|
if (ImGui::Button("Spawn Point")) {
|
||||||
m_player->set_player_pos({0.0f, 255.0f, 0.0f});
|
m_player->set_player_pos({0.0f, 255.0f, 0.0f});
|
||||||
}
|
}
|
||||||
|
ImGui::SameLine();
|
||||||
|
if (ImGui::Checkbox("Gen Thread", &m_gen_thread_running)) {
|
||||||
|
if (m_gen_thread_running) {
|
||||||
|
m_app.world().start_gen_thread();
|
||||||
|
} else {
|
||||||
|
m_app.world().stop_gen_thread();
|
||||||
|
}
|
||||||
|
}
|
||||||
ImGui::Text("Chunk Build Progress\n");
|
ImGui::Text("Chunk Build Progress\n");
|
||||||
ImGui::ProgressBar(m_app.world().chunk_gen_fraction());
|
ImGui::ProgressBar(m_app.world().chunk_gen_fraction());
|
||||||
show_biome_table_bar();
|
show_biome_table_bar();
|
||||||
|
|||||||
@@ -7,12 +7,35 @@ void BiomeBuilder::build_bottom() {
|
|||||||
ChunkGenerator& chunk_generator = get_chunk_generator();
|
ChunkGenerator& chunk_generator = get_chunk_generator();
|
||||||
Chunk& chunk = chunk_generator.chunk();
|
Chunk& chunk = chunk_generator.chunk();
|
||||||
auto& m_blocks = chunk.blocks();
|
auto& m_blocks = chunk.blocks();
|
||||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
for (int x = 0; x < CHUNK_SIZE; x++) {
|
||||||
for (int y = 0; y < 5; y++) {
|
for (int y = 0; y < 5; y++) {
|
||||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
for (int z = 0; z < CHUNK_SIZE; z++) {
|
||||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void BiomeBuilder::fill_water() {
|
||||||
|
ChunkGenerator& chunk_generator = get_chunk_generator();
|
||||||
|
Chunk& chunk = chunk_generator.chunk();
|
||||||
|
auto& m_blocks = chunk.blocks();
|
||||||
|
auto& neighbor = chunk_generator.neighbor_biome();
|
||||||
|
auto& heightmap = chunk.heightmap();
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
if (neighbor[i] == BiomeType::RIVER) {
|
||||||
|
for (int x = 0; x < SIZE_X; x++) {
|
||||||
|
for (int z = 0; z < SIZE_Z; z++) {
|
||||||
|
if (heightmap[x][z] >= SEA_LEVEL) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int height = heightmap[x][z];
|
||||||
|
for (int y = height; y < SEA_LEVEL; y++) {
|
||||||
|
m_blocks[Chunk::get_index(x, y, z)] = 7;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} // namespace Cubed
|
} // namespace Cubed
|
||||||
@@ -15,8 +15,8 @@ void DesertBuilder::build_blocks() {
|
|||||||
auto& m_chunk = m_chunk_generator.chunk();
|
auto& m_chunk = m_chunk_generator.chunk();
|
||||||
auto& m_blocks = m_chunk.blocks();
|
auto& m_blocks = m_chunk.blocks();
|
||||||
auto& m_heightmap = m_chunk.heightmap();
|
auto& m_heightmap = m_chunk.heightmap();
|
||||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
for (int x = 0; x < CHUNK_SIZE; x++) {
|
||||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
for (int z = 0; z < CHUNK_SIZE; z++) {
|
||||||
int height = static_cast<int>(m_heightmap[x][z]);
|
int height = static_cast<int>(m_heightmap[x][z]);
|
||||||
for (int y = 5; y < height - 5; y++) {
|
for (int y = 5; y < height - 5; y++) {
|
||||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||||
@@ -29,24 +29,7 @@ void DesertBuilder::build_blocks() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DesertBuilder::build_vegetation() {
|
void DesertBuilder::build_vegetation() { fill_water(); }
|
||||||
auto& m_chunk = m_chunk_generator.chunk();
|
|
||||||
auto& m_blocks = m_chunk.blocks();
|
|
||||||
auto& m_heightmap = m_chunk.heightmap();
|
|
||||||
if (m_chunk_generator.neighbor_river()) {
|
|
||||||
for (int x = 0; x < SIZE_X; x++) {
|
|
||||||
for (int z = 0; z < SIZE_Z; z++) {
|
|
||||||
int height = static_cast<int>(m_heightmap[x][z]);
|
|
||||||
if (height >= SEA_LEVEL) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
for (int y = height + 1; y < SEA_LEVEL; y++) {
|
|
||||||
m_blocks[Chunk::get_index(x, y, z)] = 7;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ChunkGenerator& DesertBuilder::get_chunk_generator() {
|
ChunkGenerator& DesertBuilder::get_chunk_generator() {
|
||||||
return m_chunk_generator;
|
return m_chunk_generator;
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ void ForestBuilder::build_blocks() {
|
|||||||
auto& m_chunk = m_chunk_generator.chunk();
|
auto& m_chunk = m_chunk_generator.chunk();
|
||||||
auto& m_blocks = m_chunk.blocks();
|
auto& m_blocks = m_chunk.blocks();
|
||||||
auto& m_heightmap = m_chunk.heightmap();
|
auto& m_heightmap = m_chunk.heightmap();
|
||||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
for (int x = 0; x < CHUNK_SIZE; x++) {
|
||||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
for (int z = 0; z < CHUNK_SIZE; z++) {
|
||||||
int height = static_cast<int>(m_heightmap[x][z]);
|
int height = static_cast<int>(m_heightmap[x][z]);
|
||||||
for (int y = 5; y < height - 5; y++) {
|
for (int y = 5; y < height - 5; y++) {
|
||||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||||
@@ -49,20 +49,7 @@ void ForestBuilder::build_vegetation() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto& m_blocks = m_chunk.blocks();
|
fill_water();
|
||||||
if (m_chunk_generator.neighbor_river()) {
|
|
||||||
for (int x = 0; x < SIZE_X; x++) {
|
|
||||||
for (int z = 0; z < SIZE_Z; z++) {
|
|
||||||
int height = static_cast<int>(m_heightmap[x][z]);
|
|
||||||
if (height >= SEA_LEVEL) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
for (int y = height + 1; y < SEA_LEVEL; y++) {
|
|
||||||
m_blocks[Chunk::get_index(x, y, z)] = 7;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ChunkGenerator& ForestBuilder::get_chunk_generator() {
|
ChunkGenerator& ForestBuilder::get_chunk_generator() {
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ void MountainBuilder::build_blocks() {
|
|||||||
auto& m_chunk = m_chunk_generator.chunk();
|
auto& m_chunk = m_chunk_generator.chunk();
|
||||||
auto& m_blocks = m_chunk.blocks();
|
auto& m_blocks = m_chunk.blocks();
|
||||||
auto& m_heightmap = m_chunk.heightmap();
|
auto& m_heightmap = m_chunk.heightmap();
|
||||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
for (int x = 0; x < CHUNK_SIZE; x++) {
|
||||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
for (int z = 0; z < CHUNK_SIZE; z++) {
|
||||||
int height = static_cast<int>(m_heightmap[x][z]);
|
int height = static_cast<int>(m_heightmap[x][z]);
|
||||||
for (int y = 5; y < height - 5; y++) {
|
for (int y = 5; y < height - 5; y++) {
|
||||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||||
@@ -29,32 +29,15 @@ void MountainBuilder::build_blocks() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (height > 110) {
|
if (height > 110) {
|
||||||
m_blocks[Chunk::get_index(x, height - 1, z)] = 3;
|
m_blocks[Chunk::get_index(x, height, z)] = 3;
|
||||||
} else {
|
} else {
|
||||||
m_blocks[Chunk::get_index(x, height - 1, z)] = 1;
|
m_blocks[Chunk::get_index(x, height, z)] = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MountainBuilder::build_vegetation() {
|
void MountainBuilder::build_vegetation() { fill_water(); }
|
||||||
auto& m_chunk = m_chunk_generator.chunk();
|
|
||||||
auto& m_blocks = m_chunk.blocks();
|
|
||||||
auto& m_heightmap = m_chunk.heightmap();
|
|
||||||
if (m_chunk_generator.neighbor_river()) {
|
|
||||||
for (int x = 0; x < SIZE_X; x++) {
|
|
||||||
for (int z = 0; z < SIZE_Z; z++) {
|
|
||||||
int height = static_cast<int>(m_heightmap[x][z]);
|
|
||||||
if (height >= SEA_LEVEL) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
for (int y = height + 1; y < SEA_LEVEL; y++) {
|
|
||||||
m_blocks[Chunk::get_index(x, y, z)] = 7;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ChunkGenerator& MountainBuilder::get_chunk_generator() {
|
ChunkGenerator& MountainBuilder::get_chunk_generator() {
|
||||||
return m_chunk_generator;
|
return m_chunk_generator;
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ void PlainBuilder::build_blocks() {
|
|||||||
auto& m_chunk = m_chunk_generator.chunk();
|
auto& m_chunk = m_chunk_generator.chunk();
|
||||||
auto& m_blocks = m_chunk.blocks();
|
auto& m_blocks = m_chunk.blocks();
|
||||||
auto& m_heightmap = m_chunk.heightmap();
|
auto& m_heightmap = m_chunk.heightmap();
|
||||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
for (int x = 0; x < CHUNK_SIZE; x++) {
|
||||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
for (int z = 0; z < CHUNK_SIZE; z++) {
|
||||||
int height = static_cast<int>(m_heightmap[x][z]);
|
int height = static_cast<int>(m_heightmap[x][z]);
|
||||||
for (int y = 5; y < height - 5; y++) {
|
for (int y = 5; y < height - 5; y++) {
|
||||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||||
@@ -29,24 +29,7 @@ void PlainBuilder::build_blocks() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlainBuilder::build_vegetation() {
|
void PlainBuilder::build_vegetation() { fill_water(); }
|
||||||
auto& m_chunk = m_chunk_generator.chunk();
|
|
||||||
auto& m_blocks = m_chunk.blocks();
|
|
||||||
auto& m_heightmap = m_chunk.heightmap();
|
|
||||||
if (m_chunk_generator.neighbor_river()) {
|
|
||||||
for (int x = 0; x < SIZE_X; x++) {
|
|
||||||
for (int z = 0; z < SIZE_Z; z++) {
|
|
||||||
int height = static_cast<int>(m_heightmap[x][z]);
|
|
||||||
if (height >= SEA_LEVEL) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
for (int y = height + 1; y < SEA_LEVEL; y++) {
|
|
||||||
m_blocks[Chunk::get_index(x, y, z)] = 7;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ChunkGenerator& PlainBuilder::get_chunk_generator() {
|
ChunkGenerator& PlainBuilder::get_chunk_generator() {
|
||||||
return m_chunk_generator;
|
return m_chunk_generator;
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ void RiverBuilder::build_blocks() {
|
|||||||
auto& m_chunk = m_chunk_generator.chunk();
|
auto& m_chunk = m_chunk_generator.chunk();
|
||||||
auto& m_blocks = m_chunk.blocks();
|
auto& m_blocks = m_chunk.blocks();
|
||||||
auto& m_heightmap = m_chunk.heightmap();
|
auto& m_heightmap = m_chunk.heightmap();
|
||||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
for (int x = 0; x < CHUNK_SIZE; x++) {
|
||||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
for (int z = 0; z < CHUNK_SIZE; z++) {
|
||||||
int height = static_cast<int>(m_heightmap[x][z]);
|
int height = static_cast<int>(m_heightmap[x][z]);
|
||||||
for (int y = 5; y < height - 5; y++) {
|
for (int y = 5; y < height - 5; y++) {
|
||||||
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
m_blocks[Chunk::get_index(x, y, z)] = 3;
|
||||||
|
|||||||
@@ -59,15 +59,15 @@ HeightMapArray Chunk::get_heightmap() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int Chunk::get_index(int x, int y, int z) {
|
int Chunk::get_index(int x, int y, int z) {
|
||||||
ASSERT(!(x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y ||
|
ASSERT(!(x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||||
z >= CHUCK_SIZE));
|
z >= CHUNK_SIZE));
|
||||||
if ((x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z < 0 ||
|
if ((x * WORLD_SIZE_Y + y) * CHUNK_SIZE + z < 0 ||
|
||||||
(x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z >=
|
(x * WORLD_SIZE_Y + y) * CHUNK_SIZE + z >=
|
||||||
CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y) {
|
CHUNK_SIZE * CHUNK_SIZE * WORLD_SIZE_Y) {
|
||||||
Logger::error("block pos x {} y {} z {} range error", x, y, z);
|
Logger::error("block pos x {} y {} z {} range error", x, y, z);
|
||||||
ASSERT(0);
|
ASSERT(0);
|
||||||
}
|
}
|
||||||
return (x * WORLD_SIZE_Y + y) * CHUCK_SIZE + z;
|
return (x * WORLD_SIZE_Y + y) * CHUNK_SIZE + z;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Chunk::get_index(const glm::vec3& pos) {
|
int Chunk::get_index(const glm::vec3& pos) {
|
||||||
@@ -89,8 +89,8 @@ void Chunk::gen_vertex_data(
|
|||||||
for (int x = 0; x < SIZE_X; x++) {
|
for (int x = 0; x < SIZE_X; x++) {
|
||||||
for (int y = 0; y < SIZE_Y; y++) {
|
for (int y = 0; y < SIZE_Y; y++) {
|
||||||
for (int z = 0; z < SIZE_Z; z++) {
|
for (int z = 0; z < SIZE_Z; z++) {
|
||||||
int world_x = x + m_chunk_pos.x * CHUCK_SIZE;
|
int world_x = x + m_chunk_pos.x * CHUNK_SIZE;
|
||||||
int world_z = z + m_chunk_pos.z * CHUCK_SIZE;
|
int world_z = z + m_chunk_pos.z * CHUNK_SIZE;
|
||||||
int world_y = y;
|
int world_y = y;
|
||||||
int cur_id = m_blocks[get_index(x, y, z)];
|
int cur_id = m_blocks[get_index(x, y, z)];
|
||||||
// air
|
// air
|
||||||
@@ -121,11 +121,11 @@ void Chunk::gen_vertex_data(
|
|||||||
}
|
}
|
||||||
int x, y, z;
|
int x, y, z;
|
||||||
y = world_ny;
|
y = world_ny;
|
||||||
x = world_nx - neighbor_x * CHUCK_SIZE;
|
x = world_nx - neighbor_x * CHUNK_SIZE;
|
||||||
z = world_nz - neighbor_z * CHUCK_SIZE;
|
z = world_nz - neighbor_z * CHUNK_SIZE;
|
||||||
if (x < 0 || y < 0 || z < 0 ||
|
if (x < 0 || y < 0 || z < 0 ||
|
||||||
x >= CHUCK_SIZE || y >= WORLD_SIZE_Y ||
|
x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||||
z >= CHUCK_SIZE) {
|
z >= CHUNK_SIZE) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +215,7 @@ void Chunk::gen_phase_one() {
|
|||||||
m_generator->assign_chunk_biome();
|
m_generator->assign_chunk_biome();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunk::gen_phase_two(const std::array<const Chunk*, 4>& adj_chunks) {
|
void Chunk::gen_phase_two(const std::array<const Chunk*, 8>& adj_chunks) {
|
||||||
if (!m_generator) {
|
if (!m_generator) {
|
||||||
Logger::error("ChunkGenerator is Nullptr");
|
Logger::error("ChunkGenerator is Nullptr");
|
||||||
return;
|
return;
|
||||||
@@ -232,12 +232,13 @@ void Chunk::gen_phase_three() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Chunk::gen_phase_four(
|
void Chunk::gen_phase_four(
|
||||||
const std::array<std::optional<HeightMapArray>, 4>& neighbor_heightmap) {
|
const std::array<std::optional<HeightMapArray>, 8>& neighbor_heightmap,
|
||||||
|
const std::array<BiomeType, 8>& neighbor_biome) {
|
||||||
if (!m_generator) {
|
if (!m_generator) {
|
||||||
Logger::error("ChunkGenerator is Nullptr");
|
Logger::error("ChunkGenerator is Nullptr");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_generator->blend_heightmap_boundaries(neighbor_heightmap);
|
m_generator->blend_heightmap_boundaries(neighbor_heightmap, neighbor_biome);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunk::gen_phase_five() {
|
void Chunk::gen_phase_five() {
|
||||||
|
|||||||
@@ -57,18 +57,14 @@ void ChunkGenerator::assign_chunk_biome() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ChunkGenerator::resolve_biome_adjacency_conflict(
|
void ChunkGenerator::resolve_biome_adjacency_conflict(
|
||||||
const std::array<const Chunk*, 4>& adj_chunks) {
|
const std::array<const Chunk*, 8>& adj_chunks) {
|
||||||
auto m_biome = m_chunk.biome();
|
auto m_biome = m_chunk.biome();
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
auto& chunk = adj_chunks[i];
|
auto& chunk = adj_chunks[i];
|
||||||
if (chunk == nullptr) {
|
if (chunk == nullptr) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
BiomeType biome = chunk->get_biome();
|
BiomeType biome = chunk->get_biome();
|
||||||
neighbor_biome[i] = biome;
|
|
||||||
if (biome == BiomeType::RIVER) {
|
|
||||||
is_neighbor_river = true;
|
|
||||||
}
|
|
||||||
for (const auto& non : NON_ADJACENT) {
|
for (const auto& non : NON_ADJACENT) {
|
||||||
if (m_biome != non.first) {
|
if (m_biome != non.first) {
|
||||||
continue;
|
continue;
|
||||||
@@ -90,13 +86,13 @@ void ChunkGenerator::generate_heightmap() {
|
|||||||
auto& m_heightmap = m_chunk.heightmap();
|
auto& m_heightmap = m_chunk.heightmap();
|
||||||
auto m_biome = m_chunk.biome();
|
auto m_biome = m_chunk.biome();
|
||||||
|
|
||||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
for (int x = 0; x < CHUNK_SIZE; x++) {
|
||||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
for (int z = 0; z < CHUNK_SIZE; z++) {
|
||||||
|
|
||||||
float world_x = static_cast<float>(x + m_chunk_pos.x * CHUCK_SIZE);
|
float world_x = static_cast<float>(x + m_chunk_pos.x * CHUNK_SIZE);
|
||||||
float world_z = static_cast<float>(z + m_chunk_pos.z * CHUCK_SIZE);
|
float world_z = static_cast<float>(z + m_chunk_pos.z * CHUNK_SIZE);
|
||||||
|
|
||||||
auto sample_height = [&](BiomeType b) -> float {
|
auto sample_height = [&](BiomeType b) -> int {
|
||||||
auto range = get_biome_height_range(b);
|
auto range = get_biome_height_range(b);
|
||||||
auto [f1, f2, f3] = get_noise_frequencies_for_biome(b);
|
auto [f1, f2, f3] = get_noise_frequencies_for_biome(b);
|
||||||
float n = 1.00f * PerlinNoise::noise(world_x * f1, 0.5f,
|
float n = 1.00f * PerlinNoise::noise(world_x * f1, 0.5f,
|
||||||
@@ -106,7 +102,7 @@ void ChunkGenerator::generate_heightmap() {
|
|||||||
0.25f * PerlinNoise::noise(world_x * f3, 0.5f,
|
0.25f * PerlinNoise::noise(world_x * f3, 0.5f,
|
||||||
world_z * f3);
|
world_z * f3);
|
||||||
n /= 1.75f;
|
n /= 1.75f;
|
||||||
return range.base_y + n * range.amplitude;
|
return range.base_y + std::round(n * range.amplitude);
|
||||||
};
|
};
|
||||||
m_heightmap[x][z] = sample_height(m_biome);
|
m_heightmap[x][z] = sample_height(m_biome);
|
||||||
}
|
}
|
||||||
@@ -114,79 +110,240 @@ void ChunkGenerator::generate_heightmap() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ChunkGenerator::blend_heightmap_boundaries(
|
void ChunkGenerator::blend_heightmap_boundaries(
|
||||||
const std::array<std::optional<HeightMapArray>, 4>& neighbor_heightmap) {
|
const std::array<std::optional<HeightMapArray>, 8>& neighbor_heightmap,
|
||||||
|
const std::array<BiomeType, 8>& neighbor_biome) {
|
||||||
auto& m_heightmap = m_chunk.heightmap();
|
auto& m_heightmap = m_chunk.heightmap();
|
||||||
auto m_biome = m_chunk.biome();
|
auto m_biome = m_chunk.biome();
|
||||||
// Width of interpolation influence (in number of cells)
|
m_neighbor_biome = neighbor_biome;
|
||||||
|
|
||||||
for (int x = 0; x < SIZE_X; x++) {
|
|
||||||
for (int z = 0; z < SIZE_Z; z++) {
|
|
||||||
float h = static_cast<float>(m_heightmap[x][z]);
|
|
||||||
float total_weight = 1.0f;
|
|
||||||
float blended = h;
|
|
||||||
|
|
||||||
// --- Right neighbor neighbor[0]: (1, 0) ---
|
// --- Right neighbor neighbor[0]: (1, 0) ---
|
||||||
// Blend when x is close to SIZE_X-1
|
for (int z = 0; z < SIZE_Z; z++) {
|
||||||
if (neighbor_heightmap[0] != std::nullopt &&
|
if (neighbor_heightmap[0] != std::nullopt &&
|
||||||
neighbor_biome[0] != m_biome) {
|
neighbor_biome[0] != m_biome) {
|
||||||
int dist = (SIZE_X - 1) - x; // distance from right border
|
is_cur_chunk_ins = true;
|
||||||
if (dist < BLEND_RADIUS) {
|
int edge_x = CHUNK_SIZE - 1;
|
||||||
// Neighbor's boundary row is its x=0 column
|
int h = m_heightmap[edge_x][z];
|
||||||
float neighbor_h =
|
int neighbor_h = (*neighbor_heightmap[0])[0][z];
|
||||||
static_cast<float>((*neighbor_heightmap[0])[0][z]);
|
if (h <= neighbor_h) {
|
||||||
float t =
|
continue;
|
||||||
1.0f - static_cast<float>(dist) /
|
|
||||||
BLEND_RADIUS; // larger weight when closer
|
|
||||||
// Use smoothstep for a more natural transition
|
|
||||||
t = t * t * (3.0f - 2.0f * t);
|
|
||||||
blended += t * neighbor_h;
|
|
||||||
total_weight += t;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
const int DIR = (edge_x == 0) ? 1 : -1;
|
||||||
|
for (int i = 0; i < BLEND_RADIUS; i++) {
|
||||||
|
int x = edge_x + DIR * i;
|
||||||
|
float t = static_cast<float>(i) / BLEND_RADIUS;
|
||||||
|
|
||||||
|
// float smooth_t = t * t * (3.0f - 2.0f * t);
|
||||||
|
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
|
||||||
|
m_heightmap[x][z] = static_cast<int>(
|
||||||
|
std::round(neighbor_h + (h - neighbor_h) * smooth_t));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// --- Left neighbor neighbor[1]: (-1, 0) ---
|
// --- Left neighbor neighbor[1]: (-1, 0) ---
|
||||||
|
for (int z = 0; z < SIZE_Z; z++) {
|
||||||
if (neighbor_heightmap[1] != std::nullopt &&
|
if (neighbor_heightmap[1] != std::nullopt &&
|
||||||
neighbor_biome[1] != m_biome) {
|
neighbor_biome[1] != m_biome) {
|
||||||
int dist = x; // distance from left border
|
is_cur_chunk_ins = true;
|
||||||
if (dist < BLEND_RADIUS) {
|
int edge_x = 0;
|
||||||
float neighbor_h = static_cast<float>(
|
int h = m_heightmap[edge_x][z];
|
||||||
(*neighbor_heightmap[1])[SIZE_X - 1][z]);
|
int neighbor_h = (*neighbor_heightmap[1])[CHUNK_SIZE - 1][z];
|
||||||
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
|
if (h <= neighbor_h) {
|
||||||
t = t * t * (3.0f - 2.0f * t);
|
continue;
|
||||||
blended += t * neighbor_h;
|
|
||||||
total_weight += t;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const int DIR = (edge_x == 0) ? 1 : -1;
|
||||||
|
for (int i = 0; i < BLEND_RADIUS; i++) {
|
||||||
|
int x = edge_x + DIR * i;
|
||||||
|
float t = static_cast<float>(i) / BLEND_RADIUS;
|
||||||
|
|
||||||
|
// float smooth_t = t * t * (3.0f - 2.0f * t);
|
||||||
|
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
|
||||||
|
m_heightmap[x][z] = static_cast<int>(
|
||||||
|
std::round(neighbor_h + (h - neighbor_h) * smooth_t));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// --- Front neighbor neighbor[2]: (0, 1) ---
|
// --- Front neighbor neighbor[2]: (0, 1) ---
|
||||||
|
for (int x = 0; x < SIZE_X; x++) {
|
||||||
if (neighbor_heightmap[2] != std::nullopt &&
|
if (neighbor_heightmap[2] != std::nullopt &&
|
||||||
neighbor_biome[2] != m_biome) {
|
neighbor_biome[2] != m_biome) {
|
||||||
int dist = (SIZE_Z - 1) - z;
|
is_cur_chunk_ins = true;
|
||||||
if (dist < BLEND_RADIUS) {
|
int edge_z = CHUNK_SIZE - 1;
|
||||||
float neighbor_h =
|
int h = m_heightmap[x][edge_z];
|
||||||
static_cast<float>((*neighbor_heightmap[2])[x][0]);
|
int neighbor_h = (*neighbor_heightmap[2])[x][0];
|
||||||
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
|
if (h <= neighbor_h) {
|
||||||
t = t * t * (3.0f - 2.0f * t);
|
continue;
|
||||||
blended += t * neighbor_h;
|
}
|
||||||
total_weight += t;
|
const int DIR = (edge_z == 0) ? 1 : -1;
|
||||||
|
for (int i = 0; i < BLEND_RADIUS; i++) {
|
||||||
|
int z = edge_z + DIR * i;
|
||||||
|
float t = static_cast<float>(i) / BLEND_RADIUS;
|
||||||
|
|
||||||
|
// float smooth_t = t * t * (3.0f - 2.0f * t);
|
||||||
|
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
|
||||||
|
m_heightmap[x][z] = static_cast<int>(
|
||||||
|
std::round(neighbor_h + (h - neighbor_h) * smooth_t));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Back neighbor neighbor[3]: (0, -1) ---
|
// --- Back neighbor neighbor[3]: (0, -1) ---
|
||||||
|
for (int x = 0; x < SIZE_X; x++) {
|
||||||
if (neighbor_heightmap[3] != std::nullopt &&
|
if (neighbor_heightmap[3] != std::nullopt &&
|
||||||
neighbor_biome[3] != m_biome) {
|
neighbor_biome[3] != m_biome) {
|
||||||
int dist = z;
|
is_cur_chunk_ins = true;
|
||||||
if (dist < BLEND_RADIUS) {
|
int edge_z = 0;
|
||||||
float neighbor_h = static_cast<float>(
|
int h = m_heightmap[x][edge_z];
|
||||||
(*neighbor_heightmap[3])[x][SIZE_Z - 1]);
|
int neighbor_h = (*neighbor_heightmap[3])[x][CHUNK_SIZE - 1];
|
||||||
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
|
if (h <= neighbor_h) {
|
||||||
t = t * t * (3.0f - 2.0f * t);
|
continue;
|
||||||
blended += t * neighbor_h;
|
}
|
||||||
total_weight += t;
|
|
||||||
|
const int DIR = (edge_z == 0) ? 1 : -1;
|
||||||
|
for (int i = 0; i < BLEND_RADIUS; i++) {
|
||||||
|
int z = edge_z + DIR * i;
|
||||||
|
float t = static_cast<float>(i) / BLEND_RADIUS;
|
||||||
|
|
||||||
|
// float smooth_t = t * t * (3.0f - 2.0f * t);
|
||||||
|
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
|
||||||
|
m_heightmap[x][z] = static_cast<int>(
|
||||||
|
std::round(neighbor_h + (h - neighbor_h) * smooth_t));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_heightmap[x][z] = static_cast<int>(blended / total_weight);
|
if (is_cur_chunk_ins) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// --- Right-Front corner neighbor[4]: (1, 1) ---
|
||||||
|
if (neighbor_heightmap[4] != std::nullopt && neighbor_biome[4] != m_biome) {
|
||||||
|
for (int i = 0; i < BLEND_RADIUS; i++) {
|
||||||
|
for (int j = 0; j < BLEND_RADIUS; j++) {
|
||||||
|
int x = (CHUNK_SIZE - 1) - i;
|
||||||
|
int z = (CHUNK_SIZE - 1) - j;
|
||||||
|
int h = m_heightmap[x][z];
|
||||||
|
|
||||||
|
int h_right = (neighbor_heightmap[0] != std::nullopt)
|
||||||
|
? (*neighbor_heightmap[0])[0][z]
|
||||||
|
: h;
|
||||||
|
|
||||||
|
int h_front = (neighbor_heightmap[2] != std::nullopt)
|
||||||
|
? (*neighbor_heightmap[2])[x][0]
|
||||||
|
: h;
|
||||||
|
|
||||||
|
int h_corner = (*neighbor_heightmap[4])[0][0];
|
||||||
|
|
||||||
|
float tx = static_cast<float>(i) / BLEND_RADIUS;
|
||||||
|
float tz = static_cast<float>(j) / BLEND_RADIUS;
|
||||||
|
|
||||||
|
float target_h = h_corner * (1 - tx) * (1 - tz) +
|
||||||
|
h_front * tx * (1 - tz) +
|
||||||
|
h_right * (1 - tx) * tz + h * tx * tz;
|
||||||
|
|
||||||
|
if (h <= static_cast<int>(std::round(target_h)))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
float t = static_cast<float>(std::max(i, j)) / BLEND_RADIUS;
|
||||||
|
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
|
||||||
|
m_heightmap[x][z] = static_cast<int>(
|
||||||
|
std::round(target_h + (h - target_h) * smooth_t));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Left-Front corner neighbor[5]: (-1, 1) ---
|
||||||
|
if (neighbor_heightmap[5] != std::nullopt && neighbor_biome[5] != m_biome) {
|
||||||
|
for (int i = 0; i < BLEND_RADIUS; i++) {
|
||||||
|
for (int j = 0; j < BLEND_RADIUS; j++) {
|
||||||
|
int x = i;
|
||||||
|
int z = (CHUNK_SIZE - 1) - j;
|
||||||
|
int h = m_heightmap[x][z];
|
||||||
|
int h_left = (neighbor_heightmap[1] != std::nullopt)
|
||||||
|
? (*neighbor_heightmap[1])[CHUNK_SIZE - 1][z]
|
||||||
|
: h;
|
||||||
|
int h_front = (neighbor_heightmap[2] != std::nullopt)
|
||||||
|
? (*neighbor_heightmap[2])[x][0]
|
||||||
|
: h;
|
||||||
|
int h_corner = (*neighbor_heightmap[5])[CHUNK_SIZE - 1][0];
|
||||||
|
|
||||||
|
float tx = static_cast<float>(i) / BLEND_RADIUS;
|
||||||
|
float tz = static_cast<float>(j) / BLEND_RADIUS;
|
||||||
|
float target_h = h_corner * (1 - tx) * (1 - tz) +
|
||||||
|
h_front * tx * (1 - tz) +
|
||||||
|
h_left * (1 - tx) * tz + h * tx * tz;
|
||||||
|
|
||||||
|
if (h <= static_cast<int>(std::round(target_h)))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
float t = static_cast<float>(std::max(i, j)) / BLEND_RADIUS;
|
||||||
|
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
|
||||||
|
m_heightmap[x][z] = static_cast<int>(
|
||||||
|
std::round(target_h + (h - target_h) * smooth_t));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Right-Back corner neighbor[6]: (1, -1) ---
|
||||||
|
if (neighbor_heightmap[6] != std::nullopt && neighbor_biome[6] != m_biome) {
|
||||||
|
for (int i = 0; i < BLEND_RADIUS; i++) {
|
||||||
|
for (int j = 0; j < BLEND_RADIUS; j++) {
|
||||||
|
int x = (CHUNK_SIZE - 1) - i;
|
||||||
|
int z = j;
|
||||||
|
int h = m_heightmap[x][z];
|
||||||
|
int h_right = (neighbor_heightmap[0] != std::nullopt)
|
||||||
|
? (*neighbor_heightmap[0])[0][z]
|
||||||
|
: h;
|
||||||
|
int h_back = (neighbor_heightmap[3] != std::nullopt)
|
||||||
|
? (*neighbor_heightmap[3])[x][CHUNK_SIZE - 1]
|
||||||
|
: h;
|
||||||
|
int h_corner = (*neighbor_heightmap[6])[0][CHUNK_SIZE - 1];
|
||||||
|
|
||||||
|
float tx = static_cast<float>(i) / BLEND_RADIUS;
|
||||||
|
float tz = static_cast<float>(j) / BLEND_RADIUS;
|
||||||
|
float target_h = h_corner * (1 - tx) * (1 - tz) +
|
||||||
|
h_back * tx * (1 - tz) +
|
||||||
|
h_right * (1 - tx) * tz + h * tx * tz;
|
||||||
|
|
||||||
|
if (h <= static_cast<int>(std::round(target_h)))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
float t = static_cast<float>(std::max(i, j)) / BLEND_RADIUS;
|
||||||
|
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
|
||||||
|
m_heightmap[x][z] = static_cast<int>(
|
||||||
|
std::round(target_h + (h - target_h) * smooth_t));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Left-Back corner neighbor[7]: (-1, -1) ---
|
||||||
|
if (neighbor_heightmap[7] != std::nullopt && neighbor_biome[7] != m_biome) {
|
||||||
|
for (int i = 0; i < BLEND_RADIUS; i++) {
|
||||||
|
for (int j = 0; j < BLEND_RADIUS; j++) {
|
||||||
|
int x = i;
|
||||||
|
int z = j;
|
||||||
|
int h = m_heightmap[x][z];
|
||||||
|
int h_left = (neighbor_heightmap[1] != std::nullopt)
|
||||||
|
? (*neighbor_heightmap[1])[CHUNK_SIZE - 1][z]
|
||||||
|
: h;
|
||||||
|
int h_back = (neighbor_heightmap[3] != std::nullopt)
|
||||||
|
? (*neighbor_heightmap[3])[x][CHUNK_SIZE - 1]
|
||||||
|
: h;
|
||||||
|
int h_corner =
|
||||||
|
(*neighbor_heightmap[7])[CHUNK_SIZE - 1][CHUNK_SIZE - 1];
|
||||||
|
|
||||||
|
float tx = static_cast<float>(i) / BLEND_RADIUS;
|
||||||
|
float tz = static_cast<float>(j) / BLEND_RADIUS;
|
||||||
|
float target_h = h_corner * (1 - tx) * (1 - tz) +
|
||||||
|
h_back * tx * (1 - tz) +
|
||||||
|
h_left * (1 - tx) * tz + h * tx * tz;
|
||||||
|
|
||||||
|
if (h <= static_cast<int>(std::round(target_h)))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
float t = static_cast<float>(std::max(i, j)) / BLEND_RADIUS;
|
||||||
|
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
|
||||||
|
m_heightmap[x][z] = static_cast<int>(
|
||||||
|
std::round(target_h + (h - target_h) * smooth_t));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -197,7 +354,7 @@ void ChunkGenerator::generate_terrain_blocks() {
|
|||||||
Logger::error("BiomeBuilder is nullptr");
|
Logger::error("BiomeBuilder is nullptr");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_chunk.blocks().assign(CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y, 0);
|
m_chunk.blocks().assign(CHUNK_SIZE * CHUNK_SIZE * WORLD_SIZE_Y, 0);
|
||||||
m_biome_builder->build_biome();
|
m_biome_builder->build_biome();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,8 +382,8 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
|||||||
};
|
};
|
||||||
|
|
||||||
// For each column (x, z)
|
// For each column (x, z)
|
||||||
for (int x = 0; x < CHUCK_SIZE; ++x) {
|
for (int x = 0; x < CHUNK_SIZE; ++x) {
|
||||||
for (int z = 0; z < CHUCK_SIZE; ++z) {
|
for (int z = 0; z < CHUNK_SIZE; ++z) {
|
||||||
// Get the current top block type of this column from m_blocks
|
// Get the current top block type of this column from m_blocks
|
||||||
uint8_t type_self = 0;
|
uint8_t type_self = 0;
|
||||||
int top_y = -1;
|
int top_y = -1;
|
||||||
@@ -241,8 +398,8 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
|||||||
weights[type_self] = 1.0f; // self weight
|
weights[type_self] = 1.0f; // self weight
|
||||||
|
|
||||||
// --- Right neighbor (index 0) ---
|
// --- Right neighbor (index 0) ---
|
||||||
if (neighbor_block[0] && x >= CHUCK_SIZE - BLEND_RADIUS) {
|
if (neighbor_block[0] && x >= CHUNK_SIZE - BLEND_RADIUS) {
|
||||||
int dist = (CHUCK_SIZE - 1) - x;
|
int dist = (CHUNK_SIZE - 1) - x;
|
||||||
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
|
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
|
||||||
t = t * t * (3.0f - 2.0f * t); // smoothstep
|
t = t * t * (3.0f - 2.0f * t); // smoothstep
|
||||||
if (t > 0.0f) {
|
if (t > 0.0f) {
|
||||||
@@ -259,14 +416,14 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
|||||||
t = t * t * (3.0f - 2.0f * t);
|
t = t * t * (3.0f - 2.0f * t);
|
||||||
if (t > 0.0f) {
|
if (t > 0.0f) {
|
||||||
uint8_t type_neighbor = get_top_block_from_neighbor(
|
uint8_t type_neighbor = get_top_block_from_neighbor(
|
||||||
*neighbor_block[1], CHUCK_SIZE - 1, z);
|
*neighbor_block[1], CHUNK_SIZE - 1, z);
|
||||||
weights[type_neighbor] += t;
|
weights[type_neighbor] += t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Front neighbor (index 2) ---
|
// --- Front neighbor (index 2) ---
|
||||||
if (neighbor_block[2] && z >= CHUCK_SIZE - BLEND_RADIUS) {
|
if (neighbor_block[2] && z >= CHUNK_SIZE - BLEND_RADIUS) {
|
||||||
int dist = (CHUCK_SIZE - 1) - z;
|
int dist = (CHUNK_SIZE - 1) - z;
|
||||||
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
|
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
|
||||||
t = t * t * (3.0f - 2.0f * t);
|
t = t * t * (3.0f - 2.0f * t);
|
||||||
if (t > 0.0f) {
|
if (t > 0.0f) {
|
||||||
@@ -283,7 +440,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
|||||||
t = t * t * (3.0f - 2.0f * t);
|
t = t * t * (3.0f - 2.0f * t);
|
||||||
if (t > 0.0f) {
|
if (t > 0.0f) {
|
||||||
uint8_t type_neighbor = get_top_block_from_neighbor(
|
uint8_t type_neighbor = get_top_block_from_neighbor(
|
||||||
*neighbor_block[3], x, CHUCK_SIZE - 1);
|
*neighbor_block[3], x, CHUNK_SIZE - 1);
|
||||||
weights[type_neighbor] += t;
|
weights[type_neighbor] += t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -304,13 +461,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
|||||||
if (m_chunk.biome() == BiomeType::RIVER && final_type == 1) {
|
if (m_chunk.biome() == BiomeType::RIVER && final_type == 1) {
|
||||||
final_type = 2;
|
final_type = 2;
|
||||||
}
|
}
|
||||||
if (is_neighbor_river && final_type == 1) {
|
|
||||||
if (top_y < SEA_LEVEL) {
|
|
||||||
final_type = 2;
|
|
||||||
} else {
|
|
||||||
final_type = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m_blocks[Chunk::get_index(x, top_y, z)] = final_type;
|
m_blocks[Chunk::get_index(x, top_y, z)] = final_type;
|
||||||
// bottom block
|
// bottom block
|
||||||
unsigned fill_type = 2;
|
unsigned fill_type = 2;
|
||||||
@@ -363,6 +514,7 @@ void ChunkGenerator::make_biome_builder() {
|
|||||||
Chunk& ChunkGenerator::chunk() { return m_chunk; }
|
Chunk& ChunkGenerator::chunk() { return m_chunk; }
|
||||||
|
|
||||||
Random& ChunkGenerator::random() { return m_random; }
|
Random& ChunkGenerator::random() { return m_random; }
|
||||||
bool ChunkGenerator::neighbor_river() const { return is_neighbor_river; }
|
const std::array<BiomeType, 8>& ChunkGenerator::neighbor_biome() const {
|
||||||
|
return m_neighbor_biome;
|
||||||
|
}
|
||||||
} // namespace Cubed
|
} // namespace Cubed
|
||||||
@@ -39,8 +39,8 @@ bool build_tree(Chunk& chunk, const glm::ivec3& pos) {
|
|||||||
int x = tree_node.x;
|
int x = tree_node.x;
|
||||||
int y = tree_node.y;
|
int y = tree_node.y;
|
||||||
int z = tree_node.z;
|
int z = tree_node.z;
|
||||||
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y ||
|
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||||
z >= CHUCK_SIZE) {
|
z >= CHUNK_SIZE) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (block[Chunk::get_index(tree_node)] != 0) {
|
if (block[Chunk::get_index(tree_node)] != 0) {
|
||||||
|
|||||||
@@ -11,8 +11,6 @@
|
|||||||
|
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
|
|
||||||
static constexpr ChunkPos CHUNK_DIR[]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
|
|
||||||
|
|
||||||
struct ChunkRenderData {
|
struct ChunkRenderData {
|
||||||
std::array<const std::vector<uint8_t>*, 4> neighbor_block;
|
std::array<const std::vector<uint8_t>*, 4> neighbor_block;
|
||||||
Chunk* chunk;
|
Chunk* chunk;
|
||||||
@@ -67,18 +65,8 @@ Player& World::get_player(const std::string& name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void World::init_world() {
|
void World::init_world() {
|
||||||
m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE);
|
m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE * 4);
|
||||||
auto t1 = std::chrono::system_clock::now();
|
auto t1 = std::chrono::system_clock::now();
|
||||||
for (int s = 0; s < PRE_LOAD_DISTANCE; s++) {
|
|
||||||
for (int t = 0; t < PRE_LOAD_DISTANCE; t++) {
|
|
||||||
int ns = s - PRE_LOAD_DISTANCE / 2;
|
|
||||||
int nt = t - PRE_LOAD_DISTANCE / 2;
|
|
||||||
|
|
||||||
ChunkPos pos{ns, nt};
|
|
||||||
|
|
||||||
m_chunks.emplace(pos, Chunk(*this, pos));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger::info("Max Support Thread is {}",
|
Logger::info("Max Support Thread is {}",
|
||||||
std::thread::hardware_concurrency());
|
std::thread::hardware_concurrency());
|
||||||
@@ -93,95 +81,162 @@ void World::init_world() {
|
|||||||
start_gen_thread();
|
start_gen_thread();
|
||||||
hot_reload();
|
hot_reload();
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
void World::init_chunks() {
|
|
||||||
std::vector<Chunk*> chunk_ptrs;
|
|
||||||
chunk_ptrs.reserve(m_chunks.size());
|
|
||||||
for (auto& [pos, chunk] : m_chunks) {
|
|
||||||
chunk_ptrs.push_back(&chunk);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::for_each(std::execution::par, chunk_ptrs.begin(), chunk_ptrs.end(),
|
|
||||||
[](auto& chunk){ chunk->init_chunk();
|
|
||||||
});
|
|
||||||
|
|
||||||
std::atomic<int> sync{0};
|
|
||||||
sync.store(1, std::memory_order_release);
|
|
||||||
sync.load(std::memory_order_acquire);
|
|
||||||
|
|
||||||
std::vector<ChunkRenderData> pending_gen_data;
|
|
||||||
pending_gen_data.reserve(m_chunks.size());
|
|
||||||
for (auto& [pos, chunk] : m_chunks) {
|
|
||||||
ChunkRenderData data;
|
|
||||||
data.chunk = &chunk;
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
auto it = m_chunks.find(pos + CHUNK_DIR[i]);
|
|
||||||
if (it != m_chunks.end()) {
|
|
||||||
data.neighbor_block[i] = &(it->second.get_chunk_blocks());
|
|
||||||
} else {
|
|
||||||
data.neighbor_block[i] = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pending_gen_data.emplace_back(std::move(data));
|
|
||||||
}
|
|
||||||
std::for_each(std::execution::par, pending_gen_data.begin(),
|
|
||||||
pending_gen_data.end(), [](ChunkRenderData& data){ if(!data.chunk) { return ;
|
|
||||||
}
|
|
||||||
data.chunk->gen_vertex_data(data.neighbor_block);
|
|
||||||
});
|
|
||||||
for (auto& chunk_map : m_chunks) {
|
|
||||||
auto& [chunk_pos, chunk] = chunk_map;
|
|
||||||
chunk.upload_to_gpu();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
void World::init_chunks() {
|
void World::init_chunks() {
|
||||||
|
|
||||||
|
int dis_x = PRE_LOAD_DISTANCE;
|
||||||
|
int dis_z = PRE_LOAD_DISTANCE;
|
||||||
|
for (int x = 0; x < dis_x; x++) {
|
||||||
|
for (int z = 0; z < dis_z; z++) {
|
||||||
|
int nx = x - dis_x / 2;
|
||||||
|
int nz = z - dis_z / 2;
|
||||||
|
ChunkPos pos{nx, nz};
|
||||||
|
auto it = m_chunks.find(pos);
|
||||||
|
if (it == m_chunks.end()) {
|
||||||
|
m_chunks.emplace(pos, Chunk(*this, pos));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ChunkHashMap temp_neighbor;
|
||||||
|
for (int x = 0; x < dis_x + 2; x++) {
|
||||||
|
for (int z = 0; z < dis_z + 2; z++) {
|
||||||
|
int nx = x - (dis_x + 2) / 2;
|
||||||
|
int nz = z - (dis_z + 2) / 2;
|
||||||
|
ChunkPos pos{nx, nz};
|
||||||
|
auto it = m_chunks.find(pos);
|
||||||
|
if (it == m_chunks.end()) {
|
||||||
|
auto it = temp_neighbor.find(pos);
|
||||||
|
if (it == temp_neighbor.end()) {
|
||||||
|
temp_neighbor.emplace(pos, Chunk(*this, pos));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
for (auto& [pos, chunks] : m_chunks) {
|
for (auto& [pos, chunks] : m_chunks) {
|
||||||
chunks.gen_phase_one();
|
chunks.gen_phase_one();
|
||||||
}
|
}
|
||||||
std::array<const Chunk*, 4> neighbor_chunks;
|
for (auto& [pos, chunks] : temp_neighbor) {
|
||||||
|
chunks.gen_phase_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::array<const Chunk*, 8> neighbor_chunks;
|
||||||
for (auto& [pos, chunks] : m_chunks) {
|
for (auto& [pos, chunks] : m_chunks) {
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
auto neighbor_pos = pos + CHUNK_DIR[i];
|
auto neighbor_pos = pos + CHUNK_DIR[i];
|
||||||
auto it = m_chunks.find(neighbor_pos);
|
auto it = m_chunks.find(neighbor_pos);
|
||||||
if (it == m_chunks.end()) {
|
if (it == m_chunks.end()) {
|
||||||
|
auto it = temp_neighbor.find(neighbor_pos);
|
||||||
|
if (it == temp_neighbor.end()) {
|
||||||
neighbor_chunks[i] = nullptr;
|
neighbor_chunks[i] = nullptr;
|
||||||
|
ASSERT_MSG(false, "Neighbor Chunk is nullptr");
|
||||||
|
} else {
|
||||||
|
neighbor_chunks[i] = &it->second;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
neighbor_chunks[i] = &it->second;
|
||||||
|
}
|
||||||
|
chunks.gen_phase_two(neighbor_chunks);
|
||||||
|
}
|
||||||
|
for (auto& [pos, chunks] : temp_neighbor) {
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
auto neighbor_pos = pos + CHUNK_DIR[i];
|
||||||
|
auto it = m_chunks.find(neighbor_pos);
|
||||||
|
if (it == m_chunks.end()) {
|
||||||
|
auto it = temp_neighbor.find(neighbor_pos);
|
||||||
|
if (it == temp_neighbor.end()) {
|
||||||
|
neighbor_chunks[i] = nullptr;
|
||||||
|
} else {
|
||||||
|
neighbor_chunks[i] = &it->second;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
neighbor_chunks[i] = &it->second;
|
neighbor_chunks[i] = &it->second;
|
||||||
}
|
}
|
||||||
chunks.gen_phase_two(neighbor_chunks);
|
chunks.gen_phase_two(neighbor_chunks);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& [pos, chunks] : m_chunks) {
|
for (auto& [pos, chunks] : m_chunks) {
|
||||||
chunks.gen_phase_three();
|
chunks.gen_phase_three();
|
||||||
}
|
}
|
||||||
std::array<std::optional<HeightMapArray>, 4> neighbor_chunk_heightmap;
|
for (auto& [pos, chunks] : temp_neighbor) {
|
||||||
for (auto& [pos, chunks] : m_chunks) {
|
chunks.gen_phase_three();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
for (auto& [pos, chunks] : temp_neighbor) {
|
||||||
|
std::array<std::optional<HeightMapArray>, 8>
|
||||||
|
neighbor_chunk_heightmap;
|
||||||
|
std::array<BiomeType, 8> neighbor_biome;
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
auto neighbor_pos = pos + CHUNK_DIR[i];
|
auto neighbor_pos = pos + CHUNK_DIR[i];
|
||||||
auto it = m_chunks.find(neighbor_pos);
|
auto it = m_chunks.find(neighbor_pos);
|
||||||
if (it == m_chunks.end()) {
|
if (it == m_chunks.end()) {
|
||||||
|
auto it = temp_neighbor.find(neighbor_pos);
|
||||||
|
if (it == temp_neighbor.end()) {
|
||||||
neighbor_chunk_heightmap[i] = std::nullopt;
|
neighbor_chunk_heightmap[i] = std::nullopt;
|
||||||
|
neighbor_biome[i] = BiomeType::NONE;
|
||||||
|
} else {
|
||||||
|
neighbor_chunk_heightmap[i] =
|
||||||
|
it->second.get_heightmap();
|
||||||
|
neighbor_biome[i] = it->second.biome();
|
||||||
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
neighbor_chunk_heightmap[i] = it->second.get_heightmap();
|
neighbor_chunk_heightmap[i] = it->second.get_heightmap();
|
||||||
|
neighbor_biome[i] = it->second.biome();
|
||||||
|
}
|
||||||
|
chunks.gen_phase_four(neighbor_chunk_heightmap, neighbor_biome);
|
||||||
|
}
|
||||||
|
for (auto& [pos, chunks] : m_chunks) {
|
||||||
|
std::array<std::optional<HeightMapArray>, 8>
|
||||||
|
neighbor_chunk_heightmap;
|
||||||
|
std::array<BiomeType, 8> neighbor_biome;
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
|
||||||
|
auto neighbor_pos = pos + CHUNK_DIR[i];
|
||||||
|
auto it = m_chunks.find(neighbor_pos);
|
||||||
|
if (it == m_chunks.end()) {
|
||||||
|
auto it = temp_neighbor.find(neighbor_pos);
|
||||||
|
if (it == temp_neighbor.end()) {
|
||||||
|
neighbor_chunk_heightmap[i] = std::nullopt;
|
||||||
|
neighbor_biome[i] = BiomeType::NONE;
|
||||||
|
ASSERT_MSG(false, "Neighbor Chunk is nullptr");
|
||||||
|
} else {
|
||||||
|
neighbor_chunk_heightmap[i] =
|
||||||
|
it->second.get_heightmap();
|
||||||
|
neighbor_biome[i] = it->second.biome();
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
neighbor_chunk_heightmap[i] = it->second.get_heightmap();
|
||||||
|
neighbor_biome[i] = it->second.biome();
|
||||||
|
}
|
||||||
|
chunks.gen_phase_four(neighbor_chunk_heightmap, neighbor_biome);
|
||||||
}
|
}
|
||||||
chunks.gen_phase_four(neighbor_chunk_heightmap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& [pos, chunks] : m_chunks) {
|
for (auto& [pos, chunks] : m_chunks) {
|
||||||
chunks.gen_phase_five();
|
chunks.gen_phase_five();
|
||||||
}
|
}
|
||||||
|
for (auto& [pos, chunks] : temp_neighbor) {
|
||||||
|
chunks.gen_phase_five();
|
||||||
|
}
|
||||||
std::array<std::optional<std::vector<uint8_t>>, 4> neighbor_block;
|
std::array<std::optional<std::vector<uint8_t>>, 4> neighbor_block;
|
||||||
for (auto& [pos, chunks] : m_chunks) {
|
for (auto& [pos, chunks] : m_chunks) {
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
auto neighbor_pos = pos + CHUNK_DIR[i];
|
auto neighbor_pos = pos + CHUNK_DIR[i];
|
||||||
auto it = m_chunks.find(neighbor_pos);
|
auto it = m_chunks.find(neighbor_pos);
|
||||||
if (it == m_chunks.end()) {
|
if (it == m_chunks.end()) {
|
||||||
|
auto it = temp_neighbor.find(neighbor_pos);
|
||||||
|
if (it == temp_neighbor.end()) {
|
||||||
neighbor_block[i] = std::nullopt;
|
neighbor_block[i] = std::nullopt;
|
||||||
|
ASSERT_MSG(false, "Neighbor Chunk is nullptr");
|
||||||
|
} else {
|
||||||
|
neighbor_block[i] = it->second.get_chunk_blocks();
|
||||||
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
neighbor_block[i] = it->second.get_chunk_blocks();
|
neighbor_block[i] = it->second.get_chunk_blocks();
|
||||||
@@ -191,6 +246,7 @@ void World::init_chunks() {
|
|||||||
for (auto& [pos, chunks] : m_chunks) {
|
for (auto& [pos, chunks] : m_chunks) {
|
||||||
chunks.gen_phase_seven();
|
chunks.gen_phase_seven();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::atomic<int> sync{0};
|
std::atomic<int> sync{0};
|
||||||
sync.store(1, std::memory_order_release);
|
sync.store(1, std::memory_order_release);
|
||||||
sync.load(std::memory_order_acquire);
|
sync.load(std::memory_order_acquire);
|
||||||
@@ -253,16 +309,16 @@ void World::render(const glm::mat4& mvp_matrix) {
|
|||||||
ChunkPos World::chunk_pos(int world_x, int world_z) {
|
ChunkPos World::chunk_pos(int world_x, int world_z) {
|
||||||
int chunk_x, chunk_z;
|
int chunk_x, chunk_z;
|
||||||
if (world_x < 0) {
|
if (world_x < 0) {
|
||||||
chunk_x = (world_x + 1) / CHUCK_SIZE - 1;
|
chunk_x = (world_x + 1) / CHUNK_SIZE - 1;
|
||||||
}
|
}
|
||||||
if (world_x >= 0) {
|
if (world_x >= 0) {
|
||||||
chunk_x = world_x / CHUCK_SIZE;
|
chunk_x = world_x / CHUNK_SIZE;
|
||||||
}
|
}
|
||||||
if (world_z < 0) {
|
if (world_z < 0) {
|
||||||
chunk_z = (world_z + 1) / CHUCK_SIZE - 1;
|
chunk_z = (world_z + 1) / CHUNK_SIZE - 1;
|
||||||
}
|
}
|
||||||
if (world_z >= 0) {
|
if (world_z >= 0) {
|
||||||
chunk_z = world_z / CHUCK_SIZE;
|
chunk_z = world_z / CHUNK_SIZE;
|
||||||
}
|
}
|
||||||
return {chunk_x, chunk_z};
|
return {chunk_x, chunk_z};
|
||||||
}
|
}
|
||||||
@@ -292,19 +348,35 @@ void World::gen_chunks_internal() {
|
|||||||
ConstChunkMap new_chunks_neighbor;
|
ConstChunkMap new_chunks_neighbor;
|
||||||
// affected neighbor
|
// affected neighbor
|
||||||
ChunkPtrUpdateList affected_neighbor;
|
ChunkPtrUpdateList affected_neighbor;
|
||||||
build_neighbor_context_for_new_chunks(new_chunks_neighbor,
|
ChunkHashMap temp_neighbor;
|
||||||
affected_neighbor, new_chunks);
|
build_neighbor_context_for_new_chunks(
|
||||||
|
new_chunks_neighbor, affected_neighbor, new_chunks, temp_neighbor);
|
||||||
std::array<const std::vector<uint8_t>*, 4> neighbor_block;
|
Logger::info("Temp neighbor sum {}", temp_neighbor.size());
|
||||||
// build new chunk, but the neighbor in m_chunks also need to re-build
|
// build new chunk, but the neighbor in m_chunks also need to re-build
|
||||||
|
|
||||||
for (auto& [pos, chunk] : new_chunks) {
|
for (auto& [pos, chunk] : new_chunks) {
|
||||||
chunk.gen_phase_one();
|
chunk.gen_phase_one();
|
||||||
}
|
}
|
||||||
|
for (auto& [pos, chunk] : temp_neighbor) {
|
||||||
|
chunk.gen_phase_one();
|
||||||
|
}
|
||||||
m_chunk_gen_fraction = 0.2f;
|
m_chunk_gen_fraction = 0.2f;
|
||||||
std::array<const Chunk*, 4> neighbor_chunks;
|
std::array<const Chunk*, 8> neighbor_chunks;
|
||||||
for (auto& [pos, chunks] : new_chunks) {
|
for (auto& [pos, chunks] : new_chunks) {
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
|
auto neighbor_pos = pos + CHUNK_DIR[i];
|
||||||
|
auto it = new_chunks_neighbor.find(neighbor_pos);
|
||||||
|
if (it == new_chunks_neighbor.end()) {
|
||||||
|
neighbor_chunks[i] = nullptr;
|
||||||
|
ASSERT_MSG(false, "Cant Find Neighbot");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
neighbor_chunks[i] = it->second;
|
||||||
|
}
|
||||||
|
chunks.gen_phase_two(neighbor_chunks);
|
||||||
|
}
|
||||||
|
for (auto& [pos, chunks] : temp_neighbor) {
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
auto neighbor_pos = pos + CHUNK_DIR[i];
|
auto neighbor_pos = pos + CHUNK_DIR[i];
|
||||||
auto it = new_chunks_neighbor.find(neighbor_pos);
|
auto it = new_chunks_neighbor.find(neighbor_pos);
|
||||||
if (it == new_chunks_neighbor.end()) {
|
if (it == new_chunks_neighbor.end()) {
|
||||||
@@ -319,27 +391,59 @@ void World::gen_chunks_internal() {
|
|||||||
for (auto& [pos, chunks] : new_chunks) {
|
for (auto& [pos, chunks] : new_chunks) {
|
||||||
chunks.gen_phase_three();
|
chunks.gen_phase_three();
|
||||||
}
|
}
|
||||||
|
for (auto& [pos, chunks] : temp_neighbor) {
|
||||||
|
chunks.gen_phase_three();
|
||||||
|
}
|
||||||
m_chunk_gen_fraction = 0.4f;
|
m_chunk_gen_fraction = 0.4f;
|
||||||
std::array<std::optional<HeightMapArray>, 4> neighbor_chunk_heightmap;
|
|
||||||
for (auto& [pos, chunks] : new_chunks) {
|
|
||||||
{
|
|
||||||
// std::lock_guard lk(m_chunks_mutex);
|
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
|
for (auto& [pos, chunks] : temp_neighbor) {
|
||||||
|
std::array<std::optional<HeightMapArray>, 8>
|
||||||
|
neighbor_chunk_heightmap;
|
||||||
|
// std::lock_guard lk(m_chunks_mutex);
|
||||||
|
std::array<BiomeType, 8> neighbor_biome;
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
auto neighbor_pos = pos + CHUNK_DIR[i];
|
auto neighbor_pos = pos + CHUNK_DIR[i];
|
||||||
auto it = new_chunks_neighbor.find(neighbor_pos);
|
auto it = new_chunks_neighbor.find(neighbor_pos);
|
||||||
if (it == new_chunks_neighbor.end()) {
|
if (it == new_chunks_neighbor.end()) {
|
||||||
neighbor_chunk_heightmap[i] = std::nullopt;
|
neighbor_chunk_heightmap[i] = std::nullopt;
|
||||||
|
neighbor_biome[i] = BiomeType::NONE;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
neighbor_chunk_heightmap[i] = it->second->get_heightmap();
|
neighbor_chunk_heightmap[i] = it->second->get_heightmap();
|
||||||
|
neighbor_biome[i] = it->second->biome();
|
||||||
|
}
|
||||||
|
|
||||||
|
chunks.gen_phase_four(neighbor_chunk_heightmap, neighbor_biome);
|
||||||
|
}
|
||||||
|
for (auto& [pos, chunks] : new_chunks) {
|
||||||
|
std::array<std::optional<HeightMapArray>, 8>
|
||||||
|
neighbor_chunk_heightmap;
|
||||||
|
// std::lock_guard lk(m_chunks_mutex);
|
||||||
|
std::array<BiomeType, 8> neighbor_biome;
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
auto neighbor_pos = pos + CHUNK_DIR[i];
|
||||||
|
auto it = new_chunks_neighbor.find(neighbor_pos);
|
||||||
|
if (it == new_chunks_neighbor.end()) {
|
||||||
|
neighbor_chunk_heightmap[i] = std::nullopt;
|
||||||
|
neighbor_biome[i] = BiomeType::NONE;
|
||||||
|
ASSERT_MSG(false, "Cant Find Neighbot");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
neighbor_chunk_heightmap[i] = it->second->get_heightmap();
|
||||||
|
neighbor_biome[i] = it->second->biome();
|
||||||
|
}
|
||||||
|
|
||||||
|
chunks.gen_phase_four(neighbor_chunk_heightmap, neighbor_biome);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
chunks.gen_phase_four(neighbor_chunk_heightmap);
|
|
||||||
}
|
|
||||||
m_chunk_gen_fraction = 0.5f;
|
m_chunk_gen_fraction = 0.5f;
|
||||||
for (auto& [pos, chunks] : new_chunks) {
|
for (auto& [pos, chunks] : new_chunks) {
|
||||||
chunks.gen_phase_five();
|
chunks.gen_phase_five();
|
||||||
}
|
}
|
||||||
|
for (auto& [pos, chunks] : temp_neighbor) {
|
||||||
|
chunks.gen_phase_five();
|
||||||
|
}
|
||||||
std::array<std::optional<std::vector<uint8_t>>, 4> neighbor_blocks_data;
|
std::array<std::optional<std::vector<uint8_t>>, 4> neighbor_blocks_data;
|
||||||
for (auto& [pos, chunks] : new_chunks) {
|
for (auto& [pos, chunks] : new_chunks) {
|
||||||
{
|
{
|
||||||
@@ -359,7 +463,9 @@ void World::gen_chunks_internal() {
|
|||||||
for (auto& [pos, chunks] : new_chunks) {
|
for (auto& [pos, chunks] : new_chunks) {
|
||||||
chunks.gen_phase_seven();
|
chunks.gen_phase_seven();
|
||||||
}
|
}
|
||||||
|
|
||||||
m_chunk_gen_fraction = 0.6f;
|
m_chunk_gen_fraction = 0.6f;
|
||||||
|
std::array<const std::vector<uint8_t>*, 4> neighbor_block;
|
||||||
for (auto& [pos, chunk] : new_chunks) {
|
for (auto& [pos, chunk] : new_chunks) {
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
auto it = new_chunks_neighbor.find(pos + CHUNK_DIR[i]);
|
auto it = new_chunks_neighbor.find(pos + CHUNK_DIR[i]);
|
||||||
@@ -441,7 +547,7 @@ void World::sync_and_collect_missing_chunks(
|
|||||||
|
|
||||||
void World::build_neighbor_context_for_new_chunks(
|
void World::build_neighbor_context_for_new_chunks(
|
||||||
ConstChunkMap& new_chunks_neighbor, ChunkPtrUpdateList& affected_neighbor,
|
ConstChunkMap& new_chunks_neighbor, ChunkPtrUpdateList& affected_neighbor,
|
||||||
const ChunkUpdateList& new_chunks) {
|
const ChunkUpdateList& new_chunks, ChunkHashMap& temp_neighbor) {
|
||||||
{
|
{
|
||||||
std::lock_guard lk(m_chunks_mutex);
|
std::lock_guard lk(m_chunks_mutex);
|
||||||
for (auto& [pos, chunk] : new_chunks) {
|
for (auto& [pos, chunk] : new_chunks) {
|
||||||
@@ -450,6 +556,8 @@ void World::build_neighbor_context_for_new_chunks(
|
|||||||
if (it != m_chunks.end()) {
|
if (it != m_chunks.end()) {
|
||||||
new_chunks_neighbor.insert({it->first, &(it->second)});
|
new_chunks_neighbor.insert({it->first, &(it->second)});
|
||||||
affected_neighbor.push_back({it->first, &(it->second)});
|
affected_neighbor.push_back({it->first, &(it->second)});
|
||||||
|
} else {
|
||||||
|
temp_neighbor.emplace(pos + dir, Chunk(*this, pos + dir));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -457,6 +565,9 @@ void World::build_neighbor_context_for_new_chunks(
|
|||||||
for (auto& [pos, chunk] : new_chunks) {
|
for (auto& [pos, chunk] : new_chunks) {
|
||||||
new_chunks_neighbor.insert({pos, &chunk});
|
new_chunks_neighbor.insert({pos, &chunk});
|
||||||
}
|
}
|
||||||
|
for (auto& [pos, chunk] : temp_neighbor) {
|
||||||
|
new_chunks_neighbor.insert({pos, &chunk});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::build_neighbor_context_for_affected_neighbors(
|
void World::build_neighbor_context_for_affected_neighbors(
|
||||||
@@ -544,10 +655,10 @@ int World::get_block(const glm::ivec3& block_pos) const {
|
|||||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
||||||
int x, y, z;
|
int x, y, z;
|
||||||
y = block_pos.y;
|
y = block_pos.y;
|
||||||
x = block_pos.x - chunk_x * CHUCK_SIZE;
|
x = block_pos.x - chunk_x * CHUNK_SIZE;
|
||||||
z = block_pos.z - chunk_z * CHUCK_SIZE;
|
z = block_pos.z - chunk_z * CHUNK_SIZE;
|
||||||
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y ||
|
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||||
z >= CHUCK_SIZE) {
|
z >= CHUNK_SIZE) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return chunk_blocks[Chunk::get_index(x, y, z)];
|
return chunk_blocks[Chunk::get_index(x, y, z)];
|
||||||
@@ -564,10 +675,10 @@ bool World::is_block(const glm::ivec3& block_pos) const {
|
|||||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
||||||
int x, y, z;
|
int x, y, z;
|
||||||
y = block_pos.y;
|
y = block_pos.y;
|
||||||
x = block_pos.x - chunk_x * CHUCK_SIZE;
|
x = block_pos.x - chunk_x * CHUNK_SIZE;
|
||||||
z = block_pos.z - chunk_z * CHUCK_SIZE;
|
z = block_pos.z - chunk_z * CHUNK_SIZE;
|
||||||
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y ||
|
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||||
z >= CHUCK_SIZE) {
|
z >= CHUNK_SIZE) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto id = chunk_blocks[Chunk::get_index(x, y, z)];
|
auto id = chunk_blocks[Chunk::get_index(x, y, z)];
|
||||||
@@ -595,10 +706,10 @@ void World::set_block(const glm::ivec3& block_pos, unsigned id) {
|
|||||||
|
|
||||||
int x, y, z;
|
int x, y, z;
|
||||||
y = world_y;
|
y = world_y;
|
||||||
x = world_x - chunk_x * CHUCK_SIZE;
|
x = world_x - chunk_x * CHUNK_SIZE;
|
||||||
z = world_z - chunk_z * CHUCK_SIZE;
|
z = world_z - chunk_z * CHUNK_SIZE;
|
||||||
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y ||
|
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||||
z >= CHUCK_SIZE) {
|
z >= CHUNK_SIZE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -677,14 +788,14 @@ void World::update(float delta_time) {
|
|||||||
}
|
}
|
||||||
m_render_snapshots.push_back(
|
m_render_snapshots.push_back(
|
||||||
{chunk.get_vbo(), chunk.get_vertex_sum(),
|
{chunk.get_vbo(), chunk.get_vertex_sum(),
|
||||||
glm::vec3(static_cast<float>(pos.x * CHUCK_SIZE) +
|
glm::vec3(static_cast<float>(pos.x * CHUNK_SIZE) +
|
||||||
static_cast<float>(CHUCK_SIZE / 2),
|
static_cast<float>(CHUNK_SIZE / 2),
|
||||||
static_cast<float>(WORLD_SIZE_Y / 2),
|
static_cast<float>(WORLD_SIZE_Y / 2),
|
||||||
static_cast<float>(pos.z * CHUCK_SIZE) +
|
static_cast<float>(pos.z * CHUNK_SIZE) +
|
||||||
static_cast<float>(CHUCK_SIZE / 2)),
|
static_cast<float>(CHUNK_SIZE / 2)),
|
||||||
glm::vec3(static_cast<float>(CHUCK_SIZE / 2),
|
glm::vec3(static_cast<float>(CHUNK_SIZE / 2),
|
||||||
static_cast<float>(WORLD_SIZE_Y / 2),
|
static_cast<float>(WORLD_SIZE_Y / 2),
|
||||||
static_cast<float>(CHUCK_SIZE / 2))});
|
static_cast<float>(CHUNK_SIZE / 2))});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user