How can I make this code add to the mesh instead of over writing it?
I am trying to make a system like Minecraft where an array of values is used to generate a 32^3 cubic chunk, and I'm working on making it so that all block faces in a 'chunk' are combined as one mesh... except for one problem:
In the code below used to render a face of a block in a given position and rotation, instead of adding a face to the mesh of a 'chunk' it is overwriting that chunk's mesh... So I've tried to make a new mesh on a child and combine them, but that is giving me this error "Combine mesh instance 0 is null" on the first attempt to combine meshes, the following attempts to combine seem to do nothing.
Is there a better way to read the cubic array and make a mesh for the visible faces of the blocks?
void AddFace (int x, int y, int z, int side){
MeshFilter mf = transform.GetChild (0).gameObject.GetComponent<MeshFilter> ();
Mesh mesh = mf.mesh;
//Vertices!
Vector3[] vert = new Vector3[4];
if (side == 0) {
vert [0] = new Vector3 (x + 1, y, z + 1);
vert [1] = new Vector3 (x, y, z + 1);
vert [2] = new Vector3 (x + 1, y + 1, z + 1);
vert [3] = new Vector3 (x, y + 1, z + 1);
} else if (side == 1) {
vert [0] = new Vector3 (x + 1, y, z);
vert [1] = new Vector3 (x + 1, y, z + 1);
vert [2] = new Vector3 (x + 1, y + 1, z);
vert [3] = new Vector3 (x + 1, y + 1, z + 1);
**Ect...**
} else if (side == 5) {
vert [0] = new Vector3 (x + 1, y, z);
vert [1] = new Vector3 (x, y, z);
vert [2] = new Vector3 (x + 1, y, z + 1);
vert [3] = new Vector3 (x, y, z + 1);
}
//Triangles!
int[] tri = new int[6];
tri [0] = 0;
tri [1] = 2;
tri [2] = 1;
tri [3] = 2;
tri [4] = 3;
tri [5] = 1;
//Normals!
Vector3[] norm = new Vector3[4];
if (side == 0) {
norm [0] = Vector3.forward;
norm [1] = Vector3.forward;
norm [2] = Vector3.forward;
norm [3] = Vector3.forward;
}
**Ect...**
else if (side == 5) {
norm [0] = -Vector3.up;
norm [1] = -Vector3.up;
norm [2] = -Vector3.up;
norm [3] = -Vector3.up;
}
//UVs
Vector2[] uv = new Vector2[4];
uv [0] = new Vector2 (0, 0);
uv [1] = new Vector2 (0, 1);
uv [2] = new Vector2 (1, 0);
uv [3] = new Vector2 (1, 1);
//Assign!
mesh.vertices = vert;
mesh.triangles = tri;
mesh.normals = norm;
mesh.uv = uv;
//combine!
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
Destroy (this.gameObject.GetComponent<MeshCollider> ());
int i = 0;
while (i < meshFilters.Length) {
combine [i].mesh = meshFilters [i].sharedMesh;
combine [i].transform = meshFilters [i].transform.localToWorldMatrix;
meshFilters [i].gameObject.SetActive (false);
i++;
}
transform.GetComponent<MeshFilter> ().mesh = new Mesh ();
transform.GetComponent<MeshFilter> ().mesh.CombineMeshes (combine, true);
transform.GetComponent<MeshFilter> ().mesh.RecalculateBounds ();
transform.GetComponent<MeshFilter> ().mesh.RecalculateNormals ();
transform.GetComponent<MeshFilter> ().mesh.Optimize ();
this.gameObject.AddComponent<MeshCollider> ();
transform.gameObject.SetActive (true);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612130116im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cubic Voxel World 0 Answers
Why can we add vertex alpha if there are no proper shaders? 1 Answer
Color mismatch between meshes 0 Answers
Ideas on Cloning A Game 0 Answers
my invertion of mesh.triangles is not working correctly, any idea why? 0 Answers