Vertex World Positions

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