问题描述
我目前正在使用 DirectX11 制作像 Minecraft 这样的体素游戏。 游戏像任何其他体素游戏一样使用块系统,但我当前用于生成块网格的算法不可扩展。 块类有一些属性,如块完整和网格类型。
class Block
{
public:
bool isFull = true;
MeshType type = MeshType::FullBlock;
Vector2i texture = { 9,1 };
Vector2i topTexture = { 9,1 };
const char* sound;
Block(){}
Block(bool isFull,MeshType type,Vector2i texture,Vector2i topTexture,const char* sound): isFull(isFull),type(type),texture(texture),topTexture(topTexture),sound(sound){}
Block(bool isFull,const char* sound) : isFull(isFull),topTexture(texture),sound(sound) {}
Block(bool isFull,Vector2i texture) : isFull(isFull),topTexture(texture) {}
};
然后在向量中初始化每个块
blocks.reserve(64);
Block air(false,MeshType::Empty,{0,0});
blocks.emplace_back(air);
Block grass(true,MeshType::FullBlock,{ 3,0 },{ 0,"Audio/grass1.ogg");
blocks.emplace_back(grass);
Block stone(true,{ 1,"Audio/stone1.ogg");
blocks.emplace_back(stone);
Block rose(false,MeshType::Cross,{ 12,"Audio/grass1.ogg");
blocks.emplace_back(rose);
Block wheat(false,MeshType::Hash,{ 8,3 });
blocks.emplace_back(wheat);
//and so on...
我有一个接受顶点向量和块数据的函数。 该函数通过一些优化循环遍历所有块,并将数据放回发送到缓冲区的向量中。
for (int x = 0; x < ChunkWidth; x++)
{
for (int y = 0; y < ChunkHeight; y++)
{
for (int z = 0; z < ChunkWidth; z++)
{
if (IsDrawable[x][y][z] == 1)
{
switch (blocks[chunk->BlockID[x + 16 * y + 16 * 256 * z]].type)
{
case MeshType::FullBlock:
BuildBlock(chunk,vertices,x,y,z);
break;
case MeshType::Cross:
FillCross(vertices,blocks[chunk->BlockID[x + 16 * y + 16 * 256 * z]],x + chunk->x * ChunkWidth,z + chunk->z * ChunkWidth);
break;
case MeshType::Hash:
FillHash(vertices,z + chunk->z * ChunkWidth);
break;
}
}
}
}
}
对于每一种新的网格类型,switch 语句都会变得更大,我认为这不是可行的方法。 我问是否有更好的方法来做到这一点。 我先谢谢你。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)