我的问题是非常相似,这邻居块,但我不能完全做出来,因为数学需要是有一点不同:https://gamedev.stackexchange.com/questions/65800/when-storing-voxels-in-chunks-how-do-i-access-them-at-the-world-level获取存在于不同的块
我试图删除块的一些面孔。如果在当前块之外,我无法解决如何获取块。
我有一个Chunk类来跟踪自己的块。为了测试,我的块是2×2,每块可以容纳4块,都是实心的。
class Chunk {
private Blocks[,,] blocks;
public Vector3 position;
public Chunk(Vector3 world_pos){
position = world_position;
// Loops for populating blocks array...
...
// Create mesh
for(...){
for(...){
for(...){
Block block = GetBlock(x, y, z + 1); // Chance of this block being outside of this chunk
}
}
}
}
public Block GetBlock(int x, int y, int z){
...
// If block is not in this chunk, use global GetBlock instead
}
}
我有称为“getBlock”块试图得到基于在x,y和z位置的块内的方法。如果索引超出范围,那么我会调用一个全局“getBlock”,它试图查找块和块内部的块。这是我挣扎的地方。
这是我的“getChunk”方法(不是我的代码,在教程中找到),这似乎很好。
public Chunk getChunk(int x, int y, int z){
float s = CHUNK_SIZE;
float d = CHUNK_DEPTH;
int posx = Mathf.FloorToInt(x/s) * CHUNK_SIZE;
int posy = Mathf.FloorToInt(y/d) * CHUNK_DEPTH;
int posz = Mathf.FloorToInt(z/s) * CHUNK_SIZE;
Chunk chunk = null;
this.chunks.TryGetValue(new Vector3(posx, posy, posz), out chunk);
return chunk;
}
这是我的全球“getBlock”。
public Block getBlock(int x, int y, int z){
Chunk chunk = this.getChunk(x, y, z);
Block block;
if(chunk == null){
block = Block_Types.AIR;
} else {
// Need to work out the block position I want here
int posx = x;
int posy = y;
int posz = z;
block = chunk.getBlock(posx, posy, posz);
}
return block;
}
这里是一个图片,如果有帮助:
感谢
这将真正受益于挖掘Minecraft的块代码。本质上你需要修改从世界坐标到块坐标的xyz值。 – Draco18s