Destroy chunked block like Minecraft

I am trying to do a Minecraft-like game (like every one have already tried I guess) and i went across the problem i expected: lag with world Generation. I did lot of searching but i can

I tried to combine them into a bigger mesh, which work, but not i can destroy a single block. So how can I place/destroy chunked (mesh combined) block? If i cant, can someone clearly explain me a solution (i am not fluent in english nor with programming expressions, i can code tho) and how to do it (with a script example if possible :) ) ? Thanks in advance !

I generated my world using 2D perlin noise. Once i have the blocks, i split them into 16x16x100 chunk meshes.

Before Making chunks, i first tried to combine all of them into one block (using the unity manual), but the result is only one cube placed at 0,0,0. Where are all the other cubes ?

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ThisWorldGen : MonoBehaviour {

public GameObject blockPrefab;
public GameObject grass;
public GameObject dirt;
public GameObject rock;
private string[] blockTag;
private GameObject stockin;

public int amp;
public int freq;
private Vector3 mypos;
[SerializeField]
private Material material;
private Vector3 blockLocations;

public List<MeshFilter> meshes = new List<MeshFilter>();

void Start()
{
Generate();
}

void Generate()
{
amp = Random.Range(0,100);
freq = Random.Range(30,100);
mypos = this.transform.position;
int cols = 100;
int rows = 100;
float startTime = Time.realtimeSinceStartup;
#region Create Mesh Data
MeshFilter blockMeshes = Instantiate(blockPrefab, Vector3.zero, Quaternion.identity).GetComponent<MeshFilter>();

for(int x = 0; x < cols;x++)
{
for(int z = 0; z < rows;z++)
{
float y = Mathf.PerlinNoise ((mypos.x + x) / freq,(mypos.z + z)/freq) * amp;
y = Mathf.Floor(y);
for(float hy = y; y > 0.0F; y -= 1.0F)
{
blockMeshes.transform.position = new Vector3(mypos.x + x,y,mypos.z +z);//move the unit cube to the intended position
meshes.Add(blockMeshes);
}
}
}
int i = 0;
MeshFilter[] listMeshes = new MeshFilter[meshes.Count];
foreach(MeshFilter cubesToAdd in meshes)
{
listMeshes[i] = cubesToAdd;
i++;
}
CombineInstance[] combine = new CombineInstance[listMeshes.Length];
int w = 0;
while(w < listMeshes.Length)
{
combine[w].mesh = listMeshes[w].sharedMesh;
combine[w].transform = listMeshes[w].transform.localToWorldMatrix;
listMeshes[w].gameObject.SetActive(false);
w++;
}
Debug.Log (combine.Length);
transform.GetComponent<MeshFilter>().mesh = new Mesh();
transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
transform.gameObject.SetActive(true);
#endregion
Debug.Log("Loaded in " + (Time.realtimeSinceStartup - startTime) + " Seconds.");
}
}


(the world was generating randomly and perfectly before trying to merge the meshes)

• "Once I have the blocks, I split them into 16x16x100 chunk meshes" - how does that chunk mesh creation look? Can you add/remove a block in your source data (as though the Perlin noise had given you a different value at that point), then repeat the chunk mesh creation from source, to produce a modified chunk? Jun 5 '19 at 2:40
• I guess checking the array of block value and then deleting the one i click and update the mesh is what you mean ? Jun 5 '19 at 2:49
• Sounds like that could work. Did you have any trouble implementing it that way? Jun 5 '19 at 2:55
• Haven't really tried. I tought about something than could not work, but i dont remember what it is. Is it how minecraft display millions of blocks ? Jun 5 '19 at 2:58
• Try it, then ask here if you run into trouble. :) Jun 5 '19 at 2:59

1 Answer

If you combined all of your blocks into a single mesh, then in order to remove a single block you'll need to generate a new mesh without the block, or depending on your game engine, adjust the vertices of your mesh to account for the missing block.

Given a large enough mesh, both solutions are likely to be very slow. Combining a lot of objects into a single mesh isn't very performant when each object is individually modifiable. Consider batching several blocks into smaller meshes, thus reducing the cost of the destruction of each block, or profiling your initial world generation in order to understand where is performance bottleneck, and optimizing around it. For example, you could evaluate the world lazily, and thus only compute the minimal set of necessary blocks.

• My first question is: what is a vectice ? And you mean having more smaller combined mesh than less bigger one ? Jun 5 '19 at 11:44
• vectice? I think you have a typo. and yes, have multiple smaller meshes. Jun 5 '19 at 11:46
• "vertices" is the plural form of "vertex". Vertices are the points forming the corners of your mesh's triangles. I think you're already using the advice nihohit offers here, since you mention your chunks are 16x16x100. You could of course try adjusting the size of each chunk if you find that 16x16 blocks are too coarse/too fine for your needs. Jun 5 '19 at 13:55
• Ok, thanks for the advice. I already started working on it. I should come up with the result in the next week, im very busy. Jun 6 '19 at 12:11
• I added the code i tried, if could can help me to figure out why its not working, id be thankful :) Jun 7 '19 at 4:41