[SOLVED] Incorrect vertex posititions

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