Modifying the mesh on just one instance of a loaded model

I have this code:

[java] static private void sharpenCard(Node n) {

    for (Spatial s: n.getChildren()) {
        if (s instanceof Node) {
            sharpenCard((Node)s);
        } else {
            Geometry g = (Geometry)s;
            Mesh m = g.getMesh().cloneForAnim();
            g.setMesh(m);
            FloatBuffer data = (FloatBuffer)m.getBuffer(VertexBuffer.Type.Position).getData();
            
            int index = 0;
            while (index < data.limit()) {
                float x = data.get(index);
                float z = data.get(index+2);
                data.put(index, x*(1-(z+2.5f)/5f));
                index += 3;
            }
            m.setBuffer(VertexBuffer.Type.Position, 3, data);
        }
    }
}

[/java]

It’s supposed to take all the geometries inside a node (in this case a model loaded from the asset manager) and “sharpen” them.

It works perfectly - except that it also modifies every other mesh using that model:

You can see that every card has been turned into a triangle, not just the one that should have been.

I thought this line: Mesh m = g.getMesh().cloneForAnim();

would stop that but it hasn’t made any difference…

Thanks,
Z

does mesh.deepClone() work?

if (getBuffer(Type.BindPosePosition) != null){

cloneForAnim would do what you expect, but only if it has bindings.

Try deepClone maybe?

1 Like

Ahh, thanks @abies - well spotted.

I guess I will just do my own re-creation of the relevant buffer and use standard clone() for now.

Yep, that fixed it - thanks :slight_smile: