我正在尝试创建一个基于体素的游戏,并且正在渲染数百万个立方体。
为了加快渲染速度,我将立方体组合成块,将 32x32x32 立方体组合成一个网格。这是为了减少对 GPU 的渲染调用次数并提高帧率。
我正在使用 ManualObject 来构建块,并且效果很好。但是,现在的问题是,由于各个块不是绑定到各个场景节点的实体,因此我找不到进行碰撞检测的方法。
食人魔有办法单独处理 ManualObject 的子网格吗?
// Build a face with triangles if the face is visible. Don't bother building faces for hidden faces.
void Chunk::createMesh()
{
begin("BoxColor");
int iVertex = 0;
Block *block;
Block *testingBlock;
for (int x = 0; x < CHUNK_SIZE.x; ++x)
{
for (int y = 0; y < CHUNK_SIZE.y; ++y)
{
for (int z = 0; z < CHUNK_SIZE.z; ++z)
{
block = m_pBlocks[x][y][z];
if (block == NULL)
{
continue;
}
//x-1
testingBlock = 0;
if (x > 0) testingBlock = m_pBlocks[x-1][y][z];
if (testingBlock == 0)
{
position(x, y, z+1); normal(-1,0,0); textureCoord(0, 1);
position(x, y+1, z+1); normal(-1,0,0); textureCoord(1, 1);
position(x, y+1, z); normal(-1,0,0); textureCoord(1, 0);
position(x, y, z); normal(-1,0,0); textureCoord(0, 0);
triangle(iVertex, iVertex+1, iVertex+2);
triangle(iVertex+2, iVertex+3, iVertex);
iVertex += 4;
}
//x+1
testingBlock = 0;
if (x < 0 + CHUNK_SIZE.x - 1) testingBlock = m_pBlocks[x+1][y][z];
if (testingBlock == 0)
{
position(x+1, y, z); normal(1,0,0); textureCoord(0, 1);
position(x+1, y+1, z); normal(1,0,0); textureCoord(1, 1);
position(x+1, y+1, z+1); normal(1,0,0); textureCoord(1, 0);
position(x+1, y, z+1); normal(1,0,0); textureCoord(0, 0);
triangle(iVertex, iVertex+1, iVertex+2);
triangle(iVertex+2, iVertex+3, iVertex);
iVertex += 4;
}
//y-1
testingBlock = 0;
if (y > 0) testingBlock = m_pBlocks[x][y-1][z];
if (testingBlock == 0)
{
position(x, y, z); normal(0,-1,0); textureCoord(0, 1);
position(x+1, y, z); normal(0,-1,0); textureCoord(1, 1);
position(x+1, y, z+1); normal(0,-1,0); textureCoord(1, 0);
position(x, y, z+1); normal(0,-1,0); textureCoord(0, 0);
triangle(iVertex, iVertex+1, iVertex+2);
triangle(iVertex+2, iVertex+3, iVertex);
iVertex += 4;
}
//y+1
testingBlock = 0;
if (y < 0 + CHUNK_SIZE.y - 1) testingBlock = m_pBlocks[x][y+1][z];
if (testingBlock == 0)
{
position(x, y+1, z+1); normal(0,1,0); textureCoord(0, 1);
position(x+1, y+1, z+1); normal(0,1,0); textureCoord(1, 1);
position(x+1, y+1, z); normal(0,1,0); textureCoord(1, 0);
position(x, y+1, z); normal(0,1,0); textureCoord(0, 0);
triangle(iVertex, iVertex+1, iVertex+2);
triangle(iVertex+2, iVertex+3, iVertex);
iVertex += 4;
}
//z-1
testingBlock = 0;
if (z > 0) testingBlock = m_pBlocks[x][y][z-1];
if (testingBlock == 0)
{
position(x, y+1, z); normal(0,0,-1); textureCoord(0, 1);
position(x+1, y+1, z); normal(0,0,-1); textureCoord(1, 1);
position(x+1, y, z); normal(0,0,-1); textureCoord(1, 0);
position(x, y, z); normal(0,0,-1); textureCoord(0, 0);
triangle(iVertex, iVertex+1, iVertex+2);
triangle(iVertex+2, iVertex+3, iVertex);
iVertex += 4;
}
//z+1
testingBlock = 0;
if (z < 0 + CHUNK_SIZE.z - 1) testingBlock = m_pBlocks[x][y][z+1];
if (testingBlock == 0)
{
position(x, y, z+1); normal(0,0,1); textureCoord(0, 1);
position(x+1, y, z+1); normal(0,0,1); textureCoord(1, 1);
position(x+1, y+1, z+1); normal(0,0,1); textureCoord(1, 0);
position(x, y+1, z+1); normal(0,0,1); textureCoord(0, 0);
triangle(iVertex, iVertex+1, iVertex+2);
triangle(iVertex+2, iVertex+3, iVertex);
iVertex += 4;
}
}
}
}
end();
}