Search Unity

deep copy of mesh

Discussion in 'Scripting' started by exiguous, Oct 1, 2011.

  1. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    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):
    1.  
    2. private static Mesh procmesh = null;
    3.  
    4. public void Start()
    5. {// create the mesh at startup or copy it if already existent
    6.  
    7.     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
    8.         procmesh = MakeMesh (); // returns the generated mesh
    9.     else
    10.     {
    11.         Mesh thismesh = GetComponent<Mesh>();
    12.         thismesh = Instantiate(procmesh, new Vector3(0.0f, 0.0f, 0.0f), Quaternion.identity) as Mesh;   // should make a copy of this but conversion error
    13.     }
    14. }
    15.  
    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
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    problem solved the "hard" and inelegant way:
    Code (csharp):
    1.  
    2. Mesh targetmesh = GetComponent<MeshFilter>().mesh;
    3. targetmesh.vertices = procmesh.vertices;
    4. targetmesh.normals = procmesh.normals;
    5. targetmesh.uv = procmesh.uv;
    6. targetmesh.triangles = procmesh.triangles;
    7. targetmesh.tangents = procmesh.tangents;
    8.  
    Another question:
    when procmesh has been optimized before (Mesh.Optimize()) its still required to call targetmesh.Optimize() right? or is the optimized data copied?
     
    atomicjoe likes this.
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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.
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    thanks andeeee, saves some runtime ;).
     
  5. Essential

    Essential

    Joined:
    Sep 8, 2011
    Posts:
    265
    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):
    1. meshes[1]._mesh = (Mesh)Instantiate (meshes[0]._mesh);
     
    Last edited: Apr 16, 2014
  6. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    395
    More current way.

    Code (CSharp):
    1. Mesh newMesh = Object.Instantiate(oldMesh);
     
    DongxuCai and Bunny83 like this.