我工作在Minecraft的克隆,我有2個塊加載問題。大塊加載和排序
第一:確定要加載的塊。
我找到一個方式,它的醜陋,但工程快給我
- 定義3D陣列(陣列)(尺寸:MAX_CHUNKS_X,MAX_CHUNKS_Y,MAX_CHUNKS_Z)
- 填寫3D陣列FALSE
- 雖然從通過如果在設置數組[chunk_x] [chunk_y] [chunk_z] = true時,檢查塊是否位於視覺範圍內的塊的列表
- ;
- 通過列表開始bassing陣列
- 對於所有的陣列[chunk_x] [chunk_y] [chunk_z] ==虛假加入LoadingList塊在後chunk_x chunk_y chunk_z
另一種方式來少醜陋,還快?
代碼:
ChunksRenderList.clear();
CChunk* Chunk = NULL;
s32 RootChunk_X_Location = (floor(RenderCenter.x)/CHUNK_SIZE);
s32 RootChunk_Y_Location = (floor(RenderCenter.y)/CHUNK_SIZE);
s32 RootChunk_Z_Location = (floor(RenderCenter.z)/CHUNK_SIZE);
if(RenderCenter.x < 0)
RootChunk_X_Location--;
if(RenderCenter.y < 0)
RootChunk_Y_Location--;
if(RenderCenter.z < 0)
RootChunk_Z_Location--;
core::vector3s RootChunkLocation(RootChunk_X_Location,RootChunk_Y_Location,RootChunk_Z_Location);
u32 XZ_ArraySide = (RenderDistance_XZ*2)+1;
u32 Y_ArraySide = (RenderDistance_Y*2)+1;
char array[XZ_ArraySide][Y_ArraySide][XZ_ArraySide];
memset(array,0,(XZ_ArraySide*XZ_ArraySide*Y_ArraySide));
for(auto it = Chunks.begin(); it != Chunks.end(); it++)
{
Chunk = (it->second);
if(Chunk->Locked)
continue;
if(Chunk->KeepAliveCounter <= 0)
{
ChunksUnloadList.push_back(Chunk);
continue;
}
else
{
Chunk->KeepAliveCounter -= WORLD_UPDATE_PERIOD;
Chunk->DistanceToCamera = RenderCenter.distance_to(Chunk->ChunkAbsolutePosition);
}
if(Chunk->ChunkPosition.x >= (RootChunk_X_Location - (s32)RenderDistance_XZ) && Chunk->ChunkPosition.x <= (RootChunk_X_Location + (s32)RenderDistance_XZ))
if(Chunk->ChunkPosition.y >= (RootChunk_Y_Location - (s32)RenderDistance_Y) && Chunk->ChunkPosition.y <= (RootChunk_Y_Location + (s32)RenderDistance_Y))
if(Chunk->ChunkPosition.z >= (RootChunk_Z_Location - (s32)RenderDistance_XZ) && Chunk->ChunkPosition.z <= (RootChunk_Z_Location + (s32)RenderDistance_XZ))
{
s32 PositionInMatrix_X = Chunk->ChunkPosition.x - (RootChunk_X_Location - (s32)RenderDistance_XZ);
s32 PositionInMatrix_Y = Chunk->ChunkPosition.y - (RootChunk_Y_Location - (s32)RenderDistance_Y);
s32 PositionInMatrix_Z = Chunk->ChunkPosition.z - (RootChunk_Z_Location - (s32)RenderDistance_XZ);
array[PositionInMatrix_X][PositionInMatrix_Y][PositionInMatrix_Z] = true;
Chunk->KeepAliveCounter = CHUNK_LIVE_TIME;
}
if(not Chunk->NeightboarsUpdated)
{
ChunksNeightboarUpdateList.push_back(Chunk);
}
if(not Chunk->ChunkUpdated)
{
ChunksRebuildList.push_back(Chunk);
}
if(not Chunk->Locked and Chunk->VisibleBlocks > 0)
{
ChunksRenderList.push_back(Chunk);
}
}
for(u32 y = 0; y < Y_ArraySide; y++)
for(u32 x = 0; x < XZ_ArraySide; x++)
for(u32 z = 0; z < XZ_ArraySide; z++)
{
s32 ChunkPosition_X = (s32)x + (RootChunk_X_Location - (s32)RenderDistance_XZ);
s32 ChunkPosition_Y = (s32)y + (RootChunk_Y_Location - (s32)RenderDistance_Y);
s32 ChunkPosition_Z = (s32)z + (RootChunk_Z_Location - (s32)RenderDistance_XZ);
if(array[x][y][z] == 0)
{
SPendingToLoad ToLoad;
ToLoad.Position.set(ChunkPosition_X,ChunkPosition_Y,ChunkPosition_Z);
ToLoad.DistanceToCamera = ToLoad.Position.distance_to_sqr(RootChunkLocation);
ChunksLoadList.push_back(ToLoad);
}
}
二: 如何排序ChunksLoadList生效喜歡留在這個PIC https://www.dropbox.com/s/owjfaaekcj2m23w/58f2e4c8.png?dl=0 紅色=最近ChunksLoadList.begin() 藍= farest到ChunksLoadList.begin()
IM嘗試使用
ChunksLoadList.sort([&RootChunkLocation](SPendingToLoad& i,SPendingToLoad& j)
{
return i.DistanceToCamera < j.DistanceToCamera;
}
);
但它的方法,以減緩大視野範圍... 我該如何重寫代碼才能獲得快速波動加載效果?
對不起我的可怕的英語,我希望你能理解我......
與相機的確切距離有多重要?和確切的順序?並且都是浮動的值? – Surt 2014-09-13 14:32:05
Surt,與相機的距離只需要從最近到最遠的排序塊。計算距離不使用sqrt()的函數(x^2 + y^2 + z^2) – AnrgyHumster 2014-09-13 15:35:32
S32是int32_t,並且類型轉換爲S32的變量是float? – Surt 2014-09-15 12:52:38