Jump to content
  • Advertisement
Sign in to follow this  
Zaphyk

Chunk Optimization Algorithm

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

If you intended to correct an error in the post then please contact us.

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

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!