While working on a three.js project, I encountered some issues with loading vertices from an STL file and converting them to world coordinates. It seems like the matrix application isn't working properly, and I suspect it could be related to the loading process itself.
loader.load( './assets/models/trajan_print.stl', function ( geometry ) {
var mesh = new THREE.Mesh( geometry, material );
mesh.name = "target";
mesh.position.set( 0, - 300, - 400 );
mesh.rotation.set( - Math.PI / 2, 0, Math.PI );
mesh.scale.set( 5, 5, 5 );
//mesh.castShadow = true;
//mesh.receiveShadow = true;
mesh.visible = false;
SCENE.add( mesh );
model.setTargets(mesh);
} );
One key function to pay attention to is the last one, model.setTargets(mesh)
. This function is crucial for obtaining the vertices of the object in world coordinates, but it seems to have a glitch:
setTargets(mesh){
this.matrixWorld = mesh.matrixWorld;
var buffer = mesh.geometry.attributes.position.array;
for(var i = 0; i < buffer.length / 3; i = i + 3){
var point = new THREE.Vector3(buffer[i], buffer[i+1], buffer[i+2]);
point.applyMatrix4(this.matrixWorld);
this.unassignedVertices.push(point);
}
}
Interestingly, if I perform the same operation outside of this function, it works perfectly fine. This function is called only when this.unassignedVertices
is true, as it was a way to handle the asynchronous loading process.
insertParticle(part) {
var point = this.unassignedVertices.pop();
point.applyMatrix4(this.matrixWorld);
part.setTargetPoint(point);
this.octree.add(part);
this.particles.add(part);
}
Another issue, which is linked to setTargets(mesh)
, is that it seems like only half of the vertices from
mesh.geometry.attributes.position.array
are being loaded. This could be caused by other factors in the code, so I'm wondering if there's something specifically in the function that could be causing this. Am I loading it incorrectly? Am I converting it improperly? Am I missing points?
Additional context: the model loads and displays correctly if I remove the visible = false
tag.