Vertex World Positions

Hi everyone,

I was hoping to get some help with getting the positions of a couple of vertices off of a model so that I can measure the distance between them. I did some searching around the forum and found this:

Along with another similar topic that was related to UV’s, but either it’s not quite what I’m looking for, or I just can’t seem to understand it. In 3ds Max I can see the id’s of the vertices that i want to get the position for, but I’m having trouble translating that into code that would allow me to pull the position information of those two specific vertices. Any help would be greatly appreciated!

see this example … it reads the positions from render components.
https://playcanvas.github.io/#/graphics/mesh-deformation

2 Likes

Hi @mvaligursky !

It took some trying, but I was able to get what I needed by examining the code in the example. Thank you very much! I think I was getting caught-up on how the vertex data was being stored/returned by the getPositions() . For anyone else trying to do something similar, this line of code in the example was a breakthrough for me:

const mesh = meshInstance.mesh;
            const srcPositions = [];
            mesh.getPositions(srcPositions);

            // store it
            allMeshes.push({
                mesh: mesh,
                srcPositions: srcPositions,
            });

Then so I could see how Playcanvas was identifying each vertex, I created a template of a text element and used a script to visually place the id’s of each vertex next to them like this:

Controller.prototype.initialize = function() {
    this.count = this.entity.render.meshInstances[0].mesh.getPositions([]);

    this.posArr = [];
    var mesh = this.entity.render.meshInstances[0].mesh;
    var pos = [];

    mesh.getPositions(pos);

    this.posArr.push({
        mesh: mesh,
        pos: pos
    });

};

Controller.prototype.generateVerts = function() {
//this.count is declared in initialize as the return value of mesh.getPositions()
    for(i = 0; i < this.count; i += 3) {

        var template = this.template.resource.instantiate();
        template.element.text = i;
        this.entity.addChild(template);
        template.setLocalPosition(this.getVertPosition(i));

    }

};

Controller.prototype.getVertPosition = function(id) {

    if(id >= 0) {
        id *= 3;

        return new pc.Vec3(this.posArr[0].pos[id], this.posArr[0].pos[id + 1 ], this.posArr[0].pos[id + 2]);
    }


    else {
        console.error('Invalid Vertex ID supplied');
    }

4 Likes