Advertisement

Chunk Optimization Algorithm

Started by February 09, 2016 02:32 AM
-1 comments, last by Zaphyk 8 years, 10 months ago

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)
					cube = CubeData.CutCubeFace(cube, CutFaceMode.ADD, Face.FRONT);
				if(xN)
				        cube = CubeData.CutCubeFace(cube, CutFaceMode.ADD, Face.BACK);
				if(zN)
				        cube = CubeData.CutCubeFace(cube, CutFaceMode.ADD, Face.LEFT);
				if(zP)
					cube = CubeData.CutCubeFace(cube, CutFaceMode.ADD, Face.RIGHT);
				if(yP)
					cube = CubeData.CutCubeFace(cube, CutFaceMode.ADD, Face.UP);
				if(yN)
					cube = CubeData.CutCubeFace(cube, CutFaceMode.ADD, Face.DOWN);
								
				if(cube.HasFaces())
					Mesh.AddCube(cube);

			}
		}
	}

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

scJg3vf.png?1

Those are air blocks separeted

bkg3N0T.png?1

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!!!!

This topic is closed to new replies.

Advertisement