一个适用于r94版本的解决方案:
function cylindricalSegment(A, B, radius, material) {
var vec = B.clone(); vec.sub(A);
var h = vec.length();
vec.normalize();
var quaternion = new THREE.Quaternion();
quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), vec);
var geometry = new THREE.CylinderGeometry(radius, radius, h, 32);
geometry.translate(0, h / 2, 0);
var cylinder = new THREE.Mesh(geometry, material);
cylinder.applyQuaternion(quaternion);
cylinder.position.set(A.x, A.y, A.z);
return cylinder;
}
编辑(2022年)
我觉得这个解决方案不再有效。无论如何,下面的解决方案更有效(假设您所有的圆柱体半径相同):
const radius = 0.07;
let geomUnitCylinder = new THREE.CylinderGeometry(radius, radius, 1, 64, 1, true);
geomUnitCylinder.translate(0, 0.5, 0);
const J = new THREE.Vector3(0, 1, 0);
function geomTube(P, Q) {
let vec = Q.clone().sub(P);
const h = vec.length();
vec.normalize();
const quaternion = new THREE.Quaternion().setFromUnitVectors(J, vec);
let geometry = geomUnitCylinder.clone();
geometry.scale(1, h, 1);
geometry.applyQuaternion(quaternion);
geometry.translate(P.x, P.y, P.z);
return geometry;
}