Public Group

# Chunk Optimization Algorithm

This topic is 1080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am struggling to create an algorithm to remove the unseen triangles from the mesh of the chunk I tried by checking if the adyacent voxels are air. Here is some of my code

	for(int y=CHUNK_HEIGHT-1;y>0;y--){
for(int x=0;x<CHUNK_WIDTH;x++){
for(int z=0;z<CHUNK_WIDTH;z++){

if(!m_blocks[x,y,z].IsActive)
continue;

CubeData cube = new CubeData(CubeData.CreateCubeColor(m_blocks[x,y,z].GetColor(m_blocks[x,y,z].Type)),new OpenTK.Vector3(x+OffsetX, y, z+OffsetZ));

cube = CubeData.CutCubeFace(cube, CutFaceMode.ONLY, Face.ALL);

bool yP = false, yN = false, xP = false, xN = false, zP = false, zN = false;

if(x < CHUNK_WIDTH-1 && m_blocks[x+1,y,z].Type == BlockType.AIR)
xP = true;

if(x > 0 && m_blocks[x-1,y,z].Type == BlockType.AIR)
xN = true;

if(z < CHUNK_WIDTH-1 && m_blocks[x,y,z+1].Type == BlockType.AIR)
zP = true;

if(z > 0 && m_blocks[x,y,z-1].Type == BlockType.AIR)
zN = true;

if(y < CHUNK_HEIGHT-1 && m_blocks[x,y+1,z].Type == BlockType.AIR)
yP = true;

if(y > 0 && m_blocks[x,y-1,z].Type == BlockType.AIR)
yN = true;

if(xP)
if(xN)
if(zN)
if(zP)
if(yP)
if(yN)

if(cube.HasFaces())

}
}
}


However this lead to some weird effects when 2 air blocks are adyacent, below are some images.

Those are air blocks separeted

As you can see when more air blocks are added just some faces appear incomplete or without sense.

Thank you for your time and If anyone can give me any advice or help i would appreciate it.

EDIT: I already solved it, but always remeber PREMATURE OPTIMIZATION IS THE ROOT OF ALL EVIL!!!!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634148
• Total Posts
3015797
×