I have an array of cubes (around 200~20 000) which are in different positions, but identical mesh and material. I want to combine all cubes to optimize for rendering. By using
mesh.CombineMeshes( CombineInstance[] )
of a same mesh (Cube) but different positions, it says “Cannot combine into a mesh that is also in the CombineInstances input: Cube”
What is “mesh” and how is that variable initialized? Have you actually create a new Mesh object? Because the error clearly said that you try to combine all those meshes into a mesh that is also in the list of object you want to combine. That doesn’t work. You need a new / separate mesh that takes the combined mesh. You need to show more context. That single line of code is not really helpful.
Hello.
Thank you for your answer.
The context is enough, an array of cubes, they are all identical mesh and material. And like you said, the function seems to doesn’t allow me add the same mesh 2nd times. But I know that Unity’s algorithm can combine itself multiple times (at the different positions), so I asked a way bypass that.
Hello.
Thank you for your answer.
I just wanted to use the built-in features of Unity. But seems like I have no other choice, I will do as you recommended.
No, that’s not what I said and is not what the error said. You seem to try to combine the meshes INTO a mesh that is part of the source list. So you would write into a mesh instance that is part of the source meshes. This is not allowed. That’s why I ask where your “mesh” in your code comes from. Did you do Mesh mesh = new Mesh();?
public class TestCombine : MonoBehaviour
{
public Mesh mesh; // bound the default cube in the inspector
pubic List<Vector3> positions;// bound in the inspector
public void Combine()
{
Mesh combined = new Mesh();
CombineInstance[] instances = new CombineInstance[positions.Count];
for(int i = 0; i < positions.Count; ++i)
{
instances[i].mesh = this.mesh;
// this is translated correctly, tested
instances[i].transform = translate_position_to_matrix(
positions[i] );
}
// error here
combined.CombineMeshes( instances );
}
}
I took your code, fixed a typo and added the necessary minimum. I added the following script to an empty gameobject in the scene. I selected the default cube mesh in the “mesh” field and the default material in the “mat” field. When I click run, I get no errors and get the following.
using System.Collections.Generic;
using UnityEngine;
public class TestCombine : MonoBehaviour
{
public Mesh mesh;
public Material mat;
public List<Vector3> positions = new List<Vector3> {
new Vector3(-2,0,0),
new Vector3(0, 0, 0),
new Vector3(2, 0, 0)
};
public void Combine()
{
Mesh combined = new Mesh();
CombineInstance[] instances = new CombineInstance[positions.Count];
for (int i = 0; i < positions.Count; ++i)
{
instances[i].mesh = mesh;
instances[i].transform = Matrix4x4.Translate(positions[i]);
}
combined.CombineMeshes(instances);
gameObject.AddComponent<MeshRenderer>().sharedMaterial = mat;
gameObject.AddComponent<MeshFilter>().sharedMesh = combined;
}
public void Start()
{
Combine();
}
}
So it does work without any errors. So please, come up with an actual repo case. You said you simplified your code. This simplification might have removed your bug. So this hasn’t really helped your case
Thank you very much. I found out the problem: I mistaken call mesh.CombineMeshes( instances ) instead of combinedMesh.CombineMeshes( instances ), that was wrong because mesh is the input.
That is a basic error but I didn’t notice that until you showed me the code, how dumb am I.
Thank you very much!