Hello, I want to be able to identify planes of an STL loaded object within a scene. Ideally I’d like to use the mouse to point and identify the plane. The end goal is to be able to position other objects parallel to the selected plane (or perpendicular or any other geometric relation). Does any body know how I could go about doing this? Currently using the below code for loading STL files:
function getMesh(fileName){
var meshGroup = new THREE.Group();
loader.load('/storage/uploadedFiles/'+fileName, function ( geometry ) {
var mesh = new THREE.Mesh( geometry, material );
meshGroup.add(mesh);
} );
return meshGroup;
}
Can you elaborate a bit on this? Stl is a triangle soup, could be many triangles all coplanar, could be each in its own plane. But in either case, the triangle itself is already a plane, what more do you need?
Yes, look for the Raycaster examples. You can obtain the mouse intersection point with the model (any model, not just STL loaded ones). From the intersection you obtain the point and also the normal vector (of the surface at that point). With the normal and point you have the plane defined.
Thank you for introducing Raycaster! I can find the normal using this. For the next step, I need to make another objects face parallel to a specific plane by changing the second objects rotation. Is there any easy way to do this?
Well, there is no easy or premade function to do that, so I’ve made this function. It would be great if you can test it in a live example (I’ve not tested it)
The extra ‘angle’ parameter lets you rotate the piece along the normal axis (can be 0)
function coupleObjects( objectA, objectAPoint, objectANormal, objectB, objectBPoint, objectBNormal, angle ) {
// objectA is the fixed object, objectB is the moving object.
// Convert objectBPoint to objectB local space
objectBPoint.applyMatrix4( objectB.matrix.getInverse() );
// Make sure the normals are unit
objectANormal.normalize();
objectBNormal.normalize();
// We want the objectANormal and objectBNormal faced against the other, so negate objectBNormal
objectBNormal.negate();
// Create an aligning quaternion that rotates from objectBNormal to objectANormal
const aligningQuaternion = new THREE.Quaternion().setFromUnitVectors( objectBNormal, objectANormal );
// Apply aligningQuaternion to objectB
objectB.quaternion.multiply( aligningQuaternion );
// Create a rotation quaternion in the coupling axis with angle 'angle'
const rotatingQuaternion = new THREE.Quaternion().setFromAxisAngle( objectANormal, angle );
// Apply rotatingQuaternion to objectB
objectB.quaternion.multiply( rotatingQuaternion );
// Convert again objectBPoint to world space
objectBPoint.applyMatrix4( objectB.matrix ).sub( objectB.position );
// Now make the points coincident
objectB.position.copy( objectAPoint ).sub( objectBPoint );
}