[SOLVED] Incorrect vertex posititions

Hey all, I’m trying to get the positions of each vertex on a mesh but it seems like the positions it reports back are incorrect/ possibly rotated?


You can see that the spheres dont match where the edges of the mesh meet
Image above is the result of spawning a sphere at each vertex, like so:

var mesh = this.entity.children[0].model.meshInstances[0].mesh;    
var it = new pc.VertexIterator(mesh.vertexBuffer);
    
for (var i = 0; i < mesh.vertexBuffer.getNumVertices(); i++) {
    var semPos = it.element[pc.SEMANTIC_POSITION];

    var pos = {
        x: semPos.array[semPos.index], 
        y: semPos.array[semPos.index + 1], 
        z: semPos.array[semPos.index + 2]
    };
    
    var b = this.cube.clone();
    b.enabled = true;
    b.reparent(this.entity);
    b.setLocalPosition(pos.x, pos.y, pos.z);
        
    it.next();
}
    
it.end();

The mesh has not been scaled, moved or rotated so I’m not sure what the issue here is! :confused:

1 Like

Hi @davidpox,

Your code seems correct, not sure what’s the issue.

Is this a project you can share to take a look?

Give me a few mins to move it to a separate project :slight_smile:

1 Like

Hey @Leonidas! Here’s the project :slight_smile:
https://playcanvas.com/project/678594/overview/vertex-pos

Many thanks for your help

Ok, cool the issue is that you are missing a base transform that the renderer does on the vertices, multiplying them by the model matrix:

Here is your corrected code:

PlanetSpikeTest.prototype.generate = function() {
    var mesh = this.entity.children[0].model.meshInstances[0].mesh;    
    var it = new pc.VertexIterator(mesh.vertexBuffer);
    
    // get matrix beforehand, don't need to call it for every vertex
    var matrix = this.entity.children[0].model.meshInstances[0].node.getWorldTransform();     
    var pos = new pc.Vec3();
    
    // none working code..
    for (var i = 0; i < mesh.vertexBuffer.getNumVertices(); i++) {
        var semPos = it.element[pc.SEMANTIC_POSITION];
        
        pos.set(semPos.array[semPos.index], semPos.array[semPos.index + 1], semPos.array[semPos.index + 2]);
        matrix.transformPoint(pos, pos);
    
        var b = this.cube.clone();
        b.enabled = true;
        b.reparent(this.entity);
        b.setPosition(pos);
        
        it.next();
    }
    
    it.end();
};

1 Like

Thanks!! You’re a star ;D

1 Like