hi, i want to make a copy of a procedurally generated mesh because generation is costly and time consuming and i need many. but there seem to be no copy constructor. instantiate does this according to documentation but it does not work for me so i seem to have some misunderstanding of it. Code (csharp): private static Mesh procmesh = null; public void Start() {// create the mesh at startup or copy it if already existent if(null == procmesh) // it takes a while to create the mesh, to speed things up creation is only done once and then the mesh is cloned procmesh = MakeMesh (); // returns the generated mesh else { Mesh thismesh = GetComponent<Mesh>(); thismesh = Instantiate(procmesh, new Vector3(0.0f, 0.0f, 0.0f), Quaternion.identity) as Mesh; // should make a copy of this but conversion error } } the given error is: if i make no assignment of instantiate it works but i dont see a mesh. so can anyone tell me please how to make a deep copy? every instance needs their own mesh. thanks henry
problem solved the "hard" and inelegant way: Code (csharp): Mesh targetmesh = GetComponent<MeshFilter>().mesh; targetmesh.vertices = procmesh.vertices; targetmesh.normals = procmesh.normals; targetmesh.uv = procmesh.uv; targetmesh.triangles = procmesh.triangles; targetmesh.tangents = procmesh.tangents; Another question: when procmesh has been optimized before (Mesh.Optimize()) its still required to call targetmesh.Optimize() right? or is the optimized data copied?
Mesh.Optimize essentially just reorders the geometry data arrays to create triangle strips and make the best use of the vertex cache. The optimised ordering of the data will be preserved if it is copied to another mesh.
Same problem here… tried using Instantiate but the two meshes are still related when changes are made. Is copying each mesh property at a time really the only way to do it? // Edit: nevemind. I think it was a problem with my code. The following does appear to properly clone a mesh. Code (csharp): meshes[1]._mesh = (Mesh)Instantiate (meshes[0]._mesh);