#include "terrain_genlayer.h" #include #include "chunk.h" #include "utils/bmutils.h" #include "heightmap_genlayer.h" #include "river_genlayer.h" using namespace godot; TerrainGenlayer::TerrainGenlayer(BMWorld * world,HeightmapGenlayer* heightmapGenlayer,RiverGenlayer* riverGenlayer) : GenerationLayer3D(world){ this->heightmapGenlayer = heightmapGenlayer; this->riverGenLayer = riverGenlayer; } TerrainChunk* TerrainGenlayer::GetChunk(Vector3i pos){ auto found = chunks.find(pos); TerrainChunk* chunk; // TerrainChunk * chunk; if(found == chunks.end()){ chunk = Generate(pos); chunks.insert(std::pair(pos,chunk)); }else{ chunk = found->second; } return chunk; } TerrainChunk * TerrainGenlayer::Generate(Vector3i chunkPos){ UtilityFunctions::print("Generating chunk ",chunkPos," in terrain GenLayer"); UpdateEditorParameters(); HeightmapChunk * heightmapChunk = heightmapGenlayer->GetChunk(Vector2i(chunkPos.x,chunkPos.z)); int32_t min = heightmapChunk->GetMinHeight() - chunkPos.y*(int32_t)chunkRes-1; int32_t max = heightmapChunk->GetMaxHeight() - chunkPos.y*(int32_t)chunkRes+1; min = min < 0 ? 0 : min; max = max < 0 ? 0 : max; min = min > chunkRes ? chunkRes : min; max = max > chunkRes ? chunkRes : max; // River* river = riverGenLayer->GetChunk(Vector2i(chunkPos.x,chunkPos.z)); // if(river!=nullptr){ // UtilityFunctions::print("Has river with length ",river->GetLength()," and ", river->GetChunkCount()," chunks, in terrain GenLayer"); // min = 0; // }else{ // min = heightmapGenLayer->Normalized2Real(heightmapChunk->GetMinHeight()) - chunkPos.y*(int32_t)chunkRes-1; // } TerrainChunk * chunk ; // if(river!=nullptr&& BMUtils::globalPos2ChunkPos(river->GetSource(),chunkRes)==chunkPos){ // chunk = new TerrainChunk(chunkRes,min,max,river); // }else{ River* river = heightmapChunk->GetRiverSource(); if(river!=nullptr){ if(BMUtils::globalPos2ChunkPos(river->GetSource(),chunkRes)==chunkPos){ chunk = new TerrainChunk(chunkRes,min,max,river); }else chunk = new TerrainChunk(chunkRes,min,max); }else chunk = new TerrainChunk(chunkRes,min,max); // } for(int32_t i = 0; i < chunkRes; i++){ for(int32_t j = min; j < max; j++){ for(int32_t k = 0; k < chunkRes; k++){ Vector3i outPos(i,j,k); Vector2i inPos(outPos.x,outPos.z); float_t v = heightmapChunk->GetLocalCell(inPos); // if(river!=nullptr){ // if(river->ContainsChunk(Vector2i(chunkPos.x,chunkPos.z))){ // float_t riverValue = river->GetChunk(Vector2i(chunkPos.x,chunkPos.z))->GetLocalCell(inPos); // if(riverValue < INFINITY) v = riverValue; // } // } int32_t height = chunkPos.y*(int32_t)chunkRes + j; v = v - height; uint32_t mat = height > 30 ? 2 : 1; mat = v < 0.0f ? 0 : mat; if(height < 20 && v <= 0.0f){ v = 1; mat = 3; } chunk->SetValue(outPos,v,mat); } } } // River // auto river = riverGenLayer->GetChunk(Vector2i(chunkPos.x,chunkPos.z)); // for(uint32_t k = 0; k < river->GetLength();k++){ // float_t radius = river->GetRadius(k); // int32_t intRadius = BMUtils::ceil(radius); // Vector3i cellPos3d = river->GetCell(k); // Vector3i origin = Vector3i(cellPos3d.x,0,cellPos3d.z); // float_t minHeight = INFINITY; // for(int32_t i = -intRadius; i < intRadius;i++){ // for(int32_t j = -intRadius; j < intRadius;j++){ // Vector2i globalPos = Vector2i(i,j) + Vector2i(origin.x,origin.z); // Vector2i chunkPos = BMUtils::globalPos2ChunkPos(globalPos,chunkRes); // RiverChunk* chunk; // if(!river->ContainsChunk(chunkPos)){ // chunk = new RiverChunk(chunkRes); // river->AddChunk(chunkPos,chunk); // rivers.insert(std::pair(chunkPos,river)); // }else // chunk = river->GetChunk(chunkPos); // Vector2i localPos = BMUtils::mod(globalPos,chunkRes); // float_t length = Vector2i(i,j).length(); // float_t d = length / radius; // if(d <= 1.0){ // d = (1.0f-d) * fallOff; // float_t height = noiseGenlayer->GetValue(globalPos); // d = height - d; // height = height; // minHeight = height < minHeight ? height : minHeight; // float_t oldValue = chunk->GetLocalCell(localPos); // if(oldValue > d ) // chunk->SetValue(localPos,d); // } // } // } // river->SetCellHeight(k,noiseGenlayer->Normalized2Real(minHeight)); // } return chunk; } void TerrainGenlayer::Clean(){ chunks.clear(); } void TerrainGenlayer::UpdateEditorParametersCallback(){ chunkRes = world->GetChunkRes(); }; TerrainChunk::TerrainChunk(uint32_t res, int min, int max,float_t lowValue,float_t highValue,uint32_t lowMat,uint32_t highMat, River* riverSource){ this->lowValue = lowValue; this->highValue = highValue; this->lowMat = lowMat; this->highMat = highMat; this->riverSource = riverSource; this->res = res; max = max < 0 ? 0 : max; max = max >= res ? res : max; min = min < 0 ? 0 : min; min = min > max ? max : min; minHeight = min; maxHeight = max; diff = maxHeight - minHeight; if(diff>0){ cells = new float_t[res*res*diff]; materialCells = new uint32_t[res*res*diff]; } } TerrainChunk::~TerrainChunk(){ delete[] cells; delete[] materialCells; } void TerrainChunk::SetValue(Vector3i pos, float_t v){ if(pos.y < minHeight) return; if(pos.y >= maxHeight) return; uint32_t memPos = pos.x*res*diff+(pos.y-minHeight)*res+pos.z; cells[memPos] = v; } void TerrainChunk::SetValue(Vector3i pos, float_t v, uint32_t m){ if(pos.y < minHeight) return; if(pos.y >= maxHeight) return; uint32_t memPos = pos.x*res*diff+(pos.y-minHeight)*res+pos.z; cells[memPos] = v; materialCells[memPos] = m; } void TerrainChunk::SetMaterial(Vector3i pos, uint32_t m){ if(pos.y < minHeight) return; if(pos.y >= maxHeight) return; uint32_t memPos = pos.x*res*diff+(pos.y-minHeight)*res+pos.z; materialCells[memPos] = m; } float_t TerrainChunk::GetLocalCell(Vector3i pos){ if(pos.y >= maxHeight) return highValue; if(pos.y < minHeight) return lowValue; return cells[pos.x*res*diff+(pos.y-minHeight)*res+pos.z]; } uint32_t TerrainChunk::GetLocalCellMaterial(Vector3i pos){ if(pos.y >= maxHeight) return highMat; if(pos.y < minHeight) return lowMat; return materialCells[pos.x*res*diff+(pos.y-minHeight)*res+pos.z]; } int TerrainChunk::GetMaxHeight(){ return maxHeight; } int TerrainChunk::GetMinHeight(){ return minHeight; }