How to duplicate a mesh

Now that I can properly edit vertices on a mesh, is there a way to duplicate a mesh so I can edit each mesh differently? Otherwise each model clone will just use the same vertex data :frowning:

I couldn’t find a clone function for a pc.Mesh so I believe I need to take the data of the base mesh and construct a new one from that?

Hi @davidpox,

Yes, there isn’t any method named mesh.clone(). But the createMesh() available in the engine will always create new vertex/index buffers instances. So basically it will create a copy of all the params you are passing.

I think you can use it for duplicating a mesh.

Yeah I tried doing something along the lines, however this provides pretty messed up results, check my code here:

DupeTest.prototype.dupeMesh = function(entity) {
    var m = entity.model.meshInstances[0].mesh;
    var it = new pc.VertexIterator(m.vertexBuffer);
   
    var pos = it.element[pc.SEMANTIC_POSITION].array;
    var uvs = it.element[pc.SEMANTIC_TEXCOORD0].array;
    var indices = Array.from(new Uint8Array(m.indexBuffer[0].storage));
    var normals = pc.calculateNormals(pos, indices);

    var mesh = pc.createMesh(this.app.graphicsDevice, pos, {
        normals: normals,
        uvs: uvs,
        indices: indices
    });
    
    var node = new pc.GraphNode();
    var mat = entity.model.meshInstances[0].material;
    
    var meshInstance = new pc.MeshInstance(node, mesh, mat);
    
    var model = new pc.Model();
    model.graph = node;
    model.meshInstances.push(meshInstance);
    
    return model;
};

Am I right how I’m getting the vertices/indices and uvs from the current mesh using SEMANTIC_POSITION, SEMANTIC_TEXCOORD and the indexBuffer?

Not sure what’s wrong, but it may be a good time for a feature request for a clone method.

There is work being done right now on a procedural mesh API. Would you like to submit your issue on the engine repo here?

1 Like

Thanks, I’ve submitted an issue about it. Hopefully I can figure out the issue anyway because its a relatively important part of the project :slight_smile:

With the above code, it currently looks like this;

The green is a wireframe of the original model - and then the grey model inside is my duplicated version.

A project can be found here if anyone wants to fork it and see for themselves.
https://playcanvas.com/project/679605/overview/mesh-duplication-test