Vertex positions on a mesh

Is there any way to get the vertex positions of a mesh?

Sure.

var buffer = this.entity.model.model.meshInstances[0].mesh.vertexBuffer;
var iterator = new pc.VertexIterator(buffer);

// Iterate though all verticles 
for(i = 0; i < buffer.getNumVertices(); i++) {

   // Current vertex's position
   var posSem = iterator.element[pc.SEMANTIC_POSITION];

   // Get position
   var posX = posSem.array[posSem.index];
   var posY = posSem.array[posSem.index + 1];
   var posZ = posSem.array[posSem.index + 2];

   // Move to the next vertex
   iterator.next();
}

iterator.end();
2 Likes

Thank you, it worked perfectly.
Just one more question, is it possible to do the same with the UV coordinates of these vertices? I need to do a mapping according to the position of the vertices on the screen, so I need to access the UV coordinates and change them

Yes, take a look at this: https://developer.playcanvas.com/en/api/pc.VertexFormat.html

I guess you need something like this:
var uv = iterator.element[pc.SEMANTIC_TEXCOORD0];

I was able to visualize and change everything you need here. Thank you very much! I was trying to find a way to do this since a month ago.

1 Like